st.c 58 KB

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