Xinqi Bao's Git
1 /* See LICENSE for license details. */
14 #include <sys/ioctl.h>
15 #include <sys/select.h>
18 #include <sys/types.h>
24 #include <fontconfig/fontconfig.h>
28 #include <X11/cursorfont.h>
29 #include <X11/Xft/Xft.h>
41 #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
43 #elif defined(__FreeBSD__) || defined(__DragonFly__)
48 #define UTF_INVALID 0xFFFD
49 #define ESC_BUF_SIZ (128*UTF_SIZ)
50 #define ESC_ARG_SIZ 16
51 #define STR_BUF_SIZ ESC_BUF_SIZ
52 #define STR_ARG_SIZ ESC_ARG_SIZ
55 #define NUMMAXLEN(x) ((int)(sizeof(x) * 2.56 + 0.5) + 1)
56 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
57 #define ISCONTROLC0(c) (BETWEEN(c, 0, 0x1f) || (c) == '\177')
58 #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
59 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
60 #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
63 #define ISO14755CMD "dmenu -w %lu -p codepoint: </dev/null"
65 enum cursor_movement
{
89 ESC_STR
= 4, /* OSC, PM, APC */
91 ESC_STR_END
= 16, /* a final string was encountered */
92 ESC_TEST
= 32, /* Enter in test mode */
97 /* CSI Escape sequence structs */
98 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
100 char buf
[ESC_BUF_SIZ
]; /* raw string */
101 int len
; /* raw string length */
103 int arg
[ESC_ARG_SIZ
];
104 int narg
; /* nb of args */
108 /* STR Escape sequence structs */
109 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
111 char type
; /* ESC type ... */
112 char buf
[STR_BUF_SIZ
]; /* raw string */
113 int len
; /* raw string length */
114 char *args
[STR_ARG_SIZ
];
115 int narg
; /* nb of args */
122 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
123 signed char appkey
; /* application keypad */
124 signed char appcursor
; /* application cursor */
125 signed char crlf
; /* crlf mode */
128 /* function definitions used in config.h */
129 static void clipcopy(const Arg
*);
130 static void clippaste(const Arg
*);
131 static void numlock(const Arg
*);
132 static void selpaste(const Arg
*);
133 static void zoom(const Arg
*);
134 static void zoomabs(const Arg
*);
135 static void zoomreset(const Arg
*);
136 static void printsel(const Arg
*);
137 static void printscreen(const Arg
*) ;
138 static void iso14755(const Arg
*);
139 static void toggleprinter(const Arg
*);
140 static void sendbreak(const Arg
*);
142 /* config.h for applying patches and the configuration. */
145 static void execsh(void);
146 static void stty(void);
147 static void sigchld(int);
149 static void csidump(void);
150 static void csihandle(void);
151 static void csiparse(void);
152 static void csireset(void);
153 static int eschandle(uchar
);
154 static void strdump(void);
155 static void strhandle(void);
156 static void strparse(void);
157 static void strreset(void);
159 static void tprinter(char *, size_t);
160 static void tdumpsel(void);
161 static void tdumpline(int);
162 static void tdump(void);
163 static void tclearregion(int, int, int, int);
164 static void tcursor(int);
165 static void tdeletechar(int);
166 static void tdeleteline(int);
167 static void tinsertblank(int);
168 static void tinsertblankline(int);
169 static int tlinelen(int);
170 static void tmoveto(int, int);
171 static void tmoveato(int, int);
172 static void tnewline(int);
173 static void tputtab(int);
174 static void tputc(Rune
);
175 static void treset(void);
176 static void tresize(int, int);
177 static void tscrollup(int, int);
178 static void tscrolldown(int, int);
179 static void tsetattr(int *, int);
180 static void tsetchar(Rune
, Glyph
*, int, int);
181 static void tsetscroll(int, int);
182 static void tswapscreen(void);
183 static void tsetmode(int, int, int *, int);
184 static void tfulldirt(void);
185 static void techo(Rune
);
186 static void tcontrolcode(uchar
);
187 static void tdectest(char );
188 static void tdefutf8(char);
189 static int32_t tdefcolor(int *, int *, int);
190 static void tdeftran(char);
191 static void tstrsequence(uchar
);
193 static void selscroll(int, int);
194 static void selsnap(int *, int *, int);
196 static Rune
utf8decodebyte(char, size_t *);
197 static char utf8encodebyte(Rune
, size_t);
198 static char *utf8strchr(char *s
, Rune u
);
199 static size_t utf8validate(Rune
*, size_t);
201 static ssize_t
xwrite(int, const char *, size_t);
202 static void *xrealloc(void *, size_t);
210 char **opt_cmd
= NULL
;
211 char *opt_class
= NULL
;
212 char *opt_embed
= NULL
;
213 char *opt_font
= NULL
;
215 char *opt_line
= NULL
;
216 char *opt_name
= NULL
;
217 char *opt_title
= NULL
;
218 int oldbutton
= 3; /* button event on startup: 3 = release */
220 static CSIEscape csiescseq
;
221 static STREscape strescseq
;
224 char *usedfont
= NULL
;
225 double usedfontsize
= 0;
226 double defaultfontsize
= 0;
228 static uchar utfbyte
[UTF_SIZ
+ 1] = {0x80, 0, 0xC0, 0xE0, 0xF0};
229 static uchar utfmask
[UTF_SIZ
+ 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
230 static Rune utfmin
[UTF_SIZ
+ 1] = { 0, 0, 0x80, 0x800, 0x10000};
231 static Rune utfmax
[UTF_SIZ
+ 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
233 /* config.h array lengths */
234 size_t colornamelen
= LEN(colorname
);
235 size_t mshortcutslen
= LEN(mshortcuts
);
236 size_t shortcutslen
= LEN(shortcuts
);
237 size_t selmaskslen
= LEN(selmasks
);
240 xwrite(int fd
, const char *s
, size_t len
)
246 r
= write(fd
, s
, len
);
259 void *p
= malloc(len
);
262 die("Out of memory\n");
268 xrealloc(void *p
, size_t len
)
270 if ((p
= realloc(p
, len
)) == NULL
)
271 die("Out of memory\n");
279 if ((s
= strdup(s
)) == NULL
)
280 die("Out of memory\n");
286 utf8decode(char *c
, Rune
*u
, size_t clen
)
288 size_t i
, j
, len
, type
;
294 udecoded
= utf8decodebyte(c
[0], &len
);
295 if (!BETWEEN(len
, 1, UTF_SIZ
))
297 for (i
= 1, j
= 1; i
< clen
&& j
< len
; ++i
, ++j
) {
298 udecoded
= (udecoded
<< 6) | utf8decodebyte(c
[i
], &type
);
305 utf8validate(u
, len
);
311 utf8decodebyte(char c
, size_t *i
)
313 for (*i
= 0; *i
< LEN(utfmask
); ++(*i
))
314 if (((uchar
)c
& utfmask
[*i
]) == utfbyte
[*i
])
315 return (uchar
)c
& ~utfmask
[*i
];
321 utf8encode(Rune u
, char *c
)
325 len
= utf8validate(&u
, 0);
329 for (i
= len
- 1; i
!= 0; --i
) {
330 c
[i
] = utf8encodebyte(u
, 0);
333 c
[0] = utf8encodebyte(u
, len
);
339 utf8encodebyte(Rune u
, size_t i
)
341 return utfbyte
[i
] | (u
& ~utfmask
[i
]);
345 utf8strchr(char *s
, Rune u
)
351 for (i
= 0, j
= 0; i
< len
; i
+= j
) {
352 if (!(j
= utf8decode(&s
[i
], &r
, len
- i
)))
362 utf8validate(Rune
*u
, size_t i
)
364 if (!BETWEEN(*u
, utfmin
[i
], utfmax
[i
]) || BETWEEN(*u
, 0xD800, 0xDFFF))
366 for (i
= 1; *u
> utfmax
[i
]; ++i
)
375 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick1
);
376 clock_gettime(CLOCK_MONOTONIC
, &sel
.tclick2
);
381 sel
.clipboard
= NULL
;
390 return LIMIT(x
, 0, term
.col
-1);
399 return LIMIT(y
, 0, term
.row
-1);
407 if (term
.line
[y
][i
- 1].mode
& ATTR_WRAP
)
410 while (i
> 0 && term
.line
[y
][i
- 1].u
== ' ')
421 if (sel
.type
== SEL_REGULAR
&& sel
.ob
.y
!= sel
.oe
.y
) {
422 sel
.nb
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.ob
.x
: sel
.oe
.x
;
423 sel
.ne
.x
= sel
.ob
.y
< sel
.oe
.y
? sel
.oe
.x
: sel
.ob
.x
;
425 sel
.nb
.x
= MIN(sel
.ob
.x
, sel
.oe
.x
);
426 sel
.ne
.x
= MAX(sel
.ob
.x
, sel
.oe
.x
);
428 sel
.nb
.y
= MIN(sel
.ob
.y
, sel
.oe
.y
);
429 sel
.ne
.y
= MAX(sel
.ob
.y
, sel
.oe
.y
);
431 selsnap(&sel
.nb
.x
, &sel
.nb
.y
, -1);
432 selsnap(&sel
.ne
.x
, &sel
.ne
.y
, +1);
434 /* expand selection over line breaks */
435 if (sel
.type
== SEL_RECTANGULAR
)
437 i
= tlinelen(sel
.nb
.y
);
440 if (tlinelen(sel
.ne
.y
) <= sel
.ne
.x
)
441 sel
.ne
.x
= term
.col
- 1;
445 selected(int x
, int y
)
447 if (sel
.mode
== SEL_EMPTY
)
450 if (sel
.type
== SEL_RECTANGULAR
)
451 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
452 && BETWEEN(x
, sel
.nb
.x
, sel
.ne
.x
);
454 return BETWEEN(y
, sel
.nb
.y
, sel
.ne
.y
)
455 && (y
!= sel
.nb
.y
|| x
>= sel
.nb
.x
)
456 && (y
!= sel
.ne
.y
|| x
<= sel
.ne
.x
);
460 selsnap(int *x
, int *y
, int direction
)
462 int newx
, newy
, xt
, yt
;
463 int delim
, prevdelim
;
469 * Snap around if the word wraps around at the end or
470 * beginning of a line.
472 prevgp
= &term
.line
[*y
][*x
];
473 prevdelim
= ISDELIM(prevgp
->u
);
475 newx
= *x
+ direction
;
477 if (!BETWEEN(newx
, 0, term
.col
- 1)) {
479 newx
= (newx
+ term
.col
) % term
.col
;
480 if (!BETWEEN(newy
, 0, term
.row
- 1))
486 yt
= newy
, xt
= newx
;
487 if (!(term
.line
[yt
][xt
].mode
& ATTR_WRAP
))
491 if (newx
>= tlinelen(newy
))
494 gp
= &term
.line
[newy
][newx
];
495 delim
= ISDELIM(gp
->u
);
496 if (!(gp
->mode
& ATTR_WDUMMY
) && (delim
!= prevdelim
497 || (delim
&& gp
->u
!= prevgp
->u
)))
508 * Snap around if the the previous line or the current one
509 * has set ATTR_WRAP at its end. Then the whole next or
510 * previous line will be selected.
512 *x
= (direction
< 0) ? 0 : term
.col
- 1;
514 for (; *y
> 0; *y
+= direction
) {
515 if (!(term
.line
[*y
-1][term
.col
-1].mode
520 } else if (direction
> 0) {
521 for (; *y
< term
.row
-1; *y
+= direction
) {
522 if (!(term
.line
[*y
][term
.col
-1].mode
536 int y
, bufsize
, lastx
, linelen
;
542 bufsize
= (term
.col
+1) * (sel
.ne
.y
-sel
.nb
.y
+1) * UTF_SIZ
;
543 ptr
= str
= xmalloc(bufsize
);
545 /* append every set & selected glyph to the selection */
546 for (y
= sel
.nb
.y
; y
<= sel
.ne
.y
; y
++) {
547 if ((linelen
= tlinelen(y
)) == 0) {
552 if (sel
.type
== SEL_RECTANGULAR
) {
553 gp
= &term
.line
[y
][sel
.nb
.x
];
556 gp
= &term
.line
[y
][sel
.nb
.y
== y
? sel
.nb
.x
: 0];
557 lastx
= (sel
.ne
.y
== y
) ? sel
.ne
.x
: term
.col
-1;
559 last
= &term
.line
[y
][MIN(lastx
, linelen
-1)];
560 while (last
>= gp
&& last
->u
== ' ')
563 for ( ; gp
<= last
; ++gp
) {
564 if (gp
->mode
& ATTR_WDUMMY
)
567 ptr
+= utf8encode(gp
->u
, ptr
);
571 * Copy and pasting of line endings is inconsistent
572 * in the inconsistent terminal and GUI world.
573 * The best solution seems like to produce '\n' when
574 * something is copied from st and convert '\n' to
575 * '\r', when something to be pasted is received by
577 * FIXME: Fix the computer world.
579 if ((y
< sel
.ne
.y
|| lastx
>= linelen
) && !(last
->mode
& ATTR_WRAP
))
587 selpaste(const Arg
*dummy
)
593 clipcopy(const Arg
*dummy
)
599 clippaste(const Arg
*dummy
)
611 tsetdirt(sel
.nb
.y
, sel
.ne
.y
);
615 die(const char *errstr
, ...)
619 va_start(ap
, errstr
);
620 vfprintf(stderr
, errstr
, ap
);
628 char **args
, *sh
, *prog
;
629 const struct passwd
*pw
;
632 if ((pw
= getpwuid(getuid())) == NULL
) {
634 die("getpwuid:%s\n", strerror(errno
));
636 die("who are you?\n");
639 if ((sh
= getenv("SHELL")) == NULL
)
640 sh
= (pw
->pw_shell
[0]) ? pw
->pw_shell
: shell
;
648 args
= (opt_cmd
) ? opt_cmd
: (char *[]) {prog
, NULL
};
653 setenv("LOGNAME", pw
->pw_name
, 1);
654 setenv("USER", pw
->pw_name
, 1);
655 setenv("SHELL", sh
, 1);
656 setenv("HOME", pw
->pw_dir
, 1);
657 setenv("TERM", termname
, 1);
660 signal(SIGCHLD
, SIG_DFL
);
661 signal(SIGHUP
, SIG_DFL
);
662 signal(SIGINT
, SIG_DFL
);
663 signal(SIGQUIT
, SIG_DFL
);
664 signal(SIGTERM
, SIG_DFL
);
665 signal(SIGALRM
, SIG_DFL
);
677 if ((p
= waitpid(pid
, &stat
, WNOHANG
)) < 0)
678 die("Waiting for pid %hd failed: %s\n", pid
, strerror(errno
));
683 if (!WIFEXITED(stat
) || WEXITSTATUS(stat
))
684 die("child finished with error '%d'\n", stat
);
692 char cmd
[_POSIX_ARG_MAX
], **p
, *q
, *s
;
695 if ((n
= strlen(stty_args
)) > sizeof(cmd
)-1)
696 die("incorrect stty parameters\n");
697 memcpy(cmd
, stty_args
, n
);
699 siz
= sizeof(cmd
) - n
;
700 for (p
= opt_cmd
; p
&& (s
= *p
); ++p
) {
701 if ((n
= strlen(s
)) > siz
-1)
702 die("stty parameter length too long\n");
709 if (system(cmd
) != 0)
710 perror("Couldn't call stty");
717 struct winsize w
= {term
.row
, term
.col
, 0, 0};
720 term
.mode
|= MODE_PRINT
;
721 iofd
= (!strcmp(opt_io
, "-")) ?
722 1 : open(opt_io
, O_WRONLY
| O_CREAT
, 0666);
724 fprintf(stderr
, "Error opening %s:%s\n",
725 opt_io
, strerror(errno
));
730 if ((cmdfd
= open(opt_line
, O_RDWR
)) < 0)
731 die("open line failed: %s\n", strerror(errno
));
737 /* seems to work fine on linux, openbsd and freebsd */
738 if (openpty(&m
, &s
, NULL
, NULL
, &w
) < 0)
739 die("openpty failed: %s\n", strerror(errno
));
741 switch (pid
= fork()) {
743 die("fork failed\n");
747 setsid(); /* create a new process group */
751 if (ioctl(s
, TIOCSCTTY
, NULL
) < 0)
752 die("ioctl TIOCSCTTY failed: %s\n", strerror(errno
));
760 signal(SIGCHLD
, sigchld
);
768 static char buf
[BUFSIZ
];
769 static int buflen
= 0;
771 int charsize
; /* size of utf8 char in bytes */
775 /* append read bytes to unprocessed bytes */
776 if ((ret
= read(cmdfd
, buf
+buflen
, LEN(buf
)-buflen
)) < 0)
777 die("Couldn't read from shell: %s\n", strerror(errno
));
783 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
784 /* process a complete utf8 char */
785 charsize
= utf8decode(ptr
, &unicodep
, buflen
);
795 tputc(*ptr
++ & 0xFF);
799 /* keep any uncomplete utf8 char for the next call */
801 memmove(buf
, ptr
, buflen
);
807 ttywrite(const char *s
, size_t n
)
814 * Remember that we are using a pty, which might be a modem line.
815 * Writing too much will clog the line. That's why we are doing this
817 * FIXME: Migrate the world to Plan 9.
825 /* Check if we can write. */
826 if (pselect(cmdfd
+1, &rfd
, &wfd
, NULL
, NULL
, NULL
) < 0) {
829 die("select failed: %s\n", strerror(errno
));
831 if (FD_ISSET(cmdfd
, &wfd
)) {
833 * Only write the bytes written by ttywrite() or the
834 * default of 256. This seems to be a reasonable value
835 * for a serial line. Bigger values might clog the I/O.
837 if ((r
= write(cmdfd
, s
, (n
< lim
)? n
: lim
)) < 0)
841 * We weren't able to write out everything.
842 * This means the buffer is getting full
850 /* All bytes have been written. */
854 if (FD_ISSET(cmdfd
, &rfd
))
860 die("write error on tty: %s\n", strerror(errno
));
864 ttysend(char *s
, size_t n
)
871 if (!IS_SET(MODE_ECHO
))
875 for (t
= s
; t
< lim
; t
+= len
) {
876 if (IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
877 len
= utf8decode(t
, &u
, n
);
896 w
.ws_xpixel
= win
.tw
;
897 w
.ws_ypixel
= win
.th
;
898 if (ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
899 fprintf(stderr
, "Couldn't set window size: %s\n", strerror(errno
));
907 for (i
= 0; i
< term
.row
-1; i
++) {
908 for (j
= 0; j
< term
.col
-1; j
++) {
909 if (term
.line
[i
][j
].mode
& attr
)
918 tsetdirt(int top
, int bot
)
922 LIMIT(top
, 0, term
.row
-1);
923 LIMIT(bot
, 0, term
.row
-1);
925 for (i
= top
; i
<= bot
; i
++)
930 tsetdirtattr(int attr
)
934 for (i
= 0; i
< term
.row
-1; i
++) {
935 for (j
= 0; j
< term
.col
-1; j
++) {
936 if (term
.line
[i
][j
].mode
& attr
) {
947 tsetdirt(0, term
.row
-1);
954 int alt
= IS_SET(MODE_ALTSCREEN
);
956 if (mode
== CURSOR_SAVE
) {
958 } else if (mode
== CURSOR_LOAD
) {
960 tmoveto(c
[alt
].x
, c
[alt
].y
);
973 }, .x
= 0, .y
= 0, .state
= CURSOR_DEFAULT
};
975 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
976 for (i
= tabspaces
; i
< term
.col
; i
+= tabspaces
)
979 term
.bot
= term
.row
- 1;
980 term
.mode
= MODE_WRAP
|MODE_UTF8
;
981 memset(term
.trantbl
, CS_USA
, sizeof(term
.trantbl
));
984 for (i
= 0; i
< 2; i
++) {
986 tcursor(CURSOR_SAVE
);
987 tclearregion(0, 0, term
.col
-1, term
.row
-1);
993 tnew(int col
, int row
)
995 term
= (Term
){ .c
= { .attr
= { .fg
= defaultfg
, .bg
= defaultbg
} } };
1005 Line
*tmp
= term
.line
;
1007 term
.line
= term
.alt
;
1009 term
.mode
^= MODE_ALTSCREEN
;
1014 tscrolldown(int orig
, int n
)
1019 LIMIT(n
, 0, term
.bot
-orig
+1);
1021 tsetdirt(orig
, term
.bot
-n
);
1022 tclearregion(0, term
.bot
-n
+1, term
.col
-1, term
.bot
);
1024 for (i
= term
.bot
; i
>= orig
+n
; i
--) {
1025 temp
= term
.line
[i
];
1026 term
.line
[i
] = term
.line
[i
-n
];
1027 term
.line
[i
-n
] = temp
;
1034 tscrollup(int orig
, int n
)
1039 LIMIT(n
, 0, term
.bot
-orig
+1);
1041 tclearregion(0, orig
, term
.col
-1, orig
+n
-1);
1042 tsetdirt(orig
+n
, term
.bot
);
1044 for (i
= orig
; i
<= term
.bot
-n
; i
++) {
1045 temp
= term
.line
[i
];
1046 term
.line
[i
] = term
.line
[i
+n
];
1047 term
.line
[i
+n
] = temp
;
1050 selscroll(orig
, -n
);
1054 selscroll(int orig
, int n
)
1059 if (BETWEEN(sel
.ob
.y
, orig
, term
.bot
) || BETWEEN(sel
.oe
.y
, orig
, term
.bot
)) {
1060 if ((sel
.ob
.y
+= n
) > term
.bot
|| (sel
.oe
.y
+= n
) < term
.top
) {
1064 if (sel
.type
== SEL_RECTANGULAR
) {
1065 if (sel
.ob
.y
< term
.top
)
1066 sel
.ob
.y
= term
.top
;
1067 if (sel
.oe
.y
> term
.bot
)
1068 sel
.oe
.y
= term
.bot
;
1070 if (sel
.ob
.y
< term
.top
) {
1071 sel
.ob
.y
= term
.top
;
1074 if (sel
.oe
.y
> term
.bot
) {
1075 sel
.oe
.y
= term
.bot
;
1076 sel
.oe
.x
= term
.col
;
1084 tnewline(int first_col
)
1088 if (y
== term
.bot
) {
1089 tscrollup(term
.top
, 1);
1093 tmoveto(first_col
? 0 : term
.c
.x
, y
);
1099 char *p
= csiescseq
.buf
, *np
;
1108 csiescseq
.buf
[csiescseq
.len
] = '\0';
1109 while (p
< csiescseq
.buf
+csiescseq
.len
) {
1111 v
= strtol(p
, &np
, 10);
1114 if (v
== LONG_MAX
|| v
== LONG_MIN
)
1116 csiescseq
.arg
[csiescseq
.narg
++] = v
;
1118 if (*p
!= ';' || csiescseq
.narg
== ESC_ARG_SIZ
)
1122 csiescseq
.mode
[0] = *p
++;
1123 csiescseq
.mode
[1] = (p
< csiescseq
.buf
+csiescseq
.len
) ? *p
: '\0';
1126 /* for absolute user moves, when decom is set */
1128 tmoveato(int x
, int y
)
1130 tmoveto(x
, y
+ ((term
.c
.state
& CURSOR_ORIGIN
) ? term
.top
: 0));
1134 tmoveto(int x
, int y
)
1138 if (term
.c
.state
& CURSOR_ORIGIN
) {
1143 maxy
= term
.row
- 1;
1145 term
.c
.state
&= ~CURSOR_WRAPNEXT
;
1146 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
1147 term
.c
.y
= LIMIT(y
, miny
, maxy
);
1151 tsetchar(Rune u
, Glyph
*attr
, int x
, int y
)
1153 static char *vt100_0
[62] = { /* 0x41 - 0x7e */
1154 "↑", "↓", "→", "←", "█", "▚", "☃", /* A - G */
1155 0, 0, 0, 0, 0, 0, 0, 0, /* H - O */
1156 0, 0, 0, 0, 0, 0, 0, 0, /* P - W */
1157 0, 0, 0, 0, 0, 0, 0, " ", /* X - _ */
1158 "◆", "▒", "␉", "␌", "␍", "␊", "°", "±", /* ` - g */
1159 "", "␋", "┘", "┐", "┌", "└", "┼", "⎺", /* h - o */
1160 "⎻", "─", "⎼", "⎽", "├", "┤", "┴", "┬", /* p - w */
1161 "│", "≤", "≥", "π", "≠", "£", "·", /* x - ~ */
1165 * The table is proudly stolen from rxvt.
1167 if (term
.trantbl
[term
.charset
] == CS_GRAPHIC0
&&
1168 BETWEEN(u
, 0x41, 0x7e) && vt100_0
[u
- 0x41])
1169 utf8decode(vt100_0
[u
- 0x41], &u
, UTF_SIZ
);
1171 if (term
.line
[y
][x
].mode
& ATTR_WIDE
) {
1172 if (x
+1 < term
.col
) {
1173 term
.line
[y
][x
+1].u
= ' ';
1174 term
.line
[y
][x
+1].mode
&= ~ATTR_WDUMMY
;
1176 } else if (term
.line
[y
][x
].mode
& ATTR_WDUMMY
) {
1177 term
.line
[y
][x
-1].u
= ' ';
1178 term
.line
[y
][x
-1].mode
&= ~ATTR_WIDE
;
1182 term
.line
[y
][x
] = *attr
;
1183 term
.line
[y
][x
].u
= u
;
1187 tclearregion(int x1
, int y1
, int x2
, int y2
)
1193 temp
= x1
, x1
= x2
, x2
= temp
;
1195 temp
= y1
, y1
= y2
, y2
= temp
;
1197 LIMIT(x1
, 0, term
.col
-1);
1198 LIMIT(x2
, 0, term
.col
-1);
1199 LIMIT(y1
, 0, term
.row
-1);
1200 LIMIT(y2
, 0, term
.row
-1);
1202 for (y
= y1
; y
<= y2
; y
++) {
1204 for (x
= x1
; x
<= x2
; x
++) {
1205 gp
= &term
.line
[y
][x
];
1208 gp
->fg
= term
.c
.attr
.fg
;
1209 gp
->bg
= term
.c
.attr
.bg
;
1222 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1226 size
= term
.col
- src
;
1227 line
= term
.line
[term
.c
.y
];
1229 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1230 tclearregion(term
.col
-n
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1239 LIMIT(n
, 0, term
.col
- term
.c
.x
);
1243 size
= term
.col
- dst
;
1244 line
= term
.line
[term
.c
.y
];
1246 memmove(&line
[dst
], &line
[src
], size
* sizeof(Glyph
));
1247 tclearregion(src
, term
.c
.y
, dst
- 1, term
.c
.y
);
1251 tinsertblankline(int n
)
1253 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1254 tscrolldown(term
.c
.y
, n
);
1260 if (BETWEEN(term
.c
.y
, term
.top
, term
.bot
))
1261 tscrollup(term
.c
.y
, n
);
1265 tdefcolor(int *attr
, int *npar
, int l
)
1270 switch (attr
[*npar
+ 1]) {
1271 case 2: /* direct color in RGB space */
1272 if (*npar
+ 4 >= l
) {
1274 "erresc(38): Incorrect number of parameters (%d)\n",
1278 r
= attr
[*npar
+ 2];
1279 g
= attr
[*npar
+ 3];
1280 b
= attr
[*npar
+ 4];
1282 if (!BETWEEN(r
, 0, 255) || !BETWEEN(g
, 0, 255) || !BETWEEN(b
, 0, 255))
1283 fprintf(stderr
, "erresc: bad rgb color (%u,%u,%u)\n",
1286 idx
= TRUECOLOR(r
, g
, b
);
1288 case 5: /* indexed color */
1289 if (*npar
+ 2 >= l
) {
1291 "erresc(38): Incorrect number of parameters (%d)\n",
1296 if (!BETWEEN(attr
[*npar
], 0, 255))
1297 fprintf(stderr
, "erresc: bad fgcolor %d\n", attr
[*npar
]);
1301 case 0: /* implemented defined (only foreground) */
1302 case 1: /* transparent */
1303 case 3: /* direct color in CMY space */
1304 case 4: /* direct color in CMYK space */
1307 "erresc(38): gfx attr %d unknown\n", attr
[*npar
]);
1315 tsetattr(int *attr
, int l
)
1320 for (i
= 0; i
< l
; i
++) {
1323 term
.c
.attr
.mode
&= ~(
1332 term
.c
.attr
.fg
= defaultfg
;
1333 term
.c
.attr
.bg
= defaultbg
;
1336 term
.c
.attr
.mode
|= ATTR_BOLD
;
1339 term
.c
.attr
.mode
|= ATTR_FAINT
;
1342 term
.c
.attr
.mode
|= ATTR_ITALIC
;
1345 term
.c
.attr
.mode
|= ATTR_UNDERLINE
;
1347 case 5: /* slow blink */
1349 case 6: /* rapid blink */
1350 term
.c
.attr
.mode
|= ATTR_BLINK
;
1353 term
.c
.attr
.mode
|= ATTR_REVERSE
;
1356 term
.c
.attr
.mode
|= ATTR_INVISIBLE
;
1359 term
.c
.attr
.mode
|= ATTR_STRUCK
;
1362 term
.c
.attr
.mode
&= ~(ATTR_BOLD
| ATTR_FAINT
);
1365 term
.c
.attr
.mode
&= ~ATTR_ITALIC
;
1368 term
.c
.attr
.mode
&= ~ATTR_UNDERLINE
;
1371 term
.c
.attr
.mode
&= ~ATTR_BLINK
;
1374 term
.c
.attr
.mode
&= ~ATTR_REVERSE
;
1377 term
.c
.attr
.mode
&= ~ATTR_INVISIBLE
;
1380 term
.c
.attr
.mode
&= ~ATTR_STRUCK
;
1383 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1384 term
.c
.attr
.fg
= idx
;
1387 term
.c
.attr
.fg
= defaultfg
;
1390 if ((idx
= tdefcolor(attr
, &i
, l
)) >= 0)
1391 term
.c
.attr
.bg
= idx
;
1394 term
.c
.attr
.bg
= defaultbg
;
1397 if (BETWEEN(attr
[i
], 30, 37)) {
1398 term
.c
.attr
.fg
= attr
[i
] - 30;
1399 } else if (BETWEEN(attr
[i
], 40, 47)) {
1400 term
.c
.attr
.bg
= attr
[i
] - 40;
1401 } else if (BETWEEN(attr
[i
], 90, 97)) {
1402 term
.c
.attr
.fg
= attr
[i
] - 90 + 8;
1403 } else if (BETWEEN(attr
[i
], 100, 107)) {
1404 term
.c
.attr
.bg
= attr
[i
] - 100 + 8;
1407 "erresc(default): gfx attr %d unknown\n",
1408 attr
[i
]), csidump();
1416 tsetscroll(int t
, int b
)
1420 LIMIT(t
, 0, term
.row
-1);
1421 LIMIT(b
, 0, term
.row
-1);
1432 tsetmode(int priv
, int set
, int *args
, int narg
)
1437 for (lim
= args
+ narg
; args
< lim
; ++args
) {
1440 case 1: /* DECCKM -- Cursor key */
1441 MODBIT(term
.mode
, set
, MODE_APPCURSOR
);
1443 case 5: /* DECSCNM -- Reverse video */
1445 MODBIT(term
.mode
, set
, MODE_REVERSE
);
1446 if (mode
!= term
.mode
)
1449 case 6: /* DECOM -- Origin */
1450 MODBIT(term
.c
.state
, set
, CURSOR_ORIGIN
);
1453 case 7: /* DECAWM -- Auto wrap */
1454 MODBIT(term
.mode
, set
, MODE_WRAP
);
1456 case 0: /* Error (IGNORED) */
1457 case 2: /* DECANM -- ANSI/VT52 (IGNORED) */
1458 case 3: /* DECCOLM -- Column (IGNORED) */
1459 case 4: /* DECSCLM -- Scroll (IGNORED) */
1460 case 8: /* DECARM -- Auto repeat (IGNORED) */
1461 case 18: /* DECPFF -- Printer feed (IGNORED) */
1462 case 19: /* DECPEX -- Printer extent (IGNORED) */
1463 case 42: /* DECNRCM -- National characters (IGNORED) */
1464 case 12: /* att610 -- Start blinking cursor (IGNORED) */
1466 case 25: /* DECTCEM -- Text Cursor Enable Mode */
1467 MODBIT(term
.mode
, !set
, MODE_HIDE
);
1469 case 9: /* X10 mouse compatibility mode */
1470 xsetpointermotion(0);
1471 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1472 MODBIT(term
.mode
, set
, MODE_MOUSEX10
);
1474 case 1000: /* 1000: report button press */
1475 xsetpointermotion(0);
1476 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1477 MODBIT(term
.mode
, set
, MODE_MOUSEBTN
);
1479 case 1002: /* 1002: report motion on button press */
1480 xsetpointermotion(0);
1481 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1482 MODBIT(term
.mode
, set
, MODE_MOUSEMOTION
);
1484 case 1003: /* 1003: enable all mouse motions */
1485 xsetpointermotion(set
);
1486 MODBIT(term
.mode
, 0, MODE_MOUSE
);
1487 MODBIT(term
.mode
, set
, MODE_MOUSEMANY
);
1489 case 1004: /* 1004: send focus events to tty */
1490 MODBIT(term
.mode
, set
, MODE_FOCUS
);
1492 case 1006: /* 1006: extended reporting mode */
1493 MODBIT(term
.mode
, set
, MODE_MOUSESGR
);
1496 MODBIT(term
.mode
, set
, MODE_8BIT
);
1498 case 1049: /* swap screen & set/restore cursor as xterm */
1499 if (!allowaltscreen
)
1501 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1503 case 47: /* swap screen */
1505 if (!allowaltscreen
)
1507 alt
= IS_SET(MODE_ALTSCREEN
);
1509 tclearregion(0, 0, term
.col
-1,
1512 if (set
^ alt
) /* set is always 1 or 0 */
1518 tcursor((set
) ? CURSOR_SAVE
: CURSOR_LOAD
);
1520 case 2004: /* 2004: bracketed paste mode */
1521 MODBIT(term
.mode
, set
, MODE_BRCKTPASTE
);
1523 /* Not implemented mouse modes. See comments there. */
1524 case 1001: /* mouse highlight mode; can hang the
1525 terminal by design when implemented. */
1526 case 1005: /* UTF-8 mouse mode; will confuse
1527 applications not supporting UTF-8
1529 case 1015: /* urxvt mangled mouse mode; incompatible
1530 and can be mistaken for other control
1534 "erresc: unknown private set/reset mode %d\n",
1540 case 0: /* Error (IGNORED) */
1542 case 2: /* KAM -- keyboard action */
1543 MODBIT(term
.mode
, set
, MODE_KBDLOCK
);
1545 case 4: /* IRM -- Insertion-replacement */
1546 MODBIT(term
.mode
, set
, MODE_INSERT
);
1548 case 12: /* SRM -- Send/Receive */
1549 MODBIT(term
.mode
, !set
, MODE_ECHO
);
1551 case 20: /* LNM -- Linefeed/new line */
1552 MODBIT(term
.mode
, set
, MODE_CRLF
);
1556 "erresc: unknown set/reset mode %d\n",
1570 switch (csiescseq
.mode
[0]) {
1573 fprintf(stderr
, "erresc: unknown csi ");
1577 case '@': /* ICH -- Insert <n> blank char */
1578 DEFAULT(csiescseq
.arg
[0], 1);
1579 tinsertblank(csiescseq
.arg
[0]);
1581 case 'A': /* CUU -- Cursor <n> Up */
1582 DEFAULT(csiescseq
.arg
[0], 1);
1583 tmoveto(term
.c
.x
, term
.c
.y
-csiescseq
.arg
[0]);
1585 case 'B': /* CUD -- Cursor <n> Down */
1586 case 'e': /* VPR --Cursor <n> Down */
1587 DEFAULT(csiescseq
.arg
[0], 1);
1588 tmoveto(term
.c
.x
, term
.c
.y
+csiescseq
.arg
[0]);
1590 case 'i': /* MC -- Media Copy */
1591 switch (csiescseq
.arg
[0]) {
1596 tdumpline(term
.c
.y
);
1602 term
.mode
&= ~MODE_PRINT
;
1605 term
.mode
|= MODE_PRINT
;
1609 case 'c': /* DA -- Device Attributes */
1610 if (csiescseq
.arg
[0] == 0)
1611 ttywrite(vtiden
, sizeof(vtiden
) - 1);
1613 case 'C': /* CUF -- Cursor <n> Forward */
1614 case 'a': /* HPR -- Cursor <n> Forward */
1615 DEFAULT(csiescseq
.arg
[0], 1);
1616 tmoveto(term
.c
.x
+csiescseq
.arg
[0], term
.c
.y
);
1618 case 'D': /* CUB -- Cursor <n> Backward */
1619 DEFAULT(csiescseq
.arg
[0], 1);
1620 tmoveto(term
.c
.x
-csiescseq
.arg
[0], term
.c
.y
);
1622 case 'E': /* CNL -- Cursor <n> Down and first col */
1623 DEFAULT(csiescseq
.arg
[0], 1);
1624 tmoveto(0, term
.c
.y
+csiescseq
.arg
[0]);
1626 case 'F': /* CPL -- Cursor <n> Up and first col */
1627 DEFAULT(csiescseq
.arg
[0], 1);
1628 tmoveto(0, term
.c
.y
-csiescseq
.arg
[0]);
1630 case 'g': /* TBC -- Tabulation clear */
1631 switch (csiescseq
.arg
[0]) {
1632 case 0: /* clear current tab stop */
1633 term
.tabs
[term
.c
.x
] = 0;
1635 case 3: /* clear all the tabs */
1636 memset(term
.tabs
, 0, term
.col
* sizeof(*term
.tabs
));
1642 case 'G': /* CHA -- Move to <col> */
1644 DEFAULT(csiescseq
.arg
[0], 1);
1645 tmoveto(csiescseq
.arg
[0]-1, term
.c
.y
);
1647 case 'H': /* CUP -- Move to <row> <col> */
1649 DEFAULT(csiescseq
.arg
[0], 1);
1650 DEFAULT(csiescseq
.arg
[1], 1);
1651 tmoveato(csiescseq
.arg
[1]-1, csiescseq
.arg
[0]-1);
1653 case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
1654 DEFAULT(csiescseq
.arg
[0], 1);
1655 tputtab(csiescseq
.arg
[0]);
1657 case 'J': /* ED -- Clear screen */
1659 switch (csiescseq
.arg
[0]) {
1661 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
1662 if (term
.c
.y
< term
.row
-1) {
1663 tclearregion(0, term
.c
.y
+1, term
.col
-1,
1669 tclearregion(0, 0, term
.col
-1, term
.c
.y
-1);
1670 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1673 tclearregion(0, 0, term
.col
-1, term
.row
-1);
1679 case 'K': /* EL -- Clear line */
1680 switch (csiescseq
.arg
[0]) {
1682 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1,
1686 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
1689 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
1693 case 'S': /* SU -- Scroll <n> line up */
1694 DEFAULT(csiescseq
.arg
[0], 1);
1695 tscrollup(term
.top
, csiescseq
.arg
[0]);
1697 case 'T': /* SD -- Scroll <n> line down */
1698 DEFAULT(csiescseq
.arg
[0], 1);
1699 tscrolldown(term
.top
, csiescseq
.arg
[0]);
1701 case 'L': /* IL -- Insert <n> blank lines */
1702 DEFAULT(csiescseq
.arg
[0], 1);
1703 tinsertblankline(csiescseq
.arg
[0]);
1705 case 'l': /* RM -- Reset Mode */
1706 tsetmode(csiescseq
.priv
, 0, csiescseq
.arg
, csiescseq
.narg
);
1708 case 'M': /* DL -- Delete <n> lines */
1709 DEFAULT(csiescseq
.arg
[0], 1);
1710 tdeleteline(csiescseq
.arg
[0]);
1712 case 'X': /* ECH -- Erase <n> char */
1713 DEFAULT(csiescseq
.arg
[0], 1);
1714 tclearregion(term
.c
.x
, term
.c
.y
,
1715 term
.c
.x
+ csiescseq
.arg
[0] - 1, term
.c
.y
);
1717 case 'P': /* DCH -- Delete <n> char */
1718 DEFAULT(csiescseq
.arg
[0], 1);
1719 tdeletechar(csiescseq
.arg
[0]);
1721 case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
1722 DEFAULT(csiescseq
.arg
[0], 1);
1723 tputtab(-csiescseq
.arg
[0]);
1725 case 'd': /* VPA -- Move to <row> */
1726 DEFAULT(csiescseq
.arg
[0], 1);
1727 tmoveato(term
.c
.x
, csiescseq
.arg
[0]-1);
1729 case 'h': /* SM -- Set terminal mode */
1730 tsetmode(csiescseq
.priv
, 1, csiescseq
.arg
, csiescseq
.narg
);
1732 case 'm': /* SGR -- Terminal attribute (color) */
1733 tsetattr(csiescseq
.arg
, csiescseq
.narg
);
1735 case 'n': /* DSR – Device Status Report (cursor position) */
1736 if (csiescseq
.arg
[0] == 6) {
1737 len
= snprintf(buf
, sizeof(buf
),"\033[%i;%iR",
1738 term
.c
.y
+1, term
.c
.x
+1);
1742 case 'r': /* DECSTBM -- Set Scrolling Region */
1743 if (csiescseq
.priv
) {
1746 DEFAULT(csiescseq
.arg
[0], 1);
1747 DEFAULT(csiescseq
.arg
[1], term
.row
);
1748 tsetscroll(csiescseq
.arg
[0]-1, csiescseq
.arg
[1]-1);
1752 case 's': /* DECSC -- Save cursor position (ANSI.SYS) */
1753 tcursor(CURSOR_SAVE
);
1755 case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
1756 tcursor(CURSOR_LOAD
);
1759 switch (csiescseq
.mode
[1]) {
1760 case 'q': /* DECSCUSR -- Set Cursor Style */
1761 DEFAULT(csiescseq
.arg
[0], 1);
1762 if (!BETWEEN(csiescseq
.arg
[0], 0, 6)) {
1765 win
.cursor
= csiescseq
.arg
[0];
1780 fprintf(stderr
, "ESC[");
1781 for (i
= 0; i
< csiescseq
.len
; i
++) {
1782 c
= csiescseq
.buf
[i
] & 0xff;
1785 } else if (c
== '\n') {
1786 fprintf(stderr
, "(\\n)");
1787 } else if (c
== '\r') {
1788 fprintf(stderr
, "(\\r)");
1789 } else if (c
== 0x1b) {
1790 fprintf(stderr
, "(\\e)");
1792 fprintf(stderr
, "(%02x)", c
);
1801 memset(&csiescseq
, 0, sizeof(csiescseq
));
1810 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
1812 par
= (narg
= strescseq
.narg
) ? atoi(strescseq
.args
[0]) : 0;
1814 switch (strescseq
.type
) {
1815 case ']': /* OSC -- Operating System Command */
1821 xsettitle(strescseq
.args
[1]);
1823 case 4: /* color set */
1826 p
= strescseq
.args
[2];
1828 case 104: /* color reset, here p = NULL */
1829 j
= (narg
> 1) ? atoi(strescseq
.args
[1]) : -1;
1830 if (xsetcolorname(j
, p
)) {
1831 fprintf(stderr
, "erresc: invalid color %s\n", p
);
1834 * TODO if defaultbg color is changed, borders
1842 case 'k': /* old title set compatibility */
1843 xsettitle(strescseq
.args
[0]);
1845 case 'P': /* DCS -- Device Control String */
1846 term
.mode
|= ESC_DCS
;
1847 case '_': /* APC -- Application Program Command */
1848 case '^': /* PM -- Privacy Message */
1852 fprintf(stderr
, "erresc: unknown str ");
1860 char *p
= strescseq
.buf
;
1863 strescseq
.buf
[strescseq
.len
] = '\0';
1868 while (strescseq
.narg
< STR_ARG_SIZ
) {
1869 strescseq
.args
[strescseq
.narg
++] = p
;
1870 while ((c
= *p
) != ';' && c
!= '\0')
1884 fprintf(stderr
, "ESC%c", strescseq
.type
);
1885 for (i
= 0; i
< strescseq
.len
; i
++) {
1886 c
= strescseq
.buf
[i
] & 0xff;
1890 } else if (isprint(c
)) {
1892 } else if (c
== '\n') {
1893 fprintf(stderr
, "(\\n)");
1894 } else if (c
== '\r') {
1895 fprintf(stderr
, "(\\r)");
1896 } else if (c
== 0x1b) {
1897 fprintf(stderr
, "(\\e)");
1899 fprintf(stderr
, "(%02x)", c
);
1902 fprintf(stderr
, "ESC\\\n");
1908 memset(&strescseq
, 0, sizeof(strescseq
));
1912 sendbreak(const Arg
*arg
)
1914 if (tcsendbreak(cmdfd
, 0))
1915 perror("Error sending break");
1919 tprinter(char *s
, size_t len
)
1921 if (iofd
!= -1 && xwrite(iofd
, s
, len
) < 0) {
1922 fprintf(stderr
, "Error writing in %s:%s\n",
1923 opt_io
, strerror(errno
));
1930 iso14755(const Arg
*arg
)
1932 unsigned long id
= xwinid();
1933 char cmd
[sizeof(ISO14755CMD
) + NUMMAXLEN(id
)];
1935 char *us
, *e
, codepoint
[9], uc
[UTF_SIZ
];
1936 unsigned long utf32
;
1938 snprintf(cmd
, sizeof(cmd
), ISO14755CMD
, id
);
1939 if (!(p
= popen(cmd
, "r")))
1942 us
= fgets(codepoint
, sizeof(codepoint
), p
);
1945 if (!us
|| *us
== '\0' || *us
== '-' || strlen(us
) > 7)
1947 if ((utf32
= strtoul(us
, &e
, 16)) == ULONG_MAX
||
1948 (*e
!= '\n' && *e
!= '\0'))
1951 ttysend(uc
, utf8encode(utf32
, uc
));
1955 toggleprinter(const Arg
*arg
)
1957 term
.mode
^= MODE_PRINT
;
1961 printscreen(const Arg
*arg
)
1967 printsel(const Arg
*arg
)
1977 if ((ptr
= getsel())) {
1978 tprinter(ptr
, strlen(ptr
));
1989 bp
= &term
.line
[n
][0];
1990 end
= &bp
[MIN(tlinelen(n
), term
.col
) - 1];
1991 if (bp
!= end
|| bp
->u
!= ' ') {
1992 for ( ;bp
<= end
; ++bp
)
1993 tprinter(buf
, utf8encode(bp
->u
, buf
));
2003 for (i
= 0; i
< term
.row
; ++i
)
2013 while (x
< term
.col
&& n
--)
2014 for (++x
; x
< term
.col
&& !term
.tabs
[x
]; ++x
)
2017 while (x
> 0 && n
++)
2018 for (--x
; x
> 0 && !term
.tabs
[x
]; --x
)
2021 term
.c
.x
= LIMIT(x
, 0, term
.col
-1);
2027 if (ISCONTROL(u
)) { /* control code */
2032 } else if (u
!= '\n' && u
!= '\r' && u
!= '\t') {
2041 tdefutf8(char ascii
)
2044 term
.mode
|= MODE_UTF8
;
2045 else if (ascii
== '@')
2046 term
.mode
&= ~MODE_UTF8
;
2050 tdeftran(char ascii
)
2052 static char cs
[] = "0B";
2053 static int vcs
[] = {CS_GRAPHIC0
, CS_USA
};
2056 if ((p
= strchr(cs
, ascii
)) == NULL
) {
2057 fprintf(stderr
, "esc unhandled charset: ESC ( %c\n", ascii
);
2059 term
.trantbl
[term
.icharset
] = vcs
[p
- cs
];
2068 if (c
== '8') { /* DEC screen alignment test. */
2069 for (x
= 0; x
< term
.col
; ++x
) {
2070 for (y
= 0; y
< term
.row
; ++y
)
2071 tsetchar('E', &term
.c
.attr
, x
, y
);
2077 tstrsequence(uchar c
)
2082 case 0x90: /* DCS -- Device Control String */
2084 term
.esc
|= ESC_DCS
;
2086 case 0x9f: /* APC -- Application Program Command */
2089 case 0x9e: /* PM -- Privacy Message */
2092 case 0x9d: /* OSC -- Operating System Command */
2097 term
.esc
|= ESC_STR
;
2101 tcontrolcode(uchar ascii
)
2108 tmoveto(term
.c
.x
-1, term
.c
.y
);
2111 tmoveto(0, term
.c
.y
);
2116 /* go to first col if the mode is set */
2117 tnewline(IS_SET(MODE_CRLF
));
2119 case '\a': /* BEL */
2120 if (term
.esc
& ESC_STR_END
) {
2121 /* backwards compatibility to xterm */
2124 if (!(win
.state
& WIN_FOCUSED
))
2130 case '\033': /* ESC */
2132 term
.esc
&= ~(ESC_CSI
|ESC_ALTCHARSET
|ESC_TEST
);
2133 term
.esc
|= ESC_START
;
2135 case '\016': /* SO (LS1 -- Locking shift 1) */
2136 case '\017': /* SI (LS0 -- Locking shift 0) */
2137 term
.charset
= 1 - (ascii
- '\016');
2139 case '\032': /* SUB */
2140 tsetchar('?', &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2141 case '\030': /* CAN */
2144 case '\005': /* ENQ (IGNORED) */
2145 case '\000': /* NUL (IGNORED) */
2146 case '\021': /* XON (IGNORED) */
2147 case '\023': /* XOFF (IGNORED) */
2148 case 0177: /* DEL (IGNORED) */
2150 case 0x80: /* TODO: PAD */
2151 case 0x81: /* TODO: HOP */
2152 case 0x82: /* TODO: BPH */
2153 case 0x83: /* TODO: NBH */
2154 case 0x84: /* TODO: IND */
2156 case 0x85: /* NEL -- Next line */
2157 tnewline(1); /* always go to first col */
2159 case 0x86: /* TODO: SSA */
2160 case 0x87: /* TODO: ESA */
2162 case 0x88: /* HTS -- Horizontal tab stop */
2163 term
.tabs
[term
.c
.x
] = 1;
2165 case 0x89: /* TODO: HTJ */
2166 case 0x8a: /* TODO: VTS */
2167 case 0x8b: /* TODO: PLD */
2168 case 0x8c: /* TODO: PLU */
2169 case 0x8d: /* TODO: RI */
2170 case 0x8e: /* TODO: SS2 */
2171 case 0x8f: /* TODO: SS3 */
2172 case 0x91: /* TODO: PU1 */
2173 case 0x92: /* TODO: PU2 */
2174 case 0x93: /* TODO: STS */
2175 case 0x94: /* TODO: CCH */
2176 case 0x95: /* TODO: MW */
2177 case 0x96: /* TODO: SPA */
2178 case 0x97: /* TODO: EPA */
2179 case 0x98: /* TODO: SOS */
2180 case 0x99: /* TODO: SGCI */
2182 case 0x9a: /* DECID -- Identify Terminal */
2183 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2185 case 0x9b: /* TODO: CSI */
2186 case 0x9c: /* TODO: ST */
2188 case 0x90: /* DCS -- Device Control String */
2189 case 0x9d: /* OSC -- Operating System Command */
2190 case 0x9e: /* PM -- Privacy Message */
2191 case 0x9f: /* APC -- Application Program Command */
2192 tstrsequence(ascii
);
2195 /* only CAN, SUB, \a and C1 chars interrupt a sequence */
2196 term
.esc
&= ~(ESC_STR_END
|ESC_STR
);
2200 * returns 1 when the sequence is finished and it hasn't to read
2201 * more characters for this sequence, otherwise 0
2204 eschandle(uchar ascii
)
2208 term
.esc
|= ESC_CSI
;
2211 term
.esc
|= ESC_TEST
;
2214 term
.esc
|= ESC_UTF8
;
2216 case 'P': /* DCS -- Device Control String */
2217 case '_': /* APC -- Application Program Command */
2218 case '^': /* PM -- Privacy Message */
2219 case ']': /* OSC -- Operating System Command */
2220 case 'k': /* old title set compatibility */
2221 tstrsequence(ascii
);
2223 case 'n': /* LS2 -- Locking shift 2 */
2224 case 'o': /* LS3 -- Locking shift 3 */
2225 term
.charset
= 2 + (ascii
- 'n');
2227 case '(': /* GZD4 -- set primary charset G0 */
2228 case ')': /* G1D4 -- set secondary charset G1 */
2229 case '*': /* G2D4 -- set tertiary charset G2 */
2230 case '+': /* G3D4 -- set quaternary charset G3 */
2231 term
.icharset
= ascii
- '(';
2232 term
.esc
|= ESC_ALTCHARSET
;
2234 case 'D': /* IND -- Linefeed */
2235 if (term
.c
.y
== term
.bot
) {
2236 tscrollup(term
.top
, 1);
2238 tmoveto(term
.c
.x
, term
.c
.y
+1);
2241 case 'E': /* NEL -- Next line */
2242 tnewline(1); /* always go to first col */
2244 case 'H': /* HTS -- Horizontal tab stop */
2245 term
.tabs
[term
.c
.x
] = 1;
2247 case 'M': /* RI -- Reverse index */
2248 if (term
.c
.y
== term
.top
) {
2249 tscrolldown(term
.top
, 1);
2251 tmoveto(term
.c
.x
, term
.c
.y
-1);
2254 case 'Z': /* DECID -- Identify Terminal */
2255 ttywrite(vtiden
, sizeof(vtiden
) - 1);
2257 case 'c': /* RIS -- Reset to inital state */
2262 case '=': /* DECPAM -- Application keypad */
2263 term
.mode
|= MODE_APPKEYPAD
;
2265 case '>': /* DECPNM -- Normal keypad */
2266 term
.mode
&= ~MODE_APPKEYPAD
;
2268 case '7': /* DECSC -- Save Cursor */
2269 tcursor(CURSOR_SAVE
);
2271 case '8': /* DECRC -- Restore Cursor */
2272 tcursor(CURSOR_LOAD
);
2274 case '\\': /* ST -- String Terminator */
2275 if (term
.esc
& ESC_STR_END
)
2279 fprintf(stderr
, "erresc: unknown sequence ESC 0x%02X '%c'\n",
2280 (uchar
) ascii
, isprint(ascii
)? ascii
:'.');
2294 control
= ISCONTROL(u
);
2295 if (!IS_SET(MODE_UTF8
) && !IS_SET(MODE_SIXEL
)) {
2299 len
= utf8encode(u
, c
);
2300 if (!control
&& (width
= wcwidth(u
)) == -1) {
2301 memcpy(c
, "\357\277\275", 4); /* UTF_INVALID */
2306 if (IS_SET(MODE_PRINT
))
2310 * STR sequence must be checked before anything else
2311 * because it uses all following characters until it
2312 * receives a ESC, a SUB, a ST or any other C1 control
2315 if (term
.esc
& ESC_STR
) {
2316 if (u
== '\a' || u
== 030 || u
== 032 || u
== 033 ||
2318 term
.esc
&= ~(ESC_START
|ESC_STR
|ESC_DCS
);
2319 if (IS_SET(MODE_SIXEL
)) {
2320 /* TODO: render sixel */;
2321 term
.mode
&= ~MODE_SIXEL
;
2324 term
.esc
|= ESC_STR_END
;
2325 goto check_control_code
;
2329 if (IS_SET(MODE_SIXEL
)) {
2330 /* TODO: implement sixel mode */
2333 if (term
.esc
&ESC_DCS
&& strescseq
.len
== 0 && u
== 'q')
2334 term
.mode
|= MODE_SIXEL
;
2336 if (strescseq
.len
+len
>= sizeof(strescseq
.buf
)-1) {
2338 * Here is a bug in terminals. If the user never sends
2339 * some code to stop the str or esc command, then st
2340 * will stop responding. But this is better than
2341 * silently failing with unknown characters. At least
2342 * then users will report back.
2344 * In the case users ever get fixed, here is the code:
2353 memmove(&strescseq
.buf
[strescseq
.len
], c
, len
);
2354 strescseq
.len
+= len
;
2360 * Actions of control codes must be performed as soon they arrive
2361 * because they can be embedded inside a control sequence, and
2362 * they must not cause conflicts with sequences.
2367 * control codes are not shown ever
2370 } else if (term
.esc
& ESC_START
) {
2371 if (term
.esc
& ESC_CSI
) {
2372 csiescseq
.buf
[csiescseq
.len
++] = u
;
2373 if (BETWEEN(u
, 0x40, 0x7E)
2374 || csiescseq
.len
>= \
2375 sizeof(csiescseq
.buf
)-1) {
2381 } else if (term
.esc
& ESC_UTF8
) {
2383 } else if (term
.esc
& ESC_ALTCHARSET
) {
2385 } else if (term
.esc
& ESC_TEST
) {
2390 /* sequence already finished */
2394 * All characters which form part of a sequence are not
2399 if (sel
.ob
.x
!= -1 && BETWEEN(term
.c
.y
, sel
.ob
.y
, sel
.oe
.y
))
2402 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2403 if (IS_SET(MODE_WRAP
) && (term
.c
.state
& CURSOR_WRAPNEXT
)) {
2404 gp
->mode
|= ATTR_WRAP
;
2406 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2409 if (IS_SET(MODE_INSERT
) && term
.c
.x
+width
< term
.col
)
2410 memmove(gp
+width
, gp
, (term
.col
- term
.c
.x
- width
) * sizeof(Glyph
));
2412 if (term
.c
.x
+width
> term
.col
) {
2414 gp
= &term
.line
[term
.c
.y
][term
.c
.x
];
2417 tsetchar(u
, &term
.c
.attr
, term
.c
.x
, term
.c
.y
);
2420 gp
->mode
|= ATTR_WIDE
;
2421 if (term
.c
.x
+1 < term
.col
) {
2423 gp
[1].mode
= ATTR_WDUMMY
;
2426 if (term
.c
.x
+width
< term
.col
) {
2427 tmoveto(term
.c
.x
+width
, term
.c
.y
);
2429 term
.c
.state
|= CURSOR_WRAPNEXT
;
2434 tresize(int col
, int row
)
2437 int minrow
= MIN(row
, term
.row
);
2438 int mincol
= MIN(col
, term
.col
);
2442 if (col
< 1 || row
< 1) {
2444 "tresize: error resizing to %dx%d\n", col
, row
);
2449 * slide screen to keep cursor where we expect it -
2450 * tscrollup would work here, but we can optimize to
2451 * memmove because we're freeing the earlier lines
2453 for (i
= 0; i
<= term
.c
.y
- row
; i
++) {
2457 /* ensure that both src and dst are not NULL */
2459 memmove(term
.line
, term
.line
+ i
, row
* sizeof(Line
));
2460 memmove(term
.alt
, term
.alt
+ i
, row
* sizeof(Line
));
2462 for (i
+= row
; i
< term
.row
; i
++) {
2467 /* resize to new width */
2468 term
.specbuf
= xrealloc(term
.specbuf
, col
* sizeof(GlyphFontSpec
));
2470 /* resize to new height */
2471 term
.line
= xrealloc(term
.line
, row
* sizeof(Line
));
2472 term
.alt
= xrealloc(term
.alt
, row
* sizeof(Line
));
2473 term
.dirty
= xrealloc(term
.dirty
, row
* sizeof(*term
.dirty
));
2474 term
.tabs
= xrealloc(term
.tabs
, col
* sizeof(*term
.tabs
));
2476 /* resize each row to new width, zero-pad if needed */
2477 for (i
= 0; i
< minrow
; i
++) {
2478 term
.line
[i
] = xrealloc(term
.line
[i
], col
* sizeof(Glyph
));
2479 term
.alt
[i
] = xrealloc(term
.alt
[i
], col
* sizeof(Glyph
));
2482 /* allocate any new rows */
2483 for (/* i == minrow */; i
< row
; i
++) {
2484 term
.line
[i
] = xmalloc(col
* sizeof(Glyph
));
2485 term
.alt
[i
] = xmalloc(col
* sizeof(Glyph
));
2487 if (col
> term
.col
) {
2488 bp
= term
.tabs
+ term
.col
;
2490 memset(bp
, 0, sizeof(*term
.tabs
) * (col
- term
.col
));
2491 while (--bp
> term
.tabs
&& !*bp
)
2493 for (bp
+= tabspaces
; bp
< term
.tabs
+ col
; bp
+= tabspaces
)
2496 /* update terminal size */
2499 /* reset scrolling region */
2500 tsetscroll(0, row
-1);
2501 /* make use of the LIMIT in tmoveto */
2502 tmoveto(term
.c
.x
, term
.c
.y
);
2503 /* Clearing both screens (it makes dirty all lines) */
2505 for (i
= 0; i
< 2; i
++) {
2506 if (mincol
< col
&& 0 < minrow
) {
2507 tclearregion(mincol
, 0, col
- 1, minrow
- 1);
2509 if (0 < col
&& minrow
< row
) {
2510 tclearregion(0, minrow
, col
- 1, row
- 1);
2513 tcursor(CURSOR_LOAD
);
2519 zoom(const Arg
*arg
)
2523 larg
.f
= usedfontsize
+ arg
->f
;
2528 zoomabs(const Arg
*arg
)
2531 xloadfonts(usedfont
, arg
->f
);
2539 zoomreset(const Arg
*arg
)
2543 if (defaultfontsize
> 0) {
2544 larg
.f
= defaultfontsize
;
2552 xsettitle(opt_title
? opt_title
: "st");
2563 match(uint mask
, uint state
)
2565 return mask
== XK_ANY_MOD
|| mask
== (state
& ~ignoremod
);
2569 numlock(const Arg
*dummy
)
2575 kmap(KeySym k
, uint state
)
2580 /* Check for mapped keys out of X11 function keys. */
2581 for (i
= 0; i
< LEN(mappedkeys
); i
++) {
2582 if (mappedkeys
[i
] == k
)
2585 if (i
== LEN(mappedkeys
)) {
2586 if ((k
& 0xFFFF) < 0xFD00)
2590 for (kp
= key
; kp
< key
+ LEN(key
); kp
++) {
2594 if (!match(kp
->mask
, state
))
2597 if (IS_SET(MODE_APPKEYPAD
) ? kp
->appkey
< 0 : kp
->appkey
> 0)
2599 if (term
.numlock
&& kp
->appkey
== 2)
2602 if (IS_SET(MODE_APPCURSOR
) ? kp
->appcursor
< 0 : kp
->appcursor
> 0)
2605 if (IS_SET(MODE_CRLF
) ? kp
->crlf
< 0 : kp
->crlf
> 0)
2615 cresize(int width
, int height
)
2624 col
= (win
.w
- 2 * borderpx
) / win
.cw
;
2625 row
= (win
.h
- 2 * borderpx
) / win
.ch
;
2634 die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
2635 " [-n name] [-o file]\n"
2636 " [-T title] [-t title] [-w windowid]"
2637 " [[-e] command [args ...]]\n"
2638 " %s [-aiv] [-c class] [-f font] [-g geometry]"
2639 " [-n name] [-o file]\n"
2640 " [-T title] [-t title] [-w windowid] -l line"
2641 " [stty_args ...]\n", argv0
, argv0
);