st.c 54 KB

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