st.c 53 KB

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