st.c 57 KB

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