st.c 55 KB

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