st.c 60 KB

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