st.c.orig 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  1. /* See LICENSE for license details. */
  2. #include <ctype.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <limits.h>
  6. #include <pwd.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/select.h>
  14. #include <sys/types.h>
  15. #include <sys/wait.h>
  16. #include <termios.h>
  17. #include <unistd.h>
  18. #include <wchar.h>
  19. #include "st.h"
  20. #include "win.h"
  21. #if defined(__linux)
  22. #include <pty.h>
  23. #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  24. #include <util.h>
  25. #elif defined(__FreeBSD__) || defined(__DragonFly__)
  26. #include <libutil.h>
  27. #endif
  28. /* Arbitrary sizes */
  29. #define UTF_INVALID 0xFFFD
  30. #define UTF_SIZ 4
  31. #define ESC_BUF_SIZ (128*UTF_SIZ)
  32. #define ESC_ARG_SIZ 16
  33. #define STR_BUF_SIZ ESC_BUF_SIZ
  34. #define STR_ARG_SIZ ESC_ARG_SIZ
  35. /* macros */
  36. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  37. #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == 0x7f)
  38. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  39. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  40. #define ISDELIM(u) (u && wcschr(worddelimiters, u))
  41. enum term_mode {
  42. MODE_WRAP = 1 << 0,
  43. MODE_INSERT = 1 << 1,
  44. MODE_ALTSCREEN = 1 << 2,
  45. MODE_CRLF = 1 << 3,
  46. MODE_ECHO = 1 << 4,
  47. MODE_PRINT = 1 << 5,
  48. MODE_UTF8 = 1 << 6,
  49. };
  50. enum cursor_movement {
  51. CURSOR_SAVE,
  52. CURSOR_LOAD
  53. };
  54. enum cursor_state {
  55. CURSOR_DEFAULT = 0,
  56. CURSOR_WRAPNEXT = 1,
  57. CURSOR_ORIGIN = 2
  58. };
  59. enum charset {
  60. CS_GRAPHIC0,
  61. CS_GRAPHIC1,
  62. CS_UK,
  63. CS_USA,
  64. CS_MULTI,
  65. CS_GER,
  66. CS_FIN
  67. };
  68. enum escape_state {
  69. ESC_START = 1,
  70. ESC_CSI = 2,
  71. ESC_STR = 4, /* DCS, OSC, PM, APC */
  72. ESC_ALTCHARSET = 8,
  73. ESC_STR_END = 16, /* a final string was encountered */
  74. ESC_TEST = 32, /* Enter in test mode */
  75. ESC_UTF8 = 64,
  76. };
  77. typedef struct {
  78. Glyph attr; /* current char attributes */
  79. int x;
  80. int y;
  81. char state;
  82. } TCursor;
  83. typedef struct {
  84. int mode;
  85. int type;
  86. int snap;
  87. /*
  88. * Selection variables:
  89. * nb – normalized coordinates of the beginning of the selection
  90. * ne – normalized coordinates of the end of the selection
  91. * ob – original coordinates of the beginning of the selection
  92. * oe – original coordinates of the end of the selection
  93. */
  94. struct {
  95. int x, y;
  96. } nb, ne, ob, oe;
  97. int alt;
  98. } Selection;
  99. /* Internal representation of the screen */
  100. typedef struct {
  101. int row; /* nb row */
  102. int col; /* nb col */
  103. Line *line; /* screen */
  104. Line *alt; /* alternate screen */
  105. int *dirty; /* dirtyness of lines */
  106. TCursor c; /* cursor */
  107. int ocx; /* old cursor col */
  108. int ocy; /* old cursor row */
  109. int top; /* top scroll limit */
  110. int bot; /* bottom scroll limit */
  111. int mode; /* terminal mode flags */
  112. int esc; /* escape state flags */
  113. char trantbl[4]; /* charset table translation */
  114. int charset; /* current charset */
  115. int icharset; /* selected charset for sequence */
  116. int *tabs;
  117. Rune lastc; /* last printed char outside of sequence, 0 if control */
  118. } Term;
  119. /* CSI Escape sequence structs */
  120. /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
  121. typedef struct {
  122. char buf[ESC_BUF_SIZ]; /* raw string */
  123. size_t len; /* raw string length */
  124. char priv;
  125. int arg[ESC_ARG_SIZ];
  126. int narg; /* nb of args */
  127. char mode[2];
  128. } CSIEscape;
  129. /* STR Escape sequence structs */
  130. /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
  131. typedef struct {
  132. char type; /* ESC type ... */
  133. char *buf; /* allocated raw string */
  134. size_t siz; /* allocation size */
  135. size_t len; /* raw string length */
  136. char *args[STR_ARG_SIZ];
  137. int narg; /* nb of args */
  138. } STREscape;
  139. static void execsh(char *, char **);
  140. static void stty(char **);
  141. static void sigchld(int);
  142. static void ttywriteraw(const char *, size_t);
  143. static void csidump(void);
  144. static void csihandle(void);
  145. static void csiparse(void);
  146. static void csireset(void);
  147. static int eschandle(uchar);
  148. static void strdump(void);
  149. static void strhandle(void);
  150. static void strparse(void);
  151. static void strreset(void);
  152. static void tprinter(char *, size_t);
  153. static void tdumpsel(void);
  154. static void tdumpline(int);
  155. static void tdump(void);
  156. static void tclearregion(int, int, int, int);
  157. static void tcursor(int);
  158. static void tdeletechar(int);
  159. static void tdeleteline(int);
  160. static void tinsertblank(int);
  161. static void tinsertblankline(int);
  162. static int tlinelen(int);
  163. static void tmoveto(int, int);
  164. static void tmoveato(int, int);
  165. static void tnewline(int);
  166. static void tputtab(int);
  167. static void tputc(Rune);
  168. static void treset(void);
  169. static void tscrollup(int, int);
  170. static void tscrolldown(int, int);
  171. static void tsetattr(const int *, int);
  172. static void tsetchar(Rune, const Glyph *, int, int);
  173. static void tsetdirt(int, int);
  174. static void tsetscroll(int, int);
  175. static void tswapscreen(void);
  176. static void tsetmode(int, int, const int *, int);
  177. static int twrite(const char *, int, int);
  178. static void tfulldirt(void);
  179. static void tcontrolcode(uchar );
  180. static void tdectest(char );
  181. static void tdefutf8(char);
  182. static int32_t tdefcolor(const int *, int *, int);
  183. static void tdeftran(char);
  184. static void tstrsequence(uchar);
  185. static void drawregion(int, int, int, int);
  186. static void selnormalize(void);
  187. static void selscroll(int, int);
  188. static void selsnap(int *, int *, int);
  189. static size_t utf8decode(const char *, Rune *, size_t);
  190. static Rune utf8decodebyte(char, size_t *);
  191. static char utf8encodebyte(Rune, size_t);
  192. static size_t utf8validate(Rune *, size_t);
  193. static char *base64dec(const char *);
  194. static char base64dec_getc(const char **);
  195. static ssize_t xwrite(int, const char *, size_t);
  196. /* Globals */
  197. static Term term;
  198. static Selection sel;
  199. static CSIEscape csiescseq;
  200. static STREscape strescseq;
  201. static int iofd = 1;
  202. static int cmdfd;
  203. static pid_t pid;
  204. static const uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
  205. static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
  206. static const Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000};
  207. static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
  208. ssize_t
  209. xwrite(int fd, const char *s, size_t len)
  210. {
  211. size_t aux = len;
  212. ssize_t r;
  213. while (len > 0) {
  214. r = write(fd, s, len);
  215. if (r < 0)
  216. return r;
  217. len -= r;
  218. s += r;
  219. }
  220. return aux;
  221. }
  222. void *
  223. xmalloc(size_t len)
  224. {
  225. void *p;
  226. if (!(p = malloc(len)))
  227. die("malloc: %s\n", strerror(errno));
  228. return p;
  229. }
  230. void *
  231. xrealloc(void *p, size_t len)
  232. {
  233. if ((p = realloc(p, len)) == NULL)
  234. die("realloc: %s\n", strerror(errno));
  235. return p;
  236. }
  237. char *
  238. xstrdup(const char *s)
  239. {
  240. char *p;
  241. if ((p = strdup(s)) == NULL)
  242. die("strdup: %s\n", strerror(errno));
  243. return p;
  244. }
  245. size_t
  246. utf8decode(const char *c, Rune *u, size_t clen)
  247. {
  248. size_t i, j, len, type;
  249. Rune udecoded;
  250. *u = UTF_INVALID;
  251. if (!clen)
  252. return 0;
  253. udecoded = utf8decodebyte(c[0], &len);
  254. if (!BETWEEN(len, 1, UTF_SIZ))
  255. return 1;
  256. for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
  257. udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
  258. if (type != 0)
  259. return j;
  260. }
  261. if (j < len)
  262. return 0;
  263. *u = udecoded;
  264. utf8validate(u, len);
  265. return len;
  266. }
  267. Rune
  268. utf8decodebyte(char c, size_t *i)
  269. {
  270. for (*i = 0; *i < LEN(utfmask); ++(*i))
  271. if (((uchar)c & utfmask[*i]) == utfbyte[*i])
  272. return (uchar)c & ~utfmask[*i];
  273. return 0;
  274. }
  275. size_t
  276. utf8encode(Rune u, char *c)
  277. {
  278. size_t len, i;
  279. len = utf8validate(&u, 0);
  280. if (len > UTF_SIZ)
  281. return 0;
  282. for (i = len - 1; i != 0; --i) {
  283. c[i] = utf8encodebyte(u, 0);
  284. u >>= 6;
  285. }
  286. c[0] = utf8encodebyte(u, len);
  287. return len;
  288. }
  289. char
  290. utf8encodebyte(Rune u, size_t i)
  291. {
  292. return utfbyte[i] | (u & ~utfmask[i]);
  293. }
  294. size_t
  295. utf8validate(Rune *u, size_t i)
  296. {
  297. if (!BETWEEN(*u, utfmin[i], utfmax[i]) || BETWEEN(*u, 0xD800, 0xDFFF))
  298. *u = UTF_INVALID;
  299. for (i = 1; *u > utfmax[i]; ++i)
  300. ;
  301. return i;
  302. }
  303. char
  304. base64dec_getc(const char **src)
  305. {
  306. while (**src && !isprint((unsigned char)**src))
  307. (*src)++;
  308. return **src ? *((*src)++) : '='; /* emulate padding if string ends */
  309. }
  310. char *
  311. base64dec(const char *src)
  312. {
  313. size_t in_len = strlen(src);
  314. char *result, *dst;
  315. static const char base64_digits[256] = {
  316. [43] = 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  317. 0, 0, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  318. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0,
  319. 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  320. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
  321. };
  322. if (in_len % 4)
  323. in_len += 4 - (in_len % 4);
  324. result = dst = xmalloc(in_len / 4 * 3 + 1);
  325. while (*src) {
  326. int a = base64_digits[(unsigned char) base64dec_getc(&src)];
  327. int b = base64_digits[(unsigned char) base64dec_getc(&src)];
  328. int c = base64_digits[(unsigned char) base64dec_getc(&src)];
  329. int d = base64_digits[(unsigned char) base64dec_getc(&src)];
  330. /* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
  331. if (a == -1 || b == -1)
  332. break;
  333. *dst++ = (a << 2) | ((b & 0x30) >> 4);
  334. if (c == -1)
  335. break;
  336. *dst++ = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2);
  337. if (d == -1)
  338. break;
  339. *dst++ = ((c & 0x03) << 6) | d;
  340. }
  341. *dst = '\0';
  342. return result;
  343. }
  344. void
  345. selinit(void)
  346. {
  347. sel.mode = SEL_IDLE;
  348. sel.snap = 0;
  349. sel.ob.x = -1;
  350. }
  351. int
  352. tlinelen(int y)
  353. {
  354. int i = term.col;
  355. if (term.line[y][i - 1].mode & ATTR_WRAP)
  356. return i;
  357. while (i > 0 && term.line[y][i - 1].u == ' ')
  358. --i;
  359. return i;
  360. }
  361. void
  362. selstart(int col, int row, int snap)
  363. {
  364. selclear();
  365. sel.mode = SEL_EMPTY;
  366. sel.type = SEL_REGULAR;
  367. sel.alt = IS_SET(MODE_ALTSCREEN);
  368. sel.snap = snap;
  369. sel.oe.x = sel.ob.x = col;
  370. sel.oe.y = sel.ob.y = row;
  371. selnormalize();
  372. if (sel.snap != 0)
  373. sel.mode = SEL_READY;
  374. tsetdirt(sel.nb.y, sel.ne.y);
  375. }
  376. void
  377. selextend(int col, int row, int type, int done)
  378. {
  379. int oldey, oldex, oldsby, oldsey, oldtype;
  380. if (sel.mode == SEL_IDLE)
  381. return;
  382. if (done && sel.mode == SEL_EMPTY) {
  383. selclear();
  384. return;
  385. }
  386. oldey = sel.oe.y;
  387. oldex = sel.oe.x;
  388. oldsby = sel.nb.y;
  389. oldsey = sel.ne.y;
  390. oldtype = sel.type;
  391. sel.oe.x = col;
  392. sel.oe.y = row;
  393. selnormalize();
  394. sel.type = type;
  395. if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type || sel.mode == SEL_EMPTY)
  396. tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
  397. sel.mode = done ? SEL_IDLE : SEL_READY;
  398. }
  399. void
  400. selnormalize(void)
  401. {
  402. int i;
  403. if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
  404. sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
  405. sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
  406. } else {
  407. sel.nb.x = MIN(sel.ob.x, sel.oe.x);
  408. sel.ne.x = MAX(sel.ob.x, sel.oe.x);
  409. }
  410. sel.nb.y = MIN(sel.ob.y, sel.oe.y);
  411. sel.ne.y = MAX(sel.ob.y, sel.oe.y);
  412. selsnap(&sel.nb.x, &sel.nb.y, -1);
  413. selsnap(&sel.ne.x, &sel.ne.y, +1);
  414. /* expand selection over line breaks */
  415. if (sel.type == SEL_RECTANGULAR)
  416. return;
  417. i = tlinelen(sel.nb.y);
  418. if (i < sel.nb.x)
  419. sel.nb.x = i;
  420. if (tlinelen(sel.ne.y) <= sel.ne.x)
  421. sel.ne.x = term.col - 1;
  422. }
  423. int
  424. selected(int x, int y)
  425. {
  426. if (sel.mode == SEL_EMPTY || sel.ob.x == -1 ||
  427. sel.alt != IS_SET(MODE_ALTSCREEN))
  428. return 0;
  429. if (sel.type == SEL_RECTANGULAR)
  430. return BETWEEN(y, sel.nb.y, sel.ne.y)
  431. && BETWEEN(x, sel.nb.x, sel.ne.x);
  432. return BETWEEN(y, sel.nb.y, sel.ne.y)
  433. && (y != sel.nb.y || x >= sel.nb.x)
  434. && (y != sel.ne.y || x <= sel.ne.x);
  435. }
  436. void
  437. selsnap(int *x, int *y, int direction)
  438. {
  439. int newx, newy, xt, yt;
  440. int delim, prevdelim;
  441. const Glyph *gp, *prevgp;
  442. switch (sel.snap) {
  443. case SNAP_WORD:
  444. /*
  445. * Snap around if the word wraps around at the end or
  446. * beginning of a line.
  447. */
  448. prevgp = &term.line[*y][*x];
  449. prevdelim = ISDELIM(prevgp->u);
  450. for (;;) {
  451. newx = *x + direction;
  452. newy = *y;
  453. if (!BETWEEN(newx, 0, term.col - 1)) {
  454. newy += direction;
  455. newx = (newx + term.col) % term.col;
  456. if (!BETWEEN(newy, 0, term.row - 1))
  457. break;
  458. if (direction > 0)
  459. yt = *y, xt = *x;
  460. else
  461. yt = newy, xt = newx;
  462. if (!(term.line[yt][xt].mode & ATTR_WRAP))
  463. break;
  464. }
  465. if (newx >= tlinelen(newy))
  466. break;
  467. gp = &term.line[newy][newx];
  468. delim = ISDELIM(gp->u);
  469. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  470. || (delim && gp->u != prevgp->u)))
  471. break;
  472. *x = newx;
  473. *y = newy;
  474. prevgp = gp;
  475. prevdelim = delim;
  476. }
  477. break;
  478. case SNAP_LINE:
  479. /*
  480. * Snap around if the the previous line or the current one
  481. * has set ATTR_WRAP at its end. Then the whole next or
  482. * previous line will be selected.
  483. */
  484. *x = (direction < 0) ? 0 : term.col - 1;
  485. if (direction < 0) {
  486. for (; *y > 0; *y += direction) {
  487. if (!(term.line[*y-1][term.col-1].mode
  488. & ATTR_WRAP)) {
  489. break;
  490. }
  491. }
  492. } else if (direction > 0) {
  493. for (; *y < term.row-1; *y += direction) {
  494. if (!(term.line[*y][term.col-1].mode
  495. & ATTR_WRAP)) {
  496. break;
  497. }
  498. }
  499. }
  500. break;
  501. }
  502. }
  503. char *
  504. getsel(void)
  505. {
  506. char *str, *ptr;
  507. int y, bufsize, lastx, linelen;
  508. const Glyph *gp, *last;
  509. if (sel.ob.x == -1)
  510. return NULL;
  511. bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
  512. ptr = str = xmalloc(bufsize);
  513. /* append every set & selected glyph to the selection */
  514. for (y = sel.nb.y; y <= sel.ne.y; y++) {
  515. if ((linelen = tlinelen(y)) == 0) {
  516. *ptr++ = '\n';
  517. continue;
  518. }
  519. if (sel.type == SEL_RECTANGULAR) {
  520. gp = &term.line[y][sel.nb.x];
  521. lastx = sel.ne.x;
  522. } else {
  523. gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  524. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  525. }
  526. last = &term.line[y][MIN(lastx, linelen-1)];
  527. while (last >= gp && last->u == ' ')
  528. --last;
  529. for ( ; gp <= last; ++gp) {
  530. if (gp->mode & ATTR_WDUMMY)
  531. continue;
  532. ptr += utf8encode(gp->u, ptr);
  533. }
  534. /*
  535. * Copy and pasting of line endings is inconsistent
  536. * in the inconsistent terminal and GUI world.
  537. * The best solution seems like to produce '\n' when
  538. * something is copied from st and convert '\n' to
  539. * '\r', when something to be pasted is received by
  540. * st.
  541. * FIXME: Fix the computer world.
  542. */
  543. if ((y < sel.ne.y || lastx >= linelen) &&
  544. (!(last->mode & ATTR_WRAP) || sel.type == SEL_RECTANGULAR))
  545. *ptr++ = '\n';
  546. }
  547. *ptr = 0;
  548. return str;
  549. }
  550. void
  551. selclear(void)
  552. {
  553. if (sel.ob.x == -1)
  554. return;
  555. sel.mode = SEL_IDLE;
  556. sel.ob.x = -1;
  557. tsetdirt(sel.nb.y, sel.ne.y);
  558. }
  559. void
  560. die(const char *errstr, ...)
  561. {
  562. va_list ap;
  563. va_start(ap, errstr);
  564. vfprintf(stderr, errstr, ap);
  565. va_end(ap);
  566. exit(1);
  567. }
  568. void
  569. execsh(char *cmd, char **args)
  570. {
  571. char *sh, *prog, *arg;
  572. const struct passwd *pw;
  573. errno = 0;
  574. if ((pw = getpwuid(getuid())) == NULL) {
  575. if (errno)
  576. die("getpwuid: %s\n", strerror(errno));
  577. else
  578. die("who are you?\n");
  579. }
  580. if ((sh = getenv("SHELL")) == NULL)
  581. sh = (pw->pw_shell[0]) ? pw->pw_shell : cmd;
  582. if (args) {
  583. prog = args[0];
  584. arg = NULL;
  585. } else if (scroll) {
  586. prog = scroll;
  587. arg = utmp ? utmp : sh;
  588. } else if (utmp) {
  589. prog = utmp;
  590. arg = NULL;
  591. } else {
  592. prog = sh;
  593. arg = NULL;
  594. }
  595. DEFAULT(args, ((char *[]) {prog, arg, NULL}));
  596. unsetenv("COLUMNS");
  597. unsetenv("LINES");
  598. unsetenv("TERMCAP");
  599. setenv("LOGNAME", pw->pw_name, 1);
  600. setenv("USER", pw->pw_name, 1);
  601. setenv("SHELL", sh, 1);
  602. setenv("HOME", pw->pw_dir, 1);
  603. setenv("TERM", termname, 1);
  604. signal(SIGCHLD, SIG_DFL);
  605. signal(SIGHUP, SIG_DFL);
  606. signal(SIGINT, SIG_DFL);
  607. signal(SIGQUIT, SIG_DFL);
  608. signal(SIGTERM, SIG_DFL);
  609. signal(SIGALRM, SIG_DFL);
  610. execvp(prog, args);
  611. _exit(1);
  612. }
  613. void
  614. sigchld(int a)
  615. {
  616. int stat;
  617. pid_t p;
  618. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  619. die("waiting for pid %hd failed: %s\n", pid, strerror(errno));
  620. if (pid != p)
  621. return;
  622. if (WIFEXITED(stat) && WEXITSTATUS(stat))
  623. die("child exited with status %d\n", WEXITSTATUS(stat));
  624. else if (WIFSIGNALED(stat))
  625. die("child terminated due to signal %d\n", WTERMSIG(stat));
  626. _exit(0);
  627. }
  628. void
  629. stty(char **args)
  630. {
  631. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  632. size_t n, siz;
  633. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  634. die("incorrect stty parameters\n");
  635. memcpy(cmd, stty_args, n);
  636. q = cmd + n;
  637. siz = sizeof(cmd) - n;
  638. for (p = args; p && (s = *p); ++p) {
  639. if ((n = strlen(s)) > siz-1)
  640. die("stty parameter length too long\n");
  641. *q++ = ' ';
  642. memcpy(q, s, n);
  643. q += n;
  644. siz -= n + 1;
  645. }
  646. *q = '\0';
  647. if (system(cmd) != 0)
  648. perror("Couldn't call stty");
  649. }
  650. int
  651. ttynew(const char *line, char *cmd, const char *out, char **args)
  652. {
  653. int m, s;
  654. if (out) {
  655. term.mode |= MODE_PRINT;
  656. iofd = (!strcmp(out, "-")) ?
  657. 1 : open(out, O_WRONLY | O_CREAT, 0666);
  658. if (iofd < 0) {
  659. fprintf(stderr, "Error opening %s:%s\n",
  660. out, strerror(errno));
  661. }
  662. }
  663. if (line) {
  664. if ((cmdfd = open(line, O_RDWR)) < 0)
  665. die("open line '%s' failed: %s\n",
  666. line, strerror(errno));
  667. dup2(cmdfd, 0);
  668. stty(args);
  669. return cmdfd;
  670. }
  671. /* seems to work fine on linux, openbsd and freebsd */
  672. if (openpty(&m, &s, NULL, NULL, NULL) < 0)
  673. die("openpty failed: %s\n", strerror(errno));
  674. switch (pid = fork()) {
  675. case -1:
  676. die("fork failed: %s\n", strerror(errno));
  677. break;
  678. case 0:
  679. close(iofd);
  680. close(m);
  681. setsid(); /* create a new process group */
  682. dup2(s, 0);
  683. dup2(s, 1);
  684. dup2(s, 2);
  685. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  686. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  687. if (s > 2)
  688. close(s);
  689. #ifdef __OpenBSD__
  690. if (pledge("stdio getpw proc exec", NULL) == -1)
  691. die("pledge\n");
  692. #endif
  693. execsh(cmd, args);
  694. break;
  695. default:
  696. #ifdef __OpenBSD__
  697. if (pledge("stdio rpath tty proc", NULL) == -1)
  698. die("pledge\n");
  699. #endif
  700. close(s);
  701. cmdfd = m;
  702. signal(SIGCHLD, sigchld);
  703. break;
  704. }
  705. return cmdfd;
  706. }
  707. size_t
  708. ttyread(void)
  709. {
  710. static char buf[BUFSIZ];
  711. static int buflen = 0;
  712. int ret, written;
  713. /* append read bytes to unprocessed bytes */
  714. ret = read(cmdfd, buf+buflen, LEN(buf)-buflen);
  715. switch (ret) {
  716. case 0:
  717. exit(0);
  718. case -1:
  719. die("couldn't read from shell: %s\n", strerror(errno));
  720. default:
  721. buflen += ret;
  722. written = twrite(buf, buflen, 0);
  723. buflen -= written;
  724. /* keep any incomplete UTF-8 byte sequence for the next call */
  725. if (buflen > 0)
  726. memmove(buf, buf + written, buflen);
  727. return ret;
  728. }
  729. }
  730. void
  731. ttywrite(const char *s, size_t n, int may_echo)
  732. {
  733. const char *next;
  734. if (may_echo && IS_SET(MODE_ECHO))
  735. twrite(s, n, 1);
  736. if (!IS_SET(MODE_CRLF)) {
  737. ttywriteraw(s, n);
  738. return;
  739. }
  740. /* This is similar to how the kernel handles ONLCR for ttys */
  741. while (n > 0) {
  742. if (*s == '\r') {
  743. next = s + 1;
  744. ttywriteraw("\r\n", 2);
  745. } else {
  746. next = memchr(s, '\r', n);
  747. DEFAULT(next, s + n);
  748. ttywriteraw(s, next - s);
  749. }
  750. n -= next - s;
  751. s = next;
  752. }
  753. }
  754. void
  755. ttywriteraw(const char *s, size_t n)
  756. {
  757. fd_set wfd, rfd;
  758. ssize_t r;
  759. size_t lim = 256;
  760. /*
  761. * Remember that we are using a pty, which might be a modem line.
  762. * Writing too much will clog the line. That's why we are doing this
  763. * dance.
  764. * FIXME: Migrate the world to Plan 9.
  765. */
  766. while (n > 0) {
  767. FD_ZERO(&wfd);
  768. FD_ZERO(&rfd);
  769. FD_SET(cmdfd, &wfd);
  770. FD_SET(cmdfd, &rfd);
  771. /* Check if we can write. */
  772. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  773. if (errno == EINTR)
  774. continue;
  775. die("select failed: %s\n", strerror(errno));
  776. }
  777. if (FD_ISSET(cmdfd, &wfd)) {
  778. /*
  779. * Only write the bytes written by ttywrite() or the
  780. * default of 256. This seems to be a reasonable value
  781. * for a serial line. Bigger values might clog the I/O.
  782. */
  783. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  784. goto write_error;
  785. if (r < n) {
  786. /*
  787. * We weren't able to write out everything.
  788. * This means the buffer is getting full
  789. * again. Empty it.
  790. */
  791. if (n < lim)
  792. lim = ttyread();
  793. n -= r;
  794. s += r;
  795. } else {
  796. /* All bytes have been written. */
  797. break;
  798. }
  799. }
  800. if (FD_ISSET(cmdfd, &rfd))
  801. lim = ttyread();
  802. }
  803. return;
  804. write_error:
  805. die("write error on tty: %s\n", strerror(errno));
  806. }
  807. void
  808. ttyresize(int tw, int th)
  809. {
  810. struct winsize w;
  811. w.ws_row = term.row;
  812. w.ws_col = term.col;
  813. w.ws_xpixel = tw;
  814. w.ws_ypixel = th;
  815. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  816. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  817. }
  818. void
  819. ttyhangup()
  820. {
  821. /* Send SIGHUP to shell */
  822. kill(pid, SIGHUP);
  823. }
  824. int
  825. tattrset(int attr)
  826. {
  827. int i, j;
  828. for (i = 0; i < term.row-1; i++) {
  829. for (j = 0; j < term.col-1; j++) {
  830. if (term.line[i][j].mode & attr)
  831. return 1;
  832. }
  833. }
  834. return 0;
  835. }
  836. void
  837. tsetdirt(int top, int bot)
  838. {
  839. int i;
  840. LIMIT(top, 0, term.row-1);
  841. LIMIT(bot, 0, term.row-1);
  842. for (i = top; i <= bot; i++)
  843. term.dirty[i] = 1;
  844. }
  845. void
  846. tsetdirtattr(int attr)
  847. {
  848. int i, j;
  849. for (i = 0; i < term.row-1; i++) {
  850. for (j = 0; j < term.col-1; j++) {
  851. if (term.line[i][j].mode & attr) {
  852. tsetdirt(i, i);
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. void
  859. tfulldirt(void)
  860. {
  861. tsetdirt(0, term.row-1);
  862. }
  863. void
  864. tcursor(int mode)
  865. {
  866. static TCursor c[2];
  867. int alt = IS_SET(MODE_ALTSCREEN);
  868. if (mode == CURSOR_SAVE) {
  869. c[alt] = term.c;
  870. } else if (mode == CURSOR_LOAD) {
  871. term.c = c[alt];
  872. tmoveto(c[alt].x, c[alt].y);
  873. }
  874. }
  875. void
  876. treset(void)
  877. {
  878. uint i;
  879. term.c = (TCursor){{
  880. .mode = ATTR_NULL,
  881. .fg = defaultfg,
  882. .bg = defaultbg
  883. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  884. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  885. for (i = tabspaces; i < term.col; i += tabspaces)
  886. term.tabs[i] = 1;
  887. term.top = 0;
  888. term.bot = term.row - 1;
  889. term.mode = MODE_WRAP|MODE_UTF8;
  890. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  891. term.charset = 0;
  892. for (i = 0; i < 2; i++) {
  893. tmoveto(0, 0);
  894. tcursor(CURSOR_SAVE);
  895. tclearregion(0, 0, term.col-1, term.row-1);
  896. tswapscreen();
  897. }
  898. }
  899. void
  900. tnew(int col, int row)
  901. {
  902. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  903. tresize(col, row);
  904. treset();
  905. }
  906. void
  907. tswapscreen(void)
  908. {
  909. Line *tmp = term.line;
  910. term.line = term.alt;
  911. term.alt = tmp;
  912. term.mode ^= MODE_ALTSCREEN;
  913. tfulldirt();
  914. }
  915. void
  916. tscrolldown(int orig, int n)
  917. {
  918. int i;
  919. Line temp;
  920. LIMIT(n, 0, term.bot-orig+1);
  921. tsetdirt(orig, term.bot-n);
  922. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  923. for (i = term.bot; i >= orig+n; i--) {
  924. temp = term.line[i];
  925. term.line[i] = term.line[i-n];
  926. term.line[i-n] = temp;
  927. }
  928. selscroll(orig, n);
  929. }
  930. void
  931. tscrollup(int orig, int n)
  932. {
  933. int i;
  934. Line temp;
  935. LIMIT(n, 0, term.bot-orig+1);
  936. tclearregion(0, orig, term.col-1, orig+n-1);
  937. tsetdirt(orig+n, term.bot);
  938. for (i = orig; i <= term.bot-n; i++) {
  939. temp = term.line[i];
  940. term.line[i] = term.line[i+n];
  941. term.line[i+n] = temp;
  942. }
  943. selscroll(orig, -n);
  944. }
  945. void
  946. selscroll(int orig, int n)
  947. {
  948. if (sel.ob.x == -1)
  949. return;
  950. if (BETWEEN(sel.nb.y, orig, term.bot) != BETWEEN(sel.ne.y, orig, term.bot)) {
  951. selclear();
  952. } else if (BETWEEN(sel.nb.y, orig, term.bot)) {
  953. sel.ob.y += n;
  954. sel.oe.y += n;
  955. if (sel.ob.y < term.top || sel.ob.y > term.bot ||
  956. sel.oe.y < term.top || sel.oe.y > term.bot) {
  957. selclear();
  958. } else {
  959. selnormalize();
  960. }
  961. }
  962. }
  963. void
  964. tnewline(int first_col)
  965. {
  966. int y = term.c.y;
  967. if (y == term.bot) {
  968. tscrollup(term.top, 1);
  969. } else {
  970. y++;
  971. }
  972. tmoveto(first_col ? 0 : term.c.x, y);
  973. }
  974. void
  975. csiparse(void)
  976. {
  977. char *p = csiescseq.buf, *np;
  978. long int v;
  979. csiescseq.narg = 0;
  980. if (*p == '?') {
  981. csiescseq.priv = 1;
  982. p++;
  983. }
  984. csiescseq.buf[csiescseq.len] = '\0';
  985. while (p < csiescseq.buf+csiescseq.len) {
  986. np = NULL;
  987. v = strtol(p, &np, 10);
  988. if (np == p)
  989. v = 0;
  990. if (v == LONG_MAX || v == LONG_MIN)
  991. v = -1;
  992. csiescseq.arg[csiescseq.narg++] = v;
  993. p = np;
  994. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  995. break;
  996. p++;
  997. }
  998. csiescseq.mode[0] = *p++;
  999. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1000. }
  1001. /* for absolute user moves, when decom is set */
  1002. void
  1003. tmoveato(int x, int y)
  1004. {
  1005. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1006. }
  1007. void
  1008. tmoveto(int x, int y)
  1009. {
  1010. int miny, maxy;
  1011. if (term.c.state & CURSOR_ORIGIN) {
  1012. miny = term.top;
  1013. maxy = term.bot;
  1014. } else {
  1015. miny = 0;
  1016. maxy = term.row - 1;
  1017. }
  1018. term.c.state &= ~CURSOR_WRAPNEXT;
  1019. term.c.x = LIMIT(x, 0, term.col-1);
  1020. term.c.y = LIMIT(y, miny, maxy);
  1021. }
  1022. void
  1023. tsetchar(Rune u, const Glyph *attr, int x, int y)
  1024. {
  1025. static const char *vt100_0[62] = { /* 0x41 - 0x7e */
  1026. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1027. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1028. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1029. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1030. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1031. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1032. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1033. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1034. };
  1035. /*
  1036. * The table is proudly stolen from rxvt.
  1037. */
  1038. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1039. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1040. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1041. if (term.line[y][x].mode & ATTR_WIDE) {
  1042. if (x+1 < term.col) {
  1043. term.line[y][x+1].u = ' ';
  1044. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1045. }
  1046. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1047. term.line[y][x-1].u = ' ';
  1048. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1049. }
  1050. term.dirty[y] = 1;
  1051. term.line[y][x] = *attr;
  1052. term.line[y][x].u = u;
  1053. }
  1054. void
  1055. tclearregion(int x1, int y1, int x2, int y2)
  1056. {
  1057. int x, y, temp;
  1058. Glyph *gp;
  1059. if (x1 > x2)
  1060. temp = x1, x1 = x2, x2 = temp;
  1061. if (y1 > y2)
  1062. temp = y1, y1 = y2, y2 = temp;
  1063. LIMIT(x1, 0, term.col-1);
  1064. LIMIT(x2, 0, term.col-1);
  1065. LIMIT(y1, 0, term.row-1);
  1066. LIMIT(y2, 0, term.row-1);
  1067. for (y = y1; y <= y2; y++) {
  1068. term.dirty[y] = 1;
  1069. for (x = x1; x <= x2; x++) {
  1070. gp = &term.line[y][x];
  1071. if (selected(x, y))
  1072. selclear();
  1073. gp->fg = term.c.attr.fg;
  1074. gp->bg = term.c.attr.bg;
  1075. gp->mode = 0;
  1076. gp->u = ' ';
  1077. }
  1078. }
  1079. }
  1080. void
  1081. tdeletechar(int n)
  1082. {
  1083. int dst, src, size;
  1084. Glyph *line;
  1085. LIMIT(n, 0, term.col - term.c.x);
  1086. dst = term.c.x;
  1087. src = term.c.x + n;
  1088. size = term.col - src;
  1089. line = term.line[term.c.y];
  1090. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1091. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1092. }
  1093. void
  1094. tinsertblank(int n)
  1095. {
  1096. int dst, src, size;
  1097. Glyph *line;
  1098. LIMIT(n, 0, term.col - term.c.x);
  1099. dst = term.c.x + n;
  1100. src = term.c.x;
  1101. size = term.col - dst;
  1102. line = term.line[term.c.y];
  1103. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1104. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1105. }
  1106. void
  1107. tinsertblankline(int n)
  1108. {
  1109. if (BETWEEN(term.c.y, term.top, term.bot))
  1110. tscrolldown(term.c.y, n);
  1111. }
  1112. void
  1113. tdeleteline(int n)
  1114. {
  1115. if (BETWEEN(term.c.y, term.top, term.bot))
  1116. tscrollup(term.c.y, n);
  1117. }
  1118. int32_t
  1119. tdefcolor(const int *attr, int *npar, int l)
  1120. {
  1121. int32_t idx = -1;
  1122. uint r, g, b;
  1123. switch (attr[*npar + 1]) {
  1124. case 2: /* direct color in RGB space */
  1125. if (*npar + 4 >= l) {
  1126. fprintf(stderr,
  1127. "erresc(38): Incorrect number of parameters (%d)\n",
  1128. *npar);
  1129. break;
  1130. }
  1131. r = attr[*npar + 2];
  1132. g = attr[*npar + 3];
  1133. b = attr[*npar + 4];
  1134. *npar += 4;
  1135. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1136. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1137. r, g, b);
  1138. else
  1139. idx = TRUECOLOR(r, g, b);
  1140. break;
  1141. case 5: /* indexed color */
  1142. if (*npar + 2 >= l) {
  1143. fprintf(stderr,
  1144. "erresc(38): Incorrect number of parameters (%d)\n",
  1145. *npar);
  1146. break;
  1147. }
  1148. *npar += 2;
  1149. if (!BETWEEN(attr[*npar], 0, 255))
  1150. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1151. else
  1152. idx = attr[*npar];
  1153. break;
  1154. case 0: /* implemented defined (only foreground) */
  1155. case 1: /* transparent */
  1156. case 3: /* direct color in CMY space */
  1157. case 4: /* direct color in CMYK space */
  1158. default:
  1159. fprintf(stderr,
  1160. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1161. break;
  1162. }
  1163. return idx;
  1164. }
  1165. void
  1166. tsetattr(const int *attr, int l)
  1167. {
  1168. int i;
  1169. int32_t idx;
  1170. for (i = 0; i < l; i++) {
  1171. switch (attr[i]) {
  1172. case 0:
  1173. term.c.attr.mode &= ~(
  1174. ATTR_BOLD |
  1175. ATTR_FAINT |
  1176. ATTR_ITALIC |
  1177. ATTR_UNDERLINE |
  1178. ATTR_BLINK |
  1179. ATTR_REVERSE |
  1180. ATTR_INVISIBLE |
  1181. ATTR_STRUCK );
  1182. term.c.attr.fg = defaultfg;
  1183. term.c.attr.bg = defaultbg;
  1184. break;
  1185. case 1:
  1186. term.c.attr.mode |= ATTR_BOLD;
  1187. break;
  1188. case 2:
  1189. term.c.attr.mode |= ATTR_FAINT;
  1190. break;
  1191. case 3:
  1192. term.c.attr.mode |= ATTR_ITALIC;
  1193. break;
  1194. case 4:
  1195. term.c.attr.mode |= ATTR_UNDERLINE;
  1196. break;
  1197. case 5: /* slow blink */
  1198. /* FALLTHROUGH */
  1199. case 6: /* rapid blink */
  1200. term.c.attr.mode |= ATTR_BLINK;
  1201. break;
  1202. case 7:
  1203. term.c.attr.mode |= ATTR_REVERSE;
  1204. break;
  1205. case 8:
  1206. term.c.attr.mode |= ATTR_INVISIBLE;
  1207. break;
  1208. case 9:
  1209. term.c.attr.mode |= ATTR_STRUCK;
  1210. break;
  1211. case 22:
  1212. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1213. break;
  1214. case 23:
  1215. term.c.attr.mode &= ~ATTR_ITALIC;
  1216. break;
  1217. case 24:
  1218. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1219. break;
  1220. case 25:
  1221. term.c.attr.mode &= ~ATTR_BLINK;
  1222. break;
  1223. case 27:
  1224. term.c.attr.mode &= ~ATTR_REVERSE;
  1225. break;
  1226. case 28:
  1227. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1228. break;
  1229. case 29:
  1230. term.c.attr.mode &= ~ATTR_STRUCK;
  1231. break;
  1232. case 38:
  1233. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1234. term.c.attr.fg = idx;
  1235. break;
  1236. case 39:
  1237. term.c.attr.fg = defaultfg;
  1238. break;
  1239. case 48:
  1240. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1241. term.c.attr.bg = idx;
  1242. break;
  1243. case 49:
  1244. term.c.attr.bg = defaultbg;
  1245. break;
  1246. default:
  1247. if (BETWEEN(attr[i], 30, 37)) {
  1248. term.c.attr.fg = attr[i] - 30;
  1249. } else if (BETWEEN(attr[i], 40, 47)) {
  1250. term.c.attr.bg = attr[i] - 40;
  1251. } else if (BETWEEN(attr[i], 90, 97)) {
  1252. term.c.attr.fg = attr[i] - 90 + 8;
  1253. } else if (BETWEEN(attr[i], 100, 107)) {
  1254. term.c.attr.bg = attr[i] - 100 + 8;
  1255. } else {
  1256. fprintf(stderr,
  1257. "erresc(default): gfx attr %d unknown\n",
  1258. attr[i]);
  1259. csidump();
  1260. }
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. void
  1266. tsetscroll(int t, int b)
  1267. {
  1268. int temp;
  1269. LIMIT(t, 0, term.row-1);
  1270. LIMIT(b, 0, term.row-1);
  1271. if (t > b) {
  1272. temp = t;
  1273. t = b;
  1274. b = temp;
  1275. }
  1276. term.top = t;
  1277. term.bot = b;
  1278. }
  1279. void
  1280. tsetmode(int priv, int set, const int *args, int narg)
  1281. {
  1282. int alt; const int *lim;
  1283. for (lim = args + narg; args < lim; ++args) {
  1284. if (priv) {
  1285. switch (*args) {
  1286. case 1: /* DECCKM -- Cursor key */
  1287. xsetmode(set, MODE_APPCURSOR);
  1288. break;
  1289. case 5: /* DECSCNM -- Reverse video */
  1290. xsetmode(set, MODE_REVERSE);
  1291. break;
  1292. case 6: /* DECOM -- Origin */
  1293. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1294. tmoveato(0, 0);
  1295. break;
  1296. case 7: /* DECAWM -- Auto wrap */
  1297. MODBIT(term.mode, set, MODE_WRAP);
  1298. break;
  1299. case 0: /* Error (IGNORED) */
  1300. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1301. case 3: /* DECCOLM -- Column (IGNORED) */
  1302. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1303. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1304. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1305. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1306. case 42: /* DECNRCM -- National characters (IGNORED) */
  1307. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1308. break;
  1309. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1310. xsetmode(!set, MODE_HIDE);
  1311. break;
  1312. case 9: /* X10 mouse compatibility mode */
  1313. xsetpointermotion(0);
  1314. xsetmode(0, MODE_MOUSE);
  1315. xsetmode(set, MODE_MOUSEX10);
  1316. break;
  1317. case 1000: /* 1000: report button press */
  1318. xsetpointermotion(0);
  1319. xsetmode(0, MODE_MOUSE);
  1320. xsetmode(set, MODE_MOUSEBTN);
  1321. break;
  1322. case 1002: /* 1002: report motion on button press */
  1323. xsetpointermotion(0);
  1324. xsetmode(0, MODE_MOUSE);
  1325. xsetmode(set, MODE_MOUSEMOTION);
  1326. break;
  1327. case 1003: /* 1003: enable all mouse motions */
  1328. xsetpointermotion(set);
  1329. xsetmode(0, MODE_MOUSE);
  1330. xsetmode(set, MODE_MOUSEMANY);
  1331. break;
  1332. case 1004: /* 1004: send focus events to tty */
  1333. xsetmode(set, MODE_FOCUS);
  1334. break;
  1335. case 1006: /* 1006: extended reporting mode */
  1336. xsetmode(set, MODE_MOUSESGR);
  1337. break;
  1338. case 1034:
  1339. xsetmode(set, MODE_8BIT);
  1340. break;
  1341. case 1049: /* swap screen & set/restore cursor as xterm */
  1342. if (!allowaltscreen)
  1343. break;
  1344. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1345. /* FALLTHROUGH */
  1346. case 47: /* swap screen */
  1347. case 1047:
  1348. if (!allowaltscreen)
  1349. break;
  1350. alt = IS_SET(MODE_ALTSCREEN);
  1351. if (alt) {
  1352. tclearregion(0, 0, term.col-1,
  1353. term.row-1);
  1354. }
  1355. if (set ^ alt) /* set is always 1 or 0 */
  1356. tswapscreen();
  1357. if (*args != 1049)
  1358. break;
  1359. /* FALLTHROUGH */
  1360. case 1048:
  1361. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1362. break;
  1363. case 2004: /* 2004: bracketed paste mode */
  1364. xsetmode(set, MODE_BRCKTPASTE);
  1365. break;
  1366. /* Not implemented mouse modes. See comments there. */
  1367. case 1001: /* mouse highlight mode; can hang the
  1368. terminal by design when implemented. */
  1369. case 1005: /* UTF-8 mouse mode; will confuse
  1370. applications not supporting UTF-8
  1371. and luit. */
  1372. case 1015: /* urxvt mangled mouse mode; incompatible
  1373. and can be mistaken for other control
  1374. codes. */
  1375. break;
  1376. default:
  1377. fprintf(stderr,
  1378. "erresc: unknown private set/reset mode %d\n",
  1379. *args);
  1380. break;
  1381. }
  1382. } else {
  1383. switch (*args) {
  1384. case 0: /* Error (IGNORED) */
  1385. break;
  1386. case 2:
  1387. xsetmode(set, MODE_KBDLOCK);
  1388. break;
  1389. case 4: /* IRM -- Insertion-replacement */
  1390. MODBIT(term.mode, set, MODE_INSERT);
  1391. break;
  1392. case 12: /* SRM -- Send/Receive */
  1393. MODBIT(term.mode, !set, MODE_ECHO);
  1394. break;
  1395. case 20: /* LNM -- Linefeed/new line */
  1396. MODBIT(term.mode, set, MODE_CRLF);
  1397. break;
  1398. default:
  1399. fprintf(stderr,
  1400. "erresc: unknown set/reset mode %d\n",
  1401. *args);
  1402. break;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. void
  1408. csihandle(void)
  1409. {
  1410. char buf[40];
  1411. int len;
  1412. switch (csiescseq.mode[0]) {
  1413. default:
  1414. unknown:
  1415. fprintf(stderr, "erresc: unknown csi ");
  1416. csidump();
  1417. /* die(""); */
  1418. break;
  1419. case '@': /* ICH -- Insert <n> blank char */
  1420. DEFAULT(csiescseq.arg[0], 1);
  1421. tinsertblank(csiescseq.arg[0]);
  1422. break;
  1423. case 'A': /* CUU -- Cursor <n> Up */
  1424. DEFAULT(csiescseq.arg[0], 1);
  1425. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1426. break;
  1427. case 'B': /* CUD -- Cursor <n> Down */
  1428. case 'e': /* VPR --Cursor <n> Down */
  1429. DEFAULT(csiescseq.arg[0], 1);
  1430. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1431. break;
  1432. case 'i': /* MC -- Media Copy */
  1433. switch (csiescseq.arg[0]) {
  1434. case 0:
  1435. tdump();
  1436. break;
  1437. case 1:
  1438. tdumpline(term.c.y);
  1439. break;
  1440. case 2:
  1441. tdumpsel();
  1442. break;
  1443. case 4:
  1444. term.mode &= ~MODE_PRINT;
  1445. break;
  1446. case 5:
  1447. term.mode |= MODE_PRINT;
  1448. break;
  1449. }
  1450. break;
  1451. case 'c': /* DA -- Device Attributes */
  1452. if (csiescseq.arg[0] == 0)
  1453. ttywrite(vtiden, strlen(vtiden), 0);
  1454. break;
  1455. case 'b': /* REP -- if last char is printable print it <n> more times */
  1456. DEFAULT(csiescseq.arg[0], 1);
  1457. if (term.lastc)
  1458. while (csiescseq.arg[0]-- > 0)
  1459. tputc(term.lastc);
  1460. break;
  1461. case 'C': /* CUF -- Cursor <n> Forward */
  1462. case 'a': /* HPR -- Cursor <n> Forward */
  1463. DEFAULT(csiescseq.arg[0], 1);
  1464. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1465. break;
  1466. case 'D': /* CUB -- Cursor <n> Backward */
  1467. DEFAULT(csiescseq.arg[0], 1);
  1468. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1469. break;
  1470. case 'E': /* CNL -- Cursor <n> Down and first col */
  1471. DEFAULT(csiescseq.arg[0], 1);
  1472. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1473. break;
  1474. case 'F': /* CPL -- Cursor <n> Up and first col */
  1475. DEFAULT(csiescseq.arg[0], 1);
  1476. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1477. break;
  1478. case 'g': /* TBC -- Tabulation clear */
  1479. switch (csiescseq.arg[0]) {
  1480. case 0: /* clear current tab stop */
  1481. term.tabs[term.c.x] = 0;
  1482. break;
  1483. case 3: /* clear all the tabs */
  1484. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1485. break;
  1486. default:
  1487. goto unknown;
  1488. }
  1489. break;
  1490. case 'G': /* CHA -- Move to <col> */
  1491. case '`': /* HPA */
  1492. DEFAULT(csiescseq.arg[0], 1);
  1493. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1494. break;
  1495. case 'H': /* CUP -- Move to <row> <col> */
  1496. case 'f': /* HVP */
  1497. DEFAULT(csiescseq.arg[0], 1);
  1498. DEFAULT(csiescseq.arg[1], 1);
  1499. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1500. break;
  1501. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1502. DEFAULT(csiescseq.arg[0], 1);
  1503. tputtab(csiescseq.arg[0]);
  1504. break;
  1505. case 'J': /* ED -- Clear screen */
  1506. switch (csiescseq.arg[0]) {
  1507. case 0: /* below */
  1508. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1509. if (term.c.y < term.row-1) {
  1510. tclearregion(0, term.c.y+1, term.col-1,
  1511. term.row-1);
  1512. }
  1513. break;
  1514. case 1: /* above */
  1515. if (term.c.y > 1)
  1516. tclearregion(0, 0, term.col-1, term.c.y-1);
  1517. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1518. break;
  1519. case 2: /* all */
  1520. tclearregion(0, 0, term.col-1, term.row-1);
  1521. break;
  1522. default:
  1523. goto unknown;
  1524. }
  1525. break;
  1526. case 'K': /* EL -- Clear line */
  1527. switch (csiescseq.arg[0]) {
  1528. case 0: /* right */
  1529. tclearregion(term.c.x, term.c.y, term.col-1,
  1530. term.c.y);
  1531. break;
  1532. case 1: /* left */
  1533. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1534. break;
  1535. case 2: /* all */
  1536. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1537. break;
  1538. }
  1539. break;
  1540. case 'S': /* SU -- Scroll <n> line up */
  1541. DEFAULT(csiescseq.arg[0], 1);
  1542. tscrollup(term.top, csiescseq.arg[0]);
  1543. break;
  1544. case 'T': /* SD -- Scroll <n> line down */
  1545. DEFAULT(csiescseq.arg[0], 1);
  1546. tscrolldown(term.top, csiescseq.arg[0]);
  1547. break;
  1548. case 'L': /* IL -- Insert <n> blank lines */
  1549. DEFAULT(csiescseq.arg[0], 1);
  1550. tinsertblankline(csiescseq.arg[0]);
  1551. break;
  1552. case 'l': /* RM -- Reset Mode */
  1553. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1554. break;
  1555. case 'M': /* DL -- Delete <n> lines */
  1556. DEFAULT(csiescseq.arg[0], 1);
  1557. tdeleteline(csiescseq.arg[0]);
  1558. break;
  1559. case 'X': /* ECH -- Erase <n> char */
  1560. DEFAULT(csiescseq.arg[0], 1);
  1561. tclearregion(term.c.x, term.c.y,
  1562. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1563. break;
  1564. case 'P': /* DCH -- Delete <n> char */
  1565. DEFAULT(csiescseq.arg[0], 1);
  1566. tdeletechar(csiescseq.arg[0]);
  1567. break;
  1568. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1569. DEFAULT(csiescseq.arg[0], 1);
  1570. tputtab(-csiescseq.arg[0]);
  1571. break;
  1572. case 'd': /* VPA -- Move to <row> */
  1573. DEFAULT(csiescseq.arg[0], 1);
  1574. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1575. break;
  1576. case 'h': /* SM -- Set terminal mode */
  1577. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1578. break;
  1579. case 'm': /* SGR -- Terminal attribute (color) */
  1580. tsetattr(csiescseq.arg, csiescseq.narg);
  1581. break;
  1582. case 'n': /* DSR – Device Status Report (cursor position) */
  1583. if (csiescseq.arg[0] == 6) {
  1584. len = snprintf(buf, sizeof(buf), "\033[%i;%iR",
  1585. term.c.y+1, term.c.x+1);
  1586. ttywrite(buf, len, 0);
  1587. }
  1588. break;
  1589. case 'r': /* DECSTBM -- Set Scrolling Region */
  1590. if (csiescseq.priv) {
  1591. goto unknown;
  1592. } else {
  1593. DEFAULT(csiescseq.arg[0], 1);
  1594. DEFAULT(csiescseq.arg[1], term.row);
  1595. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1596. tmoveato(0, 0);
  1597. }
  1598. break;
  1599. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1600. tcursor(CURSOR_SAVE);
  1601. break;
  1602. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1603. tcursor(CURSOR_LOAD);
  1604. break;
  1605. case ' ':
  1606. switch (csiescseq.mode[1]) {
  1607. case 'q': /* DECSCUSR -- Set Cursor Style */
  1608. if (xsetcursor(csiescseq.arg[0]))
  1609. goto unknown;
  1610. break;
  1611. default:
  1612. goto unknown;
  1613. }
  1614. break;
  1615. }
  1616. }
  1617. void
  1618. csidump(void)
  1619. {
  1620. size_t i;
  1621. uint c;
  1622. fprintf(stderr, "ESC[");
  1623. for (i = 0; i < csiescseq.len; i++) {
  1624. c = csiescseq.buf[i] & 0xff;
  1625. if (isprint(c)) {
  1626. putc(c, stderr);
  1627. } else if (c == '\n') {
  1628. fprintf(stderr, "(\\n)");
  1629. } else if (c == '\r') {
  1630. fprintf(stderr, "(\\r)");
  1631. } else if (c == 0x1b) {
  1632. fprintf(stderr, "(\\e)");
  1633. } else {
  1634. fprintf(stderr, "(%02x)", c);
  1635. }
  1636. }
  1637. putc('\n', stderr);
  1638. }
  1639. void
  1640. csireset(void)
  1641. {
  1642. memset(&csiescseq, 0, sizeof(csiescseq));
  1643. }
  1644. void
  1645. osc4_color_response(int num)
  1646. {
  1647. int n;
  1648. char buf[32];
  1649. unsigned char r, g, b;
  1650. if (xgetcolor(num, &r, &g, &b)) {
  1651. fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num);
  1652. return;
  1653. }
  1654. n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
  1655. num, r, r, g, g, b, b);
  1656. ttywrite(buf, n, 1);
  1657. }
  1658. void
  1659. osc_color_response(int index, int num)
  1660. {
  1661. int n;
  1662. char buf[32];
  1663. unsigned char r, g, b;
  1664. if (xgetcolor(index, &r, &g, &b)) {
  1665. fprintf(stderr, "erresc: failed to fetch osc color %d\n", index);
  1666. return;
  1667. }
  1668. n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007",
  1669. num, r, r, g, g, b, b);
  1670. ttywrite(buf, n, 1);
  1671. }
  1672. void
  1673. strhandle(void)
  1674. {
  1675. char *p = NULL, *dec;
  1676. int j, narg, par;
  1677. term.esc &= ~(ESC_STR_END|ESC_STR);
  1678. strparse();
  1679. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  1680. switch (strescseq.type) {
  1681. case ']': /* OSC -- Operating System Command */
  1682. switch (par) {
  1683. case 0:
  1684. if (narg > 1) {
  1685. xsettitle(strescseq.args[1]);
  1686. xseticontitle(strescseq.args[1]);
  1687. }
  1688. return;
  1689. case 1:
  1690. if (narg > 1)
  1691. xseticontitle(strescseq.args[1]);
  1692. return;
  1693. case 2:
  1694. if (narg > 1)
  1695. xsettitle(strescseq.args[1]);
  1696. return;
  1697. case 52:
  1698. if (narg > 2 && allowwindowops) {
  1699. dec = base64dec(strescseq.args[2]);
  1700. if (dec) {
  1701. xsetsel(dec);
  1702. xclipcopy();
  1703. } else {
  1704. fprintf(stderr, "erresc: invalid base64\n");
  1705. }
  1706. }
  1707. return;
  1708. case 10:
  1709. if (narg < 2)
  1710. break;
  1711. p = strescseq.args[1];
  1712. if (!strcmp(p, "?"))
  1713. osc_color_response(defaultfg, 10);
  1714. else if (xsetcolorname(defaultfg, p))
  1715. fprintf(stderr, "erresc: invalid foreground color: %s\n", p);
  1716. else
  1717. tfulldirt();
  1718. return;
  1719. case 11:
  1720. if (narg < 2)
  1721. break;
  1722. p = strescseq.args[1];
  1723. if (!strcmp(p, "?"))
  1724. osc_color_response(defaultbg, 11);
  1725. else if (xsetcolorname(defaultbg, p))
  1726. fprintf(stderr, "erresc: invalid background color: %s\n", p);
  1727. else
  1728. tfulldirt();
  1729. return;
  1730. case 12:
  1731. if (narg < 2)
  1732. break;
  1733. p = strescseq.args[1];
  1734. if (!strcmp(p, "?"))
  1735. osc_color_response(defaultcs, 12);
  1736. else if (xsetcolorname(defaultcs, p))
  1737. fprintf(stderr, "erresc: invalid cursor color: %s\n", p);
  1738. else
  1739. tfulldirt();
  1740. return;
  1741. case 4: /* color set */
  1742. if (narg < 3)
  1743. break;
  1744. p = strescseq.args[2];
  1745. /* FALLTHROUGH */
  1746. case 104: /* color reset */
  1747. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1748. if (p && !strcmp(p, "?"))
  1749. osc4_color_response(j);
  1750. else if (xsetcolorname(j, p)) {
  1751. if (par == 104 && narg <= 1)
  1752. return; /* color reset without parameter */
  1753. fprintf(stderr, "erresc: invalid color j=%d, p=%s\n",
  1754. j, p ? p : "(null)");
  1755. } else {
  1756. /*
  1757. * TODO if defaultbg color is changed, borders
  1758. * are dirty
  1759. */
  1760. tfulldirt();
  1761. }
  1762. return;
  1763. }
  1764. break;
  1765. case 'k': /* old title set compatibility */
  1766. xsettitle(strescseq.args[0]);
  1767. return;
  1768. case 'P': /* DCS -- Device Control String */
  1769. case '_': /* APC -- Application Program Command */
  1770. case '^': /* PM -- Privacy Message */
  1771. return;
  1772. }
  1773. fprintf(stderr, "erresc: unknown str ");
  1774. strdump();
  1775. }
  1776. void
  1777. strparse(void)
  1778. {
  1779. int c;
  1780. char *p = strescseq.buf;
  1781. strescseq.narg = 0;
  1782. strescseq.buf[strescseq.len] = '\0';
  1783. if (*p == '\0')
  1784. return;
  1785. while (strescseq.narg < STR_ARG_SIZ) {
  1786. strescseq.args[strescseq.narg++] = p;
  1787. while ((c = *p) != ';' && c != '\0')
  1788. ++p;
  1789. if (c == '\0')
  1790. return;
  1791. *p++ = '\0';
  1792. }
  1793. }
  1794. void
  1795. strdump(void)
  1796. {
  1797. size_t i;
  1798. uint c;
  1799. fprintf(stderr, "ESC%c", strescseq.type);
  1800. for (i = 0; i < strescseq.len; i++) {
  1801. c = strescseq.buf[i] & 0xff;
  1802. if (c == '\0') {
  1803. putc('\n', stderr);
  1804. return;
  1805. } else if (isprint(c)) {
  1806. putc(c, stderr);
  1807. } else if (c == '\n') {
  1808. fprintf(stderr, "(\\n)");
  1809. } else if (c == '\r') {
  1810. fprintf(stderr, "(\\r)");
  1811. } else if (c == 0x1b) {
  1812. fprintf(stderr, "(\\e)");
  1813. } else {
  1814. fprintf(stderr, "(%02x)", c);
  1815. }
  1816. }
  1817. fprintf(stderr, "ESC\\\n");
  1818. }
  1819. void
  1820. strreset(void)
  1821. {
  1822. strescseq = (STREscape){
  1823. .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
  1824. .siz = STR_BUF_SIZ,
  1825. };
  1826. }
  1827. void
  1828. sendbreak(const Arg *arg)
  1829. {
  1830. if (tcsendbreak(cmdfd, 0))
  1831. perror("Error sending break");
  1832. }
  1833. void
  1834. tprinter(char *s, size_t len)
  1835. {
  1836. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  1837. perror("Error writing to output file");
  1838. close(iofd);
  1839. iofd = -1;
  1840. }
  1841. }
  1842. void
  1843. toggleprinter(const Arg *arg)
  1844. {
  1845. term.mode ^= MODE_PRINT;
  1846. }
  1847. void
  1848. printscreen(const Arg *arg)
  1849. {
  1850. tdump();
  1851. }
  1852. void
  1853. printsel(const Arg *arg)
  1854. {
  1855. tdumpsel();
  1856. }
  1857. void
  1858. tdumpsel(void)
  1859. {
  1860. char *ptr;
  1861. if ((ptr = getsel())) {
  1862. tprinter(ptr, strlen(ptr));
  1863. free(ptr);
  1864. }
  1865. }
  1866. void
  1867. tdumpline(int n)
  1868. {
  1869. char buf[UTF_SIZ];
  1870. const Glyph *bp, *end;
  1871. bp = &term.line[n][0];
  1872. end = &bp[MIN(tlinelen(n), term.col) - 1];
  1873. if (bp != end || bp->u != ' ') {
  1874. for ( ; bp <= end; ++bp)
  1875. tprinter(buf, utf8encode(bp->u, buf));
  1876. }
  1877. tprinter("\n", 1);
  1878. }
  1879. void
  1880. tdump(void)
  1881. {
  1882. int i;
  1883. for (i = 0; i < term.row; ++i)
  1884. tdumpline(i);
  1885. }
  1886. void
  1887. tputtab(int n)
  1888. {
  1889. uint x = term.c.x;
  1890. if (n > 0) {
  1891. while (x < term.col && n--)
  1892. for (++x; x < term.col && !term.tabs[x]; ++x)
  1893. /* nothing */ ;
  1894. } else if (n < 0) {
  1895. while (x > 0 && n++)
  1896. for (--x; x > 0 && !term.tabs[x]; --x)
  1897. /* nothing */ ;
  1898. }
  1899. term.c.x = LIMIT(x, 0, term.col-1);
  1900. }
  1901. void
  1902. tdefutf8(char ascii)
  1903. {
  1904. if (ascii == 'G')
  1905. term.mode |= MODE_UTF8;
  1906. else if (ascii == '@')
  1907. term.mode &= ~MODE_UTF8;
  1908. }
  1909. void
  1910. tdeftran(char ascii)
  1911. {
  1912. static char cs[] = "0B";
  1913. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  1914. char *p;
  1915. if ((p = strchr(cs, ascii)) == NULL) {
  1916. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1917. } else {
  1918. term.trantbl[term.icharset] = vcs[p - cs];
  1919. }
  1920. }
  1921. void
  1922. tdectest(char c)
  1923. {
  1924. int x, y;
  1925. if (c == '8') { /* DEC screen alignment test. */
  1926. for (x = 0; x < term.col; ++x) {
  1927. for (y = 0; y < term.row; ++y)
  1928. tsetchar('E', &term.c.attr, x, y);
  1929. }
  1930. }
  1931. }
  1932. void
  1933. tstrsequence(uchar c)
  1934. {
  1935. switch (c) {
  1936. case 0x90: /* DCS -- Device Control String */
  1937. c = 'P';
  1938. break;
  1939. case 0x9f: /* APC -- Application Program Command */
  1940. c = '_';
  1941. break;
  1942. case 0x9e: /* PM -- Privacy Message */
  1943. c = '^';
  1944. break;
  1945. case 0x9d: /* OSC -- Operating System Command */
  1946. c = ']';
  1947. break;
  1948. }
  1949. strreset();
  1950. strescseq.type = c;
  1951. term.esc |= ESC_STR;
  1952. }
  1953. void
  1954. tcontrolcode(uchar ascii)
  1955. {
  1956. switch (ascii) {
  1957. case '\t': /* HT */
  1958. tputtab(1);
  1959. return;
  1960. case '\b': /* BS */
  1961. tmoveto(term.c.x-1, term.c.y);
  1962. return;
  1963. case '\r': /* CR */
  1964. tmoveto(0, term.c.y);
  1965. return;
  1966. case '\f': /* LF */
  1967. case '\v': /* VT */
  1968. case '\n': /* LF */
  1969. /* go to first col if the mode is set */
  1970. tnewline(IS_SET(MODE_CRLF));
  1971. return;
  1972. case '\a': /* BEL */
  1973. if (term.esc & ESC_STR_END) {
  1974. /* backwards compatibility to xterm */
  1975. strhandle();
  1976. } else {
  1977. xbell();
  1978. }
  1979. break;
  1980. case '\033': /* ESC */
  1981. csireset();
  1982. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  1983. term.esc |= ESC_START;
  1984. return;
  1985. case '\016': /* SO (LS1 -- Locking shift 1) */
  1986. case '\017': /* SI (LS0 -- Locking shift 0) */
  1987. term.charset = 1 - (ascii - '\016');
  1988. return;
  1989. case '\032': /* SUB */
  1990. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  1991. /* FALLTHROUGH */
  1992. case '\030': /* CAN */
  1993. csireset();
  1994. break;
  1995. case '\005': /* ENQ (IGNORED) */
  1996. case '\000': /* NUL (IGNORED) */
  1997. case '\021': /* XON (IGNORED) */
  1998. case '\023': /* XOFF (IGNORED) */
  1999. case 0177: /* DEL (IGNORED) */
  2000. return;
  2001. case 0x80: /* TODO: PAD */
  2002. case 0x81: /* TODO: HOP */
  2003. case 0x82: /* TODO: BPH */
  2004. case 0x83: /* TODO: NBH */
  2005. case 0x84: /* TODO: IND */
  2006. break;
  2007. case 0x85: /* NEL -- Next line */
  2008. tnewline(1); /* always go to first col */
  2009. break;
  2010. case 0x86: /* TODO: SSA */
  2011. case 0x87: /* TODO: ESA */
  2012. break;
  2013. case 0x88: /* HTS -- Horizontal tab stop */
  2014. term.tabs[term.c.x] = 1;
  2015. break;
  2016. case 0x89: /* TODO: HTJ */
  2017. case 0x8a: /* TODO: VTS */
  2018. case 0x8b: /* TODO: PLD */
  2019. case 0x8c: /* TODO: PLU */
  2020. case 0x8d: /* TODO: RI */
  2021. case 0x8e: /* TODO: SS2 */
  2022. case 0x8f: /* TODO: SS3 */
  2023. case 0x91: /* TODO: PU1 */
  2024. case 0x92: /* TODO: PU2 */
  2025. case 0x93: /* TODO: STS */
  2026. case 0x94: /* TODO: CCH */
  2027. case 0x95: /* TODO: MW */
  2028. case 0x96: /* TODO: SPA */
  2029. case 0x97: /* TODO: EPA */
  2030. case 0x98: /* TODO: SOS */
  2031. case 0x99: /* TODO: SGCI */
  2032. break;
  2033. case 0x9a: /* DECID -- Identify Terminal */
  2034. ttywrite(vtiden, strlen(vtiden), 0);
  2035. break;
  2036. case 0x9b: /* TODO: CSI */
  2037. case 0x9c: /* TODO: ST */
  2038. break;
  2039. case 0x90: /* DCS -- Device Control String */
  2040. case 0x9d: /* OSC -- Operating System Command */
  2041. case 0x9e: /* PM -- Privacy Message */
  2042. case 0x9f: /* APC -- Application Program Command */
  2043. tstrsequence(ascii);
  2044. return;
  2045. }
  2046. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  2047. term.esc &= ~(ESC_STR_END|ESC_STR);
  2048. }
  2049. /*
  2050. * returns 1 when the sequence is finished and it hasn't to read
  2051. * more characters for this sequence, otherwise 0
  2052. */
  2053. int
  2054. eschandle(uchar ascii)
  2055. {
  2056. switch (ascii) {
  2057. case '[':
  2058. term.esc |= ESC_CSI;
  2059. return 0;
  2060. case '#':
  2061. term.esc |= ESC_TEST;
  2062. return 0;
  2063. case '%':
  2064. term.esc |= ESC_UTF8;
  2065. return 0;
  2066. case 'P': /* DCS -- Device Control String */
  2067. case '_': /* APC -- Application Program Command */
  2068. case '^': /* PM -- Privacy Message */
  2069. case ']': /* OSC -- Operating System Command */
  2070. case 'k': /* old title set compatibility */
  2071. tstrsequence(ascii);
  2072. return 0;
  2073. case 'n': /* LS2 -- Locking shift 2 */
  2074. case 'o': /* LS3 -- Locking shift 3 */
  2075. term.charset = 2 + (ascii - 'n');
  2076. break;
  2077. case '(': /* GZD4 -- set primary charset G0 */
  2078. case ')': /* G1D4 -- set secondary charset G1 */
  2079. case '*': /* G2D4 -- set tertiary charset G2 */
  2080. case '+': /* G3D4 -- set quaternary charset G3 */
  2081. term.icharset = ascii - '(';
  2082. term.esc |= ESC_ALTCHARSET;
  2083. return 0;
  2084. case 'D': /* IND -- Linefeed */
  2085. if (term.c.y == term.bot) {
  2086. tscrollup(term.top, 1);
  2087. } else {
  2088. tmoveto(term.c.x, term.c.y+1);
  2089. }
  2090. break;
  2091. case 'E': /* NEL -- Next line */
  2092. tnewline(1); /* always go to first col */
  2093. break;
  2094. case 'H': /* HTS -- Horizontal tab stop */
  2095. term.tabs[term.c.x] = 1;
  2096. break;
  2097. case 'M': /* RI -- Reverse index */
  2098. if (term.c.y == term.top) {
  2099. tscrolldown(term.top, 1);
  2100. } else {
  2101. tmoveto(term.c.x, term.c.y-1);
  2102. }
  2103. break;
  2104. case 'Z': /* DECID -- Identify Terminal */
  2105. ttywrite(vtiden, strlen(vtiden), 0);
  2106. break;
  2107. case 'c': /* RIS -- Reset to initial state */
  2108. treset();
  2109. resettitle();
  2110. xloadcols();
  2111. break;
  2112. case '=': /* DECPAM -- Application keypad */
  2113. xsetmode(1, MODE_APPKEYPAD);
  2114. break;
  2115. case '>': /* DECPNM -- Normal keypad */
  2116. xsetmode(0, MODE_APPKEYPAD);
  2117. break;
  2118. case '7': /* DECSC -- Save Cursor */
  2119. tcursor(CURSOR_SAVE);
  2120. break;
  2121. case '8': /* DECRC -- Restore Cursor */
  2122. tcursor(CURSOR_LOAD);
  2123. break;
  2124. case '\\': /* ST -- String Terminator */
  2125. if (term.esc & ESC_STR_END)
  2126. strhandle();
  2127. break;
  2128. default:
  2129. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2130. (uchar) ascii, isprint(ascii)? ascii:'.');
  2131. break;
  2132. }
  2133. return 1;
  2134. }
  2135. void
  2136. tputc(Rune u)
  2137. {
  2138. char c[UTF_SIZ];
  2139. int control;
  2140. int width, len;
  2141. Glyph *gp;
  2142. control = ISCONTROL(u);
  2143. if (u < 127 || !IS_SET(MODE_UTF8)) {
  2144. c[0] = u;
  2145. width = len = 1;
  2146. } else {
  2147. len = utf8encode(u, c);
  2148. if (!control && (width = wcwidth(u)) == -1)
  2149. width = 1;
  2150. }
  2151. if (IS_SET(MODE_PRINT))
  2152. tprinter(c, len);
  2153. /*
  2154. * STR sequence must be checked before anything else
  2155. * because it uses all following characters until it
  2156. * receives a ESC, a SUB, a ST or any other C1 control
  2157. * character.
  2158. */
  2159. if (term.esc & ESC_STR) {
  2160. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2161. ISCONTROLC1(u)) {
  2162. term.esc &= ~(ESC_START|ESC_STR);
  2163. term.esc |= ESC_STR_END;
  2164. goto check_control_code;
  2165. }
  2166. if (strescseq.len+len >= strescseq.siz) {
  2167. /*
  2168. * Here is a bug in terminals. If the user never sends
  2169. * some code to stop the str or esc command, then st
  2170. * will stop responding. But this is better than
  2171. * silently failing with unknown characters. At least
  2172. * then users will report back.
  2173. *
  2174. * In the case users ever get fixed, here is the code:
  2175. */
  2176. /*
  2177. * term.esc = 0;
  2178. * strhandle();
  2179. */
  2180. if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
  2181. return;
  2182. strescseq.siz *= 2;
  2183. strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
  2184. }
  2185. memmove(&strescseq.buf[strescseq.len], c, len);
  2186. strescseq.len += len;
  2187. return;
  2188. }
  2189. check_control_code:
  2190. /*
  2191. * Actions of control codes must be performed as soon they arrive
  2192. * because they can be embedded inside a control sequence, and
  2193. * they must not cause conflicts with sequences.
  2194. */
  2195. if (control) {
  2196. tcontrolcode(u);
  2197. /*
  2198. * control codes are not shown ever
  2199. */
  2200. if (!term.esc)
  2201. term.lastc = 0;
  2202. return;
  2203. } else if (term.esc & ESC_START) {
  2204. if (term.esc & ESC_CSI) {
  2205. csiescseq.buf[csiescseq.len++] = u;
  2206. if (BETWEEN(u, 0x40, 0x7E)
  2207. || csiescseq.len >= \
  2208. sizeof(csiescseq.buf)-1) {
  2209. term.esc = 0;
  2210. csiparse();
  2211. csihandle();
  2212. }
  2213. return;
  2214. } else if (term.esc & ESC_UTF8) {
  2215. tdefutf8(u);
  2216. } else if (term.esc & ESC_ALTCHARSET) {
  2217. tdeftran(u);
  2218. } else if (term.esc & ESC_TEST) {
  2219. tdectest(u);
  2220. } else {
  2221. if (!eschandle(u))
  2222. return;
  2223. /* sequence already finished */
  2224. }
  2225. term.esc = 0;
  2226. /*
  2227. * All characters which form part of a sequence are not
  2228. * printed
  2229. */
  2230. return;
  2231. }
  2232. if (selected(term.c.x, term.c.y))
  2233. selclear();
  2234. gp = &term.line[term.c.y][term.c.x];
  2235. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2236. gp->mode |= ATTR_WRAP;
  2237. tnewline(1);
  2238. gp = &term.line[term.c.y][term.c.x];
  2239. }
  2240. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2241. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2242. if (term.c.x+width > term.col) {
  2243. tnewline(1);
  2244. gp = &term.line[term.c.y][term.c.x];
  2245. }
  2246. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2247. term.lastc = u;
  2248. if (width == 2) {
  2249. gp->mode |= ATTR_WIDE;
  2250. if (term.c.x+1 < term.col) {
  2251. if (gp[1].mode == ATTR_WIDE && term.c.x+2 < term.col) {
  2252. gp[2].u = ' ';
  2253. gp[2].mode &= ~ATTR_WDUMMY;
  2254. }
  2255. gp[1].u = '\0';
  2256. gp[1].mode = ATTR_WDUMMY;
  2257. }
  2258. }
  2259. if (term.c.x+width < term.col) {
  2260. tmoveto(term.c.x+width, term.c.y);
  2261. } else {
  2262. term.c.state |= CURSOR_WRAPNEXT;
  2263. }
  2264. }
  2265. int
  2266. twrite(const char *buf, int buflen, int show_ctrl)
  2267. {
  2268. int charsize;
  2269. Rune u;
  2270. int n;
  2271. for (n = 0; n < buflen; n += charsize) {
  2272. if (IS_SET(MODE_UTF8)) {
  2273. /* process a complete utf8 char */
  2274. charsize = utf8decode(buf + n, &u, buflen - n);
  2275. if (charsize == 0)
  2276. break;
  2277. } else {
  2278. u = buf[n] & 0xFF;
  2279. charsize = 1;
  2280. }
  2281. if (show_ctrl && ISCONTROL(u)) {
  2282. if (u & 0x80) {
  2283. u &= 0x7f;
  2284. tputc('^');
  2285. tputc('[');
  2286. } else if (u != '\n' && u != '\r' && u != '\t') {
  2287. u ^= 0x40;
  2288. tputc('^');
  2289. }
  2290. }
  2291. tputc(u);
  2292. }
  2293. return n;
  2294. }
  2295. void
  2296. tresize(int col, int row)
  2297. {
  2298. int i;
  2299. int minrow = MIN(row, term.row);
  2300. int mincol = MIN(col, term.col);
  2301. int *bp;
  2302. TCursor c;
  2303. if (col < 1 || row < 1) {
  2304. fprintf(stderr,
  2305. "tresize: error resizing to %dx%d\n", col, row);
  2306. return;
  2307. }
  2308. /*
  2309. * slide screen to keep cursor where we expect it -
  2310. * tscrollup would work here, but we can optimize to
  2311. * memmove because we're freeing the earlier lines
  2312. */
  2313. for (i = 0; i <= term.c.y - row; i++) {
  2314. free(term.line[i]);
  2315. free(term.alt[i]);
  2316. }
  2317. /* ensure that both src and dst are not NULL */
  2318. if (i > 0) {
  2319. memmove(term.line, term.line + i, row * sizeof(Line));
  2320. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2321. }
  2322. for (i += row; i < term.row; i++) {
  2323. free(term.line[i]);
  2324. free(term.alt[i]);
  2325. }
  2326. /* resize to new height */
  2327. term.line = xrealloc(term.line, row * sizeof(Line));
  2328. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2329. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2330. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2331. /* resize each row to new width, zero-pad if needed */
  2332. for (i = 0; i < minrow; i++) {
  2333. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2334. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2335. }
  2336. /* allocate any new rows */
  2337. for (/* i = minrow */; i < row; i++) {
  2338. term.line[i] = xmalloc(col * sizeof(Glyph));
  2339. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2340. }
  2341. if (col > term.col) {
  2342. bp = term.tabs + term.col;
  2343. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2344. while (--bp > term.tabs && !*bp)
  2345. /* nothing */ ;
  2346. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2347. *bp = 1;
  2348. }
  2349. /* update terminal size */
  2350. term.col = col;
  2351. term.row = row;
  2352. /* reset scrolling region */
  2353. tsetscroll(0, row-1);
  2354. /* make use of the LIMIT in tmoveto */
  2355. tmoveto(term.c.x, term.c.y);
  2356. /* Clearing both screens (it makes dirty all lines) */
  2357. c = term.c;
  2358. for (i = 0; i < 2; i++) {
  2359. if (mincol < col && 0 < minrow) {
  2360. tclearregion(mincol, 0, col - 1, minrow - 1);
  2361. }
  2362. if (0 < col && minrow < row) {
  2363. tclearregion(0, minrow, col - 1, row - 1);
  2364. }
  2365. tswapscreen();
  2366. tcursor(CURSOR_LOAD);
  2367. }
  2368. term.c = c;
  2369. }
  2370. void
  2371. resettitle(void)
  2372. {
  2373. xsettitle(NULL);
  2374. }
  2375. void
  2376. drawregion(int x1, int y1, int x2, int y2)
  2377. {
  2378. int y;
  2379. for (y = y1; y < y2; y++) {
  2380. if (!term.dirty[y])
  2381. continue;
  2382. term.dirty[y] = 0;
  2383. xdrawline(term.line[y], x1, y, x2);
  2384. }
  2385. }
  2386. void
  2387. draw(void)
  2388. {
  2389. int cx = term.c.x, ocx = term.ocx, ocy = term.ocy;
  2390. if (!xstartdraw())
  2391. return;
  2392. /* adjust cursor position */
  2393. LIMIT(term.ocx, 0, term.col-1);
  2394. LIMIT(term.ocy, 0, term.row-1);
  2395. if (term.line[term.ocy][term.ocx].mode & ATTR_WDUMMY)
  2396. term.ocx--;
  2397. if (term.line[term.c.y][cx].mode & ATTR_WDUMMY)
  2398. cx--;
  2399. drawregion(0, 0, term.col, term.row);
  2400. xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  2401. term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
  2402. term.ocx = cx;
  2403. term.ocy = term.c.y;
  2404. xfinishdraw();
  2405. if (ocx != term.ocx || ocy != term.ocy)
  2406. xximspot(term.ocx, term.ocy);
  2407. }
  2408. void
  2409. redraw(void)
  2410. {
  2411. tfulldirt();
  2412. draw();
  2413. }