st.c 54 KB

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