st.c 53 KB

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