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