st.c.orig 57 KB

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