st.c 53 KB

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