st.c 54 KB

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