st.c 55 KB

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