st.c 52 KB

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