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