1 /* See LICENSE file for copyright and license details. */
14 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
15 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
16 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
18 static void buffer(char c);
19 static void cmd(const char *cmdstr, ...);
22 static void movea(int x, int y);
23 static void mover(int x, int y);
24 static void parseesc(void);
25 static void scroll(int l);
26 static void shell(void);
27 static void sigchld(int n);
28 static char unbuffer(void);
29 static void ungetch(int c);
32 unsigned char data[BUFSIZ];
38 unsigned char data[BUFSIZ];
42 static int cols = 80, lines = 25;
43 static int cx = 0, cy = 0;
46 static _Bool bold, digit, qmark;
48 static RingBuffer buf;
49 static ReadBuffer rbuf;
53 if(buf.n < LENGTH(buf.data))
56 buf.s = (buf.s + 1) % LENGTH(buf.data);
57 buf.data[buf.e++] = c;
58 buf.e %= LENGTH(buf.data);
62 cmd(const char *cmdstr, ...) {
68 vfprintf(stdout, cmdstr, ap);
74 if(rbuf.i++ >= rbuf.n) {
75 rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data));
77 err(EXIT_FAILURE, "cannot read from slave pty");
80 return rbuf.data[rbuf.i];
89 cmd("seek(%d,%d)", x, y);
94 movea(cx + x, cy + y);
102 memset(arg, 0, LENGTH(arg));
107 for(j = 0; j < LENGTH(arg);) {
117 errx(EXIT_FAILURE, "syntax error");
134 mover(0, j ? arg[0] : 1);
137 mover(0, j ? -arg[0] : -1);
140 mover(j ? arg[0] : 1, 0);
143 mover(j ? -arg[0] : -1, 0);
146 /* movel(j ? arg[0] : 1); */
149 /* movel(j ? -arg[0] : -1); */
153 movea(j ? arg[0] : 1, cy);
157 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
159 /* insline(j ? arg[0] : 1); */
162 /* delline(j ? arg[0] : 1); */
167 scroll(j ? arg[0] : 1);
170 scroll(j ? -arg[0] : -1);
173 movea(cx, j ? arg[0] : 1);
176 for(i = 0; i < j; i++) {
177 if(arg[i] >= 30 && arg[i] <= 37)
178 cmd("#%d", arg[i] - 30);
179 if(arg[i] >= 40 && arg[i] <= 47)
180 cmd("|%d", arg[i] - 40);
181 /* xterm bright colors */
182 if(arg[i] >= 90 && arg[i] <= 97)
183 cmd("#%d", arg[i] - 90);
184 if(arg[i] >= 100 && arg[i] <= 107)
185 cmd("|%d", arg[i] - 100);
208 cmd("seek(%d,%d)", cx, cy + l);
213 static char *shell = NULL;
215 if(!shell && !(shell = getenv("SHELL")))
220 err(EXIT_FAILURE, "cannot fork");
223 dup2(pts, STDIN_FILENO);
224 dup2(pts, STDOUT_FILENO);
225 dup2(pts, STDERR_FILENO);
227 putenv("TERM=vt102");
232 signal(SIGCHLD, sigchld);
240 if(waitpid(pid, &ret, 0) == -1)
241 err(EXIT_FAILURE, "waiting for child failed");
243 exit(WEXITSTATUS(ret));
252 c = buf.data[buf.s++];
253 buf.s %= LENGTH(buf.data);
260 if(rbuf.i + 1 >= rbuf.n)
261 errx(EXIT_FAILURE, "read buffer full");
262 rbuf.data[rbuf.i++] = c;
266 main(int argc, char *argv[]) {
270 if(argc == 2 && !strcmp("-v", argv[1])) {
271 fprintf(stderr, "std-"VERSION", © 2008 Matthias-Christian Ott\n");
275 fprintf(stderr, "usage: st [-v]\n");
281 FD_SET(STDIN_FILENO, &rfds);
284 r = select(ptm + 1, &rfds, NULL, NULL, NULL);
286 err(EXIT_FAILURE, "cannot select");
287 if(FD_ISSET(ptm, &rfds)) {
297 } while(rbuf.i < rbuf.n);