st.c 57 KB

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