Bug Summary

File:st.c
Location:line 4281, column 24
Description:Division by zero

Annotated Source Code

1/* See LICENSE for license details. */
2#include <ctype.h>
3#include <errno(*__errno_location ()).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 <X11/Xatom.h>
25#include <X11/Xlib.h>
26#include <X11/Xutil.h>
27#include <X11/cursorfont.h>
28#include <X11/keysym.h>
29#include <X11/Xft/Xft.h>
30#include <X11/XKBlib.h>
31#include <fontconfig/fontconfig.h>
32#include <wchar.h>
33
34#include "arg.h"
35
36char *argv0;
37
38#define GlyphGlyph_ Glyph_
39#define FontFont_ Font_
40
41#if defined(__linux1)
42 #include <pty.h>
43#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
44 #include <util.h>
45#elif defined(__FreeBSD__) || defined(__DragonFly__)
46 #include <libutil.h>
47#endif
48
49
50/* XEMBED messages */
51#define XEMBED_FOCUS_IN4 4
52#define XEMBED_FOCUS_OUT5 5
53
54/* Arbitrary sizes */
55#define UTF_INVALID0xFFFD 0xFFFD
56#define UTF_SIZ4 4
57#define ESC_BUF_SIZ(128*4) (128*UTF_SIZ4)
58#define ESC_ARG_SIZ16 16
59#define STR_BUF_SIZ(128*4) ESC_BUF_SIZ(128*4)
60#define STR_ARG_SIZ16 ESC_ARG_SIZ16
61#define XK_ANY_MOD(2147483647 *2U +1U) UINT_MAX(2147483647 *2U +1U)
62#define XK_NO_MOD0 0
63#define XK_SWITCH_MOD(1<<13) (1<<13)
64
65/* macros */
66#define MIN(a, b)((a) < (b) ? (a) : (b)) ((a) < (b) ? (a) : (b))
67#define MAX(a, b)((a) < (b) ? (b) : (a)) ((a) < (b) ? (b) : (a))
68#define LEN(a)(sizeof(a) / sizeof(a)[0]) (sizeof(a) / sizeof(a)[0])
69#define DEFAULT(a, b)(a) = (a) ? (a) : (b) (a) = (a) ? (a) : (b)
70#define BETWEEN(x, a, b)((a) <= (x) && (x) <= (b)) ((a) <= (x) && (x) <= (b))
71#define DIVCEIL(n, d)(((n) + ((d) - 1)) / (d)) (((n) + ((d) - 1)) / (d))
72#define ISCONTROLC0(c)(((0) <= (c) && (c) <= (0x1f)) || (c) == '\177'
)
(BETWEEN(c, 0, 0x1f)((0) <= (c) && (c) <= (0x1f)) || (c) == '\177')
73#define ISCONTROLC1(c)(((0x80) <= (c) && (c) <= (0x9f))) (BETWEEN(c, 0x80, 0x9f)((0x80) <= (c) && (c) <= (0x9f)))
74#define ISCONTROL(c)((((0) <= (c) && (c) <= (0x1f)) || (c) == '\177'
) || (((0x80) <= (c) && (c) <= (0x9f))))
(ISCONTROLC0(c)(((0) <= (c) && (c) <= (0x1f)) || (c) == '\177'
)
|| ISCONTROLC1(c)(((0x80) <= (c) && (c) <= (0x9f))))
75#define ISDELIM(u)(utf8strchr(worddelimiters, u) != ((void*)0)) (utf8strchr(worddelimiters, u) != NULL((void*)0))
76#define LIMIT(x, a, b)(x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
77#define ATTRCMP(a, b)((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg
)
((a).mode != (b).mode || (a).fg != (b).fg || \
78 (a).bg != (b).bg)
79#define IS_SET(flag)((term.mode & (flag)) != 0) ((term.mode & (flag)) != 0)
80#define TIMEDIFF(t1, t2)((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6) ((t1.tv_sec-t2.tv_sec)*1000 + \
81 (t1.tv_nsec-t2.tv_nsec)/1E6)
82#define MODBIT(x, set, bit)((set) ? ((x) |= (bit)) : ((x) &= ~(bit))) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
83
84#define TRUECOLOR(r,g,b)(1 << 24 | (r) << 16 | (g) << 8 | (b)) (1 << 24 | (r) << 16 | (g) << 8 | (b))
85#define IS_TRUECOL(x)(1 << 24 & (x)) (1 << 24 & (x))
86#define TRUERED(x)(((x) & 0xff0000) >> 8) (((x) & 0xff0000) >> 8)
87#define TRUEGREEN(x)(((x) & 0xff00)) (((x) & 0xff00))
88#define TRUEBLUE(x)(((x) & 0xff) << 8) (((x) & 0xff) << 8)
89
90
91enum glyph_attribute {
92 ATTR_NULL = 0,
93 ATTR_BOLD = 1 << 0,
94 ATTR_FAINT = 1 << 1,
95 ATTR_ITALIC = 1 << 2,
96 ATTR_UNDERLINE = 1 << 3,
97 ATTR_BLINK = 1 << 4,
98 ATTR_REVERSE = 1 << 5,
99 ATTR_INVISIBLE = 1 << 6,
100 ATTR_STRUCK = 1 << 7,
101 ATTR_WRAP = 1 << 8,
102 ATTR_WIDE = 1 << 9,
103 ATTR_WDUMMY = 1 << 10,
104 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
105};
106
107enum cursor_movement {
108 CURSOR_SAVE,
109 CURSOR_LOAD
110};
111
112enum cursor_state {
113 CURSOR_DEFAULT = 0,
114 CURSOR_WRAPNEXT = 1,
115 CURSOR_ORIGIN = 2
116};
117
118enum term_mode {
119 MODE_WRAP = 1 << 0,
120 MODE_INSERT = 1 << 1,
121 MODE_APPKEYPAD = 1 << 2,
122 MODE_ALTSCREEN = 1 << 3,
123 MODE_CRLF = 1 << 4,
124 MODE_MOUSEBTN = 1 << 5,
125 MODE_MOUSEMOTION = 1 << 6,
126 MODE_REVERSE = 1 << 7,
127 MODE_KBDLOCK = 1 << 8,
128 MODE_HIDE = 1 << 9,
129 MODE_ECHO = 1 << 10,
130 MODE_APPCURSOR = 1 << 11,
131 MODE_MOUSESGR = 1 << 12,
132 MODE_8BIT = 1 << 13,
133 MODE_BLINK = 1 << 14,
134 MODE_FBLINK = 1 << 15,
135 MODE_FOCUS = 1 << 16,
136 MODE_MOUSEX10 = 1 << 17,
137 MODE_MOUSEMANY = 1 << 18,
138 MODE_BRCKTPASTE = 1 << 19,
139 MODE_PRINT = 1 << 20,
140 MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
141 |MODE_MOUSEMANY,
142};
143
144enum charset {
145 CS_GRAPHIC0,
146 CS_GRAPHIC1,
147 CS_UK,
148 CS_USA,
149 CS_MULTI,
150 CS_GER,
151 CS_FIN
152};
153
154enum escape_state {
155 ESC_START = 1,
156 ESC_CSI = 2,
157 ESC_STR = 4, /* DCS, OSC, PM, APC */
158 ESC_ALTCHARSET = 8,
159 ESC_STR_END = 16, /* a final string was encountered */
160 ESC_TEST = 32, /* Enter in test mode */
161};
162
163enum window_state {
164 WIN_VISIBLE = 1,
165 WIN_FOCUSED = 2
166};
167
168enum selection_mode {
169 SEL_IDLE = 0,
170 SEL_EMPTY = 1,
171 SEL_READY = 2
172};
173
174enum selection_type {
175 SEL_REGULAR = 1,
176 SEL_RECTANGULAR = 2
177};
178
179enum selection_snap {
180 SNAP_WORD = 1,
181 SNAP_LINE = 2
182};
183
184typedef unsigned char uchar;
185typedef unsigned int uint;
186typedef unsigned long ulong;
187typedef unsigned short ushort;
188
189typedef uint_least32_t Rune;
190
191typedef XftDraw *Draw;
192typedef XftColor Color;
193
194typedef struct {
195 Rune u; /* character code */
196 ushort mode; /* attribute flags */
197 uint32_t fg; /* foreground */
198 uint32_t bg; /* background */
199} GlyphGlyph_;
200
201typedef GlyphGlyph_ *Line;
202
203typedef struct {
204 GlyphGlyph_ attr; /* current char attributes */
205 int x;
206 int y;
207 char state;
208} TCursor;
209
210/* CSI Escape sequence structs */
211/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
212typedef struct {
213 char buf[ESC_BUF_SIZ(128*4)]; /* raw string */
214 int len; /* raw string length */
215 char priv;
216 int arg[ESC_ARG_SIZ16];
217 int narg; /* nb of args */
218 char mode[2];
219} CSIEscape;
220
221/* STR Escape sequence structs */
222/* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
223typedef struct {
224 char type; /* ESC type ... */
225 char buf[STR_BUF_SIZ(128*4)]; /* raw string */
226 int len; /* raw string length */
227 char *args[STR_ARG_SIZ16];
228 int narg; /* nb of args */
229} STREscape;
230
231/* Internal representation of the screen */
232typedef struct {
233 int row; /* nb row */
234 int col; /* nb col */
235 Line *line; /* screen */
236 Line *alt; /* alternate screen */
237 int *dirty; /* dirtyness of lines */
238 XftGlyphFontSpec *specbuf; /* font spec buffer used for rendering */
239 TCursor c; /* cursor */
240 int top; /* top scroll limit */
241 int bot; /* bottom scroll limit */
242 int mode; /* terminal mode flags */
243 int esc; /* escape state flags */
244 char trantbl[4]; /* charset table translation */
245 int charset; /* current charset */
246 int icharset; /* selected charset for sequence */
247 int numlock; /* lock numbers in keyboard */
248 int *tabs;
249} Term;
250
251/* Purely graphic info */
252typedef struct {
253 Display *dpy;
254 Colormap cmap;
255 Window win;
256 Drawable buf;
257 Atom xembed, wmdeletewin, netwmname, netwmpid;
258 XIM xim;
259 XIC xic;
260 Draw draw;
261 Visual *vis;
262 XSetWindowAttributes attrs;
263 int scr;
264 int isfixed; /* is fixed geometry? */
265 int l, t; /* left and top offset */
266 int gm; /* geometry mask */
267 int tw, th; /* tty width and height */
268 int w, h; /* window width and height */
269 int ch; /* char height */
270 int cw; /* char width */
271 char state; /* focus, redraw, visible */
272 int cursor; /* cursor style */
273} XWindow;
274
275typedef struct {
276 uint b;
277 uint mask;
278 char *s;
279} MouseShortcut;
280
281typedef struct {
282 KeySym k;
283 uint mask;
284 char *s;
285 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
286 signed char appkey; /* application keypad */
287 signed char appcursor; /* application cursor */
288 signed char crlf; /* crlf mode */
289} Key;
290
291typedef struct {
292 int mode;
293 int type;
294 int snap;
295 /*
296 * Selection variables:
297 * nb – normalized coordinates of the beginning of the selection
298 * ne – normalized coordinates of the end of the selection
299 * ob – original coordinates of the beginning of the selection
300 * oe – original coordinates of the end of the selection
301 */
302 struct {
303 int x, y;
304 } nb, ne, ob, oe;
305
306 char *primary, *clipboard;
307 Atom xtarget;
308 int alt;
309 struct timespec tclick1;
310 struct timespec tclick2;
311} Selection;
312
313typedef union {
314 int i;
315 uint ui;
316 float f;
317 const void *v;
318} Arg;
319
320typedef struct {
321 uint mod;
322 KeySym keysym;
323 void (*func)(const Arg *);
324 const Arg arg;
325} Shortcut;
326
327/* function definitions used in config.h */
328static void clipcopy(const Arg *);
329static void clippaste(const Arg *);
330static void numlock(const Arg *);
331static void selpaste(const Arg *);
332static void xzoom(const Arg *);
333static void xzoomabs(const Arg *);
334static void xzoomreset(const Arg *);
335static void printsel(const Arg *);
336static void printscreen(const Arg *) ;
337static void toggleprinter(const Arg *);
338static void sendbreak(const Arg *);
339
340/* Config.h for applying patches and the configuration. */
341#include "config.h"
342
343/* Font structure */
344typedef struct {
345 int height;
346 int width;
347 int ascent;
348 int descent;
349 short lbearing;
350 short rbearing;
351 XftFont *match;
352 FcFontSet *set;
353 FcPattern *pattern;
354} FontFont_;
355
356/* Drawing Context */
357typedef struct {
358 Color col[MAX(LEN(colorname), 256)(((sizeof(colorname) / sizeof(colorname)[0])) < (256) ? (256
) : ((sizeof(colorname) / sizeof(colorname)[0])))
];
359 FontFont_ font, bfont, ifont, ibfont;
360 GC gc;
361} DC;
362
363static void die(const char *, ...);
364static void draw(void);
365static void redraw(void);
366static void drawregion(int, int, int, int);
367static void execsh(void);
368static void stty(void);
369static void sigchld(int);
370static void run(void);
371
372static void csidump(void);
373static void csihandle(void);
374static void csiparse(void);
375static void csireset(void);
376static int eschandle(uchar);
377static void strdump(void);
378static void strhandle(void);
379static void strparse(void);
380static void strreset(void);
381
382static int tattrset(int);
383static void tprinter(char *, size_t);
384static void tdumpsel(void);
385static void tdumpline(int);
386static void tdump(void);
387static void tclearregion(int, int, int, int);
388static void tcursor(int);
389static void tdeletechar(int);
390static void tdeleteline(int);
391static void tinsertblank(int);
392static void tinsertblankline(int);
393static int tlinelen(int);
394static void tmoveto(int, int);
395static void tmoveato(int, int);
396static void tnew(int, int);
397static void tnewline(int);
398static void tputtab(int);
399static void tputc(Rune);
400static void treset(void);
401static void tresize(int, int);
402static void tscrollup(int, int);
403static void tscrolldown(int, int);
404static void tsetattr(int *, int);
405static void tsetchar(Rune, GlyphGlyph_ *, int, int);
406static void tsetscroll(int, int);
407static void tswapscreen(void);
408static void tsetdirt(int, int);
409static void tsetdirtattr(int);
410static void tsetmode(int, int, int *, int);
411static void tfulldirt(void);
412static void techo(Rune);
413static void tcontrolcode(uchar );
414static void tdectest(char );
415static int32_t tdefcolor(int *, int *, int);
416static void tdeftran(char);
417static inline int match(uint, uint);
418static void ttynew(void);
419static size_t ttyread(void);
420static void ttyresize(void);
421static void ttysend(char *, size_t);
422static void ttywrite(const char *, size_t);
423static void tstrsequence(uchar);
424
425static inline ushort sixd_to_16bit(int);
426static int xmakeglyphfontspecs(XftGlyphFontSpec *, const GlyphGlyph_ *, int, int, int);
427static void xdrawglyphfontspecs(const XftGlyphFontSpec *, GlyphGlyph_, int, int, int);
428static void xdrawglyph(GlyphGlyph_, int, int);
429static void xhints(void);
430static void xclear(int, int, int, int);
431static void xdrawcursor(void);
432static void xinit(void);
433static void xloadcols(void);
434static int xsetcolorname(int, const char *);
435static int xgeommasktogravity(int);
436static int xloadfont(FontFont_ *, FcPattern *);
437static void xloadfonts(char *, double);
438static void xsettitle(char *);
439static void xresettitle(void);
440static void xsetpointermotion(int);
441static void xseturgency(int);
442static void xsetsel(char *, Time);
443static void xunloadfont(FontFont_ *);
444static void xunloadfonts(void);
445static void xresize(int, int);
446
447static void expose(XEvent *);
448static void visibility(XEvent *);
449static void unmap(XEvent *);
450static char *kmap(KeySym, uint);
451static void kpress(XEvent *);
452static void cmessage(XEvent *);
453static void cresize(int, int);
454static void resize(XEvent *);
455static void focus(XEvent *);
456static void brelease(XEvent *);
457static void bpress(XEvent *);
458static void bmotion(XEvent *);
459static void propnotify(XEvent *);
460static void selnotify(XEvent *);
461static void selclear(XEvent *);
462static void selrequest(XEvent *);
463
464static void selinit(void);
465static void selnormalize(void);
466static inline int selected(int, int);
467static char *getsel(void);
468static void selcopy(Time);
469static void selscroll(int, int);
470static void selsnap(int *, int *, int);
471static int x2col(int);
472static int y2row(int);
473static void getbuttoninfo(XEvent *);
474static void mousereport(XEvent *);
475
476static size_t utf8decode(char *, Rune *, size_t);
477static Rune utf8decodebyte(char, size_t *);
478static size_t utf8encode(Rune, char *);
479static char utf8encodebyte(Rune, size_t);
480static char *utf8strchr(char *s, Rune u);
481static size_t utf8validate(Rune *, size_t);
482
483static ssize_t xwrite(int, const char *, size_t);
484static void *xmalloc(size_t);
485static void *xrealloc(void *, size_t);
486static char *xstrdup(char *);
487
488static void usage(void);
489
490static void (*handler[LASTEvent36])(XEvent *) = {
491 [KeyPress2] = kpress,
492 [ClientMessage33] = cmessage,
493 [ConfigureNotify22] = resize,
494 [VisibilityNotify15] = visibility,
495 [UnmapNotify18] = unmap,
496 [Expose12] = expose,
497 [FocusIn9] = focus,
498 [FocusOut10] = focus,
499 [MotionNotify6] = bmotion,
500 [ButtonPress4] = bpress,
501 [ButtonRelease5] = brelease,
502/*
503 * Uncomment if you want the selection to disappear when you select something
504 * different in another window.
505 */
506/* [SelectionClear] = selclear, */
507 [SelectionNotify31] = selnotify,
508/*
509 * PropertyNotify is only turned on when there is some INCR transfer happening
510 * for the selection retrieval.
511 */
512 [PropertyNotify28] = propnotify,
513 [SelectionRequest30] = selrequest,
514};
515
516/* Globals */
517static DC dc;
518static XWindow xw;
519static Term term;
520static CSIEscape csiescseq;
521static STREscape strescseq;
522static int cmdfd;
523static pid_t pid;
524static Selection sel;
525static int iofd = 1;
526static char **opt_cmd = NULL((void*)0);
527static char *opt_class = NULL((void*)0);
528static char *opt_embed = NULL((void*)0);
529static char *opt_font = NULL((void*)0);
530static char *opt_io = NULL((void*)0);
531static char *opt_line = NULL((void*)0);
532static char *opt_name = NULL((void*)0);
533static char *opt_title = NULL((void*)0);
534static int oldbutton = 3; /* button event on startup: 3 = release */
535
536static char *usedfont = NULL((void*)0);
537static double usedfontsize = 0;
538static double defaultfontsize = 0;
539
540static uchar utfbyte[UTF_SIZ4 + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
541static uchar utfmask[UTF_SIZ4 + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
542static Rune utfmin[UTF_SIZ4 + 1] = { 0, 0, 0x80, 0x800, 0x10000};
543static Rune utfmax[UTF_SIZ4 + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
544
545/* Font Ring Cache */
546enum {
547 FRC_NORMAL,
548 FRC_ITALIC,
549 FRC_BOLD,
550 FRC_ITALICBOLD
551};
552
553typedef struct {
554 XftFont *font;
555 int flags;
556 Rune unicodep;
557} Fontcache;
558
559/* Fontcache is an array now. A new font will be appended to the array. */
560static Fontcache frc[16];
561static int frclen = 0;
562
563ssize_t
564xwrite(int fd, const char *s, size_t len)
565{
566 size_t aux = len;
567 ssize_t r;
568
569 while (len > 0) {
570 r = write(fd, s, len);
571 if (r < 0)
572 return r;
573 len -= r;
574 s += r;
575 }
576
577 return aux;
578}
579
580void *
581xmalloc(size_t len)
582{
583 void *p = malloc(len);
584
585 if (!p)
586 die("Out of memory\n");
587
588 return p;
589}
590
591void *
592xrealloc(void *p, size_t len)
593{
594 if ((p = realloc(p, len)) == NULL((void*)0))
595 die("Out of memory\n");
596
597 return p;
598}
599
600char *
601xstrdup(char *s)
602{
603 if ((s = strdup(s)) == NULL((void*)0))
604 die("Out of memory\n");
605
606 return s;
607}
608
609size_t
610utf8decode(char *c, Rune *u, size_t clen)
611{
612 size_t i, j, len, type;
613 Rune udecoded;
614
615 *u = UTF_INVALID0xFFFD;
616 if (!clen)
617 return 0;
618 udecoded = utf8decodebyte(c[0], &len);
619 if (!BETWEEN(len, 1, UTF_SIZ)((1) <= (len) && (len) <= (4)))
620 return 1;
621 for (i = 1, j = 1; i < clen && j < len; ++i, ++j) {
622 udecoded = (udecoded << 6) | utf8decodebyte(c[i], &type);
623 if (type != 0)
624 return j;
625 }
626 if (j < len)
627 return 0;
628 *u = udecoded;
629 utf8validate(u, len);
630
631 return len;
632}
633
634Rune
635utf8decodebyte(char c, size_t *i)
636{
637 for (*i = 0; *i < LEN(utfmask)(sizeof(utfmask) / sizeof(utfmask)[0]); ++(*i))
638 if (((uchar)c & utfmask[*i]) == utfbyte[*i])
639 return (uchar)c & ~utfmask[*i];
640
641 return 0;
642}
643
644size_t
645utf8encode(Rune u, char *c)
646{
647 size_t len, i;
648
649 len = utf8validate(&u, 0);
650 if (len > UTF_SIZ4)
651 return 0;
652
653 for (i = len - 1; i != 0; --i) {
654 c[i] = utf8encodebyte(u, 0);
655 u >>= 6;
656 }
657 c[0] = utf8encodebyte(u, len);
658
659 return len;
660}
661
662char
663utf8encodebyte(Rune u, size_t i)
664{
665 return utfbyte[i] | (u & ~utfmask[i]);
666}
667
668char *
669utf8strchr(char *s, Rune u)
670{
671 Rune r;
672 size_t i, j, len;
673
674 len = strlen(s);
675 for (i = 0, j = 0; i < len; i += j) {
676 if (!(j = utf8decode(&s[i], &r, len - i)))
677 break;
678 if (r == u)
679 return &(s[i]);
680 }
681
682 return NULL((void*)0);
683}
684
685size_t
686utf8validate(Rune *u, size_t i)
687{
688 if (!BETWEEN(*u, utfmin[i], utfmax[i])((utfmin[i]) <= (*u) && (*u) <= (utfmax[i])) || BETWEEN(*u, 0xD800, 0xDFFF)((0xD800) <= (*u) && (*u) <= (0xDFFF)))
689 *u = UTF_INVALID0xFFFD;
690 for (i = 1; *u > utfmax[i]; ++i)
691 ;
692
693 return i;
694}
695
696void
697selinit(void)
698{
699 clock_gettime(CLOCK_MONOTONIC1, &sel.tclick1);
700 clock_gettime(CLOCK_MONOTONIC1, &sel.tclick2);
701 sel.mode = SEL_IDLE;
702 sel.snap = 0;
703 sel.ob.x = -1;
704 sel.primary = NULL((void*)0);
705 sel.clipboard = NULL((void*)0);
706 sel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
707 if (sel.xtarget == None0L)
708 sel.xtarget = XA_STRING((Atom) 31);
709}
710
711int
712x2col(int x)
713{
714 x -= borderpx;
715 x /= xw.cw;
716
717 return LIMIT(x, 0, term.col-1)(x) = (x) < (0) ? (0) : (x) > (term.col-1) ? (term.col-
1) : (x)
;
718}
719
720int
721y2row(int y)
722{
723 y -= borderpx;
724 y /= xw.ch;
725
726 return LIMIT(y, 0, term.row-1)(y) = (y) < (0) ? (0) : (y) > (term.row-1) ? (term.row-
1) : (y)
;
727}
728
729int
730tlinelen(int y)
731{
732 int i = term.col;
733
734 if (term.line[y][i - 1].mode & ATTR_WRAP)
735 return i;
736
737 while (i > 0 && term.line[y][i - 1].u == ' ')
738 --i;
739
740 return i;
741}
742
743void
744selnormalize(void)
745{
746 int i;
747
748 if (sel.type == SEL_REGULAR && sel.ob.y != sel.oe.y) {
749 sel.nb.x = sel.ob.y < sel.oe.y ? sel.ob.x : sel.oe.x;
750 sel.ne.x = sel.ob.y < sel.oe.y ? sel.oe.x : sel.ob.x;
751 } else {
752 sel.nb.x = MIN(sel.ob.x, sel.oe.x)((sel.ob.x) < (sel.oe.x) ? (sel.ob.x) : (sel.oe.x));
753 sel.ne.x = MAX(sel.ob.x, sel.oe.x)((sel.ob.x) < (sel.oe.x) ? (sel.oe.x) : (sel.ob.x));
754 }
755 sel.nb.y = MIN(sel.ob.y, sel.oe.y)((sel.ob.y) < (sel.oe.y) ? (sel.ob.y) : (sel.oe.y));
756 sel.ne.y = MAX(sel.ob.y, sel.oe.y)((sel.ob.y) < (sel.oe.y) ? (sel.oe.y) : (sel.ob.y));
757
758 selsnap(&sel.nb.x, &sel.nb.y, -1);
759 selsnap(&sel.ne.x, &sel.ne.y, +1);
760
761 /* expand selection over line breaks */
762 if (sel.type == SEL_RECTANGULAR)
763 return;
764 i = tlinelen(sel.nb.y);
765 if (i < sel.nb.x)
766 sel.nb.x = i;
767 if (tlinelen(sel.ne.y) <= sel.ne.x)
768 sel.ne.x = term.col - 1;
769}
770
771int
772selected(int x, int y)
773{
774 if (sel.mode == SEL_EMPTY)
775 return 0;
776
777 if (sel.type == SEL_RECTANGULAR)
778 return BETWEEN(y, sel.nb.y, sel.ne.y)((sel.nb.y) <= (y) && (y) <= (sel.ne.y))
779 && BETWEEN(x, sel.nb.x, sel.ne.x)((sel.nb.x) <= (x) && (x) <= (sel.ne.x));
780
781 return BETWEEN(y, sel.nb.y, sel.ne.y)((sel.nb.y) <= (y) && (y) <= (sel.ne.y))
782 && (y != sel.nb.y || x >= sel.nb.x)
783 && (y != sel.ne.y || x <= sel.ne.x);
784}
785
786void
787selsnap(int *x, int *y, int direction)
788{
789 int newx, newy, xt, yt;
790 int delim, prevdelim;
791 GlyphGlyph_ *gp, *prevgp;
792
793 switch (sel.snap) {
794 case SNAP_WORD:
795 /*
796 * Snap around if the word wraps around at the end or
797 * beginning of a line.
798 */
799 prevgp = &term.line[*y][*x];
800 prevdelim = ISDELIM(prevgp->u)(utf8strchr(worddelimiters, prevgp->u) != ((void*)0));
801 for (;;) {
802 newx = *x + direction;
803 newy = *y;
804 if (!BETWEEN(newx, 0, term.col - 1)((0) <= (newx) && (newx) <= (term.col - 1))) {
805 newy += direction;
806 newx = (newx + term.col) % term.col;
807 if (!BETWEEN(newy, 0, term.row - 1)((0) <= (newy) && (newy) <= (term.row - 1)))
808 break;
809
810 if (direction > 0)
811 yt = *y, xt = *x;
812 else
813 yt = newy, xt = newx;
814 if (!(term.line[yt][xt].mode & ATTR_WRAP))
815 break;
816 }
817
818 if (newx >= tlinelen(newy))
819 break;
820
821 gp = &term.line[newy][newx];
822 delim = ISDELIM(gp->u)(utf8strchr(worddelimiters, gp->u) != ((void*)0));
823 if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
824 || (delim && gp->u != prevgp->u)))
825 break;
826
827 *x = newx;
828 *y = newy;
829 prevgp = gp;
830 prevdelim = delim;
831 }
832 break;
833 case SNAP_LINE:
834 /*
835 * Snap around if the the previous line or the current one
836 * has set ATTR_WRAP at its end. Then the whole next or
837 * previous line will be selected.
838 */
839 *x = (direction < 0) ? 0 : term.col - 1;
840 if (direction < 0) {
841 for (; *y > 0; *y += direction) {
842 if (!(term.line[*y-1][term.col-1].mode
843 & ATTR_WRAP)) {
844 break;
845 }
846 }
847 } else if (direction > 0) {
848 for (; *y < term.row-1; *y += direction) {
849 if (!(term.line[*y][term.col-1].mode
850 & ATTR_WRAP)) {
851 break;
852 }
853 }
854 }
855 break;
856 }
857}
858
859void
860getbuttoninfo(XEvent *e)
861{
862 int type;
863 uint state = e->xbutton.state & ~(Button1Mask(1<<8) | forceselmod);
864
865 sel.alt = IS_SET(MODE_ALTSCREEN)((term.mode & (MODE_ALTSCREEN)) != 0);
866
867 sel.oe.x = x2col(e->xbutton.x);
868 sel.oe.y = y2row(e->xbutton.y);
869 selnormalize();
870
871 sel.type = SEL_REGULAR;
872 for (type = 1; type < LEN(selmasks)(sizeof(selmasks) / sizeof(selmasks)[0]); ++type) {
873 if (match(selmasks[type], state)) {
874 sel.type = type;
875 break;
876 }
877 }
878}
879
880void
881mousereport(XEvent *e)
882{
883 int x = x2col(e->xbutton.x), y = y2row(e->xbutton.y),
884 button = e->xbutton.button, state = e->xbutton.state,
885 len;
886 char buf[40];
887 static int ox, oy;
888
889 /* from urxvt */
890 if (e->xbutton.type == MotionNotify6) {
891 if (x == ox && y == oy)
892 return;
893 if (!IS_SET(MODE_MOUSEMOTION)((term.mode & (MODE_MOUSEMOTION)) != 0) && !IS_SET(MODE_MOUSEMANY)((term.mode & (MODE_MOUSEMANY)) != 0))
894 return;
895 /* MOUSE_MOTION: no reporting if no button is pressed */
896 if (IS_SET(MODE_MOUSEMOTION)((term.mode & (MODE_MOUSEMOTION)) != 0) && oldbutton == 3)
897 return;
898
899 button = oldbutton + 32;
900 ox = x;
901 oy = y;
902 } else {
903 if (!IS_SET(MODE_MOUSESGR)((term.mode & (MODE_MOUSESGR)) != 0) && e->xbutton.type == ButtonRelease5) {
904 button = 3;
905 } else {
906 button -= Button11;
907 if (button >= 3)
908 button += 64 - 3;
909 }
910 if (e->xbutton.type == ButtonPress4) {
911 oldbutton = button;
912 ox = x;
913 oy = y;
914 } else if (e->xbutton.type == ButtonRelease5) {
915 oldbutton = 3;
916 /* MODE_MOUSEX10: no button release reporting */
917 if (IS_SET(MODE_MOUSEX10)((term.mode & (MODE_MOUSEX10)) != 0))
918 return;
919 if (button == 64 || button == 65)
920 return;
921 }
922 }
923
924 if (!IS_SET(MODE_MOUSEX10)((term.mode & (MODE_MOUSEX10)) != 0)) {
925 button += ((state & ShiftMask(1<<0) ) ? 4 : 0)
926 + ((state & Mod4Mask(1<<6) ) ? 8 : 0)
927 + ((state & ControlMask(1<<2)) ? 16 : 0);
928 }
929
930 if (IS_SET(MODE_MOUSESGR)((term.mode & (MODE_MOUSESGR)) != 0)) {
931 len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
932 button, x+1, y+1,
933 e->xbutton.type == ButtonRelease5 ? 'm' : 'M');
934 } else if (x < 223 && y < 223) {
935 len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
936 32+button, 32+x+1, 32+y+1);
937 } else {
938 return;
939 }
940
941 ttywrite(buf, len);
942}
943
944void
945bpress(XEvent *e)
946{
947 struct timespec now;
948 MouseShortcut *ms;
949
950 if (IS_SET(MODE_MOUSE)((term.mode & (MODE_MOUSE)) != 0) && !(e->xbutton.state & forceselmod)) {
951 mousereport(e);
952 return;
953 }
954
955 for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts)(sizeof(mshortcuts) / sizeof(mshortcuts)[0]); ms++) {
956 if (e->xbutton.button == ms->b
957 && match(ms->mask, e->xbutton.state)) {
958 ttysend(ms->s, strlen(ms->s));
959 return;
960 }
961 }
962
963 if (e->xbutton.button == Button11) {
964 clock_gettime(CLOCK_MONOTONIC1, &now);
965
966 /* Clear previous selection, logically and visually. */
967 selclear(NULL((void*)0));
968 sel.mode = SEL_EMPTY;
969 sel.type = SEL_REGULAR;
970 sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
971 sel.oe.y = sel.ob.y = y2row(e->xbutton.y);
972
973 /*
974 * If the user clicks below predefined timeouts specific
975 * snapping behaviour is exposed.
976 */
977 if (TIMEDIFF(now, sel.tclick2)((now.tv_sec-sel.tclick2.tv_sec)*1000 + (now.tv_nsec-sel.tclick2
.tv_nsec)/1E6)
<= tripleclicktimeout) {
978 sel.snap = SNAP_LINE;
979 } else if (TIMEDIFF(now, sel.tclick1)((now.tv_sec-sel.tclick1.tv_sec)*1000 + (now.tv_nsec-sel.tclick1
.tv_nsec)/1E6)
<= doubleclicktimeout) {
980 sel.snap = SNAP_WORD;
981 } else {
982 sel.snap = 0;
983 }
984 selnormalize();
985
986 if (sel.snap != 0)
987 sel.mode = SEL_READY;
988 tsetdirt(sel.nb.y, sel.ne.y);
989 sel.tclick2 = sel.tclick1;
990 sel.tclick1 = now;
991 }
992}
993
994char *
995getsel(void)
996{
997 char *str, *ptr;
998 int y, bufsize, lastx, linelen;
999 GlyphGlyph_ *gp, *last;
1000
1001 if (sel.ob.x == -1)
1002 return NULL((void*)0);
1003
1004 bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ4;
1005 ptr = str = xmalloc(bufsize);
1006
1007 /* append every set & selected glyph to the selection */
1008 for (y = sel.nb.y; y <= sel.ne.y; y++) {
1009 if ((linelen = tlinelen(y)) == 0) {
1010 *ptr++ = '\n';
1011 continue;
1012 }
1013
1014 if (sel.type == SEL_RECTANGULAR) {
1015 gp = &term.line[y][sel.nb.x];
1016 lastx = sel.ne.x;
1017 } else {
1018 gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
1019 lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
1020 }
1021 last = &term.line[y][MIN(lastx, linelen-1)((lastx) < (linelen-1) ? (lastx) : (linelen-1))];
1022 while (last >= gp && last->u == ' ')
1023 --last;
1024
1025 for ( ; gp <= last; ++gp) {
1026 if (gp->mode & ATTR_WDUMMY)
1027 continue;
1028
1029 ptr += utf8encode(gp->u, ptr);
1030 }
1031
1032 /*
1033 * Copy and pasting of line endings is inconsistent
1034 * in the inconsistent terminal and GUI world.
1035 * The best solution seems like to produce '\n' when
1036 * something is copied from st and convert '\n' to
1037 * '\r', when something to be pasted is received by
1038 * st.
1039 * FIXME: Fix the computer world.
1040 */
1041 if ((y < sel.ne.y || lastx >= linelen) && !(last->mode & ATTR_WRAP))
1042 *ptr++ = '\n';
1043 }
1044 *ptr = 0;
1045 return str;
1046}
1047
1048void
1049selcopy(Time t)
1050{
1051 xsetsel(getsel(), t);
1052}
1053
1054void
1055propnotify(XEvent *e)
1056{
1057 XPropertyEvent *xpev;
1058 Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
1059
1060 xpev = &e->xproperty;
1061 if (xpev->state == PropertyNewValue0 &&
1062 (xpev->atom == XA_PRIMARY((Atom) 1) ||
1063 xpev->atom == clipboard)) {
1064 selnotify(e);
1065 }
1066}
1067
1068void
1069selnotify(XEvent *e)
1070{
1071 ulong nitems, ofs, rem;
1072 int format;
1073 uchar *data, *last, *repl;
1074 Atom type, incratom, property;
1075
1076 incratom = XInternAtom(xw.dpy, "INCR", 0);
1077
1078 ofs = 0;
1079 if (e->type == SelectionNotify31) {
1080 property = e->xselection.property;
1081 } else if(e->type == PropertyNotify28) {
1082 property = e->xproperty.atom;
1083 } else {
1084 return;
1085 }
1086 if (property == None0L)
1087 return;
1088
1089 do {
1090 if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
1091 BUFSIZ8192/4, False0, AnyPropertyType0L,
1092 &type, &format, &nitems, &rem,
1093 &data)) {
1094 fprintf(stderrstderr, "Clipboard allocation failed\n");
1095 return;
1096 }
1097
1098 if (e->type == PropertyNotify28 && nitems == 0 && rem == 0) {
1099 /*
1100 * If there is some PropertyNotify with no data, then
1101 * this is the signal of the selection owner that all
1102 * data has been transferred. We won't need to receive
1103 * PropertyNotify events anymore.
1104 */
1105 MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask)((0) ? ((xw.attrs.event_mask) |= ((1L<<22))) : ((xw.attrs
.event_mask) &= ~((1L<<22))))
;
1106 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask(1L<<11),
1107 &xw.attrs);
1108 }
1109
1110 if (type == incratom) {
1111 /*
1112 * Activate the PropertyNotify events so we receive
1113 * when the selection owner does send us the next
1114 * chunk of data.
1115 */
1116 MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask)((1) ? ((xw.attrs.event_mask) |= ((1L<<22))) : ((xw.attrs
.event_mask) &= ~((1L<<22))))
;
1117 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask(1L<<11),
1118 &xw.attrs);
1119
1120 /*
1121 * Deleting the property is the transfer start signal.
1122 */
1123 XDeleteProperty(xw.dpy, xw.win, (int)property);
1124 continue;
1125 }
1126
1127 /*
1128 * As seen in getsel:
1129 * Line endings are inconsistent in the terminal and GUI world
1130 * copy and pasting. When receiving some selection data,
1131 * replace all '\n' with '\r'.
1132 * FIXME: Fix the computer world.
1133 */
1134 repl = data;
1135 last = data + nitems * format / 8;
1136 while ((repl = memchr(repl, '\n', last - repl))) {
1137 *repl++ = '\r';
1138 }
1139
1140 if (IS_SET(MODE_BRCKTPASTE)((term.mode & (MODE_BRCKTPASTE)) != 0) && ofs == 0)
1141 ttywrite("\033[200~", 6);
1142 ttysend((char *)data, nitems * format / 8);
1143 if (IS_SET(MODE_BRCKTPASTE)((term.mode & (MODE_BRCKTPASTE)) != 0) && rem == 0)
1144 ttywrite("\033[201~", 6);
1145 XFree(data);
1146 /* number of 32-bit chunks returned */
1147 ofs += nitems * format / 32;
1148 } while (rem > 0);
1149
1150 /*
1151 * Deleting the property again tells the selection owner to send the
1152 * next data chunk in the property.
1153 */
1154 XDeleteProperty(xw.dpy, xw.win, (int)property);
1155}
1156
1157void
1158selpaste(const Arg *dummy)
1159{
1160 XConvertSelection(xw.dpy, XA_PRIMARY((Atom) 1), sel.xtarget, XA_PRIMARY((Atom) 1),
1161 xw.win, CurrentTime0L);
1162}
1163
1164void
1165clipcopy(const Arg *dummy)
1166{
1167 Atom clipboard;
1168
1169 if (sel.clipboard != NULL((void*)0))
1170 free(sel.clipboard);
1171
1172 if (sel.primary != NULL((void*)0)) {
1173 sel.clipboard = xstrdup(sel.primary);
1174 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
1175 XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime0L);
1176 }
1177}
1178
1179void
1180clippaste(const Arg *dummy)
1181{
1182 Atom clipboard;
1183
1184 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
1185 XConvertSelection(xw.dpy, clipboard, sel.xtarget, clipboard,
1186 xw.win, CurrentTime0L);
1187}
1188
1189void
1190selclear(XEvent *e)
1191{
1192 if (sel.ob.x == -1)
1193 return;
1194 sel.mode = SEL_IDLE;
1195 sel.ob.x = -1;
1196 tsetdirt(sel.nb.y, sel.ne.y);
1197}
1198
1199void
1200selrequest(XEvent *e)
1201{
1202 XSelectionRequestEvent *xsre;
1203 XSelectionEvent xev;
1204 Atom xa_targets, string, clipboard;
1205 char *seltext;
1206
1207 xsre = (XSelectionRequestEvent *) e;
1208 xev.type = SelectionNotify31;
1209 xev.requestor = xsre->requestor;
1210 xev.selection = xsre->selection;
1211 xev.target = xsre->target;
1212 xev.time = xsre->time;
1213 if (xsre->property == None0L)
1214 xsre->property = xsre->target;
1215
1216 /* reject */
1217 xev.property = None0L;
1218
1219 xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
1220 if (xsre->target == xa_targets) {
1221 /* respond with the supported type */
1222 string = sel.xtarget;
1223 XChangeProperty(xsre->display, xsre->requestor, xsre->property,
1224 XA_ATOM((Atom) 4), 32, PropModeReplace0,
1225 (uchar *) &string, 1);
1226 xev.property = xsre->property;
1227 } else if (xsre->target == sel.xtarget || xsre->target == XA_STRING((Atom) 31)) {
1228 /*
1229 * xith XA_STRING non ascii characters may be incorrect in the
1230 * requestor. It is not our problem, use utf8.
1231 */
1232 clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
1233 if (xsre->selection == XA_PRIMARY((Atom) 1)) {
1234 seltext = sel.primary;
1235 } else if (xsre->selection == clipboard) {
1236 seltext = sel.clipboard;
1237 } else {
1238 fprintf(stderrstderr,
1239 "Unhandled clipboard selection 0x%lx\n",
1240 xsre->selection);
1241 return;
1242 }
1243 if (seltext != NULL((void*)0)) {
1244 XChangeProperty(xsre->display, xsre->requestor,
1245 xsre->property, xsre->target,
1246 8, PropModeReplace0,
1247 (uchar *)seltext, strlen(seltext));
1248 xev.property = xsre->property;
1249 }
1250 }
1251
1252 /* all done, send a notification to the listener */
1253 if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
1254 fprintf(stderrstderr, "Error sending SelectionNotify event\n");
1255}
1256
1257void
1258xsetsel(char *str, Time t)
1259{
1260 free(sel.primary);
1261 sel.primary = str;
1262
1263 XSetSelectionOwner(xw.dpy, XA_PRIMARY((Atom) 1), xw.win, t);
1264 if (XGetSelectionOwner(xw.dpy, XA_PRIMARY((Atom) 1)) != xw.win)
1265 selclear(0);
1266}
1267
1268void
1269brelease(XEvent *e)
1270{
1271 if (IS_SET(MODE_MOUSE)((term.mode & (MODE_MOUSE)) != 0) && !(e->xbutton.state & forceselmod)) {
1272 mousereport(e);
1273 return;
1274 }
1275
1276 if (e->xbutton.button == Button22) {
1277 selpaste(NULL((void*)0));
1278 } else if (e->xbutton.button == Button11) {
1279 if (sel.mode == SEL_READY) {
1280 getbuttoninfo(e);
1281 selcopy(e->xbutton.time);
1282 } else
1283 selclear(NULL((void*)0));
1284 sel.mode = SEL_IDLE;
1285 tsetdirt(sel.nb.y, sel.ne.y);
1286 }
1287}
1288
1289void
1290bmotion(XEvent *e)
1291{
1292 int oldey, oldex, oldsby, oldsey;
1293
1294 if (IS_SET(MODE_MOUSE)((term.mode & (MODE_MOUSE)) != 0) && !(e->xbutton.state & forceselmod)) {
1295 mousereport(e);
1296 return;
1297 }
1298
1299 if (!sel.mode)
1300 return;
1301
1302 sel.mode = SEL_READY;
1303 oldey = sel.oe.y;
1304 oldex = sel.oe.x;
1305 oldsby = sel.nb.y;
1306 oldsey = sel.ne.y;
1307 getbuttoninfo(e);
1308
1309 if (oldey != sel.oe.y || oldex != sel.oe.x)
1310 tsetdirt(MIN(sel.nb.y, oldsby)((sel.nb.y) < (oldsby) ? (sel.nb.y) : (oldsby)), MAX(sel.ne.y, oldsey)((sel.ne.y) < (oldsey) ? (oldsey) : (sel.ne.y)));
1311}
1312
1313void
1314die(const char *errstr, ...)
1315{
1316 va_list ap;
1317
1318 va_start(ap, errstr)__builtin_va_start(ap, errstr);
1319 vfprintf(stderrstderr, errstr, ap);
1320 va_end(ap)__builtin_va_end(ap);
1321 exit(1);
1322}
1323
1324void
1325execsh(void)
1326{
1327 char **args, *sh, *prog;
1328 const struct passwd *pw;
1329 char buf[sizeof(long) * 8 + 1];
1330
1331 errno(*__errno_location ()) = 0;
1332 if ((pw = getpwuid(getuid())) == NULL((void*)0)) {
1333 if (errno(*__errno_location ()))
1334 die("getpwuid:%s\n", strerror(errno(*__errno_location ())));
1335 else
1336 die("who are you?\n");
1337 }
1338
1339 if ((sh = getenv("SHELL")) == NULL((void*)0))
1340 sh = (pw->pw_shell[0]) ? pw->pw_shell : shell;
1341
1342 if (opt_cmd)
1343 prog = opt_cmd[0];
1344 else if (utmp)
1345 prog = utmp;
1346 else
1347 prog = sh;
1348 args = (opt_cmd) ? opt_cmd : (char *[]) {prog, NULL((void*)0)};
1349
1350 snprintf(buf, sizeof(buf), "%lu", xw.win);
1351
1352 unsetenv("COLUMNS");
1353 unsetenv("LINES");
1354 unsetenv("TERMCAP");
1355 setenv("LOGNAME", pw->pw_name, 1);
1356 setenv("USER", pw->pw_name, 1);
1357 setenv("SHELL", sh, 1);
1358 setenv("HOME", pw->pw_dir, 1);
1359 setenv("TERM", termname, 1);
1360 setenv("WINDOWID", buf, 1);
1361
1362 signal(SIGCHLD17, SIG_DFL((__sighandler_t) 0));
1363 signal(SIGHUP1, SIG_DFL((__sighandler_t) 0));
1364 signal(SIGINT2, SIG_DFL((__sighandler_t) 0));
1365 signal(SIGQUIT3, SIG_DFL((__sighandler_t) 0));
1366 signal(SIGTERM15, SIG_DFL((__sighandler_t) 0));
1367 signal(SIGALRM14, SIG_DFL((__sighandler_t) 0));
1368
1369 execvp(prog, args);
1370 _exit(1);
1371}
1372
1373void
1374sigchld(int a)
1375{
1376 int stat;
1377 pid_t p;
1378
1379 if ((p = waitpid(pid, &stat, WNOHANG1)) < 0)
1380 die("Waiting for pid %hd failed: %s\n", pid, strerror(errno(*__errno_location ())));
1381
1382 if (pid != p)
1383 return;
1384
1385 if (!WIFEXITED(stat)((((stat)) & 0x7f) == 0) || WEXITSTATUS(stat)((((stat)) & 0xff00) >> 8))
1386 die("child finished with error '%d'\n", stat);
1387 exit(0);
1388}
1389
1390
1391void
1392stty(void)
1393{
1394 char cmd[_POSIX_ARG_MAX4096], **p, *q, *s;
1395 size_t n, siz;
1396
1397 if ((n = strlen(stty_args)) > sizeof(cmd)-1)
1398 die("incorrect stty parameters\n");
1399 memcpy(cmd, stty_args, n);
1400 q = cmd + n;
1401 siz = sizeof(cmd) - n;
1402 for (p = opt_cmd; p && (s = *p); ++p) {
1403 if ((n = strlen(s)) > siz-1)
1404 die("stty parameter length too long\n");
1405 *q++ = ' ';
1406 memcpy(q, s, n);
1407 q += n;
1408 siz -= n + 1;
1409 }
1410 *q = '\0';
1411 if (system(cmd) != 0)
1412 perror("Couldn't call stty");
1413}
1414
1415void
1416ttynew(void)
1417{
1418 int m, s;
1419 struct winsize w = {term.row, term.col, 0, 0};
1420
1421 if (opt_io) {
1422 term.mode |= MODE_PRINT;
1423 iofd = (!strcmp(opt_io, "-")) ?
1424 1 : open(opt_io, O_WRONLY01 | O_CREAT0100, 0666);
1425 if (iofd < 0) {
1426 fprintf(stderrstderr, "Error opening %s:%s\n",
1427 opt_io, strerror(errno(*__errno_location ())));
1428 }
1429 }
1430
1431 if (opt_line) {
1432 if ((cmdfd = open(opt_line, O_RDWR02)) < 0)
1433 die("open line failed: %s\n", strerror(errno(*__errno_location ())));
1434 dup2(cmdfd, 0);
1435 stty();
1436 return;
1437 }
1438
1439 /* seems to work fine on linux, openbsd and freebsd */
1440 if (openpty(&m, &s, NULL((void*)0), NULL((void*)0), &w) < 0)
1441 die("openpty failed: %s\n", strerror(errno(*__errno_location ())));
1442
1443 switch (pid = fork()) {
1444 case -1:
1445 die("fork failed\n");
1446 break;
1447 case 0:
1448 close(iofd);
1449 setsid(); /* create a new process group */
1450 dup2(s, 0);
1451 dup2(s, 1);
1452 dup2(s, 2);
1453 if (ioctl(s, TIOCSCTTY0x540E, NULL((void*)0)) < 0)
1454 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno(*__errno_location ())));
1455 close(s);
1456 close(m);
1457 execsh();
1458 break;
1459 default:
1460 close(s);
1461 cmdfd = m;
1462 signal(SIGCHLD17, sigchld);
1463 break;
1464 }
1465}
1466
1467size_t
1468ttyread(void)
1469{
1470 static char buf[BUFSIZ8192];
1471 static int buflen = 0;
1472 char *ptr;
1473 int charsize; /* size of utf8 char in bytes */
1474 Rune unicodep;
1475 int ret;
1476
1477 /* append read bytes to unprocessed bytes */
1478 if ((ret = read(cmdfd, buf+buflen, LEN(buf)(sizeof(buf) / sizeof(buf)[0])-buflen)) < 0)
1479 die("Couldn't read from shell: %s\n", strerror(errno(*__errno_location ())));
1480
1481 /* process every complete utf8 char */
1482 buflen += ret;
1483 ptr = buf;
1484 while ((charsize = utf8decode(ptr, &unicodep, buflen))) {
1485 tputc(unicodep);
1486 ptr += charsize;
1487 buflen -= charsize;
1488 }
1489
1490 /* keep any uncomplete utf8 char for the next call */
1491 memmove(buf, ptr, buflen);
1492
1493 return ret;
1494}
1495
1496void
1497ttywrite(const char *s, size_t n)
1498{
1499 fd_set wfd, rfd;
1500 ssize_t r;
1501 size_t lim = 256;
1502
1503 /*
1504 * Remember that we are using a pty, which might be a modem line.
1505 * Writing too much will clog the line. That's why we are doing this
1506 * dance.
1507 * FIXME: Migrate the world to Plan 9.
1508 */
1509 while (n > 0) {
1510 FD_ZERO(&wfd)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq"
: "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) /
sizeof (__fd_mask)), "1" (&((&wfd)->fds_bits)[0])
: "memory"); } while (0)
;
1511 FD_ZERO(&rfd)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq"
: "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) /
sizeof (__fd_mask)), "1" (&((&rfd)->fds_bits)[0])
: "memory"); } while (0)
;
1512 FD_SET(cmdfd, &wfd)((void) (((&wfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof
(__fd_mask)))] |= ((__fd_mask) 1 << ((cmdfd) % (8 * (int
) sizeof (__fd_mask))))))
;
1513 FD_SET(cmdfd, &rfd)((void) (((&rfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof
(__fd_mask)))] |= ((__fd_mask) 1 << ((cmdfd) % (8 * (int
) sizeof (__fd_mask))))))
;
1514
1515 /* Check if we can write. */
1516 if (pselect(cmdfd+1, &rfd, &wfd, NULL((void*)0), NULL((void*)0), NULL((void*)0)) < 0) {
1517 if (errno(*__errno_location ()) == EINTR4)
1518 continue;
1519 die("select failed: %s\n", strerror(errno(*__errno_location ())));
1520 }
1521 if (FD_ISSET(cmdfd, &wfd)((((&wfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((cmdfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
) {
1522 /*
1523 * Only write the bytes written by ttywrite() or the
1524 * default of 256. This seems to be a reasonable value
1525 * for a serial line. Bigger values might clog the I/O.
1526 */
1527 if ((r = write(cmdfd, s, (n < lim)? n : lim)) < 0)
1528 goto write_error;
1529 if (r < n) {
1530 /*
1531 * We weren't able to write out everything.
1532 * This means the buffer is getting full
1533 * again. Empty it.
1534 */
1535 if (n < lim)
1536 lim = ttyread();
1537 n -= r;
1538 s += r;
1539 } else {
1540 /* All bytes have been written. */
1541 break;
1542 }
1543 }
1544 if (FD_ISSET(cmdfd, &rfd)((((&rfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((cmdfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
)
1545 lim = ttyread();
1546 }
1547 return;
1548
1549write_error:
1550 die("write error on tty: %s\n", strerror(errno(*__errno_location ())));
1551}
1552
1553void
1554ttysend(char *s, size_t n)
1555{
1556 int len;
1557 Rune u;
1558
1559 ttywrite(s, n);
1560 if (IS_SET(MODE_ECHO)((term.mode & (MODE_ECHO)) != 0))
1561 while ((len = utf8decode(s, &u, n)) > 0) {
1562 techo(u);
1563 n -= len;
1564 s += len;
1565 }
1566}
1567
1568void
1569ttyresize(void)
1570{
1571 struct winsize w;
1572
1573 w.ws_row = term.row;
1574 w.ws_col = term.col;
1575 w.ws_xpixel = xw.tw;
1576 w.ws_ypixel = xw.th;
1577 if (ioctl(cmdfd, TIOCSWINSZ0x5414, &w) < 0)
1578 fprintf(stderrstderr, "Couldn't set window size: %s\n", strerror(errno(*__errno_location ())));
1579}
1580
1581int
1582tattrset(int attr)
1583{
1584 int i, j;
1585
1586 for (i = 0; i < term.row-1; i++) {
1587 for (j = 0; j < term.col-1; j++) {
1588 if (term.line[i][j].mode & attr)
1589 return 1;
1590 }
1591 }
1592
1593 return 0;
1594}
1595
1596void
1597tsetdirt(int top, int bot)
1598{
1599 int i;
1600
1601 LIMIT(top, 0, term.row-1)(top) = (top) < (0) ? (0) : (top) > (term.row-1) ? (term
.row-1) : (top)
;
1602 LIMIT(bot, 0, term.row-1)(bot) = (bot) < (0) ? (0) : (bot) > (term.row-1) ? (term
.row-1) : (bot)
;
1603
1604 for (i = top; i <= bot; i++)
1605 term.dirty[i] = 1;
1606}
1607
1608void
1609tsetdirtattr(int attr)
1610{
1611 int i, j;
1612
1613 for (i = 0; i < term.row-1; i++) {
1614 for (j = 0; j < term.col-1; j++) {
1615 if (term.line[i][j].mode & attr) {
1616 tsetdirt(i, i);
1617 break;
1618 }
1619 }
1620 }
1621}
1622
1623void
1624tfulldirt(void)
1625{
1626 tsetdirt(0, term.row-1);
1627}
1628
1629void
1630tcursor(int mode)
1631{
1632 static TCursor c[2];
1633 int alt = IS_SET(MODE_ALTSCREEN)((term.mode & (MODE_ALTSCREEN)) != 0);
1634
1635 if (mode == CURSOR_SAVE) {
1636 c[alt] = term.c;
1637 } else if (mode == CURSOR_LOAD) {
1638 term.c = c[alt];
1639 tmoveto(c[alt].x, c[alt].y);
1640 }
1641}
1642
1643void
1644treset(void)
1645{
1646 uint i;
1647
1648 term.c = (TCursor){{
1649 .mode = ATTR_NULL,
1650 .fg = defaultfg,
1651 .bg = defaultbg
1652 }, .x = 0, .y = 0, .state = CURSOR_DEFAULT};
1653
1654 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
1655 for (i = tabspaces; i < term.col; i += tabspaces)
1656 term.tabs[i] = 1;
1657 term.top = 0;
1658 term.bot = term.row - 1;
1659 term.mode = MODE_WRAP;
1660 memset(term.trantbl, CS_USA, sizeof(term.trantbl));
1661 term.charset = 0;
1662
1663 for (i = 0; i < 2; i++) {
1664 tmoveto(0, 0);
1665 tcursor(CURSOR_SAVE);
1666 tclearregion(0, 0, term.col-1, term.row-1);
1667 tswapscreen();
1668 }
1669}
1670
1671void
1672tnew(int col, int row)
1673{
1674 term = (Term){ .c = { .attr = { .fg = defaultfg, .bg = defaultbg } } };
1675 tresize(col, row);
1676 term.numlock = 1;
1677
1678 treset();
1679}
1680
1681void
1682tswapscreen(void)
1683{
1684 Line *tmp = term.line;
1685
1686 term.line = term.alt;
1687 term.alt = tmp;
1688 term.mode ^= MODE_ALTSCREEN;
1689 tfulldirt();
1690}
1691
1692void
1693tscrolldown(int orig, int n)
1694{
1695 int i;
1696 Line temp;
1697
1698 LIMIT(n, 0, term.bot-orig+1)(n) = (n) < (0) ? (0) : (n) > (term.bot-orig+1) ? (term
.bot-orig+1) : (n)
;
1699
1700 tsetdirt(orig, term.bot-n);
1701 tclearregion(0, term.bot-n+1, term.col-1, term.bot);
1702
1703 for (i = term.bot; i >= orig+n; i--) {
1704 temp = term.line[i];
1705 term.line[i] = term.line[i-n];
1706 term.line[i-n] = temp;
1707 }
1708
1709 selscroll(orig, n);
1710}
1711
1712void
1713tscrollup(int orig, int n)
1714{
1715 int i;
1716 Line temp;
1717
1718 LIMIT(n, 0, term.bot-orig+1)(n) = (n) < (0) ? (0) : (n) > (term.bot-orig+1) ? (term
.bot-orig+1) : (n)
;
1719
1720 tclearregion(0, orig, term.col-1, orig+n-1);
1721 tsetdirt(orig+n, term.bot);
1722
1723 for (i = orig; i <= term.bot-n; i++) {
1724 temp = term.line[i];
1725 term.line[i] = term.line[i+n];
1726 term.line[i+n] = temp;
1727 }
1728
1729 selscroll(orig, -n);
1730}
1731
1732void
1733selscroll(int orig, int n)
1734{
1735 if (sel.ob.x == -1)
1736 return;
1737
1738 if (BETWEEN(sel.ob.y, orig, term.bot)((orig) <= (sel.ob.y) && (sel.ob.y) <= (term.bot
))
|| BETWEEN(sel.oe.y, orig, term.bot)((orig) <= (sel.oe.y) && (sel.oe.y) <= (term.bot
))
) {
1739 if ((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
1740 selclear(NULL((void*)0));
1741 return;
1742 }
1743 if (sel.type == SEL_RECTANGULAR) {
1744 if (sel.ob.y < term.top)
1745 sel.ob.y = term.top;
1746 if (sel.oe.y > term.bot)
1747 sel.oe.y = term.bot;
1748 } else {
1749 if (sel.ob.y < term.top) {
1750 sel.ob.y = term.top;
1751 sel.ob.x = 0;
1752 }
1753 if (sel.oe.y > term.bot) {
1754 sel.oe.y = term.bot;
1755 sel.oe.x = term.col;
1756 }
1757 }
1758 selnormalize();
1759 }
1760}
1761
1762void
1763tnewline(int first_col)
1764{
1765 int y = term.c.y;
1766
1767 if (y == term.bot) {
1768 tscrollup(term.top, 1);
1769 } else {
1770 y++;
1771 }
1772 tmoveto(first_col ? 0 : term.c.x, y);
1773}
1774
1775void
1776csiparse(void)
1777{
1778 char *p = csiescseq.buf, *np;
1779 long int v;
1780
1781 csiescseq.narg = 0;
1782 if (*p == '?') {
1783 csiescseq.priv = 1;
1784 p++;
1785 }
1786
1787 csiescseq.buf[csiescseq.len] = '\0';
1788 while (p < csiescseq.buf+csiescseq.len) {
1789 np = NULL((void*)0);
1790 v = strtol(p, &np, 10);
1791 if (np == p)
1792 v = 0;
1793 if (v == LONG_MAX9223372036854775807L || v == LONG_MIN(-9223372036854775807L -1L))
1794 v = -1;
1795 csiescseq.arg[csiescseq.narg++] = v;
1796 p = np;
1797 if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ16)
1798 break;
1799 p++;
1800 }
1801 csiescseq.mode[0] = *p++;
1802 csiescseq.mode[1] = (p < csiescseq.buf+csiescseq.len) ? *p : '\0';
1803}
1804
1805/* for absolute user moves, when decom is set */
1806void
1807tmoveato(int x, int y)
1808{
1809 tmoveto(x, y + ((term.c.state & CURSOR_ORIGIN) ? term.top: 0));
1810}
1811
1812void
1813tmoveto(int x, int y)
1814{
1815 int miny, maxy;
1816
1817 if (term.c.state & CURSOR_ORIGIN) {
1818 miny = term.top;
1819 maxy = term.bot;
1820 } else {
1821 miny = 0;
1822 maxy = term.row - 1;
1823 }
1824 term.c.state &= ~CURSOR_WRAPNEXT;
1825 term.c.x = LIMIT(x, 0, term.col-1)(x) = (x) < (0) ? (0) : (x) > (term.col-1) ? (term.col-
1) : (x)
;
1826 term.c.y = LIMIT(y, miny, maxy)(y) = (y) < (miny) ? (miny) : (y) > (maxy) ? (maxy) : (
y)
;
1827}
1828
1829void
1830tsetchar(Rune u, GlyphGlyph_ *attr, int x, int y)
1831{
1832 static char *vt100_0[62] = { /* 0x41 - 0x7e */
1833 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1834 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1835 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1836 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1837 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1838 "␤", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1839 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1840 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1841 };
1842
1843 /*
1844 * The table is proudly stolen from rxvt.
1845 */
1846 if (term.trantbl[term.charset] == CS_GRAPHIC0 &&
1847 BETWEEN(u, 0x41, 0x7e)((0x41) <= (u) && (u) <= (0x7e)) && vt100_0[u - 0x41])
1848 utf8decode(vt100_0[u - 0x41], &u, UTF_SIZ4);
1849
1850 if (term.line[y][x].mode & ATTR_WIDE) {
1851 if (x+1 < term.col) {
1852 term.line[y][x+1].u = ' ';
1853 term.line[y][x+1].mode &= ~ATTR_WDUMMY;
1854 }
1855 } else if (term.line[y][x].mode & ATTR_WDUMMY) {
1856 term.line[y][x-1].u = ' ';
1857 term.line[y][x-1].mode &= ~ATTR_WIDE;
1858 }
1859
1860 term.dirty[y] = 1;
1861 term.line[y][x] = *attr;
1862 term.line[y][x].u = u;
1863}
1864
1865void
1866tclearregion(int x1, int y1, int x2, int y2)
1867{
1868 int x, y, temp;
1869 GlyphGlyph_ *gp;
1870
1871 if (x1 > x2)
1872 temp = x1, x1 = x2, x2 = temp;
1873 if (y1 > y2)
1874 temp = y1, y1 = y2, y2 = temp;
1875
1876 LIMIT(x1, 0, term.col-1)(x1) = (x1) < (0) ? (0) : (x1) > (term.col-1) ? (term.col
-1) : (x1)
;
1877 LIMIT(x2, 0, term.col-1)(x2) = (x2) < (0) ? (0) : (x2) > (term.col-1) ? (term.col
-1) : (x2)
;
1878 LIMIT(y1, 0, term.row-1)(y1) = (y1) < (0) ? (0) : (y1) > (term.row-1) ? (term.row
-1) : (y1)
;
1879 LIMIT(y2, 0, term.row-1)(y2) = (y2) < (0) ? (0) : (y2) > (term.row-1) ? (term.row
-1) : (y2)
;
1880
1881 for (y = y1; y <= y2; y++) {
1882 term.dirty[y] = 1;
1883 for (x = x1; x <= x2; x++) {
1884 gp = &term.line[y][x];
1885 if (selected(x, y))
1886 selclear(NULL((void*)0));
1887 gp->fg = term.c.attr.fg;
1888 gp->bg = term.c.attr.bg;
1889 gp->mode = 0;
1890 gp->u = ' ';
1891 }
1892 }
1893}
1894
1895void
1896tdeletechar(int n)
1897{
1898 int dst, src, size;
1899 GlyphGlyph_ *line;
1900
1901 LIMIT(n, 0, term.col - term.c.x)(n) = (n) < (0) ? (0) : (n) > (term.col - term.c.x) ? (
term.col - term.c.x) : (n)
;
1902
1903 dst = term.c.x;
1904 src = term.c.x + n;
1905 size = term.col - src;
1906 line = term.line[term.c.y];
1907
1908 memmove(&line[dst], &line[src], size * sizeof(GlyphGlyph_));
1909 tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
1910}
1911
1912void
1913tinsertblank(int n)
1914{
1915 int dst, src, size;
1916 GlyphGlyph_ *line;
1917
1918 LIMIT(n, 0, term.col - term.c.x)(n) = (n) < (0) ? (0) : (n) > (term.col - term.c.x) ? (
term.col - term.c.x) : (n)
;
1919
1920 dst = term.c.x + n;
1921 src = term.c.x;
1922 size = term.col - dst;
1923 line = term.line[term.c.y];
1924
1925 memmove(&line[dst], &line[src], size * sizeof(GlyphGlyph_));
1926 tclearregion(src, term.c.y, dst - 1, term.c.y);
1927}
1928
1929void
1930tinsertblankline(int n)
1931{
1932 if (BETWEEN(term.c.y, term.top, term.bot)((term.top) <= (term.c.y) && (term.c.y) <= (term
.bot))
)
1933 tscrolldown(term.c.y, n);
1934}
1935
1936void
1937tdeleteline(int n)
1938{
1939 if (BETWEEN(term.c.y, term.top, term.bot)((term.top) <= (term.c.y) && (term.c.y) <= (term
.bot))
)
1940 tscrollup(term.c.y, n);
1941}
1942
1943int32_t
1944tdefcolor(int *attr, int *npar, int l)
1945{
1946 int32_t idx = -1;
1947 uint r, g, b;
1948
1949 switch (attr[*npar + 1]) {
1950 case 2: /* direct color in RGB space */
1951 if (*npar + 4 >= l) {
1952 fprintf(stderrstderr,
1953 "erresc(38): Incorrect number of parameters (%d)\n",
1954 *npar);
1955 break;
1956 }
1957 r = attr[*npar + 2];
1958 g = attr[*npar + 3];
1959 b = attr[*npar + 4];
1960 *npar += 4;
1961 if (!BETWEEN(r, 0, 255)((0) <= (r) && (r) <= (255)) || !BETWEEN(g, 0, 255)((0) <= (g) && (g) <= (255)) || !BETWEEN(b, 0, 255)((0) <= (b) && (b) <= (255)))
1962 fprintf(stderrstderr, "erresc: bad rgb color (%u,%u,%u)\n",
1963 r, g, b);
1964 else
1965 idx = TRUECOLOR(r, g, b)(1 << 24 | (r) << 16 | (g) << 8 | (b));
1966 break;
1967 case 5: /* indexed color */
1968 if (*npar + 2 >= l) {
1969 fprintf(stderrstderr,
1970 "erresc(38): Incorrect number of parameters (%d)\n",
1971 *npar);
1972 break;
1973 }
1974 *npar += 2;
1975 if (!BETWEEN(attr[*npar], 0, 255)((0) <= (attr[*npar]) && (attr[*npar]) <= (255)
)
)
1976 fprintf(stderrstderr, "erresc: bad fgcolor %d\n", attr[*npar]);
1977 else
1978 idx = attr[*npar];
1979 break;
1980 case 0: /* implemented defined (only foreground) */
1981 case 1: /* transparent */
1982 case 3: /* direct color in CMY space */
1983 case 4: /* direct color in CMYK space */
1984 default:
1985 fprintf(stderrstderr,
1986 "erresc(38): gfx attr %d unknown\n", attr[*npar]);
1987 break;
1988 }
1989
1990 return idx;
1991}
1992
1993void
1994tsetattr(int *attr, int l)
1995{
1996 int i;
1997 int32_t idx;
1998
1999 for (i = 0; i < l; i++) {
2000 switch (attr[i]) {
2001 case 0:
2002 term.c.attr.mode &= ~(
2003 ATTR_BOLD |
2004 ATTR_FAINT |
2005 ATTR_ITALIC |
2006 ATTR_UNDERLINE |
2007 ATTR_BLINK |
2008 ATTR_REVERSE |
2009 ATTR_INVISIBLE |
2010 ATTR_STRUCK );
2011 term.c.attr.fg = defaultfg;
2012 term.c.attr.bg = defaultbg;
2013 break;
2014 case 1:
2015 term.c.attr.mode |= ATTR_BOLD;
2016 break;
2017 case 2:
2018 term.c.attr.mode |= ATTR_FAINT;
2019 break;
2020 case 3:
2021 term.c.attr.mode |= ATTR_ITALIC;
2022 break;
2023 case 4:
2024 term.c.attr.mode |= ATTR_UNDERLINE;
2025 break;
2026 case 5: /* slow blink */
2027 /* FALLTHROUGH */
2028 case 6: /* rapid blink */
2029 term.c.attr.mode |= ATTR_BLINK;
2030 break;
2031 case 7:
2032 term.c.attr.mode |= ATTR_REVERSE;
2033 break;
2034 case 8:
2035 term.c.attr.mode |= ATTR_INVISIBLE;
2036 break;
2037 case 9:
2038 term.c.attr.mode |= ATTR_STRUCK;
2039 break;
2040 case 22:
2041 term.c.attr.mode &= ~(ATTR_BOLD | ATTR_FAINT);
2042 break;
2043 case 23:
2044 term.c.attr.mode &= ~ATTR_ITALIC;
2045 break;
2046 case 24:
2047 term.c.attr.mode &= ~ATTR_UNDERLINE;
2048 break;
2049 case 25:
2050 term.c.attr.mode &= ~ATTR_BLINK;
2051 break;
2052 case 27:
2053 term.c.attr.mode &= ~ATTR_REVERSE;
2054 break;
2055 case 28:
2056 term.c.attr.mode &= ~ATTR_INVISIBLE;
2057 break;
2058 case 29:
2059 term.c.attr.mode &= ~ATTR_STRUCK;
2060 break;
2061 case 38:
2062 if ((idx = tdefcolor(attr, &i, l)) >= 0)
2063 term.c.attr.fg = idx;
2064 break;
2065 case 39:
2066 term.c.attr.fg = defaultfg;
2067 break;
2068 case 48:
2069 if ((idx = tdefcolor(attr, &i, l)) >= 0)
2070 term.c.attr.bg = idx;
2071 break;
2072 case 49:
2073 term.c.attr.bg = defaultbg;
2074 break;
2075 default:
2076 if (BETWEEN(attr[i], 30, 37)((30) <= (attr[i]) && (attr[i]) <= (37))) {
2077 term.c.attr.fg = attr[i] - 30;
2078 } else if (BETWEEN(attr[i], 40, 47)((40) <= (attr[i]) && (attr[i]) <= (47))) {
2079 term.c.attr.bg = attr[i] - 40;
2080 } else if (BETWEEN(attr[i], 90, 97)((90) <= (attr[i]) && (attr[i]) <= (97))) {
2081 term.c.attr.fg = attr[i] - 90 + 8;
2082 } else if (BETWEEN(attr[i], 100, 107)((100) <= (attr[i]) && (attr[i]) <= (107))) {
2083 term.c.attr.bg = attr[i] - 100 + 8;
2084 } else {
2085 fprintf(stderrstderr,
2086 "erresc(default): gfx attr %d unknown\n",
2087 attr[i]), csidump();
2088 }
2089 break;
2090 }
2091 }
2092}
2093
2094void
2095tsetscroll(int t, int b)
2096{
2097 int temp;
2098
2099 LIMIT(t, 0, term.row-1)(t) = (t) < (0) ? (0) : (t) > (term.row-1) ? (term.row-
1) : (t)
;
2100 LIMIT(b, 0, term.row-1)(b) = (b) < (0) ? (0) : (b) > (term.row-1) ? (term.row-
1) : (b)
;
2101 if (t > b) {
2102 temp = t;
2103 t = b;
2104 b = temp;
2105 }
2106 term.top = t;
2107 term.bot = b;
2108}
2109
2110void
2111tsetmode(int priv, int set, int *args, int narg)
2112{
2113 int *lim, mode;
2114 int alt;
2115
2116 for (lim = args + narg; args < lim; ++args) {
2117 if (priv) {
2118 switch (*args) {
2119 case 1: /* DECCKM -- Cursor key */
2120 MODBIT(term.mode, set, MODE_APPCURSOR)((set) ? ((term.mode) |= (MODE_APPCURSOR)) : ((term.mode) &=
~(MODE_APPCURSOR)))
;
2121 break;
2122 case 5: /* DECSCNM -- Reverse video */
2123 mode = term.mode;
2124 MODBIT(term.mode, set, MODE_REVERSE)((set) ? ((term.mode) |= (MODE_REVERSE)) : ((term.mode) &=
~(MODE_REVERSE)))
;
2125 if (mode != term.mode)
2126 redraw();
2127 break;
2128 case 6: /* DECOM -- Origin */
2129 MODBIT(term.c.state, set, CURSOR_ORIGIN)((set) ? ((term.c.state) |= (CURSOR_ORIGIN)) : ((term.c.state
) &= ~(CURSOR_ORIGIN)))
;
2130 tmoveato(0, 0);
2131 break;
2132 case 7: /* DECAWM -- Auto wrap */
2133 MODBIT(term.mode, set, MODE_WRAP)((set) ? ((term.mode) |= (MODE_WRAP)) : ((term.mode) &= ~
(MODE_WRAP)))
;
2134 break;
2135 case 0: /* Error (IGNORED) */
2136 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
2137 case 3: /* DECCOLM -- Column (IGNORED) */
2138 case 4: /* DECSCLM -- Scroll (IGNORED) */
2139 case 8: /* DECARM -- Auto repeat (IGNORED) */
2140 case 18: /* DECPFF -- Printer feed (IGNORED) */
2141 case 19: /* DECPEX -- Printer extent (IGNORED) */
2142 case 42: /* DECNRCM -- National characters (IGNORED) */
2143 case 12: /* att610 -- Start blinking cursor (IGNORED) */
2144 break;
2145 case 25: /* DECTCEM -- Text Cursor Enable Mode */
2146 MODBIT(term.mode, !set, MODE_HIDE)((!set) ? ((term.mode) |= (MODE_HIDE)) : ((term.mode) &= ~
(MODE_HIDE)))
;
2147 break;
2148 case 9: /* X10 mouse compatibility mode */
2149 xsetpointermotion(0);
2150 MODBIT(term.mode, 0, MODE_MOUSE)((0) ? ((term.mode) |= (MODE_MOUSE)) : ((term.mode) &= ~(
MODE_MOUSE)))
;
2151 MODBIT(term.mode, set, MODE_MOUSEX10)((set) ? ((term.mode) |= (MODE_MOUSEX10)) : ((term.mode) &=
~(MODE_MOUSEX10)))
;
2152 break;
2153 case 1000: /* 1000: report button press */
2154 xsetpointermotion(0);
2155 MODBIT(term.mode, 0, MODE_MOUSE)((0) ? ((term.mode) |= (MODE_MOUSE)) : ((term.mode) &= ~(
MODE_MOUSE)))
;
2156 MODBIT(term.mode, set, MODE_MOUSEBTN)((set) ? ((term.mode) |= (MODE_MOUSEBTN)) : ((term.mode) &=
~(MODE_MOUSEBTN)))
;
2157 break;
2158 case 1002: /* 1002: report motion on button press */
2159 xsetpointermotion(0);
2160 MODBIT(term.mode, 0, MODE_MOUSE)((0) ? ((term.mode) |= (MODE_MOUSE)) : ((term.mode) &= ~(
MODE_MOUSE)))
;
2161 MODBIT(term.mode, set, MODE_MOUSEMOTION)((set) ? ((term.mode) |= (MODE_MOUSEMOTION)) : ((term.mode) &=
~(MODE_MOUSEMOTION)))
;
2162 break;
2163 case 1003: /* 1003: enable all mouse motions */
2164 xsetpointermotion(set);
2165 MODBIT(term.mode, 0, MODE_MOUSE)((0) ? ((term.mode) |= (MODE_MOUSE)) : ((term.mode) &= ~(
MODE_MOUSE)))
;
2166 MODBIT(term.mode, set, MODE_MOUSEMANY)((set) ? ((term.mode) |= (MODE_MOUSEMANY)) : ((term.mode) &=
~(MODE_MOUSEMANY)))
;
2167 break;
2168 case 1004: /* 1004: send focus events to tty */
2169 MODBIT(term.mode, set, MODE_FOCUS)((set) ? ((term.mode) |= (MODE_FOCUS)) : ((term.mode) &= ~
(MODE_FOCUS)))
;
2170 break;
2171 case 1006: /* 1006: extended reporting mode */
2172 MODBIT(term.mode, set, MODE_MOUSESGR)((set) ? ((term.mode) |= (MODE_MOUSESGR)) : ((term.mode) &=
~(MODE_MOUSESGR)))
;
2173 break;
2174 case 1034:
2175 MODBIT(term.mode, set, MODE_8BIT)((set) ? ((term.mode) |= (MODE_8BIT)) : ((term.mode) &= ~
(MODE_8BIT)))
;
2176 break;
2177 case 1049: /* swap screen & set/restore cursor as xterm */
2178 if (!allowaltscreen)
2179 break;
2180 tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
2181 /* FALLTHROUGH */
2182 case 47: /* swap screen */
2183 case 1047:
2184 if (!allowaltscreen)
2185 break;
2186 alt = IS_SET(MODE_ALTSCREEN)((term.mode & (MODE_ALTSCREEN)) != 0);
2187 if (alt) {
2188 tclearregion(0, 0, term.col-1,
2189 term.row-1);
2190 }
2191 if (set ^ alt) /* set is always 1 or 0 */
2192 tswapscreen();
2193 if (*args != 1049)
2194 break;
2195 /* FALLTHROUGH */
2196 case 1048:
2197 tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
2198 break;
2199 case 2004: /* 2004: bracketed paste mode */
2200 MODBIT(term.mode, set, MODE_BRCKTPASTE)((set) ? ((term.mode) |= (MODE_BRCKTPASTE)) : ((term.mode) &=
~(MODE_BRCKTPASTE)))
;
2201 break;
2202 /* Not implemented mouse modes. See comments there. */
2203 case 1001: /* mouse highlight mode; can hang the
2204 terminal by design when implemented. */
2205 case 1005: /* UTF-8 mouse mode; will confuse
2206 applications not supporting UTF-8
2207 and luit. */
2208 case 1015: /* urxvt mangled mouse mode; incompatible
2209 and can be mistaken for other control
2210 codes. */
2211 default:
2212 fprintf(stderrstderr,
2213 "erresc: unknown private set/reset mode %d\n",
2214 *args);
2215 break;
2216 }
2217 } else {
2218 switch (*args) {
2219 case 0: /* Error (IGNORED) */
2220 break;
2221 case 2: /* KAM -- keyboard action */
2222 MODBIT(term.mode, set, MODE_KBDLOCK)((set) ? ((term.mode) |= (MODE_KBDLOCK)) : ((term.mode) &=
~(MODE_KBDLOCK)))
;
2223 break;
2224 case 4: /* IRM -- Insertion-replacement */
2225 MODBIT(term.mode, set, MODE_INSERT)((set) ? ((term.mode) |= (MODE_INSERT)) : ((term.mode) &=
~(MODE_INSERT)))
;
2226 break;
2227 case 12: /* SRM -- Send/Receive */
2228 MODBIT(term.mode, !set, MODE_ECHO)((!set) ? ((term.mode) |= (MODE_ECHO)) : ((term.mode) &= ~
(MODE_ECHO)))
;
2229 break;
2230 case 20: /* LNM -- Linefeed/new line */
2231 MODBIT(term.mode, set, MODE_CRLF)((set) ? ((term.mode) |= (MODE_CRLF)) : ((term.mode) &= ~
(MODE_CRLF)))
;
2232 break;
2233 default:
2234 fprintf(stderrstderr,
2235 "erresc: unknown set/reset mode %d\n",
2236 *args);
2237 break;
2238 }
2239 }
2240 }
2241}
2242
2243void
2244csihandle(void)
2245{
2246 char buf[40];
2247 int len;
2248
2249 switch (csiescseq.mode[0]) {
2250 default:
2251 unknown:
2252 fprintf(stderrstderr, "erresc: unknown csi ");
2253 csidump();
2254 /* die(""); */
2255 break;
2256 case '@': /* ICH -- Insert <n> blank char */
2257 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2258 tinsertblank(csiescseq.arg[0]);
2259 break;
2260 case 'A': /* CUU -- Cursor <n> Up */
2261 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2262 tmoveto(term.c.x, term.c.y-csiescseq.arg[0]);
2263 break;
2264 case 'B': /* CUD -- Cursor <n> Down */
2265 case 'e': /* VPR --Cursor <n> Down */
2266 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2267 tmoveto(term.c.x, term.c.y+csiescseq.arg[0]);
2268 break;
2269 case 'i': /* MC -- Media Copy */
2270 switch (csiescseq.arg[0]) {
2271 case 0:
2272 tdump();
2273 break;
2274 case 1:
2275 tdumpline(term.c.y);
2276 break;
2277 case 2:
2278 tdumpsel();
2279 break;
2280 case 4:
2281 term.mode &= ~MODE_PRINT;
2282 break;
2283 case 5:
2284 term.mode |= MODE_PRINT;
2285 break;
2286 }
2287 break;
2288 case 'c': /* DA -- Device Attributes */
2289 if (csiescseq.arg[0] == 0)
2290 ttywrite(vtiden, sizeof(vtiden) - 1);
2291 break;
2292 case 'C': /* CUF -- Cursor <n> Forward */
2293 case 'a': /* HPR -- Cursor <n> Forward */
2294 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2295 tmoveto(term.c.x+csiescseq.arg[0], term.c.y);
2296 break;
2297 case 'D': /* CUB -- Cursor <n> Backward */
2298 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2299 tmoveto(term.c.x-csiescseq.arg[0], term.c.y);
2300 break;
2301 case 'E': /* CNL -- Cursor <n> Down and first col */
2302 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2303 tmoveto(0, term.c.y+csiescseq.arg[0]);
2304 break;
2305 case 'F': /* CPL -- Cursor <n> Up and first col */
2306 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2307 tmoveto(0, term.c.y-csiescseq.arg[0]);
2308 break;
2309 case 'g': /* TBC -- Tabulation clear */
2310 switch (csiescseq.arg[0]) {
2311 case 0: /* clear current tab stop */
2312 term.tabs[term.c.x] = 0;
2313 break;
2314 case 3: /* clear all the tabs */
2315 memset(term.tabs, 0, term.col * sizeof(*term.tabs));
2316 break;
2317 default:
2318 goto unknown;
2319 }
2320 break;
2321 case 'G': /* CHA -- Move to <col> */
2322 case '`': /* HPA */
2323 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2324 tmoveto(csiescseq.arg[0]-1, term.c.y);
2325 break;
2326 case 'H': /* CUP -- Move to <row> <col> */
2327 case 'f': /* HVP */
2328 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2329 DEFAULT(csiescseq.arg[1], 1)(csiescseq.arg[1]) = (csiescseq.arg[1]) ? (csiescseq.arg[1]) :
(1)
;
2330 tmoveato(csiescseq.arg[1]-1, csiescseq.arg[0]-1);
2331 break;
2332 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
2333 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2334 tputtab(csiescseq.arg[0]);
2335 break;
2336 case 'J': /* ED -- Clear screen */
2337 selclear(NULL((void*)0));
2338 switch (csiescseq.arg[0]) {
2339 case 0: /* below */
2340 tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
2341 if (term.c.y < term.row-1) {
2342 tclearregion(0, term.c.y+1, term.col-1,
2343 term.row-1);
2344 }
2345 break;
2346 case 1: /* above */
2347 if (term.c.y > 1)
2348 tclearregion(0, 0, term.col-1, term.c.y-1);
2349 tclearregion(0, term.c.y, term.c.x, term.c.y);
2350 break;
2351 case 2: /* all */
2352 tclearregion(0, 0, term.col-1, term.row-1);
2353 break;
2354 default:
2355 goto unknown;
2356 }
2357 break;
2358 case 'K': /* EL -- Clear line */
2359 switch (csiescseq.arg[0]) {
2360 case 0: /* right */
2361 tclearregion(term.c.x, term.c.y, term.col-1,
2362 term.c.y);
2363 break;
2364 case 1: /* left */
2365 tclearregion(0, term.c.y, term.c.x, term.c.y);
2366 break;
2367 case 2: /* all */
2368 tclearregion(0, term.c.y, term.col-1, term.c.y);
2369 break;
2370 }
2371 break;
2372 case 'S': /* SU -- Scroll <n> line up */
2373 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2374 tscrollup(term.top, csiescseq.arg[0]);
2375 break;
2376 case 'T': /* SD -- Scroll <n> line down */
2377 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2378 tscrolldown(term.top, csiescseq.arg[0]);
2379 break;
2380 case 'L': /* IL -- Insert <n> blank lines */
2381 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2382 tinsertblankline(csiescseq.arg[0]);
2383 break;
2384 case 'l': /* RM -- Reset Mode */
2385 tsetmode(csiescseq.priv, 0, csiescseq.arg, csiescseq.narg);
2386 break;
2387 case 'M': /* DL -- Delete <n> lines */
2388 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2389 tdeleteline(csiescseq.arg[0]);
2390 break;
2391 case 'X': /* ECH -- Erase <n> char */
2392 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2393 tclearregion(term.c.x, term.c.y,
2394 term.c.x + csiescseq.arg[0] - 1, term.c.y);
2395 break;
2396 case 'P': /* DCH -- Delete <n> char */
2397 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2398 tdeletechar(csiescseq.arg[0]);
2399 break;
2400 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
2401 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2402 tputtab(-csiescseq.arg[0]);
2403 break;
2404 case 'd': /* VPA -- Move to <row> */
2405 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2406 tmoveato(term.c.x, csiescseq.arg[0]-1);
2407 break;
2408 case 'h': /* SM -- Set terminal mode */
2409 tsetmode(csiescseq.priv, 1, csiescseq.arg, csiescseq.narg);
2410 break;
2411 case 'm': /* SGR -- Terminal attribute (color) */
2412 tsetattr(csiescseq.arg, csiescseq.narg);
2413 break;
2414 case 'n': /* DSR – Device Status Report (cursor position) */
2415 if (csiescseq.arg[0] == 6) {
2416 len = snprintf(buf, sizeof(buf),"\033[%i;%iR",
2417 term.c.y+1, term.c.x+1);
2418 ttywrite(buf, len);
2419 }
2420 break;
2421 case 'r': /* DECSTBM -- Set Scrolling Region */
2422 if (csiescseq.priv) {
2423 goto unknown;
2424 } else {
2425 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2426 DEFAULT(csiescseq.arg[1], term.row)(csiescseq.arg[1]) = (csiescseq.arg[1]) ? (csiescseq.arg[1]) :
(term.row)
;
2427 tsetscroll(csiescseq.arg[0]-1, csiescseq.arg[1]-1);
2428 tmoveato(0, 0);
2429 }
2430 break;
2431 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
2432 tcursor(CURSOR_SAVE);
2433 break;
2434 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
2435 tcursor(CURSOR_LOAD);
2436 break;
2437 case ' ':
2438 switch (csiescseq.mode[1]) {
2439 case 'q': /* DECSCUSR -- Set Cursor Style */
2440 DEFAULT(csiescseq.arg[0], 1)(csiescseq.arg[0]) = (csiescseq.arg[0]) ? (csiescseq.arg[0]) :
(1)
;
2441 if (!BETWEEN(csiescseq.arg[0], 0, 6)((0) <= (csiescseq.arg[0]) && (csiescseq.arg[0]) <=
(6))
) {
2442 goto unknown;
2443 }
2444 xw.cursor = csiescseq.arg[0];
2445 break;
2446 default:
2447 goto unknown;
2448 }
2449 break;
2450 }
2451}
2452
2453void
2454csidump(void)
2455{
2456 int i;
2457 uint c;
2458
2459 printf("ESC[");
2460 for (i = 0; i < csiescseq.len; i++) {
2461 c = csiescseq.buf[i] & 0xff;
2462 if (isprint(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint
)
) {
2463 putchar(c);
2464 } else if (c == '\n') {
2465 printf("(\\n)");
2466 } else if (c == '\r') {
2467 printf("(\\r)");
2468 } else if (c == 0x1b) {
2469 printf("(\\e)");
2470 } else {
2471 printf("(%02x)", c);
2472 }
2473 }
2474 putchar('\n');
2475}
2476
2477void
2478csireset(void)
2479{
2480 memset(&csiescseq, 0, sizeof(csiescseq));
2481}
2482
2483void
2484strhandle(void)
2485{
2486 char *p = NULL((void*)0);
2487 int j, narg, par;
2488
2489 term.esc &= ~(ESC_STR_END|ESC_STR);
2490 strparse();
2491 par = (narg = strescseq.narg) ? atoi(strescseq.args[0]) : 0;
2492
2493 switch (strescseq.type) {
2494 case ']': /* OSC -- Operating System Command */
2495 switch (par) {
2496 case 0:
2497 case 1:
2498 case 2:
2499 if (narg > 1)
2500 xsettitle(strescseq.args[1]);
2501 return;
2502 case 4: /* color set */
2503 if (narg < 3)
2504 break;
2505 p = strescseq.args[2];
2506 /* FALLTHROUGH */
2507 case 104: /* color reset, here p = NULL */
2508 j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
2509 if (xsetcolorname(j, p)) {
2510 fprintf(stderrstderr, "erresc: invalid color %s\n", p);
2511 } else {
2512 /*
2513 * TODO if defaultbg color is changed, borders
2514 * are dirty
2515 */
2516 redraw();
2517 }
2518 return;
2519 }
2520 break;
2521 case 'k': /* old title set compatibility */
2522 xsettitle(strescseq.args[0]);
2523 return;
2524 case 'P': /* DCS -- Device Control String */
2525 case '_': /* APC -- Application Program Command */
2526 case '^': /* PM -- Privacy Message */
2527 return;
2528 }
2529
2530 fprintf(stderrstderr, "erresc: unknown str ");
2531 strdump();
2532}
2533
2534void
2535strparse(void)
2536{
2537 int c;
2538 char *p = strescseq.buf;
2539
2540 strescseq.narg = 0;
2541 strescseq.buf[strescseq.len] = '\0';
2542
2543 if (*p == '\0')
2544 return;
2545
2546 while (strescseq.narg < STR_ARG_SIZ16) {
2547 strescseq.args[strescseq.narg++] = p;
2548 while ((c = *p) != ';' && c != '\0')
2549 ++p;
2550 if (c == '\0')
2551 return;
2552 *p++ = '\0';
2553 }
2554}
2555
2556void
2557strdump(void)
2558{
2559 int i;
2560 uint c;
2561
2562 printf("ESC%c", strescseq.type);
2563 for (i = 0; i < strescseq.len; i++) {
2564 c = strescseq.buf[i] & 0xff;
2565 if (c == '\0') {
2566 return;
2567 } else if (isprint(c)((*__ctype_b_loc ())[(int) ((c))] & (unsigned short int) _ISprint
)
) {
2568 putchar(c);
2569 } else if (c == '\n') {
2570 printf("(\\n)");
2571 } else if (c == '\r') {
2572 printf("(\\r)");
2573 } else if (c == 0x1b) {
2574 printf("(\\e)");
2575 } else {
2576 printf("(%02x)", c);
2577 }
2578 }
2579 printf("ESC\\\n");
2580}
2581
2582void
2583strreset(void)
2584{
2585 memset(&strescseq, 0, sizeof(strescseq));
2586}
2587
2588void
2589sendbreak(const Arg *arg)
2590{
2591 if (tcsendbreak(cmdfd, 0))
2592 perror("Error sending break");
2593}
2594
2595void
2596tprinter(char *s, size_t len)
2597{
2598 if (iofd != -1 && xwrite(iofd, s, len) < 0) {
2599 fprintf(stderrstderr, "Error writing in %s:%s\n",
2600 opt_io, strerror(errno(*__errno_location ())));
2601 close(iofd);
2602 iofd = -1;
2603 }
2604}
2605
2606void
2607toggleprinter(const Arg *arg)
2608{
2609 term.mode ^= MODE_PRINT;
2610}
2611
2612void
2613printscreen(const Arg *arg)
2614{
2615 tdump();
2616}
2617
2618void
2619printsel(const Arg *arg)
2620{
2621 tdumpsel();
2622}
2623
2624void
2625tdumpsel(void)
2626{
2627 char *ptr;
2628
2629 if ((ptr = getsel())) {
2630 tprinter(ptr, strlen(ptr));
2631 free(ptr);
2632 }
2633}
2634
2635void
2636tdumpline(int n)
2637{
2638 char buf[UTF_SIZ4];
2639 GlyphGlyph_ *bp, *end;
2640
2641 bp = &term.line[n][0];
2642 end = &bp[MIN(tlinelen(n), term.col)((tlinelen(n)) < (term.col) ? (tlinelen(n)) : (term.col)) - 1];
2643 if (bp != end || bp->u != ' ') {
2644 for ( ;bp <= end; ++bp)
2645 tprinter(buf, utf8encode(bp->u, buf));
2646 }
2647 tprinter("\n", 1);
2648}
2649
2650void
2651tdump(void)
2652{
2653 int i;
2654
2655 for (i = 0; i < term.row; ++i)
2656 tdumpline(i);
2657}
2658
2659void
2660tputtab(int n)
2661{
2662 uint x = term.c.x;
2663
2664 if (n > 0) {
2665 while (x < term.col && n--)
2666 for (++x; x < term.col && !term.tabs[x]; ++x)
2667 /* nothing */ ;
2668 } else if (n < 0) {
2669 while (x > 0 && n++)
2670 for (--x; x > 0 && !term.tabs[x]; --x)
2671 /* nothing */ ;
2672 }
2673 term.c.x = LIMIT(x, 0, term.col-1)(x) = (x) < (0) ? (0) : (x) > (term.col-1) ? (term.col-
1) : (x)
;
2674}
2675
2676void
2677techo(Rune u)
2678{
2679 if (ISCONTROL(u)((((0) <= (u) && (u) <= (0x1f)) || (u) == '\177'
) || (((0x80) <= (u) && (u) <= (0x9f))))
) { /* control code */
2680 if (u & 0x80) {
2681 u &= 0x7f;
2682 tputc('^');
2683 tputc('[');
2684 } else if (u != '\n' && u != '\r' && u != '\t') {
2685 u ^= 0x40;
2686 tputc('^');
2687 }
2688 }
2689 tputc(u);
2690}
2691
2692void
2693tdeftran(char ascii)
2694{
2695 static char cs[] = "0B";
2696 static int vcs[] = {CS_GRAPHIC0, CS_USA};
2697 char *p;
2698
2699 if ((p = strchr(cs, ascii)) == NULL((void*)0)) {
2700 fprintf(stderrstderr, "esc unhandled charset: ESC ( %c\n", ascii);
2701 } else {
2702 term.trantbl[term.icharset] = vcs[p - cs];
2703 }
2704}
2705
2706void
2707tdectest(char c)
2708{
2709 int x, y;
2710
2711 if (c == '8') { /* DEC screen alignment test. */
2712 for (x = 0; x < term.col; ++x) {
2713 for (y = 0; y < term.row; ++y)
2714 tsetchar('E', &term.c.attr, x, y);
2715 }
2716 }
2717}
2718
2719void
2720tstrsequence(uchar c)
2721{
2722 switch (c) {
2723 case 0x90: /* DCS -- Device Control String */
2724 c = 'P';
2725 break;
2726 case 0x9f: /* APC -- Application Program Command */
2727 c = '_';
2728 break;
2729 case 0x9e: /* PM -- Privacy Message */
2730 c = '^';
2731 break;
2732 case 0x9d: /* OSC -- Operating System Command */
2733 c = ']';
2734 break;
2735 }
2736 strreset();
2737 strescseq.type = c;
2738 term.esc |= ESC_STR;
2739}
2740
2741void
2742tcontrolcode(uchar ascii)
2743{
2744 switch (ascii) {
2745 case '\t': /* HT */
2746 tputtab(1);
2747 return;
2748 case '\b': /* BS */
2749 tmoveto(term.c.x-1, term.c.y);
2750 return;
2751 case '\r': /* CR */
2752 tmoveto(0, term.c.y);
2753 return;
2754 case '\f': /* LF */
2755 case '\v': /* VT */
2756 case '\n': /* LF */
2757 /* go to first col if the mode is set */
2758 tnewline(IS_SET(MODE_CRLF)((term.mode & (MODE_CRLF)) != 0));
2759 return;
2760 case '\a': /* BEL */
2761 if (term.esc & ESC_STR_END) {
2762 /* backwards compatibility to xterm */
2763 strhandle();
2764 } else {
2765 if (!(xw.state & WIN_FOCUSED))
2766 xseturgency(1);
2767 if (bellvolume)
2768 XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL((void*)0));
2769 }
2770 break;
2771 case '\033': /* ESC */
2772 csireset();
2773 term.esc &= ~(ESC_CSI|ESC_ALTCHARSET|ESC_TEST);
2774 term.esc |= ESC_START;
2775 return;
2776 case '\016': /* SO (LS1 -- Locking shift 1) */
2777 case '\017': /* SI (LS0 -- Locking shift 0) */
2778 term.charset = 1 - (ascii - '\016');
2779 return;
2780 case '\032': /* SUB */
2781 tsetchar('?', &term.c.attr, term.c.x, term.c.y);
2782 case '\030': /* CAN */
2783 csireset();
2784 break;
2785 case '\005': /* ENQ (IGNORED) */
2786 case '\000': /* NUL (IGNORED) */
2787 case '\021': /* XON (IGNORED) */
2788 case '\023': /* XOFF (IGNORED) */
2789 case 0177: /* DEL (IGNORED) */
2790 return;
2791 case 0x80: /* TODO: PAD */
2792 case 0x81: /* TODO: HOP */
2793 case 0x82: /* TODO: BPH */
2794 case 0x83: /* TODO: NBH */
2795 case 0x84: /* TODO: IND */
2796 break;
2797 case 0x85: /* NEL -- Next line */
2798 tnewline(1); /* always go to first col */
2799 break;
2800 case 0x86: /* TODO: SSA */
2801 case 0x87: /* TODO: ESA */
2802 break;
2803 case 0x88: /* HTS -- Horizontal tab stop */
2804 term.tabs[term.c.x] = 1;
2805 break;
2806 case 0x89: /* TODO: HTJ */
2807 case 0x8a: /* TODO: VTS */
2808 case 0x8b: /* TODO: PLD */
2809 case 0x8c: /* TODO: PLU */
2810 case 0x8d: /* TODO: RI */
2811 case 0x8e: /* TODO: SS2 */
2812 case 0x8f: /* TODO: SS3 */
2813 case 0x91: /* TODO: PU1 */
2814 case 0x92: /* TODO: PU2 */
2815 case 0x93: /* TODO: STS */
2816 case 0x94: /* TODO: CCH */
2817 case 0x95: /* TODO: MW */
2818 case 0x96: /* TODO: SPA */
2819 case 0x97: /* TODO: EPA */
2820 case 0x98: /* TODO: SOS */
2821 case 0x99: /* TODO: SGCI */
2822 break;
2823 case 0x9a: /* DECID -- Identify Terminal */
2824 ttywrite(vtiden, sizeof(vtiden) - 1);
2825 break;
2826 case 0x9b: /* TODO: CSI */
2827 case 0x9c: /* TODO: ST */
2828 break;
2829 case 0x90: /* DCS -- Device Control String */
2830 case 0x9d: /* OSC -- Operating System Command */
2831 case 0x9e: /* PM -- Privacy Message */
2832 case 0x9f: /* APC -- Application Program Command */
2833 tstrsequence(ascii);
2834 return;
2835 }
2836 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2837 term.esc &= ~(ESC_STR_END|ESC_STR);
2838}
2839
2840/*
2841 * returns 1 when the sequence is finished and it hasn't to read
2842 * more characters for this sequence, otherwise 0
2843 */
2844int
2845eschandle(uchar ascii)
2846{
2847 switch (ascii) {
2848 case '[':
2849 term.esc |= ESC_CSI;
2850 return 0;
2851 case '#':
2852 term.esc |= ESC_TEST;
2853 return 0;
2854 case 'P': /* DCS -- Device Control String */
2855 case '_': /* APC -- Application Program Command */
2856 case '^': /* PM -- Privacy Message */
2857 case ']': /* OSC -- Operating System Command */
2858 case 'k': /* old title set compatibility */
2859 tstrsequence(ascii);
2860 return 0;
2861 case 'n': /* LS2 -- Locking shift 2 */
2862 case 'o': /* LS3 -- Locking shift 3 */
2863 term.charset = 2 + (ascii - 'n');
2864 break;
2865 case '(': /* GZD4 -- set primary charset G0 */
2866 case ')': /* G1D4 -- set secondary charset G1 */
2867 case '*': /* G2D4 -- set tertiary charset G2 */
2868 case '+': /* G3D4 -- set quaternary charset G3 */
2869 term.icharset = ascii - '(';
2870 term.esc |= ESC_ALTCHARSET;
2871 return 0;
2872 case 'D': /* IND -- Linefeed */
2873 if (term.c.y == term.bot) {
2874 tscrollup(term.top, 1);
2875 } else {
2876 tmoveto(term.c.x, term.c.y+1);
2877 }
2878 break;
2879 case 'E': /* NEL -- Next line */
2880 tnewline(1); /* always go to first col */
2881 break;
2882 case 'H': /* HTS -- Horizontal tab stop */
2883 term.tabs[term.c.x] = 1;
2884 break;
2885 case 'M': /* RI -- Reverse index */
2886 if (term.c.y == term.top) {
2887 tscrolldown(term.top, 1);
2888 } else {
2889 tmoveto(term.c.x, term.c.y-1);
2890 }
2891 break;
2892 case 'Z': /* DECID -- Identify Terminal */
2893 ttywrite(vtiden, sizeof(vtiden) - 1);
2894 break;
2895 case 'c': /* RIS -- Reset to inital state */
2896 treset();
2897 xresettitle();
2898 xloadcols();
2899 break;
2900 case '=': /* DECPAM -- Application keypad */
2901 term.mode |= MODE_APPKEYPAD;
2902 break;
2903 case '>': /* DECPNM -- Normal keypad */
2904 term.mode &= ~MODE_APPKEYPAD;
2905 break;
2906 case '7': /* DECSC -- Save Cursor */
2907 tcursor(CURSOR_SAVE);
2908 break;
2909 case '8': /* DECRC -- Restore Cursor */
2910 tcursor(CURSOR_LOAD);
2911 break;
2912 case '\\': /* ST -- String Terminator */
2913 if (term.esc & ESC_STR_END)
2914 strhandle();
2915 break;
2916 default:
2917 fprintf(stderrstderr, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2918 (uchar) ascii, isprint(ascii)((*__ctype_b_loc ())[(int) ((ascii))] & (unsigned short int
) _ISprint)
? ascii:'.');
2919 break;
2920 }
2921 return 1;
2922}
2923
2924void
2925tputc(Rune u)
2926{
2927 char c[UTF_SIZ4];
2928 int control;
2929 int width, len;
2930 GlyphGlyph_ *gp;
2931
2932 control = ISCONTROL(u)((((0) <= (u) && (u) <= (0x1f)) || (u) == '\177'
) || (((0x80) <= (u) && (u) <= (0x9f))))
;
2933 len = utf8encode(u, c);
2934 if (!control && (width = wcwidth(u)) == -1) {
2935 memcpy(c, "\357\277\275", 4); /* UTF_INVALID */
2936 width = 1;
2937 }
2938
2939 if (IS_SET(MODE_PRINT)((term.mode & (MODE_PRINT)) != 0))
2940 tprinter(c, len);
2941
2942 /*
2943 * STR sequence must be checked before anything else
2944 * because it uses all following characters until it
2945 * receives a ESC, a SUB, a ST or any other C1 control
2946 * character.
2947 */
2948 if (term.esc & ESC_STR) {
2949 if (u == '\a' || u == 030 || u == 032 || u == 033 ||
2950 ISCONTROLC1(u)(((0x80) <= (u) && (u) <= (0x9f)))) {
2951 term.esc &= ~(ESC_START|ESC_STR);
2952 term.esc |= ESC_STR_END;
2953 } else if (strescseq.len + len < sizeof(strescseq.buf) - 1) {
2954 memmove(&strescseq.buf[strescseq.len], c, len);
2955 strescseq.len += len;
2956 return;
2957 } else {
2958 /*
2959 * Here is a bug in terminals. If the user never sends
2960 * some code to stop the str or esc command, then st
2961 * will stop responding. But this is better than
2962 * silently failing with unknown characters. At least
2963 * then users will report back.
2964 *
2965 * In the case users ever get fixed, here is the code:
2966 */
2967 /*
2968 * term.esc = 0;
2969 * strhandle();
2970 */
2971 return;
2972 }
2973 }
2974
2975 /*
2976 * Actions of control codes must be performed as soon they arrive
2977 * because they can be embedded inside a control sequence, and
2978 * they must not cause conflicts with sequences.
2979 */
2980 if (control) {
2981 tcontrolcode(u);
2982 /*
2983 * control codes are not shown ever
2984 */
2985 return;
2986 } else if (term.esc & ESC_START) {
2987 if (term.esc & ESC_CSI) {
2988 csiescseq.buf[csiescseq.len++] = u;
2989 if (BETWEEN(u, 0x40, 0x7E)((0x40) <= (u) && (u) <= (0x7E))
2990 || csiescseq.len >= \
2991 sizeof(csiescseq.buf)-1) {
2992 term.esc = 0;
2993 csiparse();
2994 csihandle();
2995 }
2996 return;
2997 } else if (term.esc & ESC_ALTCHARSET) {
2998 tdeftran(u);
2999 } else if (term.esc & ESC_TEST) {
3000 tdectest(u);
3001 } else {
3002 if (!eschandle(u))
3003 return;
3004 /* sequence already finished */
3005 }
3006 term.esc = 0;
3007 /*
3008 * All characters which form part of a sequence are not
3009 * printed
3010 */
3011 return;
3012 }
3013 if (sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y)((sel.ob.y) <= (term.c.y) && (term.c.y) <= (sel
.oe.y))
)
3014 selclear(NULL((void*)0));
3015
3016 gp = &term.line[term.c.y][term.c.x];
3017 if (IS_SET(MODE_WRAP)((term.mode & (MODE_WRAP)) != 0) && (term.c.state & CURSOR_WRAPNEXT)) {
3018 gp->mode |= ATTR_WRAP;
3019 tnewline(1);
3020 gp = &term.line[term.c.y][term.c.x];
3021 }
3022
3023 if (IS_SET(MODE_INSERT)((term.mode & (MODE_INSERT)) != 0) && term.c.x+width < term.col)
3024 memmove(gp+width, gp, (term.col - term.c.x - width) * sizeof(GlyphGlyph_));
3025
3026 if (term.c.x+width > term.col) {
3027 tnewline(1);
3028 gp = &term.line[term.c.y][term.c.x];
3029 }
3030
3031 tsetchar(u, &term.c.attr, term.c.x, term.c.y);
3032
3033 if (width == 2) {
3034 gp->mode |= ATTR_WIDE;
3035 if (term.c.x+1 < term.col) {
3036 gp[1].u = '\0';
3037 gp[1].mode = ATTR_WDUMMY;
3038 }
3039 }
3040 if (term.c.x+width < term.col) {
3041 tmoveto(term.c.x+width, term.c.y);
3042 } else {
3043 term.c.state |= CURSOR_WRAPNEXT;
3044 }
3045}
3046
3047void
3048tresize(int col, int row)
3049{
3050 int i;
3051 int minrow = MIN(row, term.row)((row) < (term.row) ? (row) : (term.row));
3052 int mincol = MIN(col, term.col)((col) < (term.col) ? (col) : (term.col));
3053 int *bp;
3054 TCursor c;
3055
3056 if (col < 1 || row < 1) {
3057 fprintf(stderrstderr,
3058 "tresize: error resizing to %dx%d\n", col, row);
3059 return;
3060 }
3061
3062 /*
3063 * slide screen to keep cursor where we expect it -
3064 * tscrollup would work here, but we can optimize to
3065 * memmove because we're freeing the earlier lines
3066 */
3067 for (i = 0; i <= term.c.y - row; i++) {
3068 free(term.line[i]);
3069 free(term.alt[i]);
3070 }
3071 /* ensure that both src and dst are not NULL */
3072 if (i > 0) {
3073 memmove(term.line, term.line + i, row * sizeof(Line));
3074 memmove(term.alt, term.alt + i, row * sizeof(Line));
3075 }
3076 for (i += row; i < term.row; i++) {
3077 free(term.line[i]);
3078 free(term.alt[i]);
3079 }
3080
3081 /* resize to new width */
3082 term.specbuf = xrealloc(term.specbuf, col * sizeof(XftGlyphFontSpec));
3083
3084 /* resize to new height */
3085 term.line = xrealloc(term.line, row * sizeof(Line));
3086 term.alt = xrealloc(term.alt, row * sizeof(Line));
3087 term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
3088 term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
3089
3090 /* resize each row to new width, zero-pad if needed */
3091 for (i = 0; i < minrow; i++) {
3092 term.line[i] = xrealloc(term.line[i], col * sizeof(GlyphGlyph_));
3093 term.alt[i] = xrealloc(term.alt[i], col * sizeof(GlyphGlyph_));
3094 }
3095
3096 /* allocate any new rows */
3097 for (/* i == minrow */; i < row; i++) {
3098 term.line[i] = xmalloc(col * sizeof(GlyphGlyph_));
3099 term.alt[i] = xmalloc(col * sizeof(GlyphGlyph_));
3100 }
3101 if (col > term.col) {
3102 bp = term.tabs + term.col;
3103
3104 memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
3105 while (--bp > term.tabs && !*bp)
3106 /* nothing */ ;
3107 for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
3108 *bp = 1;
3109 }
3110 /* update terminal size */
3111 term.col = col;
3112 term.row = row;
3113 /* reset scrolling region */
3114 tsetscroll(0, row-1);
3115 /* make use of the LIMIT in tmoveto */
3116 tmoveto(term.c.x, term.c.y);
3117 /* Clearing both screens (it makes dirty all lines) */
3118 c = term.c;
3119 for (i = 0; i < 2; i++) {
3120 if (mincol < col && 0 < minrow) {
3121 tclearregion(mincol, 0, col - 1, minrow - 1);
3122 }
3123 if (0 < col && minrow < row) {
3124 tclearregion(0, minrow, col - 1, row - 1);
3125 }
3126 tswapscreen();
3127 tcursor(CURSOR_LOAD);
3128 }
3129 term.c = c;
3130}
3131
3132void
3133xresize(int col, int row)
3134{
3135 xw.tw = MAX(1, col * xw.cw)((1) < (col * xw.cw) ? (col * xw.cw) : (1));
3136 xw.th = MAX(1, row * xw.ch)((1) < (row * xw.ch) ? (row * xw.ch) : (1));
3137
3138 XFreePixmap(xw.dpy, xw.buf);
3139 xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
3140 DefaultDepth(xw.dpy, xw.scr)((&((_XPrivDisplay)xw.dpy)->screens[xw.scr])->root_depth
)
);
3141 XftDrawChange(xw.draw, xw.buf);
3142 xclear(0, 0, xw.w, xw.h);
3143}
3144
3145ushort
3146sixd_to_16bit(int x)
3147{
3148 return x == 0 ? 0 : 0x3737 + 0x2828 * x;
3149}
3150
3151int
3152xloadcolor(int i, const char *name, Color *ncolor)
3153{
3154 XRenderColor color = { .alpha = 0xffff };
3155
3156 if (!name) {
3157 if (BETWEEN(i, 16, 255)((16) <= (i) && (i) <= (255))) { /* 256 color */
3158 if (i < 6*6*6+16) { /* same colors as xterm */
3159 color.red = sixd_to_16bit( ((i-16)/36)%6 );
3160 color.green = sixd_to_16bit( ((i-16)/6) %6 );
3161 color.blue = sixd_to_16bit( ((i-16)/1) %6 );
3162 } else { /* greyscale */
3163 color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
3164 color.green = color.blue = color.red;
3165 }
3166 return XftColorAllocValue(xw.dpy, xw.vis,
3167 xw.cmap, &color, ncolor);
3168 } else
3169 name = colorname[i];
3170 }
3171
3172 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
3173}
3174
3175void
3176xloadcols(void)
3177{
3178 int i;
3179 static int loaded;
3180 Color *cp;
3181
3182 if (loaded) {
3183 for (cp = dc.col; cp < &dc.col[LEN(dc.col)(sizeof(dc.col) / sizeof(dc.col)[0])]; ++cp)
3184 XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
3185 }
3186
3187 for (i = 0; i < LEN(dc.col)(sizeof(dc.col) / sizeof(dc.col)[0]); i++)
3188 if (!xloadcolor(i, NULL((void*)0), &dc.col[i])) {
3189 if (colorname[i])
3190 die("Could not allocate color '%s'\n", colorname[i]);
3191 else
3192 die("Could not allocate color %d\n", i);
3193 }
3194 loaded = 1;
3195}
3196
3197int
3198xsetcolorname(int x, const char *name)
3199{
3200 Color ncolor;
3201
3202 if (!BETWEEN(x, 0, LEN(dc.col))((0) <= (x) && (x) <= ((sizeof(dc.col) / sizeof
(dc.col)[0])))
)
3203 return 1;
3204
3205
3206 if (!xloadcolor(x, name, &ncolor))
3207 return 1;
3208
3209 XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
3210 dc.col[x] = ncolor;
3211
3212 return 0;
3213}
3214
3215/*
3216 * Absolute coordinates.
3217 */
3218void
3219xclear(int x1, int y1, int x2, int y2)
3220{
3221 XftDrawRect(xw.draw,
3222 &dc.col[IS_SET(MODE_REVERSE)((term.mode & (MODE_REVERSE)) != 0)? defaultfg : defaultbg],
3223 x1, y1, x2-x1, y2-y1);
3224}
3225
3226void
3227xhints(void)
3228{
3229 XClassHint class = {opt_name ? opt_name : termname,
3230 opt_class ? opt_class : termname};
3231 XWMHints wm = {.flags = InputHint(1L << 0), .input = 1};
3232 XSizeHints *sizeh = NULL((void*)0);
3233
3234 sizeh = XAllocSizeHints();
3235
3236 sizeh->flags = PSize(1L << 3) | PResizeInc(1L << 6) | PBaseSize(1L << 8);
3237 sizeh->height = xw.h;
3238 sizeh->width = xw.w;
3239 sizeh->height_inc = xw.ch;
3240 sizeh->width_inc = xw.cw;
3241 sizeh->base_height = 2 * borderpx;
3242 sizeh->base_width = 2 * borderpx;
3243 if (xw.isfixed) {
3244 sizeh->flags |= PMaxSize(1L << 5) | PMinSize(1L << 4);
3245 sizeh->min_width = sizeh->max_width = xw.w;
3246 sizeh->min_height = sizeh->max_height = xw.h;
3247 }
3248 if (xw.gm & (XValue0x0001|YValue0x0002)) {
3249 sizeh->flags |= USPosition(1L << 0) | PWinGravity(1L << 9);
3250 sizeh->x = xw.l;
3251 sizeh->y = xw.t;
3252 sizeh->win_gravity = xgeommasktogravity(xw.gm);
3253 }
3254
3255 XSetWMProperties(xw.dpy, xw.win, NULL((void*)0), NULL((void*)0), NULL((void*)0), 0, sizeh, &wm,
3256 &class);
3257 XFree(sizeh);
3258}
3259
3260int
3261xgeommasktogravity(int mask)
3262{
3263 switch (mask & (XNegative0x0010|YNegative0x0020)) {
3264 case 0:
3265 return NorthWestGravity1;
3266 case XNegative0x0010:
3267 return NorthEastGravity3;
3268 case YNegative0x0020:
3269 return SouthWestGravity7;
3270 }
3271
3272 return SouthEastGravity9;
3273}
3274
3275int
3276xloadfont(FontFont_ *f, FcPattern *pattern)
3277{
3278 FcPattern *match;
3279 FcResult result;
3280 XGlyphInfo extents;
3281
3282 match = XftFontMatch(xw.dpy, xw.scr, pattern, &result);
3283 if (!match)
3284 return 1;
3285
3286 if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
3287 FcPatternDestroy(match);
3288 return 1;
3289 }
3290
3291 XftTextExtentsUtf8(xw.dpy, f->match,
3292 (const FcChar8 *) ascii_printable,
3293 strlen(ascii_printable), &extents);
3294
3295 f->set = NULL((void*)0);
3296 f->pattern = FcPatternDuplicate(pattern);
3297
3298 f->ascent = f->match->ascent;
3299 f->descent = f->match->descent;
3300 f->lbearing = 0;
3301 f->rbearing = f->match->max_advance_width;
3302
3303 f->height = f->ascent + f->descent;
3304 f->width = DIVCEIL(extents.xOff, strlen(ascii_printable))(((extents.xOff) + ((strlen(ascii_printable)) - 1)) / (strlen
(ascii_printable)))
;
3305
3306 return 0;
3307}
3308
3309void
3310xloadfonts(char *fontstr, double fontsize)
3311{
3312 FcPattern *pattern;
3313 double fontval;
3314 float ceilf(float);
3315
3316 if (fontstr[0] == '-') {
3317 pattern = XftXlfdParse(fontstr, False0, False0);
3318 } else {
3319 pattern = FcNameParse((FcChar8 *)fontstr);
3320 }
3321
3322 if (!pattern)
3323 die("st: can't open font %s\n", fontstr);
3324
3325 if (fontsize > 1) {
3326 FcPatternDel(pattern, FC_PIXEL_SIZE"pixelsize");
3327 FcPatternDel(pattern, FC_SIZE"size");
3328 FcPatternAddDouble(pattern, FC_PIXEL_SIZE"pixelsize", (double)fontsize);
3329 usedfontsize = fontsize;
3330 } else {
3331 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE"pixelsize", 0, &fontval) ==
3332 FcResultMatch) {
3333 usedfontsize = fontval;
3334 } else if (FcPatternGetDouble(pattern, FC_SIZE"size", 0, &fontval) ==
3335 FcResultMatch) {
3336 usedfontsize = -1;
3337 } else {
3338 /*
3339 * Default font size is 12, if none given. This is to
3340 * have a known usedfontsize value.
3341 */
3342 FcPatternAddDouble(pattern, FC_PIXEL_SIZE"pixelsize", 12);
3343 usedfontsize = 12;
3344 }
3345 defaultfontsize = usedfontsize;
3346 }
3347
3348 if (xloadfont(&dc.font, pattern))
3349 die("st: can't open font %s\n", fontstr);
3350
3351 if (usedfontsize < 0) {
3352 FcPatternGetDouble(dc.font.match->pattern,
3353 FC_PIXEL_SIZE"pixelsize", 0, &fontval);
3354 usedfontsize = fontval;
3355 if (fontsize == 0)
3356 defaultfontsize = fontval;
3357 }
3358
3359 /* Setting character width and height. */
3360 xw.cw = ceilf(dc.font.width * cwscale);
3361 xw.ch = ceilf(dc.font.height * chscale);
3362
3363 FcPatternDel(pattern, FC_SLANT"slant");
3364 FcPatternAddInteger(pattern, FC_SLANT"slant", FC_SLANT_ITALIC100);
3365 if (xloadfont(&dc.ifont, pattern))
3366 die("st: can't open font %s\n", fontstr);
3367
3368 FcPatternDel(pattern, FC_WEIGHT"weight");
3369 FcPatternAddInteger(pattern, FC_WEIGHT"weight", FC_WEIGHT_BOLD200);
3370 if (xloadfont(&dc.ibfont, pattern))
3371 die("st: can't open font %s\n", fontstr);
3372
3373 FcPatternDel(pattern, FC_SLANT"slant");
3374 FcPatternAddInteger(pattern, FC_SLANT"slant", FC_SLANT_ROMAN0);
3375 if (xloadfont(&dc.bfont, pattern))
3376 die("st: can't open font %s\n", fontstr);
3377
3378 FcPatternDestroy(pattern);
3379}
3380
3381void
3382xunloadfont(FontFont_ *f)
3383{
3384 XftFontClose(xw.dpy, f->match);
3385 FcPatternDestroy(f->pattern);
3386 if (f->set)
3387 FcFontSetDestroy(f->set);
3388}
3389
3390void
3391xunloadfonts(void)
3392{
3393 /* Free the loaded fonts in the font cache. */
3394 while (frclen > 0)
3395 XftFontClose(xw.dpy, frc[--frclen].font);
3396
3397 xunloadfont(&dc.font);
3398 xunloadfont(&dc.bfont);
3399 xunloadfont(&dc.ifont);
3400 xunloadfont(&dc.ibfont);
3401}
3402
3403void
3404xzoom(const Arg *arg)
3405{
3406 Arg larg;
3407
3408 larg.f = usedfontsize + arg->f;
3409 xzoomabs(&larg);
3410}
3411
3412void
3413xzoomabs(const Arg *arg)
3414{
3415 xunloadfonts();
3416 xloadfonts(usedfont, arg->f);
3417 cresize(0, 0);
3418 ttyresize();
3419 redraw();
3420 xhints();
3421}
3422
3423void
3424xzoomreset(const Arg *arg)
3425{
3426 Arg larg;
3427
3428 if (defaultfontsize > 0) {
3429 larg.f = defaultfontsize;
3430 xzoomabs(&larg);
3431 }
3432}
3433
3434void
3435xinit(void)
3436{
3437 XGCValues gcvalues;
3438 Cursor cursor;
3439 Window parent;
3440 pid_t thispid = getpid();
3441 XColor xmousefg, xmousebg;
3442
3443 if (!(xw.dpy = XOpenDisplay(NULL((void*)0))))
3444 die("Can't open display\n");
3445 xw.scr = XDefaultScreen(xw.dpy);
3446 xw.vis = XDefaultVisual(xw.dpy, xw.scr);
3447
3448 /* font */
3449 if (!FcInit())
3450 die("Could not init fontconfig.\n");
3451
3452 usedfont = (opt_font == NULL((void*)0))? font : opt_font;
3453 xloadfonts(usedfont, 0);
3454
3455 /* colors */
3456 xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
3457 xloadcols();
3458
3459 /* adjust fixed window geometry */
3460 xw.w = 2 * borderpx + term.col * xw.cw;
3461 xw.h = 2 * borderpx + term.row * xw.ch;
3462 if (xw.gm & XNegative0x0010)
3463 xw.l += DisplayWidth(xw.dpy, xw.scr)((&((_XPrivDisplay)xw.dpy)->screens[xw.scr])->width
)
- xw.w - 2;
3464 if (xw.gm & YNegative0x0020)
3465 xw.t += DisplayHeight(xw.dpy, xw.scr)((&((_XPrivDisplay)xw.dpy)->screens[xw.scr])->height
)
- xw.h - 2;
3466
3467 /* Events */
3468 xw.attrs.background_pixel = dc.col[defaultbg].pixel;
3469 xw.attrs.border_pixel = dc.col[defaultbg].pixel;
3470 xw.attrs.bit_gravity = NorthWestGravity1;
3471 xw.attrs.event_mask = FocusChangeMask(1L<<21) | KeyPressMask(1L<<0)
3472 | ExposureMask(1L<<15) | VisibilityChangeMask(1L<<16) | StructureNotifyMask(1L<<17)
3473 | ButtonMotionMask(1L<<13) | ButtonPressMask(1L<<2) | ButtonReleaseMask(1L<<3);
3474 xw.attrs.colormap = xw.cmap;
3475
3476 if (!(opt_embed && (parent = strtol(opt_embed, NULL((void*)0), 0))))
3477 parent = XRootWindow(xw.dpy, xw.scr);
3478 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
3479 xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput1,
3480 xw.vis, CWBackPixel(1L<<1) | CWBorderPixel(1L<<3) | CWBitGravity(1L<<4)
3481 | CWEventMask(1L<<11) | CWColormap(1L<<13), &xw.attrs);
3482
3483 memset(&gcvalues, 0, sizeof(gcvalues));
3484 gcvalues.graphics_exposures = False0;
3485 dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures(1L<<16),
3486 &gcvalues);
3487 xw.buf = XCreatePixmap(xw.dpy, xw.win, xw.w, xw.h,
3488 DefaultDepth(xw.dpy, xw.scr)((&((_XPrivDisplay)xw.dpy)->screens[xw.scr])->root_depth
)
);
3489 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
3490 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, xw.w, xw.h);
3491
3492 /* Xft rendering context */
3493 xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
3494
3495 /* input methods */
3496 if ((xw.xim = XOpenIM(xw.dpy, NULL((void*)0), NULL((void*)0), NULL((void*)0))) == NULL((void*)0)) {
3497 XSetLocaleModifiers("@im=local");
3498 if ((xw.xim = XOpenIM(xw.dpy, NULL((void*)0), NULL((void*)0), NULL((void*)0))) == NULL((void*)0)) {
3499 XSetLocaleModifiers("@im=");
3500 if ((xw.xim = XOpenIM(xw.dpy,
3501 NULL((void*)0), NULL((void*)0), NULL((void*)0))) == NULL((void*)0)) {
3502 die("XOpenIM failed. Could not open input"
3503 " device.\n");
3504 }
3505 }
3506 }
3507 xw.xic = XCreateIC(xw.xim, XNInputStyle"inputStyle", XIMPreeditNothing0x0008L
3508 | XIMStatusNothing0x0400L, XNClientWindow"clientWindow", xw.win,
3509 XNFocusWindow"focusWindow", xw.win, NULL((void*)0));
3510 if (xw.xic == NULL((void*)0))
3511 die("XCreateIC failed. Could not obtain input method.\n");
3512
3513 /* white cursor, black outline */
3514 cursor = XCreateFontCursor(xw.dpy, mouseshape);
3515 XDefineCursor(xw.dpy, xw.win, cursor);
3516
3517 if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
3518 xmousefg.red = 0xffff;
3519 xmousefg.green = 0xffff;
3520 xmousefg.blue = 0xffff;
3521 }
3522
3523 if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
3524 xmousebg.red = 0x0000;
3525 xmousebg.green = 0x0000;
3526 xmousebg.blue = 0x0000;
3527 }
3528
3529 XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
3530
3531 xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False0);
3532 xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False0);
3533 xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False0);
3534 XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
3535
3536 xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False0);
3537 XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL((Atom) 6), 32,
3538 PropModeReplace0, (uchar *)&thispid, 1);
3539
3540 xresettitle();
3541 XMapWindow(xw.dpy, xw.win);
3542 xhints();
3543 XSync(xw.dpy, False0);
3544}
3545
3546int
3547xmakeglyphfontspecs(XftGlyphFontSpec *specs, const GlyphGlyph_ *glyphs, int len, int x, int y)
3548{
3549 float winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch, xp, yp;
3550 ushort mode, prevmode = USHRT_MAX(32767 *2 +1);
3551 FontFont_ *font = &dc.font;
3552 int frcflags = FRC_NORMAL;
3553 float runewidth = xw.cw;
3554 Rune rune;
3555 FT_UInt glyphidx;
3556 FcResult fcres;
3557 FcPattern *fcpattern, *fontpattern;
3558 FcFontSet *fcsets[] = { NULL((void*)0) };
3559 FcCharSet *fccharset;
3560 int i, f, numspecs = 0;
3561
3562 for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
3563 /* Fetch rune and mode for current glyph. */
3564 rune = glyphs[i].u;
3565 mode = glyphs[i].mode;
3566
3567 /* Skip dummy wide-character spacing. */
3568 if (mode == ATTR_WDUMMY)
3569 continue;
3570
3571 /* Determine font for glyph if different from previous glyph. */
3572 if (prevmode != mode) {
3573 prevmode = mode;
3574 font = &dc.font;
3575 frcflags = FRC_NORMAL;
3576 runewidth = xw.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
3577 if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
3578 font = &dc.ibfont;
3579 frcflags = FRC_ITALICBOLD;
3580 } else if (mode & ATTR_ITALIC) {
3581 font = &dc.ifont;
3582 frcflags = FRC_ITALIC;
3583 } else if (mode & ATTR_BOLD) {
3584 font = &dc.bfont;
3585 frcflags = FRC_BOLD;
3586 }
3587 yp = winy + font->ascent;
3588 }
3589
3590 /* Lookup character index with default font. */
3591 glyphidx = XftCharIndex(xw.dpy, font->match, rune);
3592 if (glyphidx) {
3593 specs[numspecs].font = font->match;
3594 specs[numspecs].glyph = glyphidx;
3595 specs[numspecs].x = (short)xp;
3596 specs[numspecs].y = (short)yp;
3597 xp += runewidth;
3598 numspecs++;
3599 continue;
3600 }
3601
3602 /* Fallback on font cache, search the font cache for match. */
3603 for (f = 0; f < frclen; f++) {
3604 glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
3605 /* Everything correct. */
3606 if (glyphidx && frc[f].flags == frcflags)
3607 break;
3608 /* We got a default font for a not found glyph. */
3609 if (!glyphidx && frc[f].flags == frcflags
3610 && frc[f].unicodep == rune) {
3611 break;
3612 }
3613 }
3614
3615 /* Nothing was found. Use fontconfig to find matching font. */
3616 if (f >= frclen) {
3617 if (!font->set)
3618 font->set = FcFontSort(0, font->pattern,
3619 1, 0, &fcres);
3620 fcsets[0] = font->set;
3621
3622 /*
3623 * Nothing was found in the cache. Now use
3624 * some dozen of Fontconfig calls to get the
3625 * font for one single character.
3626 *
3627 * Xft and fontconfig are design failures.
3628 */
3629 fcpattern = FcPatternDuplicate(font->pattern);
3630 fccharset = FcCharSetCreate();
3631
3632 FcCharSetAddChar(fccharset, rune);
3633 FcPatternAddCharSet(fcpattern, FC_CHARSET"charset",
3634 fccharset);
3635 FcPatternAddBool(fcpattern, FC_SCALABLE"scalable", 1);
3636
3637 FcConfigSubstitute(0, fcpattern,
3638 FcMatchPattern);
3639 FcDefaultSubstitute(fcpattern);
3640
3641 fontpattern = FcFontSetMatch(0, fcsets, 1,
3642 fcpattern, &fcres);
3643
3644 /*
3645 * Overwrite or create the new cache entry.
3646 */
3647 if (frclen >= LEN(frc)(sizeof(frc) / sizeof(frc)[0])) {
3648 frclen = LEN(frc)(sizeof(frc) / sizeof(frc)[0]) - 1;
3649 XftFontClose(xw.dpy, frc[frclen].font);
3650 frc[frclen].unicodep = 0;
3651 }
3652
3653 frc[frclen].font = XftFontOpenPattern(xw.dpy,
3654 fontpattern);
3655 frc[frclen].flags = frcflags;
3656 frc[frclen].unicodep = rune;
3657
3658 glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
3659
3660 f = frclen;
3661 frclen++;
3662
3663 FcPatternDestroy(fcpattern);
3664 FcCharSetDestroy(fccharset);
3665 }
3666
3667 specs[numspecs].font = frc[f].font;
3668 specs[numspecs].glyph = glyphidx;
3669 specs[numspecs].x = (short)xp;
3670 specs[numspecs].y = (short)yp;
3671 xp += runewidth;
3672 numspecs++;
3673 }
3674
3675 return numspecs;
3676}
3677
3678void
3679xdrawglyphfontspecs(const XftGlyphFontSpec *specs, GlyphGlyph_ base, int len, int x, int y)
3680{
3681 int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
3682 int winx = borderpx + x * xw.cw, winy = borderpx + y * xw.ch,
3683 width = charlen * xw.cw;
3684 Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
3685 XRenderColor colfg, colbg;
3686 XRectangle r;
3687
3688 /* Determine foreground and background colors based on mode. */
3689 if (base.fg == defaultfg) {
3690 if (base.mode & ATTR_ITALIC)
3691 base.fg = defaultitalic;
3692 else if ((base.mode & ATTR_ITALIC) && (base.mode & ATTR_BOLD))
3693 base.fg = defaultitalic;
3694 else if (base.mode & ATTR_UNDERLINE)
3695 base.fg = defaultunderline;
3696 }
3697
3698 if (IS_TRUECOL(base.fg)(1 << 24 & (base.fg))) {
3699 colfg.alpha = 0xffff;
3700 colfg.red = TRUERED(base.fg)(((base.fg) & 0xff0000) >> 8);
3701 colfg.green = TRUEGREEN(base.fg)(((base.fg) & 0xff00));
3702 colfg.blue = TRUEBLUE(base.fg)(((base.fg) & 0xff) << 8);
3703 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
3704 fg = &truefg;
3705 } else {
3706 fg = &dc.col[base.fg];
3707 }
3708
3709 if (IS_TRUECOL(base.bg)(1 << 24 & (base.bg))) {
3710 colbg.alpha = 0xffff;
3711 colbg.green = TRUEGREEN(base.bg)(((base.bg) & 0xff00));
3712 colbg.red = TRUERED(base.bg)(((base.bg) & 0xff0000) >> 8);
3713 colbg.blue = TRUEBLUE(base.bg)(((base.bg) & 0xff) << 8);
3714 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
3715 bg = &truebg;
3716 } else {
3717 bg = &dc.col[base.bg];
3718 }
3719
3720 /* Change basic system colors [0-7] to bright system colors [8-15] */
3721 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7)((0) <= (base.fg) && (base.fg) <= (7)))
3722 fg = &dc.col[base.fg + 8];
3723
3724 if (IS_SET(MODE_REVERSE)((term.mode & (MODE_REVERSE)) != 0)) {
3725 if (fg == &dc.col[defaultfg]) {
3726 fg = &dc.col[defaultbg];
3727 } else {
3728 colfg.red = ~fg->color.red;
3729 colfg.green = ~fg->color.green;
3730 colfg.blue = ~fg->color.blue;
3731 colfg.alpha = fg->color.alpha;
3732 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
3733 &revfg);
3734 fg = &revfg;
3735 }
3736
3737 if (bg == &dc.col[defaultbg]) {
3738 bg = &dc.col[defaultfg];
3739 } else {
3740 colbg.red = ~bg->color.red;
3741 colbg.green = ~bg->color.green;
3742 colbg.blue = ~bg->color.blue;
3743 colbg.alpha = bg->color.alpha;
3744 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
3745 &revbg);
3746 bg = &revbg;
3747 }
3748 }
3749
3750 if (base.mode & ATTR_REVERSE) {
3751 temp = fg;
3752 fg = bg;
3753 bg = temp;
3754 }
3755
3756 if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
3757 colfg.red = fg->color.red / 2;
3758 colfg.green = fg->color.green / 2;
3759 colfg.blue = fg->color.blue / 2;
3760 XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
3761 fg = &revfg;
3762 }
3763
3764 if (base.mode & ATTR_BLINK && term.mode & MODE_BLINK)
3765 fg = bg;
3766
3767 if (base.mode & ATTR_INVISIBLE)
3768 fg = bg;
3769
3770 /* Intelligent cleaning up of the borders. */
3771 if (x == 0) {
3772 xclear(0, (y == 0)? 0 : winy, borderpx,
3773 winy + xw.ch + ((y >= term.row-1)? xw.h : 0));
3774 }
3775 if (x + charlen >= term.col) {
3776 xclear(winx + width, (y == 0)? 0 : winy, xw.w,
3777 ((y >= term.row-1)? xw.h : (winy + xw.ch)));
3778 }
3779 if (y == 0)
3780 xclear(winx, 0, winx + width, borderpx);
3781 if (y == term.row-1)
3782 xclear(winx, winy + xw.ch, winx + width, xw.h);
3783
3784 /* Clean up the region we want to draw to. */
3785 XftDrawRect(xw.draw, bg, winx, winy, width, xw.ch);
3786
3787 /* Set the clip region because Xft is sometimes dirty. */
3788 r.x = 0;
3789 r.y = 0;
3790 r.height = xw.ch;
3791 r.width = width;
3792 XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
3793
3794 /* Render the glyphs. */
3795 XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
3796
3797 /* Render underline and strikethrough. */
3798 if (base.mode & ATTR_UNDERLINE) {
3799 XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
3800 width, 1);
3801 }
3802
3803 if (base.mode & ATTR_STRUCK) {
3804 XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
3805 width, 1);
3806 }
3807
3808 /* Reset clip to none. */
3809 XftDrawSetClip(xw.draw, 0);
3810}
3811
3812void
3813xdrawglyph(GlyphGlyph_ g, int x, int y)
3814{
3815 int numspecs;
3816 XftGlyphFontSpec spec;
3817
3818 numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
3819 xdrawglyphfontspecs(&spec, g, numspecs, x, y);
3820}
3821
3822void
3823xdrawcursor(void)
3824{
3825 static int oldx = 0, oldy = 0;
3826 int curx;
3827 GlyphGlyph_ g = {' ', ATTR_NULL, defaultbg, defaultcs}, og;
3828 int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN)((term.mode & (MODE_ALTSCREEN)) != 0);
3829 Color drawcol;
3830
3831 LIMIT(oldx, 0, term.col-1)(oldx) = (oldx) < (0) ? (0) : (oldx) > (term.col-1) ? (
term.col-1) : (oldx)
;
3832 LIMIT(oldy, 0, term.row-1)(oldy) = (oldy) < (0) ? (0) : (oldy) > (term.row-1) ? (
term.row-1) : (oldy)
;
3833
3834 curx = term.c.x;
3835
3836 /* adjust position if in dummy */
3837 if (term.line[oldy][oldx].mode & ATTR_WDUMMY)
3838 oldx--;
3839 if (term.line[term.c.y][curx].mode & ATTR_WDUMMY)
3840 curx--;
3841
3842 /* remove the old cursor */
3843 og = term.line[oldy][oldx];
3844 if (ena_sel && selected(oldx, oldy))
3845 og.mode ^= ATTR_REVERSE;
3846 xdrawglyph(og, oldx, oldy);
3847
3848 g.u = term.line[term.c.y][term.c.x].u;
3849
3850 /*
3851 * Select the right color for the right mode.
3852 */
3853 if (IS_SET(MODE_REVERSE)((term.mode & (MODE_REVERSE)) != 0)) {
3854 g.mode |= ATTR_REVERSE;
3855 g.bg = defaultfg;
3856 if (ena_sel && selected(term.c.x, term.c.y)) {
3857 drawcol = dc.col[defaultcs];
3858 g.fg = defaultrcs;
3859 } else {
3860 drawcol = dc.col[defaultrcs];
3861 g.fg = defaultcs;
3862 }
3863 } else {
3864 if (ena_sel && selected(term.c.x, term.c.y)) {
3865 drawcol = dc.col[defaultrcs];
3866 g.fg = defaultfg;
3867 g.bg = defaultrcs;
3868 } else {
3869 drawcol = dc.col[defaultcs];
3870 }
3871 }
3872
3873 if (IS_SET(MODE_HIDE)((term.mode & (MODE_HIDE)) != 0))
3874 return;
3875
3876 /* draw the new one */
3877 if (xw.state & WIN_FOCUSED) {
3878 switch (xw.cursor) {
3879 case 7: /* st extension: snowman */
3880 utf8decode("☃", &g.u, UTF_SIZ4);
3881 case 0: /* Blinking Block */
3882 case 1: /* Blinking Block (Default) */
3883 case 2: /* Steady Block */
3884 g.mode |= term.line[term.c.y][curx].mode & ATTR_WIDE;
3885 xdrawglyph(g, term.c.x, term.c.y);
3886 break;
3887 case 3: /* Blinking Underline */
3888 case 4: /* Steady Underline */
3889 XftDrawRect(xw.draw, &drawcol,
3890 borderpx + curx * xw.cw,
3891 borderpx + (term.c.y + 1) * xw.ch - \
3892 cursorthickness,
3893 xw.cw, cursorthickness);
3894 break;
3895 case 5: /* Blinking bar */
3896 case 6: /* Steady bar */
3897 XftDrawRect(xw.draw, &drawcol,
3898 borderpx + curx * xw.cw,
3899 borderpx + term.c.y * xw.ch,
3900 cursorthickness, xw.ch);
3901 break;
3902 }
3903 } else {
3904 XftDrawRect(xw.draw, &drawcol,
3905 borderpx + curx * xw.cw,
3906 borderpx + term.c.y * xw.ch,
3907 xw.cw - 1, 1);
3908 XftDrawRect(xw.draw, &drawcol,
3909 borderpx + curx * xw.cw,
3910 borderpx + term.c.y * xw.ch,
3911 1, xw.ch - 1);
3912 XftDrawRect(xw.draw, &drawcol,
3913 borderpx + (curx + 1) * xw.cw - 1,
3914 borderpx + term.c.y * xw.ch,
3915 1, xw.ch - 1);
3916 XftDrawRect(xw.draw, &drawcol,
3917 borderpx + curx * xw.cw,
3918 borderpx + (term.c.y + 1) * xw.ch - 1,
3919 xw.cw, 1);
3920 }
3921 oldx = curx, oldy = term.c.y;
3922}
3923
3924
3925void
3926xsettitle(char *p)
3927{
3928 XTextProperty prop;
3929
3930 Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
3931 &prop);
3932 XSetWMName(xw.dpy, xw.win, &prop);
3933 XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
3934 XFree(prop.value);
3935}
3936
3937void
3938xresettitle(void)
3939{
3940 xsettitle(opt_title ? opt_title : "st");
3941}
3942
3943void
3944redraw(void)
3945{
3946 tfulldirt();
3947 draw();
3948}
3949
3950void
3951draw(void)
3952{
3953 drawregion(0, 0, term.col, term.row);
3954 XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, xw.w,
3955 xw.h, 0, 0);
3956 XSetForeground(xw.dpy, dc.gc,
3957 dc.col[IS_SET(MODE_REVERSE)((term.mode & (MODE_REVERSE)) != 0)?
3958 defaultfg : defaultbg].pixel);
3959}
3960
3961void
3962drawregion(int x1, int y1, int x2, int y2)
3963{
3964 int i, x, y, ox, numspecs;
3965 GlyphGlyph_ base, new;
3966 XftGlyphFontSpec *specs;
3967 int ena_sel = sel.ob.x != -1 && sel.alt == IS_SET(MODE_ALTSCREEN)((term.mode & (MODE_ALTSCREEN)) != 0);
3968
3969 if (!(xw.state & WIN_VISIBLE))
3970 return;
3971
3972 for (y = y1; y < y2; y++) {
3973 if (!term.dirty[y])
3974 continue;
3975
3976 term.dirty[y] = 0;
3977
3978 specs = term.specbuf;
3979 numspecs = xmakeglyphfontspecs(specs, &term.line[y][x1], x2 - x1, x1, y);
3980
3981 i = ox = 0;
3982 for (x = x1; x < x2 && i < numspecs; x++) {
3983 new = term.line[y][x];
3984 if (new.mode == ATTR_WDUMMY)
3985 continue;
3986 if (ena_sel && selected(x, y))
3987 new.mode ^= ATTR_REVERSE;
3988 if (i > 0 && ATTRCMP(base, new)((base).mode != (new).mode || (base).fg != (new).fg || (base)
.bg != (new).bg)
) {
3989 xdrawglyphfontspecs(specs, base, i, ox, y);
3990 specs += i;
3991 numspecs -= i;
3992 i = 0;
3993 }
3994 if (i == 0) {
3995 ox = x;
3996 base = new;
3997 }
3998 i++;
3999 }
4000 if (i > 0)
4001 xdrawglyphfontspecs(specs, base, i, ox, y);
4002 }
4003 xdrawcursor();
4004}
4005
4006void
4007expose(XEvent *ev)
4008{
4009 redraw();
4010}
4011
4012void
4013visibility(XEvent *ev)
4014{
4015 XVisibilityEvent *e = &ev->xvisibility;
4016
4017 MODBIT(xw.state, e->state != VisibilityFullyObscured, WIN_VISIBLE)((e->state != 2) ? ((xw.state) |= (WIN_VISIBLE)) : ((xw.state
) &= ~(WIN_VISIBLE)))
;
4018}
4019
4020void
4021unmap(XEvent *ev)
4022{
4023 xw.state &= ~WIN_VISIBLE;
4024}
4025
4026void
4027xsetpointermotion(int set)
4028{
4029 MODBIT(xw.attrs.event_mask, set, PointerMotionMask)((set) ? ((xw.attrs.event_mask) |= ((1L<<6))) : ((xw.attrs
.event_mask) &= ~((1L<<6))))
;
4030 XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask(1L<<11), &xw.attrs);
4031}
4032
4033void
4034xseturgency(int add)
4035{
4036 XWMHints *h = XGetWMHints(xw.dpy, xw.win);
4037
4038 MODBIT(h->flags, add, XUrgencyHint)((add) ? ((h->flags) |= ((1L << 8))) : ((h->flags
) &= ~((1L << 8))))
;
4039 XSetWMHints(xw.dpy, xw.win, h);
4040 XFree(h);
4041}
4042
4043void
4044focus(XEvent *ev)
4045{
4046 XFocusChangeEvent *e = &ev->xfocus;
4047
4048 if (e->mode == NotifyGrab1)
4049 return;
4050
4051 if (ev->type == FocusIn9) {
4052 XSetICFocus(xw.xic);
4053 xw.state |= WIN_FOCUSED;
4054 xseturgency(0);
4055 if (IS_SET(MODE_FOCUS)((term.mode & (MODE_FOCUS)) != 0))
4056 ttywrite("\033[I", 3);
4057 } else {
4058 XUnsetICFocus(xw.xic);
4059 xw.state &= ~WIN_FOCUSED;
4060 if (IS_SET(MODE_FOCUS)((term.mode & (MODE_FOCUS)) != 0))
4061 ttywrite("\033[O", 3);
4062 }
4063}
4064
4065int
4066match(uint mask, uint state)
4067{
4068 return mask == XK_ANY_MOD(2147483647 *2U +1U) || mask == (state & ~ignoremod);
4069}
4070
4071void
4072numlock(const Arg *dummy)
4073{
4074 term.numlock ^= 1;
4075}
4076
4077char*
4078kmap(KeySym k, uint state)
4079{
4080 Key *kp;
4081 int i;
4082
4083 /* Check for mapped keys out of X11 function keys. */
4084 for (i = 0; i < LEN(mappedkeys)(sizeof(mappedkeys) / sizeof(mappedkeys)[0]); i++) {
4085 if (mappedkeys[i] == k)
4086 break;
4087 }
4088 if (i == LEN(mappedkeys)(sizeof(mappedkeys) / sizeof(mappedkeys)[0])) {
4089 if ((k & 0xFFFF) < 0xFD00)
4090 return NULL((void*)0);
4091 }
4092
4093 for (kp = key; kp < key + LEN(key)(sizeof(key) / sizeof(key)[0]); kp++) {
4094 if (kp->k != k)
4095 continue;
4096
4097 if (!match(kp->mask, state))
4098 continue;
4099
4100 if (IS_SET(MODE_APPKEYPAD)((term.mode & (MODE_APPKEYPAD)) != 0) ? kp->appkey < 0 : kp->appkey > 0)
4101 continue;
4102 if (term.numlock && kp->appkey == 2)
4103 continue;
4104
4105 if (IS_SET(MODE_APPCURSOR)((term.mode & (MODE_APPCURSOR)) != 0) ? kp->appcursor < 0 : kp->appcursor > 0)
4106 continue;
4107
4108 if (IS_SET(MODE_CRLF)((term.mode & (MODE_CRLF)) != 0) ? kp->crlf < 0 : kp->crlf > 0)
4109 continue;
4110
4111 return kp->s;
4112 }
4113
4114 return NULL((void*)0);
4115}
4116
4117void
4118kpress(XEvent *ev)
4119{
4120 XKeyEvent *e = &ev->xkey;
4121 KeySym ksym;
4122 char buf[32], *customkey;
4123 int len;
4124 Rune c;
4125 Statusint status;
4126 Shortcut *bp;
4127
4128 if (IS_SET(MODE_KBDLOCK)((term.mode & (MODE_KBDLOCK)) != 0))
4129 return;
4130
4131 len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
4132 /* 1. shortcuts */
4133 for (bp = shortcuts; bp < shortcuts + LEN(shortcuts)(sizeof(shortcuts) / sizeof(shortcuts)[0]); bp++) {
4134 if (ksym == bp->keysym && match(bp->mod, e->state)) {
4135 bp->func(&(bp->arg));
4136 return;
4137 }
4138 }
4139
4140 /* 2. custom keys from config.h */
4141 if ((customkey = kmap(ksym, e->state))) {
4142 ttysend(customkey, strlen(customkey));
4143 return;
4144 }
4145
4146 /* 3. composed string from input method */
4147 if (len == 0)
4148 return;
4149 if (len == 1 && e->state & Mod1Mask(1<<3)) {
4150 if (IS_SET(MODE_8BIT)((term.mode & (MODE_8BIT)) != 0)) {
4151 if (*buf < 0177) {
4152 c = *buf | 0x80;
4153 len = utf8encode(c, buf);
4154 }
4155 } else {
4156 buf[1] = buf[0];
4157 buf[0] = '\033';
4158 len = 2;
4159 }
4160 }
4161 ttysend(buf, len);
4162}
4163
4164
4165void
4166cmessage(XEvent *e)
4167{
4168 /*
4169 * See xembed specs
4170 * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
4171 */
4172 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
4173 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN4) {
4174 xw.state |= WIN_FOCUSED;
4175 xseturgency(0);
4176 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT5) {
4177 xw.state &= ~WIN_FOCUSED;
4178 }
4179 } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
4180 /* Send SIGHUP to shell */
4181 kill(pid, SIGHUP1);
4182 exit(0);
4183 }
4184}
4185
4186void
4187cresize(int width, int height)
4188{
4189 int col, row;
4190
4191 if (width != 0)
5
Assuming 'width' is equal to 0
6
Taking false branch
4192 xw.w = width;
4193 if (height != 0)
7
Assuming 'height' is equal to 0
8
Taking false branch
4194 xw.h = height;
4195
4196 col = (xw.w - 2 * borderpx) / xw.cw;
4197 row = (xw.h - 2 * borderpx) / xw.ch;
4198
4199 tresize(col, row);
9
Value assigned to 'actionfps'
4200 xresize(col, row);
4201}
4202
4203void
4204resize(XEvent *e)
4205{
4206 if (e->xconfigure.width == xw.w && e->xconfigure.height == xw.h)
4207 return;
4208
4209 cresize(e->xconfigure.width, e->xconfigure.height);
4210 ttyresize();
4211}
4212
4213void
4214run(void)
4215{
4216 XEvent ev;
4217 int w = xw.w, h = xw.h;
4218 fd_set rfd;
4219 int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
4220 struct timespec drawtimeout, *tv = NULL((void*)0), now, last, lastblink;
4221 long deltatime;
4222
4223 /* Waiting for window mapping */
4224 do {
3
Loop condition is false. Exiting loop
4225 XNextEvent(xw.dpy, &ev);
4226 /*
4227 * This XFilterEvent call is required because of XOpenIM. It
4228 * does filter out the key event and some client message for
4229 * the input method too.
4230 */
4231 if (XFilterEvent(&ev, None0L))
1
Taking false branch
4232 continue;
4233 if (ev.type == ConfigureNotify22) {
2
Taking false branch
4234 w = ev.xconfigure.width;
4235 h = ev.xconfigure.height;
4236 }
4237 } while (ev.type != MapNotify19);
4238
4239 cresize(w, h);
4
Calling 'cresize'
10
Returning from 'cresize'
4240 ttynew();
4241 ttyresize();
4242
4243 clock_gettime(CLOCK_MONOTONIC1, &last);
4244 lastblink = last;
4245
4246 for (xev = actionfps;;) {
11
Loop condition is true. Entering loop body
4247 FD_ZERO(&rfd)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq"
: "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) /
sizeof (__fd_mask)), "1" (&((&rfd)->fds_bits)[0])
: "memory"); } while (0)
;
4248 FD_SET(cmdfd, &rfd)((void) (((&rfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof
(__fd_mask)))] |= ((__fd_mask) 1 << ((cmdfd) % (8 * (int
) sizeof (__fd_mask))))))
;
4249 FD_SET(xfd, &rfd)((void) (((&rfd)->fds_bits)[((xfd) / (8 * (int) sizeof
(__fd_mask)))] |= ((__fd_mask) 1 << ((xfd) % (8 * (int
) sizeof (__fd_mask))))))
;
4250
4251 if (pselect(MAX(xfd, cmdfd)((xfd) < (cmdfd) ? (cmdfd) : (xfd))+1, &rfd, NULL((void*)0), NULL((void*)0), tv, NULL((void*)0)) < 0) {
12
Taking false branch
4252 if (errno(*__errno_location ()) == EINTR4)
4253 continue;
4254 die("select failed: %s\n", strerror(errno(*__errno_location ())));
4255 }
4256 if (FD_ISSET(cmdfd, &rfd)((((&rfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((cmdfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
) {
13
Taking false branch
4257 ttyread();
4258 if (blinktimeout) {
4259 blinkset = tattrset(ATTR_BLINK);
4260 if (!blinkset)
4261 MODBIT(term.mode, 0, MODE_BLINK)((0) ? ((term.mode) |= (MODE_BLINK)) : ((term.mode) &= ~(
MODE_BLINK)))
;
4262 }
4263 }
4264
4265 if (FD_ISSET(xfd, &rfd)((((&rfd)->fds_bits)[((xfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((xfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
)
14
Taking false branch
4266 xev = actionfps;
4267
4268 clock_gettime(CLOCK_MONOTONIC1, &now);
4269 drawtimeout.tv_sec = 0;
4270 drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
4271 tv = &drawtimeout;
4272
4273 dodraw = 0;
4274 if (blinktimeout && TIMEDIFF(now, lastblink)((now.tv_sec-lastblink.tv_sec)*1000 + (now.tv_nsec-lastblink.
tv_nsec)/1E6)
> blinktimeout) {
4275 tsetdirtattr(ATTR_BLINK);
4276 term.mode ^= MODE_BLINK;
4277 lastblink = now;
4278 dodraw = 1;
4279 }
4280 deltatime = TIMEDIFF(now, last)((now.tv_sec-last.tv_sec)*1000 + (now.tv_nsec-last.tv_nsec)/1E6
)
;
4281 if (deltatime > 1000 / (xev ? xfps : actionfps)) {
15
Assuming 'xev' is 0
16
'?' condition is false
17
Division by zero
4282 dodraw = 1;
4283 last = now;
4284 }
4285
4286 if (dodraw) {
4287 while (XPending(xw.dpy)) {
4288 XNextEvent(xw.dpy, &ev);
4289 if (XFilterEvent(&ev, None0L))
4290 continue;
4291 if (handler[ev.type])
4292 (handler[ev.type])(&ev);
4293 }
4294
4295 draw();
4296 XFlush(xw.dpy);
4297
4298 if (xev && !FD_ISSET(xfd, &rfd)((((&rfd)->fds_bits)[((xfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((xfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
)
4299 xev--;
4300 if (!FD_ISSET(cmdfd, &rfd)((((&rfd)->fds_bits)[((cmdfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((cmdfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
&& !FD_ISSET(xfd, &rfd)((((&rfd)->fds_bits)[((xfd) / (8 * (int) sizeof (__fd_mask
)))] & ((__fd_mask) 1 << ((xfd) % (8 * (int) sizeof
(__fd_mask))))) != 0)
) {
4301 if (blinkset) {
4302 if (TIMEDIFF(now, lastblink)((now.tv_sec-lastblink.tv_sec)*1000 + (now.tv_nsec-lastblink.
tv_nsec)/1E6)
\
4303 > blinktimeout) {
4304 drawtimeout.tv_nsec = 1000;
4305 } else {
4306 drawtimeout.tv_nsec = (1E6 * \
4307 (blinktimeout - \
4308 TIMEDIFF(now,((now.tv_sec-lastblink.tv_sec)*1000 + (now.tv_nsec-lastblink.
tv_nsec)/1E6)
4309 lastblink)((now.tv_sec-lastblink.tv_sec)*1000 + (now.tv_nsec-lastblink.
tv_nsec)/1E6)
));
4310 }
4311 drawtimeout.tv_sec = \
4312 drawtimeout.tv_nsec / 1E9;
4313 drawtimeout.tv_nsec %= (long)1E9;
4314 } else {
4315 tv = NULL((void*)0);
4316 }
4317 }
4318 }
4319 }
4320}
4321
4322void
4323usage(void)
4324{
4325 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
4326 " [-n name] [-o file]\n"
4327 " [-T title] [-t title] [-w windowid]"
4328 " [[-e] command [args ...]]\n"
4329 " %s [-aiv] [-c class] [-f font] [-g geometry]"
4330 " [-n name] [-o file]\n"
4331 " [-T title] [-t title] [-w windowid] -l line"
4332 " [stty_args ...]\n", argv0, argv0);
4333}
4334
4335int
4336main(int argc, char *argv[])
4337{
4338 uint cols = 80, rows = 24;
4339
4340 xw.l = xw.t = 0;
4341 xw.isfixed = False0;
4342 xw.cursor = cursorshape;
4343
4344 ARGBEGINfor (argv0 = *argv, argv++, argc--; argv[0] && argv[0
][0] == '-' && argv[0][1]; argc--, argv++) { char argc_
; char **argv_; int brk_; if (argv[0][1] == '-' && argv
[0][2] == '\0') { argv++; argc--; break; } for (brk_ = 0, argv
[0]++, argv_ = argv; argv[0][0] && !brk_; argv[0]++) {
if (argv_ != argv) break; argc_ = argv[0][0]; switch (argc_)
{
4345 case 'a':
4346 allowaltscreen = 0;
4347 break;
4348 case 'c':
4349 opt_class = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4350 break;
4351 case 'e':
4352 if (argc > 0)
4353 --argc, ++argv;
4354 goto run;
4355 case 'f':
4356 opt_font = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4357 break;
4358 case 'g':
4359 xw.gm = XParseGeometry(EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
,
4360 &xw.l, &xw.t, &cols, &rows);
4361 break;
4362 case 'i':
4363 xw.isfixed = 1;
4364 break;
4365 case 'o':
4366 opt_io = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4367 break;
4368 case 'l':
4369 opt_line = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4370 break;
4371 case 'n':
4372 opt_name = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4373 break;
4374 case 't':
4375 case 'T':
4376 opt_title = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4377 break;
4378 case 'w':
4379 opt_embed = EARGF(usage())((argv[0][1] == '\0' && argv[1] == ((void*)0))? ((usage
()), abort(), (char *)0) : (brk_ = 1, (argv[0][1] != '\0')? (
&argv[0][1]) : (argc--, argv++, argv[0])))
;
4380 break;
4381 case 'v':
4382 die("%s " VERSION"0.6" " (c) 2010-2016 st engineers\n", argv0);
4383 break;
4384 default:
4385 usage();
4386 } ARGEND} };
4387
4388run:
4389 if (argc > 0) {
4390 /* eat all remaining arguments */
4391 opt_cmd = argv;
4392 if (!opt_title && !opt_line)
4393 opt_title = basename__xpg_basename(xstrdup(argv[0]));
4394 }
4395 setlocale(LC_CTYPE0, "");
4396 XSetLocaleModifiers("");
4397 tnew(MAX(cols, 1)((cols) < (1) ? (1) : (cols)), MAX(rows, 1)((rows) < (1) ? (1) : (rows)));
4398 xinit();
4399 selinit();
4400 run();
4401
4402 return 0;
4403}
4404