st.c.orig 56 KB

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