st.c 54 KB

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