st.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  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 int twrite(const char *, int, int);
  148. static void tfulldirt(void);
  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(const 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. int written;
  665. int ret;
  666. /* append read bytes to unprocessed bytes */
  667. if ((ret = read(cmdfd, buf+buflen, LEN(buf)-buflen)) < 0)
  668. die("Couldn't read from shell: %s\n", strerror(errno));
  669. buflen += ret;
  670. written = twrite(buf, buflen, 0);
  671. buflen -= written;
  672. /* keep any uncomplete utf8 char for the next call */
  673. if (buflen > 0)
  674. memmove(buf, buf + written, buflen);
  675. return ret;
  676. }
  677. void
  678. ttywrite(const char *s, size_t n)
  679. {
  680. fd_set wfd, rfd;
  681. ssize_t r;
  682. size_t lim = 256;
  683. /*
  684. * Remember that we are using a pty, which might be a modem line.
  685. * Writing too much will clog the line. That's why we are doing this
  686. * dance.
  687. * FIXME: Migrate the world to Plan 9.
  688. */
  689. while (n > 0) {
  690. FD_ZERO(&wfd);
  691. FD_ZERO(&rfd);
  692. FD_SET(cmdfd, &wfd);
  693. FD_SET(cmdfd, &rfd);
  694. /* Check if we can write. */
  695. if (pselect(cmdfd+1, &rfd, &wfd, NULL, NULL, NULL) < 0) {
  696. if (errno == EINTR)
  697. continue;
  698. die("select failed: %s\n", strerror(errno));
  699. }
  700. if (FD_ISSET(cmdfd, &wfd)) {
  701. /*
  702. * Only write the bytes written by ttywrite() or the
  703. * default of 256. This seems to be a reasonable value
  704. * for a serial line. Bigger values might clog the I/O.
  705. */
  706. if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
  707. goto write_error;
  708. if (r < n) {
  709. /*
  710. * We weren't able to write out everything.
  711. * This means the buffer is getting full
  712. * again. Empty it.
  713. */
  714. if (n < lim)
  715. lim = ttyread();
  716. n -= r;
  717. s += r;
  718. } else {
  719. /* All bytes have been written. */
  720. break;
  721. }
  722. }
  723. if (FD_ISSET(cmdfd, &rfd))
  724. lim = ttyread();
  725. }
  726. return;
  727. write_error:
  728. die("write error on tty: %s\n", strerror(errno));
  729. }
  730. void
  731. ttysend(char *s, size_t n)
  732. {
  733. ttywrite(s, n);
  734. if (IS_SET(MODE_ECHO))
  735. twrite(s, n, 1);
  736. }
  737. void
  738. ttyresize(int tw, int th)
  739. {
  740. struct winsize w;
  741. w.ws_row = term.row;
  742. w.ws_col = term.col;
  743. w.ws_xpixel = tw;
  744. w.ws_ypixel = th;
  745. if (ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
  746. fprintf(stderr, "Couldn't set window size: %s\n", strerror(errno));
  747. }
  748. int
  749. tattrset(int attr)
  750. {
  751. int i, j;
  752. for (i = 0; i < term.row-1; i++) {
  753. for (j = 0; j < term.col-1; j++) {
  754. if (term.line[i][j].mode & attr)
  755. return 1;
  756. }
  757. }
  758. return 0;
  759. }
  760. void
  761. tsetdirt(int top, int bot)
  762. {
  763. int i;
  764. LIMIT(top, 0, term.row-1);
  765. LIMIT(bot, 0, term.row-1);
  766. for (i = top; i <= bot; i++)
  767. term.dirty[i] = 1;
  768. }
  769. void
  770. tsetdirtattr(int attr)
  771. {
  772. int i, j;
  773. for (i = 0; i < term.row-1; i++) {
  774. for (j = 0; j < term.col-1; j++) {
  775. if (term.line[i][j].mode & attr) {
  776. tsetdirt(i, i);
  777. break;
  778. }
  779. }
  780. }
  781. }
  782. void
  783. tfulldirt(void)
  784. {
  785. tsetdirt(0, term.row-1);
  786. }
  787. void
  788. tcursor(int mode)
  789. {
  790. static TCursor c[2];
  791. int alt = IS_SET(MODE_ALTSCREEN);
  792. if (mode == CURSOR_SAVE) {
  793. c[alt] = term.c;
  794. } else if (mode == CURSOR_LOAD) {
  795. term.c = c[alt];
  796. tmoveto(c[alt].x, c[alt].y);
  797. }
  798. }
  799. void
  800. treset(void)
  801. {
  802. uint i;
  803. term.c = (TCursor){{
  804. .mode = ATTR_NULL,
  805. .fg = defaultfg,
  806. .bg = defaultbg
  807. }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
  808. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  809. for (i = tabspaces; i < term.col; i += tabspaces)
  810. term.tabs[i] = 1;
  811. term.top = 0;
  812. term.bot = term.row - 1;
  813. term.mode = MODE_WRAP|MODE_UTF8;
  814. memset(term.trantbl, CS_USA, sizeof(term.trantbl));
  815. term.charset = 0;
  816. for (i = 0; i < 2; i++) {
  817. tmoveto(0, 0);
  818. tcursor(CURSOR_SAVE);
  819. tclearregion(0, 0, term.col-1, term.row-1);
  820. tswapscreen();
  821. }
  822. }
  823. void
  824. tnew(int col, int row)
  825. {
  826. term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
  827. tresize(col, row);
  828. term.numlock = 1;
  829. treset();
  830. }
  831. void
  832. tswapscreen(void)
  833. {
  834. Line *tmp = term.line;
  835. term.line = term.alt;
  836. term.alt = tmp;
  837. term.mode ^= MODE_ALTSCREEN;
  838. tfulldirt();
  839. }
  840. void
  841. tscrolldown(int orig, int n)
  842. {
  843. int i;
  844. Line temp;
  845. LIMIT(n, 0, term.bot-orig+1);
  846. tsetdirt(orig, term.bot-n);
  847. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  848. for (i = term.bot; i >= orig+n; i--) {
  849. temp = term.line[i];
  850. term.line[i] = term.line[i-n];
  851. term.line[i-n] = temp;
  852. }
  853. selscroll(orig, n);
  854. }
  855. void
  856. tscrollup(int orig, int n)
  857. {
  858. int i;
  859. Line temp;
  860. LIMIT(n, 0, term.bot-orig+1);
  861. tclearregion(0, orig, term.col-1, orig+n-1);
  862. tsetdirt(orig+n, term.bot);
  863. for (i = orig; i <= term.bot-n; i++) {
  864. temp = term.line[i];
  865. term.line[i] = term.line[i+n];
  866. term.line[i+n] = temp;
  867. }
  868. selscroll(orig, -n);
  869. }
  870. void
  871. selscroll(int orig, int n)
  872. {
  873. if (sel.ob.x == -1)
  874. return;
  875. if (BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
  876. if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
  877. selclear();
  878. return;
  879. }
  880. if (sel.type == SEL_RECTANGULAR) {
  881. if (sel.ob.y < term.top)
  882. sel.ob.y = term.top;
  883. if (sel.oe.y > term.bot)
  884. sel.oe.y = term.bot;
  885. } else {
  886. if (sel.ob.y < term.top) {
  887. sel.ob.y = term.top;
  888. sel.ob.x = 0;
  889. }
  890. if (sel.oe.y > term.bot) {
  891. sel.oe.y = term.bot;
  892. sel.oe.x = term.col;
  893. }
  894. }
  895. selnormalize();
  896. }
  897. }
  898. void
  899. tnewline(int first_col)
  900. {
  901. int y = term.c.y;
  902. if (y == term.bot) {
  903. tscrollup(term.top, 1);
  904. } else {
  905. y++;
  906. }
  907. tmoveto(first_col ? 0 : term.c.x, y);
  908. }
  909. void
  910. csiparse(void)
  911. {
  912. char *p = csiescseq.buf, *np;
  913. long int v;
  914. csiescseq.narg = 0;
  915. if (*p == '?') {
  916. csiescseq.priv = 1;
  917. p++;
  918. }
  919. csiescseq.buf[csiescseq.len] = '\0';
  920. while (p < csiescseq.buf+csiescseq.len) {
  921. np = NULL;
  922. v = strtol(p, &np, 10);
  923. if (np == p)
  924. v = 0;
  925. if (v == LONG_MAX || v == LONG_MIN)
  926. v = -1;
  927. csiescseq.arg[csiescseq.narg++] = v;
  928. p = np;
  929. if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
  930. break;
  931. p++;
  932. }
  933. csiescseq.mode[0] = *p++;
  934. csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
  935. }
  936. /* for absolute user moves, when decom is set */
  937. void
  938. tmoveato(int x, int y)
  939. {
  940. tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
  941. }
  942. void
  943. tmoveto(int x, int y)
  944. {
  945. int miny, maxy;
  946. if (term.c.state & CURSOR_ORIGIN) {
  947. miny = term.top;
  948. maxy = term.bot;
  949. } else {
  950. miny = 0;
  951. maxy = term.row - 1;
  952. }
  953. term.c.state &= ~CURSOR_WRAPNEXT;
  954. term.c.x = LIMIT(x, 0, term.col-1);
  955. term.c.y = LIMIT(y, miny, maxy);
  956. }
  957. void
  958. tsetchar(Rune u, Glyph *attr, int x, int y)
  959. {
  960. static char *vt100_0[62] = { /* 0x41 - 0x7e */
  961. "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
  962. 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
  963. 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
  964. 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
  965. "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
  966. "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
  967. "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
  968. "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
  969. };
  970. /*
  971. * The table is proudly stolen from rxvt.
  972. */
  973. if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
  974. BETWEEN(u, 0x41, 0x7e) && vt100_0[u - 0x41])
  975. utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ);
  976. if (term.line[y][x].mode & ATTR_WIDE) {
  977. if (x+1 < term.col) {
  978. term.line[y][x+1].u = ' ';
  979. term.line[y][x+1].mode &= ~ATTR_WDUMMY;
  980. }
  981. } else if (term.line[y][x].mode & ATTR_WDUMMY) {
  982. term.line[y][x-1].u = ' ';
  983. term.line[y][x-1].mode &= ~ATTR_WIDE;
  984. }
  985. term.dirty[y] = 1;
  986. term.line[y][x] = *attr;
  987. term.line[y][x].u = u;
  988. }
  989. void
  990. tclearregion(int x1, int y1, int x2, int y2)
  991. {
  992. int x, y, temp;
  993. Glyph *gp;
  994. if (x1 > x2)
  995. temp = x1, x1 = x2, x2 = temp;
  996. if (y1 > y2)
  997. temp = y1, y1 = y2, y2 = temp;
  998. LIMIT(x1, 0, term.col-1);
  999. LIMIT(x2, 0, term.col-1);
  1000. LIMIT(y1, 0, term.row-1);
  1001. LIMIT(y2, 0, term.row-1);
  1002. for (y = y1; y <= y2; y++) {
  1003. term.dirty[y] = 1;
  1004. for (x = x1; x <= x2; x++) {
  1005. gp = &term.line[y][x];
  1006. if (selected(x, y))
  1007. selclear();
  1008. gp->fg = term.c.attr.fg;
  1009. gp->bg = term.c.attr.bg;
  1010. gp->mode = 0;
  1011. gp->u = ' ';
  1012. }
  1013. }
  1014. }
  1015. void
  1016. tdeletechar(int n)
  1017. {
  1018. int dst, src, size;
  1019. Glyph *line;
  1020. LIMIT(n, 0, term.col - term.c.x);
  1021. dst = term.c.x;
  1022. src = term.c.x + n;
  1023. size = term.col - src;
  1024. line = term.line[term.c.y];
  1025. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1026. tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
  1027. }
  1028. void
  1029. tinsertblank(int n)
  1030. {
  1031. int dst, src, size;
  1032. Glyph *line;
  1033. LIMIT(n, 0, term.col - term.c.x);
  1034. dst = term.c.x + n;
  1035. src = term.c.x;
  1036. size = term.col - dst;
  1037. line = term.line[term.c.y];
  1038. memmove(&line[dst], &line[src], size * sizeof(Glyph));
  1039. tclearregion(src, term.c.y, dst - 1, term.c.y);
  1040. }
  1041. void
  1042. tinsertblankline(int n)
  1043. {
  1044. if (BETWEEN(term.c.y, term.top, term.bot))
  1045. tscrolldown(term.c.y, n);
  1046. }
  1047. void
  1048. tdeleteline(int n)
  1049. {
  1050. if (BETWEEN(term.c.y, term.top, term.bot))
  1051. tscrollup(term.c.y, n);
  1052. }
  1053. int32_t
  1054. tdefcolor(int *attr, int *npar, int l)
  1055. {
  1056. int32_t idx = -1;
  1057. uint r, g, b;
  1058. switch (attr[*npar + 1]) {
  1059. case 2: /* direct color in RGB space */
  1060. if (*npar + 4 >= l) {
  1061. fprintf(stderr,
  1062. "erresc(38): Incorrect number of parameters (%d)\n",
  1063. *npar);
  1064. break;
  1065. }
  1066. r = attr[*npar + 2];
  1067. g = attr[*npar + 3];
  1068. b = attr[*npar + 4];
  1069. *npar += 4;
  1070. if (!BETWEEN(r, 0, 255) || !BETWEEN(g, 0, 255) || !BETWEEN(b, 0, 255))
  1071. fprintf(stderr, "erresc: bad rgb color (%u,%u,%u)\n",
  1072. r, g, b);
  1073. else
  1074. idx = TRUECOLOR(r, g, b);
  1075. break;
  1076. case 5: /* indexed color */
  1077. if (*npar + 2 >= l) {
  1078. fprintf(stderr,
  1079. "erresc(38): Incorrect number of parameters (%d)\n",
  1080. *npar);
  1081. break;
  1082. }
  1083. *npar += 2;
  1084. if (!BETWEEN(attr[*npar], 0, 255))
  1085. fprintf(stderr, "erresc: bad fgcolor %d\n", attr[*npar]);
  1086. else
  1087. idx = attr[*npar];
  1088. break;
  1089. case 0: /* implemented defined (only foreground) */
  1090. case 1: /* transparent */
  1091. case 3: /* direct color in CMY space */
  1092. case 4: /* direct color in CMYK space */
  1093. default:
  1094. fprintf(stderr,
  1095. "erresc(38): gfx attr %d unknown\n", attr[*npar]);
  1096. break;
  1097. }
  1098. return idx;
  1099. }
  1100. void
  1101. tsetattr(int *attr, int l)
  1102. {
  1103. int i;
  1104. int32_t idx;
  1105. for (i = 0; i < l; i++) {
  1106. switch (attr[i]) {
  1107. case 0:
  1108. term.c.attr.mode &= ~(
  1109. ATTR_BOLD |
  1110. ATTR_FAINT |
  1111. ATTR_ITALIC |
  1112. ATTR_UNDERLINE |
  1113. ATTR_BLINK |
  1114. ATTR_REVERSE |
  1115. ATTR_INVISIBLE |
  1116. ATTR_STRUCK );
  1117. term.c.attr.fg = defaultfg;
  1118. term.c.attr.bg = defaultbg;
  1119. break;
  1120. case 1:
  1121. term.c.attr.mode |= ATTR_BOLD;
  1122. break;
  1123. case 2:
  1124. term.c.attr.mode |= ATTR_FAINT;
  1125. break;
  1126. case 3:
  1127. term.c.attr.mode |= ATTR_ITALIC;
  1128. break;
  1129. case 4:
  1130. term.c.attr.mode |= ATTR_UNDERLINE;
  1131. break;
  1132. case 5: /* slow blink */
  1133. /* FALLTHROUGH */
  1134. case 6: /* rapid blink */
  1135. term.c.attr.mode |= ATTR_BLINK;
  1136. break;
  1137. case 7:
  1138. term.c.attr.mode |= ATTR_REVERSE;
  1139. break;
  1140. case 8:
  1141. term.c.attr.mode |= ATTR_INVISIBLE;
  1142. break;
  1143. case 9:
  1144. term.c.attr.mode |= ATTR_STRUCK;
  1145. break;
  1146. case 22:
  1147. term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
  1148. break;
  1149. case 23:
  1150. term.c.attr.mode &= ~ATTR_ITALIC;
  1151. break;
  1152. case 24:
  1153. term.c.attr.mode &= ~ATTR_UNDERLINE;
  1154. break;
  1155. case 25:
  1156. term.c.attr.mode &= ~ATTR_BLINK;
  1157. break;
  1158. case 27:
  1159. term.c.attr.mode &= ~ATTR_REVERSE;
  1160. break;
  1161. case 28:
  1162. term.c.attr.mode &= ~ATTR_INVISIBLE;
  1163. break;
  1164. case 29:
  1165. term.c.attr.mode &= ~ATTR_STRUCK;
  1166. break;
  1167. case 38:
  1168. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1169. term.c.attr.fg = idx;
  1170. break;
  1171. case 39:
  1172. term.c.attr.fg = defaultfg;
  1173. break;
  1174. case 48:
  1175. if ((idx = tdefcolor(attr, &i, l)) >= 0)
  1176. term.c.attr.bg = idx;
  1177. break;
  1178. case 49:
  1179. term.c.attr.bg = defaultbg;
  1180. break;
  1181. default:
  1182. if (BETWEEN(attr[i], 30, 37)) {
  1183. term.c.attr.fg = attr[i] - 30;
  1184. } else if (BETWEEN(attr[i], 40, 47)) {
  1185. term.c.attr.bg = attr[i] - 40;
  1186. } else if (BETWEEN(attr[i], 90, 97)) {
  1187. term.c.attr.fg = attr[i] - 90 + 8;
  1188. } else if (BETWEEN(attr[i], 100, 107)) {
  1189. term.c.attr.bg = attr[i] - 100 + 8;
  1190. } else {
  1191. fprintf(stderr,
  1192. "erresc(default): gfx attr %d unknown\n",
  1193. attr[i]), csidump();
  1194. }
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. void
  1200. tsetscroll(int t, int b)
  1201. {
  1202. int temp;
  1203. LIMIT(t, 0, term.row-1);
  1204. LIMIT(b, 0, term.row-1);
  1205. if (t > b) {
  1206. temp = t;
  1207. t = b;
  1208. b = temp;
  1209. }
  1210. term.top = t;
  1211. term.bot = b;
  1212. }
  1213. void
  1214. tsetmode(int priv, int set, int *args, int narg)
  1215. {
  1216. int *lim, mode;
  1217. int alt;
  1218. for (lim = args + narg; args < lim; ++args) {
  1219. if (priv) {
  1220. switch (*args) {
  1221. case 1: /* DECCKM -- Cursor key */
  1222. MODBIT(term.mode, set, MODE_APPCURSOR);
  1223. break;
  1224. case 5: /* DECSCNM -- Reverse video */
  1225. mode = term.mode;
  1226. MODBIT(term.mode, set, MODE_REVERSE);
  1227. if (mode != term.mode)
  1228. redraw();
  1229. break;
  1230. case 6: /* DECOM -- Origin */
  1231. MODBIT(term.c.state, set, CURSOR_ORIGIN);
  1232. tmoveato(0, 0);
  1233. break;
  1234. case 7: /* DECAWM -- Auto wrap */
  1235. MODBIT(term.mode, set, MODE_WRAP);
  1236. break;
  1237. case 0: /* Error (IGNORED) */
  1238. case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
  1239. case 3: /* DECCOLM -- Column (IGNORED) */
  1240. case 4: /* DECSCLM -- Scroll (IGNORED) */
  1241. case 8: /* DECARM -- Auto repeat (IGNORED) */
  1242. case 18: /* DECPFF -- Printer feed (IGNORED) */
  1243. case 19: /* DECPEX -- Printer extent (IGNORED) */
  1244. case 42: /* DECNRCM -- National characters (IGNORED) */
  1245. case 12: /* att610 -- Start blinking cursor (IGNORED) */
  1246. break;
  1247. case 25: /* DECTCEM -- Text Cursor Enable Mode */
  1248. MODBIT(term.mode, !set, MODE_HIDE);
  1249. break;
  1250. case 9: /* X10 mouse compatibility mode */
  1251. xsetpointermotion(0);
  1252. MODBIT(term.mode, 0, MODE_MOUSE);
  1253. MODBIT(term.mode, set, MODE_MOUSEX10);
  1254. break;
  1255. case 1000: /* 1000: report button press */
  1256. xsetpointermotion(0);
  1257. MODBIT(term.mode, 0, MODE_MOUSE);
  1258. MODBIT(term.mode, set, MODE_MOUSEBTN);
  1259. break;
  1260. case 1002: /* 1002: report motion on button press */
  1261. xsetpointermotion(0);
  1262. MODBIT(term.mode, 0, MODE_MOUSE);
  1263. MODBIT(term.mode, set, MODE_MOUSEMOTION);
  1264. break;
  1265. case 1003: /* 1003: enable all mouse motions */
  1266. xsetpointermotion(set);
  1267. MODBIT(term.mode, 0, MODE_MOUSE);
  1268. MODBIT(term.mode, set, MODE_MOUSEMANY);
  1269. break;
  1270. case 1004: /* 1004: send focus events to tty */
  1271. MODBIT(term.mode, set, MODE_FOCUS);
  1272. break;
  1273. case 1006: /* 1006: extended reporting mode */
  1274. MODBIT(term.mode, set, MODE_MOUSESGR);
  1275. break;
  1276. case 1034:
  1277. MODBIT(term.mode, set, MODE_8BIT);
  1278. break;
  1279. case 1049: /* swap screen & set/restore cursor as xterm */
  1280. if (!allowaltscreen)
  1281. break;
  1282. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1283. /* FALLTHROUGH */
  1284. case 47: /* swap screen */
  1285. case 1047:
  1286. if (!allowaltscreen)
  1287. break;
  1288. alt = IS_SET(MODE_ALTSCREEN);
  1289. if (alt) {
  1290. tclearregion(0, 0, term.col-1,
  1291. term.row-1);
  1292. }
  1293. if (set ^ alt) /* set is always 1 or 0 */
  1294. tswapscreen();
  1295. if (*args != 1049)
  1296. break;
  1297. /* FALLTHROUGH */
  1298. case 1048:
  1299. tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
  1300. break;
  1301. case 2004: /* 2004: bracketed paste mode */
  1302. MODBIT(term.mode, set, MODE_BRCKTPASTE);
  1303. break;
  1304. /* Not implemented mouse modes. See comments there. */
  1305. case 1001: /* mouse highlight mode; can hang the
  1306. terminal by design when implemented. */
  1307. case 1005: /* UTF-8 mouse mode; will confuse
  1308. applications not supporting UTF-8
  1309. and luit. */
  1310. case 1015: /* urxvt mangled mouse mode; incompatible
  1311. and can be mistaken for other control
  1312. codes. */
  1313. default:
  1314. fprintf(stderr,
  1315. "erresc: unknown private set/reset mode %d\n",
  1316. *args);
  1317. break;
  1318. }
  1319. } else {
  1320. switch (*args) {
  1321. case 0: /* Error (IGNORED) */
  1322. break;
  1323. case 2: /* KAM -- keyboard action */
  1324. MODBIT(term.mode, set, MODE_KBDLOCK);
  1325. break;
  1326. case 4: /* IRM -- Insertion-replacement */
  1327. MODBIT(term.mode, set, MODE_INSERT);
  1328. break;
  1329. case 12: /* SRM -- Send/Receive */
  1330. MODBIT(term.mode, !set, MODE_ECHO);
  1331. break;
  1332. case 20: /* LNM -- Linefeed/new line */
  1333. MODBIT(term.mode, set, MODE_CRLF);
  1334. break;
  1335. default:
  1336. fprintf(stderr,
  1337. "erresc: unknown set/reset mode %d\n",
  1338. *args);
  1339. break;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. void
  1345. csihandle(void)
  1346. {
  1347. char buf[40];
  1348. int len;
  1349. switch (csiescseq.mode[0]) {
  1350. default:
  1351. unknown:
  1352. fprintf(stderr, "erresc: unknown csi ");
  1353. csidump();
  1354. /* die(""); */
  1355. break;
  1356. case '@': /* ICH -- Insert <n> blank char */
  1357. DEFAULT(csiescseq.arg[0], 1);
  1358. tinsertblank(csiescseq.arg[0]);
  1359. break;
  1360. case 'A': /* CUU -- Cursor <n> Up */
  1361. DEFAULT(csiescseq.arg[0], 1);
  1362. tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
  1363. break;
  1364. case 'B': /* CUD -- Cursor <n> Down */
  1365. case 'e': /* VPR --Cursor <n> Down */
  1366. DEFAULT(csiescseq.arg[0], 1);
  1367. tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
  1368. break;
  1369. case 'i': /* MC -- Media Copy */
  1370. switch (csiescseq.arg[0]) {
  1371. case 0:
  1372. tdump();
  1373. break;
  1374. case 1:
  1375. tdumpline(term.c.y);
  1376. break;
  1377. case 2:
  1378. tdumpsel();
  1379. break;
  1380. case 4:
  1381. term.mode &= ~MODE_PRINT;
  1382. break;
  1383. case 5:
  1384. term.mode |= MODE_PRINT;
  1385. break;
  1386. }
  1387. break;
  1388. case 'c': /* DA -- Device Attributes */
  1389. if (csiescseq.arg[0] == 0)
  1390. ttywrite(vtiden, sizeof(vtiden) - 1);
  1391. break;
  1392. case 'C': /* CUF -- Cursor <n> Forward */
  1393. case 'a': /* HPR -- Cursor <n> Forward */
  1394. DEFAULT(csiescseq.arg[0], 1);
  1395. tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
  1396. break;
  1397. case 'D': /* CUB -- Cursor <n> Backward */
  1398. DEFAULT(csiescseq.arg[0], 1);
  1399. tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
  1400. break;
  1401. case 'E': /* CNL -- Cursor <n> Down and first col */
  1402. DEFAULT(csiescseq.arg[0], 1);
  1403. tmoveto(0, term.c.y+csiescseq.arg[0]);
  1404. break;
  1405. case 'F': /* CPL -- Cursor <n> Up and first col */
  1406. DEFAULT(csiescseq.arg[0], 1);
  1407. tmoveto(0, term.c.y-csiescseq.arg[0]);
  1408. break;
  1409. case 'g': /* TBC -- Tabulation clear */
  1410. switch (csiescseq.arg[0]) {
  1411. case 0: /* clear current tab stop */
  1412. term.tabs[term.c.x] = 0;
  1413. break;
  1414. case 3: /* clear all the tabs */
  1415. memset(term.tabs, 0, term.col * sizeof(*term.tabs));
  1416. break;
  1417. default:
  1418. goto unknown;
  1419. }
  1420. break;
  1421. case 'G': /* CHA -- Move to <col> */
  1422. case '`': /* HPA */
  1423. DEFAULT(csiescseq.arg[0], 1);
  1424. tmoveto(csiescseq.arg[0]-1, term.c.y);
  1425. break;
  1426. case 'H': /* CUP -- Move to <row> <col> */
  1427. case 'f': /* HVP */
  1428. DEFAULT(csiescseq.arg[0], 1);
  1429. DEFAULT(csiescseq.arg[1], 1);
  1430. tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
  1431. break;
  1432. case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
  1433. DEFAULT(csiescseq.arg[0], 1);
  1434. tputtab(csiescseq.arg[0]);
  1435. break;
  1436. case 'J': /* ED -- Clear screen */
  1437. selclear();
  1438. switch (csiescseq.arg[0]) {
  1439. case 0: /* below */
  1440. tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
  1441. if (term.c.y < term.row-1) {
  1442. tclearregion(0, term.c.y+1, term.col-1,
  1443. term.row-1);
  1444. }
  1445. break;
  1446. case 1: /* above */
  1447. if (term.c.y > 1)
  1448. tclearregion(0, 0, term.col-1, term.c.y-1);
  1449. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1450. break;
  1451. case 2: /* all */
  1452. tclearregion(0, 0, term.col-1, term.row-1);
  1453. break;
  1454. default:
  1455. goto unknown;
  1456. }
  1457. break;
  1458. case 'K': /* EL -- Clear line */
  1459. switch (csiescseq.arg[0]) {
  1460. case 0: /* right */
  1461. tclearregion(term.c.x, term.c.y, term.col-1,
  1462. term.c.y);
  1463. break;
  1464. case 1: /* left */
  1465. tclearregion(0, term.c.y, term.c.x, term.c.y);
  1466. break;
  1467. case 2: /* all */
  1468. tclearregion(0, term.c.y, term.col-1, term.c.y);
  1469. break;
  1470. }
  1471. break;
  1472. case 'S': /* SU -- Scroll <n> line up */
  1473. DEFAULT(csiescseq.arg[0], 1);
  1474. tscrollup(term.top, csiescseq.arg[0]);
  1475. break;
  1476. case 'T': /* SD -- Scroll <n> line down */
  1477. DEFAULT(csiescseq.arg[0], 1);
  1478. tscrolldown(term.top, csiescseq.arg[0]);
  1479. break;
  1480. case 'L': /* IL -- Insert <n> blank lines */
  1481. DEFAULT(csiescseq.arg[0], 1);
  1482. tinsertblankline(csiescseq.arg[0]);
  1483. break;
  1484. case 'l': /* RM -- Reset Mode */
  1485. tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
  1486. break;
  1487. case 'M': /* DL -- Delete <n> lines */
  1488. DEFAULT(csiescseq.arg[0], 1);
  1489. tdeleteline(csiescseq.arg[0]);
  1490. break;
  1491. case 'X': /* ECH -- Erase <n> char */
  1492. DEFAULT(csiescseq.arg[0], 1);
  1493. tclearregion(term.c.x, term.c.y,
  1494. term.c.x + csiescseq.arg[0] - 1, term.c.y);
  1495. break;
  1496. case 'P': /* DCH -- Delete <n> char */
  1497. DEFAULT(csiescseq.arg[0], 1);
  1498. tdeletechar(csiescseq.arg[0]);
  1499. break;
  1500. case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
  1501. DEFAULT(csiescseq.arg[0], 1);
  1502. tputtab(-csiescseq.arg[0]);
  1503. break;
  1504. case 'd': /* VPA -- Move to <row> */
  1505. DEFAULT(csiescseq.arg[0], 1);
  1506. tmoveato(term.c.x, csiescseq.arg[0]-1);
  1507. break;
  1508. case 'h': /* SM -- Set terminal mode */
  1509. tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
  1510. break;
  1511. case 'm': /* SGR -- Terminal attribute (color) */
  1512. tsetattr(csiescseq.arg, csiescseq.narg);
  1513. break;
  1514. case 'n': /* DSR – Device Status Report (cursor position) */
  1515. if (csiescseq.arg[0] == 6) {
  1516. len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
  1517. term.c.y+1, term.c.x+1);
  1518. ttywrite(buf, len);
  1519. }
  1520. break;
  1521. case 'r': /* DECSTBM -- Set Scrolling Region */
  1522. if (csiescseq.priv) {
  1523. goto unknown;
  1524. } else {
  1525. DEFAULT(csiescseq.arg[0], 1);
  1526. DEFAULT(csiescseq.arg[1], term.row);
  1527. tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
  1528. tmoveato(0, 0);
  1529. }
  1530. break;
  1531. case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
  1532. tcursor(CURSOR_SAVE);
  1533. break;
  1534. case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
  1535. tcursor(CURSOR_LOAD);
  1536. break;
  1537. case ' ':
  1538. switch (csiescseq.mode[1]) {
  1539. case 'q': /* DECSCUSR -- Set Cursor Style */
  1540. DEFAULT(csiescseq.arg[0], 1);
  1541. if (!BETWEEN(csiescseq.arg[0], 0, 6)) {
  1542. goto unknown;
  1543. }
  1544. win.cursor = csiescseq.arg[0];
  1545. break;
  1546. default:
  1547. goto unknown;
  1548. }
  1549. break;
  1550. }
  1551. }
  1552. void
  1553. csidump(void)
  1554. {
  1555. int i;
  1556. uint c;
  1557. fprintf(stderr, "ESC[");
  1558. for (i = 0; i < csiescseq.len; i++) {
  1559. c = csiescseq.buf[i] & 0xff;
  1560. if (isprint(c)) {
  1561. putc(c, stderr);
  1562. } else if (c == '\n') {
  1563. fprintf(stderr, "(\\n)");
  1564. } else if (c == '\r') {
  1565. fprintf(stderr, "(\\r)");
  1566. } else if (c == 0x1b) {
  1567. fprintf(stderr, "(\\e)");
  1568. } else {
  1569. fprintf(stderr, "(%02x)", c);
  1570. }
  1571. }
  1572. putc('\n', stderr);
  1573. }
  1574. void
  1575. csireset(void)
  1576. {
  1577. memset(&csiescseq, 0, sizeof(csiescseq));
  1578. }
  1579. void
  1580. strhandle(void)
  1581. {
  1582. char *p = NULL;
  1583. int j, narg, par;
  1584. term.esc &= ~(ESC_STR_END|ESC_STR);
  1585. strparse();
  1586. par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
  1587. switch (strescseq.type) {
  1588. case ']': /* OSC -- Operating System Command */
  1589. switch (par) {
  1590. case 0:
  1591. case 1:
  1592. case 2:
  1593. if (narg > 1)
  1594. xsettitle(strescseq.args[1]);
  1595. return;
  1596. case 52:
  1597. if (narg > 2) {
  1598. char *dec;
  1599. dec = base64dec(strescseq.args[2]);
  1600. if (dec) {
  1601. xsetsel(dec, CurrentTime);
  1602. clipcopy(NULL);
  1603. } else {
  1604. fprintf(stderr, "erresc: invalid base64\n");
  1605. }
  1606. }
  1607. return;
  1608. case 4: /* color set */
  1609. if (narg < 3)
  1610. break;
  1611. p = strescseq.args[2];
  1612. /* FALLTHROUGH */
  1613. case 104: /* color reset, here p = NULL */
  1614. j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
  1615. if (xsetcolorname(j, p)) {
  1616. fprintf(stderr, "erresc: invalid color %s\n", p);
  1617. } else {
  1618. /*
  1619. * TODO if defaultbg color is changed, borders
  1620. * are dirty
  1621. */
  1622. redraw();
  1623. }
  1624. return;
  1625. }
  1626. break;
  1627. case 'k': /* old title set compatibility */
  1628. xsettitle(strescseq.args[0]);
  1629. return;
  1630. case 'P': /* DCS -- Device Control String */
  1631. term.mode |= ESC_DCS;
  1632. case '_': /* APC -- Application Program Command */
  1633. case '^': /* PM -- Privacy Message */
  1634. return;
  1635. }
  1636. fprintf(stderr, "erresc: unknown str ");
  1637. strdump();
  1638. }
  1639. void
  1640. strparse(void)
  1641. {
  1642. int c;
  1643. char *p = strescseq.buf;
  1644. strescseq.narg = 0;
  1645. strescseq.buf[strescseq.len] = '\0';
  1646. if (*p == '\0')
  1647. return;
  1648. while (strescseq.narg < STR_ARG_SIZ) {
  1649. strescseq.args[strescseq.narg++] = p;
  1650. while ((c = *p) != ';' && c != '\0')
  1651. ++p;
  1652. if (c == '\0')
  1653. return;
  1654. *p++ = '\0';
  1655. }
  1656. }
  1657. void
  1658. strdump(void)
  1659. {
  1660. int i;
  1661. uint c;
  1662. fprintf(stderr, "ESC%c", strescseq.type);
  1663. for (i = 0; i < strescseq.len; i++) {
  1664. c = strescseq.buf[i] & 0xff;
  1665. if (c == '\0') {
  1666. putc('\n', stderr);
  1667. return;
  1668. } else if (isprint(c)) {
  1669. putc(c, stderr);
  1670. } else if (c == '\n') {
  1671. fprintf(stderr, "(\\n)");
  1672. } else if (c == '\r') {
  1673. fprintf(stderr, "(\\r)");
  1674. } else if (c == 0x1b) {
  1675. fprintf(stderr, "(\\e)");
  1676. } else {
  1677. fprintf(stderr, "(%02x)", c);
  1678. }
  1679. }
  1680. fprintf(stderr, "ESC\\\n");
  1681. }
  1682. void
  1683. strreset(void)
  1684. {
  1685. memset(&strescseq, 0, sizeof(strescseq));
  1686. }
  1687. void
  1688. sendbreak(const Arg *arg)
  1689. {
  1690. if (tcsendbreak(cmdfd, 0))
  1691. perror("Error sending break");
  1692. }
  1693. void
  1694. tprinter(char *s, size_t len)
  1695. {
  1696. if (iofd != -1 && xwrite(iofd, s, len) < 0) {
  1697. perror("Error writing to output file");
  1698. close(iofd);
  1699. iofd = -1;
  1700. }
  1701. }
  1702. void
  1703. iso14755(const Arg *arg)
  1704. {
  1705. FILE *p;
  1706. char *us, *e, codepoint[9], uc[UTF_SIZ];
  1707. unsigned long utf32;
  1708. if (!(p = popen(ISO14755CMD, "r")))
  1709. return;
  1710. us = fgets(codepoint, sizeof(codepoint), p);
  1711. pclose(p);
  1712. if (!us || *us == '\0' || *us == '-' || strlen(us) > 7)
  1713. return;
  1714. if ((utf32 = strtoul(us, &e, 16)) == ULONG_MAX ||
  1715. (*e != '\n' && *e != '\0'))
  1716. return;
  1717. ttysend(uc, utf8encode(utf32, uc));
  1718. }
  1719. void
  1720. toggleprinter(const Arg *arg)
  1721. {
  1722. term.mode ^= MODE_PRINT;
  1723. }
  1724. void
  1725. printscreen(const Arg *arg)
  1726. {
  1727. tdump();
  1728. }
  1729. void
  1730. printsel(const Arg *arg)
  1731. {
  1732. tdumpsel();
  1733. }
  1734. void
  1735. tdumpsel(void)
  1736. {
  1737. char *ptr;
  1738. if ((ptr = getsel())) {
  1739. tprinter(ptr, strlen(ptr));
  1740. free(ptr);
  1741. }
  1742. }
  1743. void
  1744. tdumpline(int n)
  1745. {
  1746. char buf[UTF_SIZ];
  1747. Glyph *bp, *end;
  1748. bp = &term.line[n][0];
  1749. end = &bp[MIN(tlinelen(n), term.col) - 1];
  1750. if (bp != end || bp->u != ' ') {
  1751. for ( ;bp <= end; ++bp)
  1752. tprinter(buf, utf8encode(bp->u, buf));
  1753. }
  1754. tprinter("\n", 1);
  1755. }
  1756. void
  1757. tdump(void)
  1758. {
  1759. int i;
  1760. for (i = 0; i < term.row; ++i)
  1761. tdumpline(i);
  1762. }
  1763. void
  1764. tputtab(int n)
  1765. {
  1766. uint x = term.c.x;
  1767. if (n > 0) {
  1768. while (x < term.col && n--)
  1769. for (++x; x < term.col && !term.tabs[x]; ++x)
  1770. /* nothing */ ;
  1771. } else if (n < 0) {
  1772. while (x > 0 && n++)
  1773. for (--x; x > 0 && !term.tabs[x]; --x)
  1774. /* nothing */ ;
  1775. }
  1776. term.c.x = LIMIT(x, 0, term.col-1);
  1777. }
  1778. void
  1779. tdefutf8(char ascii)
  1780. {
  1781. if (ascii == 'G')
  1782. term.mode |= MODE_UTF8;
  1783. else if (ascii == '@')
  1784. term.mode &= ~MODE_UTF8;
  1785. }
  1786. void
  1787. tdeftran(char ascii)
  1788. {
  1789. static char cs[] = "0B";
  1790. static int vcs[] = {CS_GRAPHIC0, CS_USA};
  1791. char *p;
  1792. if ((p = strchr(cs, ascii)) == NULL) {
  1793. fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
  1794. } else {
  1795. term.trantbl[term.icharset] = vcs[p - cs];
  1796. }
  1797. }
  1798. void
  1799. tdectest(char c)
  1800. {
  1801. int x, y;
  1802. if (c == '8') { /* DEC screen alignment test. */
  1803. for (x = 0; x < term.col; ++x) {
  1804. for (y = 0; y < term.row; ++y)
  1805. tsetchar('E', &term.c.attr, x, y);
  1806. }
  1807. }
  1808. }
  1809. void
  1810. tstrsequence(uchar c)
  1811. {
  1812. strreset();
  1813. switch (c) {
  1814. case 0x90: /* DCS -- Device Control String */
  1815. c = 'P';
  1816. term.esc |= ESC_DCS;
  1817. break;
  1818. case 0x9f: /* APC -- Application Program Command */
  1819. c = '_';
  1820. break;
  1821. case 0x9e: /* PM -- Privacy Message */
  1822. c = '^';
  1823. break;
  1824. case 0x9d: /* OSC -- Operating System Command */
  1825. c = ']';
  1826. break;
  1827. }
  1828. strescseq.type = c;
  1829. term.esc |= ESC_STR;
  1830. }
  1831. void
  1832. tcontrolcode(uchar ascii)
  1833. {
  1834. switch (ascii) {
  1835. case '\t': /* HT */
  1836. tputtab(1);
  1837. return;
  1838. case '\b': /* BS */
  1839. tmoveto(term.c.x-1, term.c.y);
  1840. return;
  1841. case '\r': /* CR */
  1842. tmoveto(0, term.c.y);
  1843. return;
  1844. case '\f': /* LF */
  1845. case '\v': /* VT */
  1846. case '\n': /* LF */
  1847. /* go to first col if the mode is set */
  1848. tnewline(IS_SET(MODE_CRLF));
  1849. return;
  1850. case '\a': /* BEL */
  1851. if (term.esc & ESC_STR_END) {
  1852. /* backwards compatibility to xterm */
  1853. strhandle();
  1854. } else {
  1855. xbell();
  1856. }
  1857. break;
  1858. case '\033': /* ESC */
  1859. csireset();
  1860. term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
  1861. term.esc |= ESC_START;
  1862. return;
  1863. case '\016': /* SO (LS1 -- Locking shift 1) */
  1864. case '\017': /* SI (LS0 -- Locking shift 0) */
  1865. term.charset = 1 - (ascii - '\016');
  1866. return;
  1867. case '\032': /* SUB */
  1868. tsetchar('?', &term.c.attr, term.c.x, term.c.y);
  1869. case '\030': /* CAN */
  1870. csireset();
  1871. break;
  1872. case '\005': /* ENQ (IGNORED) */
  1873. case '\000': /* NUL (IGNORED) */
  1874. case '\021': /* XON (IGNORED) */
  1875. case '\023': /* XOFF (IGNORED) */
  1876. case 0177: /* DEL (IGNORED) */
  1877. return;
  1878. case 0x80: /* TODO: PAD */
  1879. case 0x81: /* TODO: HOP */
  1880. case 0x82: /* TODO: BPH */
  1881. case 0x83: /* TODO: NBH */
  1882. case 0x84: /* TODO: IND */
  1883. break;
  1884. case 0x85: /* NEL -- Next line */
  1885. tnewline(1); /* always go to first col */
  1886. break;
  1887. case 0x86: /* TODO: SSA */
  1888. case 0x87: /* TODO: ESA */
  1889. break;
  1890. case 0x88: /* HTS -- Horizontal tab stop */
  1891. term.tabs[term.c.x] = 1;
  1892. break;
  1893. case 0x89: /* TODO: HTJ */
  1894. case 0x8a: /* TODO: VTS */
  1895. case 0x8b: /* TODO: PLD */
  1896. case 0x8c: /* TODO: PLU */
  1897. case 0x8d: /* TODO: RI */
  1898. case 0x8e: /* TODO: SS2 */
  1899. case 0x8f: /* TODO: SS3 */
  1900. case 0x91: /* TODO: PU1 */
  1901. case 0x92: /* TODO: PU2 */
  1902. case 0x93: /* TODO: STS */
  1903. case 0x94: /* TODO: CCH */
  1904. case 0x95: /* TODO: MW */
  1905. case 0x96: /* TODO: SPA */
  1906. case 0x97: /* TODO: EPA */
  1907. case 0x98: /* TODO: SOS */
  1908. case 0x99: /* TODO: SGCI */
  1909. break;
  1910. case 0x9a: /* DECID -- Identify Terminal */
  1911. ttywrite(vtiden, sizeof(vtiden) - 1);
  1912. break;
  1913. case 0x9b: /* TODO: CSI */
  1914. case 0x9c: /* TODO: ST */
  1915. break;
  1916. case 0x90: /* DCS -- Device Control String */
  1917. case 0x9d: /* OSC -- Operating System Command */
  1918. case 0x9e: /* PM -- Privacy Message */
  1919. case 0x9f: /* APC -- Application Program Command */
  1920. tstrsequence(ascii);
  1921. return;
  1922. }
  1923. /* only CAN, SUB, \a and C1 chars interrupt a sequence */
  1924. term.esc &= ~(ESC_STR_END|ESC_STR);
  1925. }
  1926. /*
  1927. * returns 1 when the sequence is finished and it hasn't to read
  1928. * more characters for this sequence, otherwise 0
  1929. */
  1930. int
  1931. eschandle(uchar ascii)
  1932. {
  1933. switch (ascii) {
  1934. case '[':
  1935. term.esc |= ESC_CSI;
  1936. return 0;
  1937. case '#':
  1938. term.esc |= ESC_TEST;
  1939. return 0;
  1940. case '%':
  1941. term.esc |= ESC_UTF8;
  1942. return 0;
  1943. case 'P': /* DCS -- Device Control String */
  1944. case '_': /* APC -- Application Program Command */
  1945. case '^': /* PM -- Privacy Message */
  1946. case ']': /* OSC -- Operating System Command */
  1947. case 'k': /* old title set compatibility */
  1948. tstrsequence(ascii);
  1949. return 0;
  1950. case 'n': /* LS2 -- Locking shift 2 */
  1951. case 'o': /* LS3 -- Locking shift 3 */
  1952. term.charset = 2 + (ascii - 'n');
  1953. break;
  1954. case '(': /* GZD4 -- set primary charset G0 */
  1955. case ')': /* G1D4 -- set secondary charset G1 */
  1956. case '*': /* G2D4 -- set tertiary charset G2 */
  1957. case '+': /* G3D4 -- set quaternary charset G3 */
  1958. term.icharset = ascii - '(';
  1959. term.esc |= ESC_ALTCHARSET;
  1960. return 0;
  1961. case 'D': /* IND -- Linefeed */
  1962. if (term.c.y == term.bot) {
  1963. tscrollup(term.top, 1);
  1964. } else {
  1965. tmoveto(term.c.x, term.c.y+1);
  1966. }
  1967. break;
  1968. case 'E': /* NEL -- Next line */
  1969. tnewline(1); /* always go to first col */
  1970. break;
  1971. case 'H': /* HTS -- Horizontal tab stop */
  1972. term.tabs[term.c.x] = 1;
  1973. break;
  1974. case 'M': /* RI -- Reverse index */
  1975. if (term.c.y == term.top) {
  1976. tscrolldown(term.top, 1);
  1977. } else {
  1978. tmoveto(term.c.x, term.c.y-1);
  1979. }
  1980. break;
  1981. case 'Z': /* DECID -- Identify Terminal */
  1982. ttywrite(vtiden, sizeof(vtiden) - 1);
  1983. break;
  1984. case 'c': /* RIS -- Reset to inital state */
  1985. treset();
  1986. resettitle();
  1987. xloadcols();
  1988. break;
  1989. case '=': /* DECPAM -- Application keypad */
  1990. term.mode |= MODE_APPKEYPAD;
  1991. break;
  1992. case '>': /* DECPNM -- Normal keypad */
  1993. term.mode &= ~MODE_APPKEYPAD;
  1994. break;
  1995. case '7': /* DECSC -- Save Cursor */
  1996. tcursor(CURSOR_SAVE);
  1997. break;
  1998. case '8': /* DECRC -- Restore Cursor */
  1999. tcursor(CURSOR_LOAD);
  2000. break;
  2001. case '\\': /* ST -- String Terminator */
  2002. if (term.esc & ESC_STR_END)
  2003. strhandle();
  2004. break;
  2005. default:
  2006. fprintf(stderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
  2007. (uchar) ascii, isprint(ascii)? ascii:'.');
  2008. break;
  2009. }
  2010. return 1;
  2011. }
  2012. void
  2013. tputc(Rune u)
  2014. {
  2015. char c[UTF_SIZ];
  2016. int control;
  2017. int width, len;
  2018. Glyph *gp;
  2019. control = ISCONTROL(u);
  2020. if (!IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2021. c[0] = u;
  2022. width = len = 1;
  2023. } else {
  2024. len = utf8encode(u, c);
  2025. if (!control && (width = wcwidth(u)) == -1) {
  2026. memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
  2027. width = 1;
  2028. }
  2029. }
  2030. if (IS_SET(MODE_PRINT))
  2031. tprinter(c, len);
  2032. /*
  2033. * STR sequence must be checked before anything else
  2034. * because it uses all following characters until it
  2035. * receives a ESC, a SUB, a ST or any other C1 control
  2036. * character.
  2037. */
  2038. if (term.esc & ESC_STR) {
  2039. if (u == '\a' || u == 030 || u == 032 || u == 033 ||
  2040. ISCONTROLC1(u)) {
  2041. term.esc &= ~(ESC_START|ESC_STR|ESC_DCS);
  2042. if (IS_SET(MODE_SIXEL)) {
  2043. /* TODO: render sixel */;
  2044. term.mode &= ~MODE_SIXEL;
  2045. return;
  2046. }
  2047. term.esc |= ESC_STR_END;
  2048. goto check_control_code;
  2049. }
  2050. if (IS_SET(MODE_SIXEL)) {
  2051. /* TODO: implement sixel mode */
  2052. return;
  2053. }
  2054. if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
  2055. term.mode |= MODE_SIXEL;
  2056. if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
  2057. /*
  2058. * Here is a bug in terminals. If the user never sends
  2059. * some code to stop the str or esc command, then st
  2060. * will stop responding. But this is better than
  2061. * silently failing with unknown characters. At least
  2062. * then users will report back.
  2063. *
  2064. * In the case users ever get fixed, here is the code:
  2065. */
  2066. /*
  2067. * term.esc = 0;
  2068. * strhandle();
  2069. */
  2070. return;
  2071. }
  2072. memmove(&strescseq.buf[strescseq.len], c, len);
  2073. strescseq.len += len;
  2074. return;
  2075. }
  2076. check_control_code:
  2077. /*
  2078. * Actions of control codes must be performed as soon they arrive
  2079. * because they can be embedded inside a control sequence, and
  2080. * they must not cause conflicts with sequences.
  2081. */
  2082. if (control) {
  2083. tcontrolcode(u);
  2084. /*
  2085. * control codes are not shown ever
  2086. */
  2087. return;
  2088. } else if (term.esc & ESC_START) {
  2089. if (term.esc & ESC_CSI) {
  2090. csiescseq.buf[csiescseq.len++] = u;
  2091. if (BETWEEN(u, 0x40, 0x7E)
  2092. || csiescseq.len >= \
  2093. sizeof(csiescseq.buf)-1) {
  2094. term.esc = 0;
  2095. csiparse();
  2096. csihandle();
  2097. }
  2098. return;
  2099. } else if (term.esc & ESC_UTF8) {
  2100. tdefutf8(u);
  2101. } else if (term.esc & ESC_ALTCHARSET) {
  2102. tdeftran(u);
  2103. } else if (term.esc & ESC_TEST) {
  2104. tdectest(u);
  2105. } else {
  2106. if (!eschandle(u))
  2107. return;
  2108. /* sequence already finished */
  2109. }
  2110. term.esc = 0;
  2111. /*
  2112. * All characters which form part of a sequence are not
  2113. * printed
  2114. */
  2115. return;
  2116. }
  2117. if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
  2118. selclear();
  2119. gp = &term.line[term.c.y][term.c.x];
  2120. if (IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
  2121. gp->mode |= ATTR_WRAP;
  2122. tnewline(1);
  2123. gp = &term.line[term.c.y][term.c.x];
  2124. }
  2125. if (IS_SET(MODE_INSERT) && term.c.x+width < term.col)
  2126. memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(Glyph));
  2127. if (term.c.x+width > term.col) {
  2128. tnewline(1);
  2129. gp = &term.line[term.c.y][term.c.x];
  2130. }
  2131. tsetchar(u, &term.c.attr, term.c.x, term.c.y);
  2132. if (width == 2) {
  2133. gp->mode |= ATTR_WIDE;
  2134. if (term.c.x+1 < term.col) {
  2135. gp[1].u = '\0';
  2136. gp[1].mode = ATTR_WDUMMY;
  2137. }
  2138. }
  2139. if (term.c.x+width < term.col) {
  2140. tmoveto(term.c.x+width, term.c.y);
  2141. } else {
  2142. term.c.state |= CURSOR_WRAPNEXT;
  2143. }
  2144. }
  2145. int
  2146. twrite(const char *buf, int buflen, int show_ctrl)
  2147. {
  2148. int charsize;
  2149. Rune u;
  2150. int n;
  2151. for (n = 0; n < buflen; n += charsize) {
  2152. if (IS_SET(MODE_UTF8) && !IS_SET(MODE_SIXEL)) {
  2153. /* process a complete utf8 char */
  2154. charsize = utf8decode(buf + n, &u, buflen - n);
  2155. if (charsize == 0)
  2156. break;
  2157. } else {
  2158. u = buf[n] & 0xFF;
  2159. charsize = 1;
  2160. }
  2161. if (show_ctrl && ISCONTROL(u)) {
  2162. if (u & 0x80) {
  2163. u &= 0x7f;
  2164. tputc('^');
  2165. tputc('[');
  2166. } else if (u != '\n' && u != '\r' && u != '\t') {
  2167. u ^= 0x40;
  2168. tputc('^');
  2169. }
  2170. }
  2171. tputc(u);
  2172. }
  2173. return n;
  2174. }
  2175. void
  2176. tresize(int col, int row)
  2177. {
  2178. int i;
  2179. int minrow = MIN(row, term.row);
  2180. int mincol = MIN(col, term.col);
  2181. int *bp;
  2182. TCursor c;
  2183. if (col < 1 || row < 1) {
  2184. fprintf(stderr,
  2185. "tresize: error resizing to %dx%d\n", col, row);
  2186. return;
  2187. }
  2188. /*
  2189. * slide screen to keep cursor where we expect it -
  2190. * tscrollup would work here, but we can optimize to
  2191. * memmove because we're freeing the earlier lines
  2192. */
  2193. for (i = 0; i <= term.c.y - row; i++) {
  2194. free(term.line[i]);
  2195. free(term.alt[i]);
  2196. }
  2197. /* ensure that both src and dst are not NULL */
  2198. if (i > 0) {
  2199. memmove(term.line, term.line + i, row * sizeof(Line));
  2200. memmove(term.alt, term.alt + i, row * sizeof(Line));
  2201. }
  2202. for (i += row; i < term.row; i++) {
  2203. free(term.line[i]);
  2204. free(term.alt[i]);
  2205. }
  2206. /* resize to new height */
  2207. term.line = xrealloc(term.line, row * sizeof(Line));
  2208. term.alt = xrealloc(term.alt, row * sizeof(Line));
  2209. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  2210. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  2211. /* resize each row to new width, zero-pad if needed */
  2212. for (i = 0; i < minrow; i++) {
  2213. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  2214. term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph));
  2215. }
  2216. /* allocate any new rows */
  2217. for (/* i = minrow */; i < row; i++) {
  2218. term.line[i] = xmalloc(col * sizeof(Glyph));
  2219. term.alt[i] = xmalloc(col * sizeof(Glyph));
  2220. }
  2221. if (col > term.col) {
  2222. bp = term.tabs + term.col;
  2223. memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
  2224. while (--bp > term.tabs && !*bp)
  2225. /* nothing */ ;
  2226. for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
  2227. *bp = 1;
  2228. }
  2229. /* update terminal size */
  2230. term.col = col;
  2231. term.row = row;
  2232. /* reset scrolling region */
  2233. tsetscroll(0, row-1);
  2234. /* make use of the LIMIT in tmoveto */
  2235. tmoveto(term.c.x, term.c.y);
  2236. /* Clearing both screens (it makes dirty all lines) */
  2237. c = term.c;
  2238. for (i = 0; i < 2; i++) {
  2239. if (mincol < col && 0 < minrow) {
  2240. tclearregion(mincol, 0, col - 1, minrow - 1);
  2241. }
  2242. if (0 < col && minrow < row) {
  2243. tclearregion(0, minrow, col - 1, row - 1);
  2244. }
  2245. tswapscreen();
  2246. tcursor(CURSOR_LOAD);
  2247. }
  2248. term.c = c;
  2249. }
  2250. void
  2251. resettitle(void)
  2252. {
  2253. xsettitle(NULL);
  2254. }
  2255. void
  2256. redraw(void)
  2257. {
  2258. tfulldirt();
  2259. draw();
  2260. }
  2261. void
  2262. numlock(const Arg *dummy)
  2263. {
  2264. term.numlock ^= 1;
  2265. }