st.c 56 KB

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