st.c 54 KB

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