st.c 58 KB

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