st.c 55 KB

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