st.c 54 KB

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