st.c 54 KB

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