st.c.orig 56 KB

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