st.c 54 KB

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