st.c 57 KB

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