st.c 54 KB

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