st.c 54 KB

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