st.c 56 KB

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