st.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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 \"$WINDOWID\" -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. signal(SIGCHLD, SIG_DFL);
  610. signal(SIGHUP, SIG_DFL);
  611. signal(SIGINT, SIG_DFL);
  612. signal(SIGQUIT, SIG_DFL);
  613. signal(SIGTERM, SIG_DFL);
  614. signal(SIGALRM, SIG_DFL);
  615. execvp(prog, args);
  616. _exit(1);
  617. }
  618. void
  619. sigchld(int a)
  620. {
  621. int stat;
  622. pid_t p;
  623. if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
  624. die("Waiting for pid %hd failed: %s\n", pid, strerror(errno));
  625. if (pid != p)
  626. return;
  627. if (!WIFEXITED(stat) || WEXITSTATUS(stat))
  628. die("child finished with error '%d'\n", stat);
  629. exit(0);
  630. }
  631. void
  632. stty(void)
  633. {
  634. char cmd[_POSIX_ARG_MAX], **p, *q, *s;
  635. size_t n, siz;
  636. if ((n = strlen(stty_args)) > sizeof(cmd)-1)
  637. die("incorrect stty parameters\n");
  638. memcpy(cmd, stty_args, n);
  639. q = cmd + n;
  640. siz = sizeof(cmd) - n;
  641. for (p = opt_cmd; p && (s = *p); ++p) {
  642. if ((n = strlen(s)) > siz-1)
  643. die("stty parameter length too long\n");
  644. *q++ = ' ';
  645. memcpy(q, s, n);
  646. q += n;
  647. siz -= n + 1;
  648. }
  649. *q = '\0';
  650. if (system(cmd) != 0)
  651. perror("Couldn't call stty");
  652. }
  653. void
  654. ttynew(void)
  655. {
  656. int m, s;
  657. struct winsize w = {term.row, term.col, 0, 0};
  658. if (opt_io) {
  659. term.mode |= MODE_PRINT;
  660. iofd = (!strcmp(opt_io, "-")) ?
  661. 1 : open(opt_io, O_WRONLY | O_CREAT, 0666);
  662. if (iofd < 0) {
  663. fprintf(stderr, "Error opening %s:%s\n",
  664. opt_io, strerror(errno));
  665. }
  666. }
  667. if (opt_line) {
  668. if ((cmdfd = open(opt_line, O_RDWR)) < 0)
  669. die("open line failed: %s\n", strerror(errno));
  670. dup2(cmdfd, 0);
  671. stty();
  672. return;
  673. }
  674. /* seems to work fine on linux, openbsd and freebsd */
  675. if (openpty(&m, &s, NULL, NULL, &w) < 0)
  676. die("openpty failed: %s\n", strerror(errno));
  677. switch (pid = fork()) {
  678. case -1:
  679. die("fork failed\n");
  680. break;
  681. case 0:
  682. close(iofd);
  683. setsid(); /* create a new process group */
  684. dup2(s, 0);
  685. dup2(s, 1);
  686. dup2(s, 2);
  687. if (ioctl(s, TIOCSCTTY, NULL) < 0)
  688. die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
  689. close(s);
  690. close(m);
  691. execsh();
  692. break;
  693. default:
  694. close(s);
  695. cmdfd = m;
  696. signal(SIGCHLD, sigchld);
  697. break;
  698. }
  699. }
  700. size_t
  701. ttyread(void)
  702. {
  703. static char buf[BUFSIZ];
  704. static int buflen = 0;
  705. char *ptr;
  706. int charsize; /* size of utf8 char in bytes */
  707. Rune unicodep;
  708. int ret;
  709. /* append read bytes to unprocessed bytes */
  710. if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  711. die("Couldn't read from shell: %s\n", strerror(errno));
  712. buflen += ret;
  713. ptr = buf;
  714. for (;;) {
  715. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  716. /* process a complete utf8 char */
  717. charsize = utf8decode(ptr, &unicodep, buflen);
  718. if (charsize == 0)
  719. break;
  720. tputc(unicodep);
  721. ptr += charsize;
  722. buflen -= charsize;
  723. } else {
  724. if (buflen <= 0)
  725. break;
  726. tputc(*ptr++ & 0xFF);
  727. buflen--;
  728. }
  729. }
  730. /* keep any uncomplete utf8 char for the next call */
  731. if (buflen > 0)
  732. memmove(buf, ptr, buflen);
  733. return ret;
  734. }
  735. void
  736. ttywrite(const char *s, size_t n)
  737. {
  738. fd_set wfd, rfd;
  739. ssize_t r;
  740. size_t lim = 256;
  741. /*
  742. * Remember that we are using a pty, which might be a modem line.
  743. * Writing too much will clog the line. That's why we are doing this
  744. * dance.
  745. * FIXME: Migrate the world to Plan 9.
  746. */
  747. while (n > 0) {
  748. FD_ZERO(&wfd);
  749. FD_ZERO(&rfd);
  750. FD_SET(cmdfd, &wfd);
  751. FD_SET(cmdfd, &rfd);
  752. /* Check if we can write. */
  753. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  754. if (errno == EINTR)
  755. continue;
  756. die("select failed: %s\n", strerror(errno));
  757. }
  758. if (FD_ISSET(cmdfd, &wfd)) {
  759. /*
  760. * Only write the bytes written by ttywrite() or the
  761. * default of 256. This seems to be a reasonable value
  762. * for a serial line. Bigger values might clog the I/O.
  763. */
  764. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  765. goto write_error;
  766. if (r < n) {
  767. /*
  768. * We weren't able to write out everything.
  769. * This means the buffer is getting full
  770. * again. Empty it.
  771. */
  772. if (n < lim)
  773. lim = ttyread();
  774. n -= r;
  775. s += r;
  776. } else {
  777. /* All bytes have been written. */
  778. break;
  779. }
  780. }
  781. if (FD_ISSET(cmdfd, &rfd))
  782. lim = ttyread();
  783. }
  784. return;
  785. write_error:
  786. die("write error on tty: %s\n", strerror(errno));
  787. }
  788. void
  789. ttysend(char *s, size_t n)
  790. {
  791. int len;
  792. char *t, *lim;
  793. Rune u;
  794. ttywrite(s, n);
  795. if (!IS_SET(MODE_ECHO))
  796. return;
  797. lim = &s[n];
  798. for (t = s; t < lim; t += len) {
  799. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  800. len = utf8decode(t, &u, n);
  801. } else {
  802. u = *t & 0xFF;
  803. len = 1;
  804. }
  805. if (len <= 0)
  806. break;
  807. techo(u);
  808. n -= len;
  809. }
  810. }
  811. void
  812. ttyresize(void)
  813. {
  814. struct winsize w;
  815. w.ws_row = term.row;
  816. w.ws_col = term.col;
  817. w.ws_xpixel = win.tw;
  818. w.ws_ypixel = win.th;
  819. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  820. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  821. }
  822. int
  823. tattrset(int attr)
  824. {
  825. int i, j;
  826. for (i = 0; i < term.row-1; i++) {
  827. for (j = 0; j < term.col-1; j++) {
  828. if (term.line[i][j].mode & attr)
  829. return 1;
  830. }
  831. }
  832. return 0;
  833. }
  834. void
  835. tsetdirt(int top, int bot)
  836. {
  837. int i;
  838. LIMIT(top, 0, term.row-1);
  839. LIMIT(bot, 0, term.row-1);
  840. for (i = top; i <= bot; i++)
  841. term.dirty[i] = 1;
  842. }
  843. void
  844. tsetdirtattr(int attr)
  845. {
  846. int i, j;
  847. for (i = 0; i < term.row-1; i++) {
  848. for (j = 0; j < term.col-1; j++) {
  849. if (term.line[i][j].mode & attr) {
  850. tsetdirt(i, i);
  851. break;
  852. }
  853. }
  854. }
  855. }
  856. void
  857. tfulldirt(void)
  858. {
  859. tsetdirt(0, term.row-1);
  860. }
  861. void
  862. tcursor(int mode)
  863. {
  864. static TCursor c[2];
  865. int alt = IS_SET(MODE_ALTSCREEN);
  866. if (mode == CURSOR_SAVE) {
  867. c[alt] = term.c;
  868. } else if (mode == CURSOR_LOAD) {
  869. term.c = c[alt];
  870. tmoveto(c[alt].x, c[alt].y);
  871. }
  872. }
  873. void
  874. treset(void)
  875. {
  876. uint i;
  877. term.c = (TCursor){{
  878. .mode = ATTR_NULL,
  879. .fg = defaultfg,
  880. .bg = defaultbg
  881. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  882. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  883. for (i = tabspaces; i < term.col; i += tabspaces)
  884. term.tabs[i] = 1;
  885. term.top = 0;
  886. term.bot = term.row - 1;
  887. term.mode = MODE_WRAP|MODE_UTF8;
  888. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  889. term.charset = 0;
  890. for (i = 0; i < 2; i++) {
  891. tmoveto(0, 0);
  892. tcursor(CURSOR_SAVE);
  893. tclearregion(0, 0, term.col-1, term.row-1);
  894. tswapscreen();
  895. }
  896. }
  897. void
  898. tnew(int col, int row)
  899. {
  900. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  901. tresize(col, row);
  902. term.numlock = 1;
  903. treset();
  904. }
  905. void
  906. tswapscreen(void)
  907. {
  908. Line *tmp = term.line;
  909. term.line = term.alt;
  910. term.alt = tmp;
  911. term.mode ^= MODE_ALTSCREEN;
  912. tfulldirt();
  913. }
  914. void
  915. tscrolldown(int orig, int n)
  916. {
  917. int i;
  918. Line temp;
  919. LIMIT(n, 0, term.bot-orig+1);
  920. tsetdirt(orig, term.bot-n);
  921. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  922. for (i = term.bot; i >= orig+n; i--) {
  923. temp = term.line[i];
  924. term.line[i] = term.line[i-n];
  925. term.line[i-n] = temp;
  926. }
  927. selscroll(orig, n);
  928. }
  929. void
  930. tscrollup(int orig, int n)
  931. {
  932. int i;
  933. Line temp;
  934. LIMIT(n, 0, term.bot-orig+1);
  935. tclearregion(0, orig, term.col-1, orig+n-1);
  936. tsetdirt(orig+n, term.bot);
  937. for (i = orig; i <= term.bot-n; i++) {
  938. temp = term.line[i];
  939. term.line[i] = term.line[i+n];
  940. term.line[i+n] = temp;
  941. }
  942. selscroll(orig, -n);
  943. }
  944. void
  945. selscroll(int orig, int n)
  946. {
  947. if (sel.ob.x == -1)
  948. return;
  949. if (BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  950. if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  951. selclear();
  952. return;
  953. }
  954. if (sel.type == SEL_RECTANGULAR) {
  955. if (sel.ob.y < term.top)
  956. sel.ob.y = term.top;
  957. if (sel.oe.y > term.bot)
  958. sel.oe.y = term.bot;
  959. } else {
  960. if (sel.ob.y < term.top) {
  961. sel.ob.y = term.top;
  962. sel.ob.x = 0;
  963. }
  964. if (sel.oe.y > term.bot) {
  965. sel.oe.y = term.bot;
  966. sel.oe.x = term.col;
  967. }
  968. }
  969. selnormalize();
  970. }
  971. }
  972. void
  973. tnewline(int first_col)
  974. {
  975. int y = term.c.y;
  976. if (y == term.bot) {
  977. tscrollup(term.top, 1);
  978. } else {
  979. y++;
  980. }
  981. tmoveto(first_col ? 0 : term.c.x, y);
  982. }
  983. void
  984. csiparse(void)
  985. {
  986. char *p = csiescseq.buf, *np;
  987. long int v;
  988. csiescseq.narg = 0;
  989. if (*p == '?') {
  990. csiescseq.priv = 1;
  991. p++;
  992. }
  993. csiescseq.buf[csiescseq.len] = '\0';
  994. while (p < csiescseq.buf+csiescseq.len) {
  995. np = NULL;
  996. v = strtol(p, &np, 10);
  997. if (np == p)
  998. v = 0;
  999. if (v == LONG_MAX || v == LONG_MIN)
  1000. v = -1;
  1001. csiescseq.arg[csiescseq.narg++] = v;
  1002. p = np;
  1003. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  1004. break;
  1005. p++;
  1006. }
  1007. csiescseq.mode[0] = *p++;
  1008. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  1009. }
  1010. /* for absolute user moves, when decom is set */
  1011. void
  1012. tmoveato(int x, int y)
  1013. {
  1014. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  1015. }
  1016. void
  1017. tmoveto(int x, int y)
  1018. {
  1019. int miny, maxy;
  1020. if (term.c.state & CURSOR_ORIGIN) {
  1021. miny = term.top;
  1022. maxy = term.bot;
  1023. } else {
  1024. miny = 0;
  1025. maxy = term.row - 1;
  1026. }
  1027. term.c.state &= ~CURSOR_WRAPNEXT;
  1028. term.c.x = LIMIT(x, 0, term.col-1);
  1029. term.c.y = LIMIT(y, miny, maxy);
  1030. }
  1031. void
  1032. tsetchar(Rune u, Glyph *attr, int x, int y)
  1033. {
  1034. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  1035. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  1036. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  1037. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  1038. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  1039. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  1040. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  1041. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  1042. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  1043. };
  1044. /*
  1045. * The table is proudly stolen from rxvt.
  1046. */
  1047. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  1048. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  1049. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  1050. if (term.line[y][x].mode & ATTR_WIDE) {
  1051. if (x+1 < term.col) {
  1052. term.line[y][x+1].u = ' ';
  1053. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  1054. }
  1055. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  1056. term.line[y][x-1].u = ' ';
  1057. term.line[y][x-1].mode &= ~ATTR_WIDE;
  1058. }
  1059. term.dirty[y] = 1;
  1060. term.line[y][x] = *attr;
  1061. term.line[y][x].u = u;
  1062. }
  1063. void
  1064. tclearregion(int x1, int y1, int x2, int y2)
  1065. {
  1066. int x, y, temp;
  1067. Glyph *gp;
  1068. if (x1 > x2)
  1069. temp = x1, x1 = x2, x2 = temp;
  1070. if (y1 > y2)
  1071. temp = y1, y1 = y2, y2 = temp;
  1072. LIMIT(x1, 0, term.col-1);
  1073. LIMIT(x2, 0, term.col-1);
  1074. LIMIT(y1, 0, term.row-1);
  1075. LIMIT(y2, 0, term.row-1);
  1076. for (y = y1; y <= y2; y++) {
  1077. term.dirty[y] = 1;
  1078. for (x = x1; x <= x2; x++) {
  1079. gp = &term.line[y][x];
  1080. if (selected(x, y))
  1081. selclear();
  1082. gp->fg = term.c.attr.fg;
  1083. gp->bg = term.c.attr.bg;
  1084. gp->mode = 0;
  1085. gp->u = ' ';
  1086. }
  1087. }
  1088. }
  1089. void
  1090. tdeletechar(int n)
  1091. {
  1092. int dst, src, size;
  1093. Glyph *line;
  1094. LIMIT(n, 0, term.col - term.c.x);
  1095. dst = term.c.x;
  1096. src = term.c.x + n;
  1097. size = term.col - src;
  1098. line = term.line[term.c.y];
  1099. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1100. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1101. }
  1102. void
  1103. tinsertblank(int n)
  1104. {
  1105. int dst, src, size;
  1106. Glyph *line;
  1107. LIMIT(n, 0, term.col - term.c.x);
  1108. dst = term.c.x + n;
  1109. src = term.c.x;
  1110. size = term.col - dst;
  1111. line = term.line[term.c.y];
  1112. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1113. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1114. }
  1115. void
  1116. tinsertblankline(int n)
  1117. {
  1118. if (BETWEEN(term.c.y, term.top, term.bot))
  1119. tscrolldown(term.c.y, n);
  1120. }
  1121. void
  1122. tdeleteline(int n)
  1123. {
  1124. if (BETWEEN(term.c.y, term.top, term.bot))
  1125. tscrollup(term.c.y, n);
  1126. }
  1127. int32_t
  1128. tdefcolor(int *attr, int *npar, int l)
  1129. {
  1130. int32_t idx = -1;
  1131. uint r, g, b;
  1132. switch (attr[*npar + 1]) {
  1133. case 2: /* direct color in RGB space */
  1134. if (*npar + 4 >= l) {
  1135. fprintf(stderr,
  1136. "erresc(38): Incorrect number of parameters (%d)\n",
  1137. *npar);
  1138. break;
  1139. }
  1140. r = attr[*npar + 2];
  1141. g = attr[*npar + 3];
  1142. b = attr[*npar + 4];
  1143. *npar += 4;
  1144. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1145. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1146. r, g, b);
  1147. else
  1148. idx = TRUECOLOR(r, g, b);
  1149. break;
  1150. case 5: /* indexed color */
  1151. if (*npar + 2 >= l) {
  1152. fprintf(stderr,
  1153. "erresc(38): Incorrect number of parameters (%d)\n",
  1154. *npar);
  1155. break;
  1156. }
  1157. *npar += 2;
  1158. if (!BETWEEN(attr[*npar], 0, 255))
  1159. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1160. else
  1161. idx = attr[*npar];
  1162. break;
  1163. case 0: /* implemented defined (only foreground) */
  1164. case 1: /* transparent */
  1165. case 3: /* direct color in CMY space */
  1166. case 4: /* direct color in CMYK space */
  1167. default:
  1168. fprintf(stderr,
  1169. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1170. break;
  1171. }
  1172. return idx;
  1173. }
  1174. void
  1175. tsetattr(int *attr, int l)
  1176. {
  1177. int i;
  1178. int32_t idx;
  1179. for (i = 0; i < l; i++) {
  1180. switch (attr[i]) {
  1181. case 0:
  1182. term.c.attr.mode &= ~(
  1183. ATTR_BOLD |
  1184. ATTR_FAINT |
  1185. ATTR_ITALIC |
  1186. ATTR_UNDERLINE |
  1187. ATTR_BLINK |
  1188. ATTR_REVERSE |
  1189. ATTR_INVISIBLE |
  1190. ATTR_STRUCK );
  1191. term.c.attr.fg = defaultfg;
  1192. term.c.attr.bg = defaultbg;
  1193. break;
  1194. case 1:
  1195. term.c.attr.mode |= ATTR_BOLD;
  1196. break;
  1197. case 2:
  1198. term.c.attr.mode |= ATTR_FAINT;
  1199. break;
  1200. case 3:
  1201. term.c.attr.mode |= ATTR_ITALIC;
  1202. break;
  1203. case 4:
  1204. term.c.attr.mode |= ATTR_UNDERLINE;
  1205. break;
  1206. case 5: /* slow blink */
  1207. /* FALLTHROUGH */
  1208. case 6: /* rapid blink */
  1209. term.c.attr.mode |= ATTR_BLINK;
  1210. break;
  1211. case 7:
  1212. term.c.attr.mode |= ATTR_REVERSE;
  1213. break;
  1214. case 8:
  1215. term.c.attr.mode |= ATTR_INVISIBLE;
  1216. break;
  1217. case 9:
  1218. term.c.attr.mode |= ATTR_STRUCK;
  1219. break;
  1220. case 22:
  1221. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1222. break;
  1223. case 23:
  1224. term.c.attr.mode &= ~ATTR_ITALIC;
  1225. break;
  1226. case 24:
  1227. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1228. break;
  1229. case 25:
  1230. term.c.attr.mode &= ~ATTR_BLINK;
  1231. break;
  1232. case 27:
  1233. term.c.attr.mode &= ~ATTR_REVERSE;
  1234. break;
  1235. case 28:
  1236. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1237. break;
  1238. case 29:
  1239. term.c.attr.mode &= ~ATTR_STRUCK;
  1240. break;
  1241. case 38:
  1242. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1243. term.c.attr.fg = idx;
  1244. break;
  1245. case 39:
  1246. term.c.attr.fg = defaultfg;
  1247. break;
  1248. case 48:
  1249. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1250. term.c.attr.bg = idx;
  1251. break;
  1252. case 49:
  1253. term.c.attr.bg = defaultbg;
  1254. break;
  1255. default:
  1256. if (BETWEEN(attr[i], 30, 37)) {
  1257. term.c.attr.fg = attr[i] - 30;
  1258. } else if (BETWEEN(attr[i], 40, 47)) {
  1259. term.c.attr.bg = attr[i] - 40;
  1260. } else if (BETWEEN(attr[i], 90, 97)) {
  1261. term.c.attr.fg = attr[i] - 90 + 8;
  1262. } else if (BETWEEN(attr[i], 100, 107)) {
  1263. term.c.attr.bg = attr[i] - 100 + 8;
  1264. } else {
  1265. fprintf(stderr,
  1266. "erresc(default): gfx attr %d unknown\n",
  1267. attr[i]), csidump();
  1268. }
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. void
  1274. tsetscroll(int t, int b)
  1275. {
  1276. int temp;
  1277. LIMIT(t, 0, term.row-1);
  1278. LIMIT(b, 0, term.row-1);
  1279. if (t > b) {
  1280. temp = t;
  1281. t = b;
  1282. b = temp;
  1283. }
  1284. term.top = t;
  1285. term.bot = b;
  1286. }
  1287. void
  1288. tsetmode(int priv, int set, int *args, int narg)
  1289. {
  1290. int *lim, mode;
  1291. int alt;
  1292. for (lim = args + narg; args < lim; ++args) {
  1293. if (priv) {
  1294. switch (*args) {
  1295. case 1: /* DECCKM -- Cursor key */
  1296. MODBIT(term.mode, set, MODE_APPCURSOR);
  1297. break;
  1298. case 5: /* DECSCNM -- Reverse video */
  1299. mode = term.mode;
  1300. MODBIT(term.mode, set, MODE_REVERSE);
  1301. if (mode != term.mode)
  1302. redraw();
  1303. break;
  1304. case 6: /* DECOM -- Origin */
  1305. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1306. tmoveato(0, 0);
  1307. break;
  1308. case 7: /* DECAWM -- Auto wrap */
  1309. MODBIT(term.mode, set, MODE_WRAP);
  1310. break;
  1311. case 0: /* Error (IGNORED) */
  1312. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1313. case 3: /* DECCOLM -- Column (IGNORED) */
  1314. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1315. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1316. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1317. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1318. case 42: /* DECNRCM -- National characters (IGNORED) */
  1319. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1320. break;
  1321. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1322. MODBIT(term.mode, !set, MODE_HIDE);
  1323. break;
  1324. case 9: /* X10 mouse compatibility mode */
  1325. xsetpointermotion(0);
  1326. MODBIT(term.mode, 0, MODE_MOUSE);
  1327. MODBIT(term.mode, set, MODE_MOUSEX10);
  1328. break;
  1329. case 1000: /* 1000: report button press */
  1330. xsetpointermotion(0);
  1331. MODBIT(term.mode, 0, MODE_MOUSE);
  1332. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1333. break;
  1334. case 1002: /* 1002: report motion on button press */
  1335. xsetpointermotion(0);
  1336. MODBIT(term.mode, 0, MODE_MOUSE);
  1337. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1338. break;
  1339. case 1003: /* 1003: enable all mouse motions */
  1340. xsetpointermotion(set);
  1341. MODBIT(term.mode, 0, MODE_MOUSE);
  1342. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1343. break;
  1344. case 1004: /* 1004: send focus events to tty */
  1345. MODBIT(term.mode, set, MODE_FOCUS);
  1346. break;
  1347. case 1006: /* 1006: extended reporting mode */
  1348. MODBIT(term.mode, set, MODE_MOUSESGR);
  1349. break;
  1350. case 1034:
  1351. MODBIT(term.mode, set, MODE_8BIT);
  1352. break;
  1353. case 1049: /* swap screen & set/restore cursor as xterm */
  1354. if (!allowaltscreen)
  1355. break;
  1356. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1357. /* FALLTHROUGH */
  1358. case 47: /* swap screen */
  1359. case 1047:
  1360. if (!allowaltscreen)
  1361. break;
  1362. alt = IS_SET(MODE_ALTSCREEN);
  1363. if (alt) {
  1364. tclearregion(0, 0, term.col-1,
  1365. term.row-1);
  1366. }
  1367. if (set ^ alt) /* set is always 1 or 0 */
  1368. tswapscreen();
  1369. if (*args != 1049)
  1370. break;
  1371. /* FALLTHROUGH */
  1372. case 1048:
  1373. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1374. break;
  1375. case 2004: /* 2004: bracketed paste mode */
  1376. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1377. break;
  1378. /* Not implemented mouse modes. See comments there. */
  1379. case 1001: /* mouse highlight mode; can hang the
  1380. terminal by design when implemented. */
  1381. case 1005: /* UTF-8 mouse mode; will confuse
  1382. applications not supporting UTF-8
  1383. and luit. */
  1384. case 1015: /* urxvt mangled mouse mode; incompatible
  1385. and can be mistaken for other control
  1386. codes. */
  1387. default:
  1388. fprintf(stderr,
  1389. "erresc: unknown private set/reset mode %d\n",
  1390. *args);
  1391. break;
  1392. }
  1393. } else {
  1394. switch (*args) {
  1395. case 0: /* Error (IGNORED) */
  1396. break;
  1397. case 2: /* KAM -- keyboard action */
  1398. MODBIT(term.mode, set, MODE_KBDLOCK);
  1399. break;
  1400. case 4: /* IRM -- Insertion-replacement */
  1401. MODBIT(term.mode, set, MODE_INSERT);
  1402. break;
  1403. case 12: /* SRM -- Send/Receive */
  1404. MODBIT(term.mode, !set, MODE_ECHO);
  1405. break;
  1406. case 20: /* LNM -- Linefeed/new line */
  1407. MODBIT(term.mode, set, MODE_CRLF);
  1408. break;
  1409. default:
  1410. fprintf(stderr,
  1411. "erresc: unknown set/reset mode %d\n",
  1412. *args);
  1413. break;
  1414. }
  1415. }
  1416. }
  1417. }
  1418. void
  1419. csihandle(void)
  1420. {
  1421. char buf[40];
  1422. int len;
  1423. switch (csiescseq.mode[0]) {
  1424. default:
  1425. unknown:
  1426. fprintf(stderr, "erresc: unknown csi ");
  1427. csidump();
  1428. /* die(""); */
  1429. break;
  1430. case '@': /* ICH -- Insert <n> blank char */
  1431. DEFAULT(csiescseq.arg[0], 1);
  1432. tinsertblank(csiescseq.arg[0]);
  1433. break;
  1434. case 'A': /* CUU -- Cursor <n> Up */
  1435. DEFAULT(csiescseq.arg[0], 1);
  1436. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1437. break;
  1438. case 'B': /* CUD -- Cursor <n> Down */
  1439. case 'e': /* VPR --Cursor <n> Down */
  1440. DEFAULT(csiescseq.arg[0], 1);
  1441. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1442. break;
  1443. case 'i': /* MC -- Media Copy */
  1444. switch (csiescseq.arg[0]) {
  1445. case 0:
  1446. tdump();
  1447. break;
  1448. case 1:
  1449. tdumpline(term.c.y);
  1450. break;
  1451. case 2:
  1452. tdumpsel();
  1453. break;
  1454. case 4:
  1455. term.mode &= ~MODE_PRINT;
  1456. break;
  1457. case 5:
  1458. term.mode |= MODE_PRINT;
  1459. break;
  1460. }
  1461. break;
  1462. case 'c': /* DA -- Device Attributes */
  1463. if (csiescseq.arg[0] == 0)
  1464. ttywrite(vtiden, sizeof(vtiden) - 1);
  1465. break;
  1466. case 'C': /* CUF -- Cursor <n> Forward */
  1467. case 'a': /* HPR -- Cursor <n> Forward */
  1468. DEFAULT(csiescseq.arg[0], 1);
  1469. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1470. break;
  1471. case 'D': /* CUB -- Cursor <n> Backward */
  1472. DEFAULT(csiescseq.arg[0], 1);
  1473. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1474. break;
  1475. case 'E': /* CNL -- Cursor <n> Down and first col */
  1476. DEFAULT(csiescseq.arg[0], 1);
  1477. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1478. break;
  1479. case 'F': /* CPL -- Cursor <n> Up and first col */
  1480. DEFAULT(csiescseq.arg[0], 1);
  1481. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1482. break;
  1483. case 'g': /* TBC -- Tabulation clear */
  1484. switch (csiescseq.arg[0]) {
  1485. case 0: /* clear current tab stop */
  1486. term.tabs[term.c.x] = 0;
  1487. break;
  1488. case 3: /* clear all the tabs */
  1489. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1490. break;
  1491. default:
  1492. goto unknown;
  1493. }
  1494. break;
  1495. case 'G': /* CHA -- Move to <col> */
  1496. case '`': /* HPA */
  1497. DEFAULT(csiescseq.arg[0], 1);
  1498. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1499. break;
  1500. case 'H': /* CUP -- Move to <row> <col> */
  1501. case 'f': /* HVP */
  1502. DEFAULT(csiescseq.arg[0], 1);
  1503. DEFAULT(csiescseq.arg[1], 1);
  1504. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1505. break;
  1506. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1507. DEFAULT(csiescseq.arg[0], 1);
  1508. tputtab(csiescseq.arg[0]);
  1509. break;
  1510. case 'J': /* ED -- Clear screen */
  1511. selclear();
  1512. switch (csiescseq.arg[0]) {
  1513. case 0: /* below */
  1514. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1515. if (term.c.y < term.row-1) {
  1516. tclearregion(0, term.c.y+1, term.col-1,
  1517. term.row-1);
  1518. }
  1519. break;
  1520. case 1: /* above */
  1521. if (term.c.y > 1)
  1522. tclearregion(0, 0, term.col-1, term.c.y-1);
  1523. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1524. break;
  1525. case 2: /* all */
  1526. tclearregion(0, 0, term.col-1, term.row-1);
  1527. break;
  1528. default:
  1529. goto unknown;
  1530. }
  1531. break;
  1532. case 'K': /* EL -- Clear line */
  1533. switch (csiescseq.arg[0]) {
  1534. case 0: /* right */
  1535. tclearregion(term.c.x, term.c.y, term.col-1,
  1536. term.c.y);
  1537. break;
  1538. case 1: /* left */
  1539. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1540. break;
  1541. case 2: /* all */
  1542. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1543. break;
  1544. }
  1545. break;
  1546. case 'S': /* SU -- Scroll <n> line up */
  1547. DEFAULT(csiescseq.arg[0], 1);
  1548. tscrollup(term.top, csiescseq.arg[0]);
  1549. break;
  1550. case 'T': /* SD -- Scroll <n> line down */
  1551. DEFAULT(csiescseq.arg[0], 1);
  1552. tscrolldown(term.top, csiescseq.arg[0]);
  1553. break;
  1554. case 'L': /* IL -- Insert <n> blank lines */
  1555. DEFAULT(csiescseq.arg[0], 1);
  1556. tinsertblankline(csiescseq.arg[0]);
  1557. break;
  1558. case 'l': /* RM -- Reset Mode */
  1559. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1560. break;
  1561. case 'M': /* DL -- Delete <n> lines */
  1562. DEFAULT(csiescseq.arg[0], 1);
  1563. tdeleteline(csiescseq.arg[0]);
  1564. break;
  1565. case 'X': /* ECH -- Erase <n> char */
  1566. DEFAULT(csiescseq.arg[0], 1);
  1567. tclearregion(term.c.x, term.c.y,
  1568. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1569. break;
  1570. case 'P': /* DCH -- Delete <n> char */
  1571. DEFAULT(csiescseq.arg[0], 1);
  1572. tdeletechar(csiescseq.arg[0]);
  1573. break;
  1574. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1575. DEFAULT(csiescseq.arg[0], 1);
  1576. tputtab(-csiescseq.arg[0]);
  1577. break;
  1578. case 'd': /* VPA -- Move to <row> */
  1579. DEFAULT(csiescseq.arg[0], 1);
  1580. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1581. break;
  1582. case 'h': /* SM -- Set terminal mode */
  1583. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1584. break;
  1585. case 'm': /* SGR -- Terminal attribute (color) */
  1586. tsetattr(csiescseq.arg, csiescseq.narg);
  1587. break;
  1588. case 'n': /* DSR – Device Status Report (cursor position) */
  1589. if (csiescseq.arg[0] == 6) {
  1590. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  1591. term.c.y+1, term.c.x+1);
  1592. ttywrite(buf, len);
  1593. }
  1594. break;
  1595. case 'r': /* DECSTBM -- Set Scrolling Region */
  1596. if (csiescseq.priv) {
  1597. goto unknown;
  1598. } else {
  1599. DEFAULT(csiescseq.arg[0], 1);
  1600. DEFAULT(csiescseq.arg[1], term.row);
  1601. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1602. tmoveato(0, 0);
  1603. }
  1604. break;
  1605. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1606. tcursor(CURSOR_SAVE);
  1607. break;
  1608. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1609. tcursor(CURSOR_LOAD);
  1610. break;
  1611. case ' ':
  1612. switch (csiescseq.mode[1]) {
  1613. case 'q': /* DECSCUSR -- Set Cursor Style */
  1614. DEFAULT(csiescseq.arg[0], 1);
  1615. if (!BETWEEN(csiescseq.arg[0], 0, 6)) {
  1616. goto unknown;
  1617. }
  1618. win.cursor = csiescseq.arg[0];
  1619. break;
  1620. default:
  1621. goto unknown;
  1622. }
  1623. break;
  1624. }
  1625. }
  1626. void
  1627. csidump(void)
  1628. {
  1629. int i;
  1630. uint c;
  1631. fprintf(stderr, "ESC[");
  1632. for (i = 0; i < csiescseq.len; i++) {
  1633. c = csiescseq.buf[i] & 0xff;
  1634. if (isprint(c)) {
  1635. putc(c, stderr);
  1636. } else if (c == '\n') {
  1637. fprintf(stderr, "(\\n)");
  1638. } else if (c == '\r') {
  1639. fprintf(stderr, "(\\r)");
  1640. } else if (c == 0x1b) {
  1641. fprintf(stderr, "(\\e)");
  1642. } else {
  1643. fprintf(stderr, "(%02x)", c);
  1644. }
  1645. }
  1646. putc('\n', stderr);
  1647. }
  1648. void
  1649. csireset(void)
  1650. {
  1651. memset(&csiescseq, 0, sizeof(csiescseq));
  1652. }
  1653. void
  1654. strhandle(void)
  1655. {
  1656. char *p = NULL;
  1657. int j, narg, par;
  1658. term.esc &= ~(ESC_STR_END|ESC_STR);
  1659. strparse();
  1660. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  1661. switch (strescseq.type) {
  1662. case ']': /* OSC -- Operating System Command */
  1663. switch (par) {
  1664. case 0:
  1665. case 1:
  1666. case 2:
  1667. if (narg > 1)
  1668. xsettitle(strescseq.args[1]);
  1669. return;
  1670. case 52:
  1671. if (narg > 2) {
  1672. char *dec;
  1673. dec = base64dec(strescseq.args[2]);
  1674. if (dec) {
  1675. xsetsel(dec, CurrentTime);
  1676. clipcopy(NULL);
  1677. } else {
  1678. fprintf(stderr, "erresc: invalid base64\n");
  1679. }
  1680. }
  1681. return;
  1682. case 4: /* color set */
  1683. if (narg < 3)
  1684. break;
  1685. p = strescseq.args[2];
  1686. /* FALLTHROUGH */
  1687. case 104: /* color reset, here p = NULL */
  1688. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1689. if (xsetcolorname(j, p)) {
  1690. fprintf(stderr, "erresc: invalid color %s\n", p);
  1691. } else {
  1692. /*
  1693. * TODO if defaultbg color is changed, borders
  1694. * are dirty
  1695. */
  1696. redraw();
  1697. }
  1698. return;
  1699. }
  1700. break;
  1701. case 'k': /* old title set compatibility */
  1702. xsettitle(strescseq.args[0]);
  1703. return;
  1704. case 'P': /* DCS -- Device Control String */
  1705. term.mode |= ESC_DCS;
  1706. case '_': /* APC -- Application Program Command */
  1707. case '^': /* PM -- Privacy Message */
  1708. return;
  1709. }
  1710. fprintf(stderr, "erresc: unknown str ");
  1711. strdump();
  1712. }
  1713. void
  1714. strparse(void)
  1715. {
  1716. int c;
  1717. char *p = strescseq.buf;
  1718. strescseq.narg = 0;
  1719. strescseq.buf[strescseq.len] = '\0';
  1720. if (*p == '\0')
  1721. return;
  1722. while (strescseq.narg < STR_ARG_SIZ) {
  1723. strescseq.args[strescseq.narg++] = p;
  1724. while ((c = *p) != ';' && c != '\0')
  1725. ++p;
  1726. if (c == '\0')
  1727. return;
  1728. *p++ = '\0';
  1729. }
  1730. }
  1731. void
  1732. strdump(void)
  1733. {
  1734. int i;
  1735. uint c;
  1736. fprintf(stderr, "ESC%c", strescseq.type);
  1737. for (i = 0; i < strescseq.len; i++) {
  1738. c = strescseq.buf[i] & 0xff;
  1739. if (c == '\0') {
  1740. putc('\n', stderr);
  1741. return;
  1742. } else if (isprint(c)) {
  1743. putc(c, stderr);
  1744. } else if (c == '\n') {
  1745. fprintf(stderr, "(\\n)");
  1746. } else if (c == '\r') {
  1747. fprintf(stderr, "(\\r)");
  1748. } else if (c == 0x1b) {
  1749. fprintf(stderr, "(\\e)");
  1750. } else {
  1751. fprintf(stderr, "(%02x)", c);
  1752. }
  1753. }
  1754. fprintf(stderr, "ESC\\\n");
  1755. }
  1756. void
  1757. strreset(void)
  1758. {
  1759. memset(&strescseq, 0, sizeof(strescseq));
  1760. }
  1761. void
  1762. sendbreak(const Arg *arg)
  1763. {
  1764. if (tcsendbreak(cmdfd, 0))
  1765. perror("Error sending break");
  1766. }
  1767. void
  1768. tprinter(char *s, size_t len)
  1769. {
  1770. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  1771. fprintf(stderr, "Error writing in %s:%s\n",
  1772. opt_io, strerror(errno));
  1773. close(iofd);
  1774. iofd = -1;
  1775. }
  1776. }
  1777. void
  1778. iso14755(const Arg *arg)
  1779. {
  1780. FILE *p;
  1781. char *us, *e, codepoint[9], uc[UTF_SIZ];
  1782. unsigned long utf32;
  1783. if (!(p = popen(ISO14755CMD, "r")))
  1784. return;
  1785. us = fgets(codepoint, sizeof(codepoint), p);
  1786. pclose(p);
  1787. if (!us || *us == '\0' || *us == '-' || strlen(us) > 7)
  1788. return;
  1789. if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX ||
  1790. (*e != '\n' && *e != '\0'))
  1791. return;
  1792. ttysend(uc, utf8encode(utf32, uc));
  1793. }
  1794. void
  1795. toggleprinter(const Arg *arg)
  1796. {
  1797. term.mode ^= MODE_PRINT;
  1798. }
  1799. void
  1800. printscreen(const Arg *arg)
  1801. {
  1802. tdump();
  1803. }
  1804. void
  1805. printsel(const Arg *arg)
  1806. {
  1807. tdumpsel();
  1808. }
  1809. void
  1810. tdumpsel(void)
  1811. {
  1812. char *ptr;
  1813. if ((ptr = getsel())) {
  1814. tprinter(ptr, strlen(ptr));
  1815. free(ptr);
  1816. }
  1817. }
  1818. void
  1819. tdumpline(int n)
  1820. {
  1821. char buf[UTF_SIZ];
  1822. Glyph *bp, *end;
  1823. bp = &term.line[n][0];
  1824. end = &bp[MIN(tlinelen(n), term.col) - 1];
  1825. if (bp != end || bp->u != ' ') {
  1826. for ( ;bp <= end; ++bp)
  1827. tprinter(buf, utf8encode(bp->u, buf));
  1828. }
  1829. tprinter("\n", 1);
  1830. }
  1831. void
  1832. tdump(void)
  1833. {
  1834. int i;
  1835. for (i = 0; i < term.row; ++i)
  1836. tdumpline(i);
  1837. }
  1838. void
  1839. tputtab(int n)
  1840. {
  1841. uint x = term.c.x;
  1842. if (n > 0) {
  1843. while (x < term.col && n--)
  1844. for (++x; x < term.col && !term.tabs[x]; ++x)
  1845. /* nothing */ ;
  1846. } else if (n < 0) {
  1847. while (x > 0 && n++)
  1848. for (--x; x > 0 && !term.tabs[x]; --x)
  1849. /* nothing */ ;
  1850. }
  1851. term.c.x = LIMIT(x, 0, term.col-1);
  1852. }
  1853. void
  1854. techo(Rune u)
  1855. {
  1856. if (ISCONTROL(u)) { /* control code */
  1857. if (u & 0x80) {
  1858. u &= 0x7f;
  1859. tputc('^');
  1860. tputc('[');
  1861. } else if (u != '\n' && u != '\r' && u != '\t') {
  1862. u ^= 0x40;
  1863. tputc('^');
  1864. }
  1865. }
  1866. tputc(u);
  1867. }
  1868. void
  1869. tdefutf8(char ascii)
  1870. {
  1871. if (ascii == 'G')
  1872. term.mode |= MODE_UTF8;
  1873. else if (ascii == '@')
  1874. term.mode &= ~MODE_UTF8;
  1875. }
  1876. void
  1877. tdeftran(char ascii)
  1878. {
  1879. static char cs[] = "0B";
  1880. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  1881. char *p;
  1882. if ((p = strchr(cs, ascii)) == NULL) {
  1883. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1884. } else {
  1885. term.trantbl[term.icharset] = vcs[p - cs];
  1886. }
  1887. }
  1888. void
  1889. tdectest(char c)
  1890. {
  1891. int x, y;
  1892. if (c == '8') { /* DEC screen alignment test. */
  1893. for (x = 0; x < term.col; ++x) {
  1894. for (y = 0; y < term.row; ++y)
  1895. tsetchar('E', &term.c.attr, x, y);
  1896. }
  1897. }
  1898. }
  1899. void
  1900. tstrsequence(uchar c)
  1901. {
  1902. strreset();
  1903. switch (c) {
  1904. case 0x90: /* DCS -- Device Control String */
  1905. c = 'P';
  1906. term.esc |= ESC_DCS;
  1907. break;
  1908. case 0x9f: /* APC -- Application Program Command */
  1909. c = '_';
  1910. break;
  1911. case 0x9e: /* PM -- Privacy Message */
  1912. c = '^';
  1913. break;
  1914. case 0x9d: /* OSC -- Operating System Command */
  1915. c = ']';
  1916. break;
  1917. }
  1918. strescseq.type = c;
  1919. term.esc |= ESC_STR;
  1920. }
  1921. void
  1922. tcontrolcode(uchar ascii)
  1923. {
  1924. switch (ascii) {
  1925. case '\t': /* HT */
  1926. tputtab(1);
  1927. return;
  1928. case '\b': /* BS */
  1929. tmoveto(term.c.x-1, term.c.y);
  1930. return;
  1931. case '\r': /* CR */
  1932. tmoveto(0, term.c.y);
  1933. return;
  1934. case '\f': /* LF */
  1935. case '\v': /* VT */
  1936. case '\n': /* LF */
  1937. /* go to first col if the mode is set */
  1938. tnewline(IS_SET(MODE_CRLF));
  1939. return;
  1940. case '\a': /* BEL */
  1941. if (term.esc & ESC_STR_END) {
  1942. /* backwards compatibility to xterm */
  1943. strhandle();
  1944. } else {
  1945. if (!(win.state & WIN_FOCUSED))
  1946. xseturgency(1);
  1947. if (bellvolume)
  1948. xbell(bellvolume);
  1949. }
  1950. break;
  1951. case '\033': /* ESC */
  1952. csireset();
  1953. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  1954. term.esc |= ESC_START;
  1955. return;
  1956. case '\016': /* SO (LS1 -- Locking shift 1) */
  1957. case '\017': /* SI (LS0 -- Locking shift 0) */
  1958. term.charset = 1 - (ascii - '\016');
  1959. return;
  1960. case '\032': /* SUB */
  1961. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  1962. case '\030': /* CAN */
  1963. csireset();
  1964. break;
  1965. case '\005': /* ENQ (IGNORED) */
  1966. case '\000': /* NUL (IGNORED) */
  1967. case '\021': /* XON (IGNORED) */
  1968. case '\023': /* XOFF (IGNORED) */
  1969. case 0177: /* DEL (IGNORED) */
  1970. return;
  1971. case 0x80: /* TODO: PAD */
  1972. case 0x81: /* TODO: HOP */
  1973. case 0x82: /* TODO: BPH */
  1974. case 0x83: /* TODO: NBH */
  1975. case 0x84: /* TODO: IND */
  1976. break;
  1977. case 0x85: /* NEL -- Next line */
  1978. tnewline(1); /* always go to first col */
  1979. break;
  1980. case 0x86: /* TODO: SSA */
  1981. case 0x87: /* TODO: ESA */
  1982. break;
  1983. case 0x88: /* HTS -- Horizontal tab stop */
  1984. term.tabs[term.c.x] = 1;
  1985. break;
  1986. case 0x89: /* TODO: HTJ */
  1987. case 0x8a: /* TODO: VTS */
  1988. case 0x8b: /* TODO: PLD */
  1989. case 0x8c: /* TODO: PLU */
  1990. case 0x8d: /* TODO: RI */
  1991. case 0x8e: /* TODO: SS2 */
  1992. case 0x8f: /* TODO: SS3 */
  1993. case 0x91: /* TODO: PU1 */
  1994. case 0x92: /* TODO: PU2 */
  1995. case 0x93: /* TODO: STS */
  1996. case 0x94: /* TODO: CCH */
  1997. case 0x95: /* TODO: MW */
  1998. case 0x96: /* TODO: SPA */
  1999. case 0x97: /* TODO: EPA */
  2000. case 0x98: /* TODO: SOS */
  2001. case 0x99: /* TODO: SGCI */
  2002. break;
  2003. case 0x9a: /* DECID -- Identify Terminal */
  2004. ttywrite(vtiden, sizeof(vtiden) - 1);
  2005. break;
  2006. case 0x9b: /* TODO: CSI */
  2007. case 0x9c: /* TODO: ST */
  2008. break;
  2009. case 0x90: /* DCS -- Device Control String */
  2010. case 0x9d: /* OSC -- Operating System Command */
  2011. case 0x9e: /* PM -- Privacy Message */
  2012. case 0x9f: /* APC -- Application Program Command */
  2013. tstrsequence(ascii);
  2014. return;
  2015. }
  2016. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  2017. term.esc &= ~(ESC_STR_END|ESC_STR);
  2018. }
  2019. /*
  2020. * returns 1 when the sequence is finished and it hasn't to read
  2021. * more characters for this sequence, otherwise 0
  2022. */
  2023. int
  2024. eschandle(uchar ascii)
  2025. {
  2026. switch (ascii) {
  2027. case '[':
  2028. term.esc |= ESC_CSI;
  2029. return 0;
  2030. case '#':
  2031. term.esc |= ESC_TEST;
  2032. return 0;
  2033. case '%':
  2034. term.esc |= ESC_UTF8;
  2035. return 0;
  2036. case 'P': /* DCS -- Device Control String */
  2037. case '_': /* APC -- Application Program Command */
  2038. case '^': /* PM -- Privacy Message */
  2039. case ']': /* OSC -- Operating System Command */
  2040. case 'k': /* old title set compatibility */
  2041. tstrsequence(ascii);
  2042. return 0;
  2043. case 'n': /* LS2 -- Locking shift 2 */
  2044. case 'o': /* LS3 -- Locking shift 3 */
  2045. term.charset = 2 + (ascii - 'n');
  2046. break;
  2047. case '(': /* GZD4 -- set primary charset G0 */
  2048. case ')': /* G1D4 -- set secondary charset G1 */
  2049. case '*': /* G2D4 -- set tertiary charset G2 */
  2050. case '+': /* G3D4 -- set quaternary charset G3 */
  2051. term.icharset = ascii - '(';
  2052. term.esc |= ESC_ALTCHARSET;
  2053. return 0;
  2054. case 'D': /* IND -- Linefeed */
  2055. if (term.c.y == term.bot) {
  2056. tscrollup(term.top, 1);
  2057. } else {
  2058. tmoveto(term.c.x, term.c.y+1);
  2059. }
  2060. break;
  2061. case 'E': /* NEL -- Next line */
  2062. tnewline(1); /* always go to first col */
  2063. break;
  2064. case 'H': /* HTS -- Horizontal tab stop */
  2065. term.tabs[term.c.x] = 1;
  2066. break;
  2067. case 'M': /* RI -- Reverse index */
  2068. if (term.c.y == term.top) {
  2069. tscrolldown(term.top, 1);
  2070. } else {
  2071. tmoveto(term.c.x, term.c.y-1);
  2072. }
  2073. break;
  2074. case 'Z': /* DECID -- Identify Terminal */
  2075. ttywrite(vtiden, sizeof(vtiden) - 1);
  2076. break;
  2077. case 'c': /* RIS -- Reset to inital state */
  2078. treset();
  2079. resettitle();
  2080. xloadcols();
  2081. break;
  2082. case '=': /* DECPAM -- Application keypad */
  2083. term.mode |= MODE_APPKEYPAD;
  2084. break;
  2085. case '>': /* DECPNM -- Normal keypad */
  2086. term.mode &= ~MODE_APPKEYPAD;
  2087. break;
  2088. case '7': /* DECSC -- Save Cursor */
  2089. tcursor(CURSOR_SAVE);
  2090. break;
  2091. case '8': /* DECRC -- Restore Cursor */
  2092. tcursor(CURSOR_LOAD);
  2093. break;
  2094. case '\\': /* ST -- String Terminator */
  2095. if (term.esc & ESC_STR_END)
  2096. strhandle();
  2097. break;
  2098. default:
  2099. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2100. (uchar) ascii, isprint(ascii)? ascii:'.');
  2101. break;
  2102. }
  2103. return 1;
  2104. }
  2105. void
  2106. tputc(Rune u)
  2107. {
  2108. char c[UTF_SIZ];
  2109. int control;
  2110. int width, len;
  2111. Glyph *gp;
  2112. control = ISCONTROL(u);
  2113. if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2114. c[0] = u;
  2115. width = len = 1;
  2116. } else {
  2117. len = utf8encode(u, c);
  2118. if (!control && (width = wcwidth(u)) == -1) {
  2119. memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
  2120. width = 1;
  2121. }
  2122. }
  2123. if (IS_SET(MODE_PRINT))
  2124. tprinter(c, len);
  2125. /*
  2126. * STR sequence must be checked before anything else
  2127. * because it uses all following characters until it
  2128. * receives a ESC, a SUB, a ST or any other C1 control
  2129. * character.
  2130. */
  2131. if (term.esc & ESC_STR) {
  2132. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2133. ISCONTROLC1(u)) {
  2134. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2135. if (IS_SET(MODE_SIXEL)) {
  2136. /* TODO: render sixel */;
  2137. term.mode &= ~MODE_SIXEL;
  2138. return;
  2139. }
  2140. term.esc |= ESC_STR_END;
  2141. goto check_control_code;
  2142. }
  2143. if (IS_SET(MODE_SIXEL)) {
  2144. /* TODO: implement sixel mode */
  2145. return;
  2146. }
  2147. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2148. term.mode |= MODE_SIXEL;
  2149. if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
  2150. /*
  2151. * Here is a bug in terminals. If the user never sends
  2152. * some code to stop the str or esc command, then st
  2153. * will stop responding. But this is better than
  2154. * silently failing with unknown characters. At least
  2155. * then users will report back.
  2156. *
  2157. * In the case users ever get fixed, here is the code:
  2158. */
  2159. /*
  2160. * term.esc = 0;
  2161. * strhandle();
  2162. */
  2163. return;
  2164. }
  2165. memmove(&strescseq.buf[strescseq.len], c, len);
  2166. strescseq.len += len;
  2167. return;
  2168. }
  2169. check_control_code:
  2170. /*
  2171. * Actions of control codes must be performed as soon they arrive
  2172. * because they can be embedded inside a control sequence, and
  2173. * they must not cause conflicts with sequences.
  2174. */
  2175. if (control) {
  2176. tcontrolcode(u);
  2177. /*
  2178. * control codes are not shown ever
  2179. */
  2180. return;
  2181. } else if (term.esc & ESC_START) {
  2182. if (term.esc & ESC_CSI) {
  2183. csiescseq.buf[csiescseq.len++] = u;
  2184. if (BETWEEN(u, 0x40, 0x7E)
  2185. || csiescseq.len >= \
  2186. sizeof(csiescseq.buf)-1) {
  2187. term.esc = 0;
  2188. csiparse();
  2189. csihandle();
  2190. }
  2191. return;
  2192. } else if (term.esc & ESC_UTF8) {
  2193. tdefutf8(u);
  2194. } else if (term.esc & ESC_ALTCHARSET) {
  2195. tdeftran(u);
  2196. } else if (term.esc & ESC_TEST) {
  2197. tdectest(u);
  2198. } else {
  2199. if (!eschandle(u))
  2200. return;
  2201. /* sequence already finished */
  2202. }
  2203. term.esc = 0;
  2204. /*
  2205. * All characters which form part of a sequence are not
  2206. * printed
  2207. */
  2208. return;
  2209. }
  2210. if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2211. selclear();
  2212. gp = &term.line[term.c.y][term.c.x];
  2213. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2214. gp->mode |= ATTR_WRAP;
  2215. tnewline(1);
  2216. gp = &term.line[term.c.y][term.c.x];
  2217. }
  2218. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2219. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2220. if (term.c.x+width > term.col) {
  2221. tnewline(1);
  2222. gp = &term.line[term.c.y][term.c.x];
  2223. }
  2224. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2225. if (width == 2) {
  2226. gp->mode |= ATTR_WIDE;
  2227. if (term.c.x+1 < term.col) {
  2228. gp[1].u = '\0';
  2229. gp[1].mode = ATTR_WDUMMY;
  2230. }
  2231. }
  2232. if (term.c.x+width < term.col) {
  2233. tmoveto(term.c.x+width, term.c.y);
  2234. } else {
  2235. term.c.state |= CURSOR_WRAPNEXT;
  2236. }
  2237. }
  2238. void
  2239. tresize(int col, int row)
  2240. {
  2241. int i;
  2242. int minrow = MIN(row, term.row);
  2243. int mincol = MIN(col, term.col);
  2244. int *bp;
  2245. TCursor c;
  2246. if (col < 1 || row < 1) {
  2247. fprintf(stderr,
  2248. "tresize: error resizing to %dx%d\n", col, row);
  2249. return;
  2250. }
  2251. /*
  2252. * slide screen to keep cursor where we expect it -
  2253. * tscrollup would work here, but we can optimize to
  2254. * memmove because we're freeing the earlier lines
  2255. */
  2256. for (i = 0; i <= term.c.y - row; i++) {
  2257. free(term.line[i]);
  2258. free(term.alt[i]);
  2259. }
  2260. /* ensure that both src and dst are not NULL */
  2261. if (i > 0) {
  2262. memmove(term.line, term.line + i, row * sizeof(Line));
  2263. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2264. }
  2265. for (i += row; i < term.row; i++) {
  2266. free(term.line[i]);
  2267. free(term.alt[i]);
  2268. }
  2269. /* resize to new width */
  2270. term.specbuf = xrealloc(term.specbuf, col * sizeof(GlyphFontSpec));
  2271. /* resize to new height */
  2272. term.line = xrealloc(term.line, row * sizeof(Line));
  2273. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2274. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2275. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2276. /* resize each row to new width, zero-pad if needed */
  2277. for (i = 0; i < minrow; i++) {
  2278. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2279. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2280. }
  2281. /* allocate any new rows */
  2282. for (/* i = minrow */; i < row; i++) {
  2283. term.line[i] = xmalloc(col * sizeof(Glyph));
  2284. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2285. }
  2286. if (col > term.col) {
  2287. bp = term.tabs + term.col;
  2288. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2289. while (--bp > term.tabs && !*bp)
  2290. /* nothing */ ;
  2291. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2292. *bp = 1;
  2293. }
  2294. /* update terminal size */
  2295. term.col = col;
  2296. term.row = row;
  2297. /* reset scrolling region */
  2298. tsetscroll(0, row-1);
  2299. /* make use of the LIMIT in tmoveto */
  2300. tmoveto(term.c.x, term.c.y);
  2301. /* Clearing both screens (it makes dirty all lines) */
  2302. c = term.c;
  2303. for (i = 0; i < 2; i++) {
  2304. if (mincol < col && 0 < minrow) {
  2305. tclearregion(mincol, 0, col - 1, minrow - 1);
  2306. }
  2307. if (0 < col && minrow < row) {
  2308. tclearregion(0, minrow, col - 1, row - 1);
  2309. }
  2310. tswapscreen();
  2311. tcursor(CURSOR_LOAD);
  2312. }
  2313. term.c = c;
  2314. }
  2315. void
  2316. zoom(const Arg *arg)
  2317. {
  2318. Arg larg;
  2319. larg.f = usedfontsize + arg->f;
  2320. zoomabs(&larg);
  2321. }
  2322. void
  2323. zoomabs(const Arg *arg)
  2324. {
  2325. xunloadfonts();
  2326. xloadfonts(usedfont, arg->f);
  2327. cresize(0, 0);
  2328. ttyresize();
  2329. redraw();
  2330. xhints();
  2331. }
  2332. void
  2333. zoomreset(const Arg *arg)
  2334. {
  2335. Arg larg;
  2336. if (defaultfontsize > 0) {
  2337. larg.f = defaultfontsize;
  2338. zoomabs(&larg);
  2339. }
  2340. }
  2341. void
  2342. resettitle(void)
  2343. {
  2344. xsettitle(opt_title ? opt_title : "st");
  2345. }
  2346. void
  2347. redraw(void)
  2348. {
  2349. tfulldirt();
  2350. draw();
  2351. }
  2352. int
  2353. match(uint mask, uint state)
  2354. {
  2355. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  2356. }
  2357. void
  2358. numlock(const Arg *dummy)
  2359. {
  2360. term.numlock ^= 1;
  2361. }
  2362. char*
  2363. kmap(KeySym k, uint state)
  2364. {
  2365. Key *kp;
  2366. int i;
  2367. /* Check for mapped keys out of X11 function keys. */
  2368. for (i = 0; i < LEN(mappedkeys); i++) {
  2369. if (mappedkeys[i] == k)
  2370. break;
  2371. }
  2372. if (i == LEN(mappedkeys)) {
  2373. if ((k & 0xFFFF) < 0xFD00)
  2374. return NULL;
  2375. }
  2376. for (kp = key; kp < key + LEN(key); kp++) {
  2377. if (kp->k != k)
  2378. continue;
  2379. if (!match(kp->mask, state))
  2380. continue;
  2381. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  2382. continue;
  2383. if (term.numlock && kp->appkey == 2)
  2384. continue;
  2385. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  2386. continue;
  2387. if (IS_SET(MODE_CRLF) ? kp->crlf < 0 : kp->crlf > 0)
  2388. continue;
  2389. return kp->s;
  2390. }
  2391. return NULL;
  2392. }
  2393. void
  2394. cresize(int width, int height)
  2395. {
  2396. int col, row;
  2397. if (width != 0)
  2398. win.w = width;
  2399. if (height != 0)
  2400. win.h = height;
  2401. col = (win.w - 2 * borderpx) / win.cw;
  2402. row = (win.h - 2 * borderpx) / win.ch;
  2403. tresize(col, row);
  2404. xresize(col, row);
  2405. }
  2406. void
  2407. usage(void)
  2408. {
  2409. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  2410. " [-n name] [-o file]\n"
  2411. " [-T title] [-t title] [-w windowid]"
  2412. " [[-e] command [args ...]]\n"
  2413. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  2414. " [-n name] [-o file]\n"
  2415. " [-T title] [-t title] [-w windowid] -l line"
  2416. " [stty_args ...]\n", argv0, argv0);
  2417. }