st.c 56 KB

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