1 /* See LICENSE file for copyright and license details. */
13 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
14 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
15 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
17 static void buffer(char c);
18 static void cmd(const char *cmdstr, ...);
21 static void movea(int x, int y);
22 static void mover(int x, int y);
23 static void parseesc(void);
24 static void scroll(int l);
25 static void shell(void);
26 static void sigchld(int n);
27 static char unbuffer(void);
28 static void ungetch(int c);
31 unsigned char data[BUFSIZ];
37 unsigned char data[BUFSIZ];
41 static int cols = 80, lines = 25;
42 static int cx = 0, cy = 0;
45 static _Bool bold, digit, qmark;
47 static RingBuffer buf;
48 static ReadBuffer rbuf;
52 if(buf.n < LENGTH(buf.data))
55 buf.s = (buf.s + 1) % LENGTH(buf.data);
56 buf.data[buf.e++] = c;
57 buf.e %= LENGTH(buf.data);
61 cmd(const char *cmdstr, ...) {
67 vfprintf(stdout, cmdstr, ap);
73 if(rbuf.i++ >= rbuf.n) {
74 rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data));
76 eprintn("error, cannot read from slave pty");
79 return rbuf.data[rbuf.i];
88 cmd("seek(%d,%d)", x, y);
93 movea(cx + x, cy + y);
101 memset(arg, 0, LENGTH(arg));
106 for(j = 0; j < LENGTH(arg);) {
116 eprint("syntax error\n");
133 mover(0, j ? arg[0] : 1);
136 mover(0, j ? -arg[0] : -1);
139 mover(j ? arg[0] : 1, 0);
142 mover(j ? -arg[0] : -1, 0);
145 /* movel(j ? arg[0] : 1); */
148 /* movel(j ? -arg[0] : -1); */
152 movea(j ? arg[0] : 1, cy);
156 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
158 /* insline(j ? arg[0] : 1); */
161 /* delline(j ? arg[0] : 1); */
166 scroll(j ? arg[0] : 1);
169 scroll(j ? -arg[0] : -1);
172 movea(cx, j ? arg[0] : 1);
175 for(i = 0; i < j; i++) {
176 if(arg[i] >= 30 && arg[i] <= 37)
177 cmd("#%d", arg[i] - 30);
178 if(arg[i] >= 40 && arg[i] <= 47)
179 cmd("|%d", arg[i] - 40);
180 /* xterm bright colors */
181 if(arg[i] >= 90 && arg[i] <= 97)
182 cmd("#%d", arg[i] - 90);
183 if(arg[i] >= 100 && arg[i] <= 107)
184 cmd("|%d", arg[i] - 100);
207 cmd("seek(%d,%d)", cx, cy + l);
212 static char *shell = NULL;
214 if(!shell && !(shell = getenv("SHELL")))
219 eprint("error, cannot fork\n");
222 dup2(pts, STDIN_FILENO);
223 dup2(pts, STDOUT_FILENO);
224 dup2(pts, STDERR_FILENO);
226 putenv("TERM=vt102");
231 signal(SIGCHLD, sigchld);
239 if(waitpid(pid, &ret, 0) == -1)
240 eprintn("error, waiting for child failed");
242 exit(WEXITSTATUS(ret));
251 c = buf.data[buf.s++];
252 buf.s %= LENGTH(buf.data);
259 if(rbuf.i + 1 >= rbuf.n)
260 eprint("error, read buffer full\n");
261 rbuf.data[rbuf.i++] = c;
265 main(int argc, char *argv[]) {
269 if(argc == 2 && !strcmp("-v", argv[1]))
270 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
272 eprint("usage: st [-v]\n");
276 FD_SET(STDIN_FILENO, &rfds);
279 r = select(ptm + 1, &rfds, NULL, NULL, NULL);
281 eprintn("error, cannot select");
282 if(FD_ISSET(ptm, &rfds)) {
292 } while(rbuf.i < rbuf.n);