st.c 56 KB

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