st.c 55 KB

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