st.c 58 KB

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