st.c 56 KB

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