st.c 57 KB

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