st.c 52 KB

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