2 #include <sys/select.h>
16 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
17 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
18 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
21 void cmd(const char *cmdstr, ...);
22 void *emallocz(unsigned int size);
23 void eprint(const char *errstr, ...);
24 void eprintn(const char *errstr, ...);
26 void movea(int x, int y);
27 void mover(int x, int y);
35 unsigned char data[BUFSIZ];
40 int cols = 80, lines = 25;
45 _Bool bold, digit, qmark;
51 if(buf.n < LENGTH(buf.data))
54 buf.s = (buf.s + 1) % LENGTH(buf.data);
55 buf.data[buf.e++] = c;
56 buf.e %= LENGTH(buf.data);
60 cmd(const char *cmdstr, ...) {
66 vfprintf(stdout, cmdstr, ap);
71 emallocz(unsigned int size) {
72 void *res = calloc(1, size);
75 eprint("fatal: could not malloc() %u bytes\n", size);
80 eprint(const char *errstr, ...) {
84 vfprintf(stderr, errstr, ap);
90 eprintn(const char *errstr, ...) {
94 vfprintf(stderr, errstr, ap);
96 fprintf(stderr, ": %s\n", strerror(errno));
101 movea(int x, int y) {
106 cmd("s %d,%d", x, y);
110 mover(int x, int y) {
111 movea(cx + x, cy + y);
119 memset(arg, 0, LENGTH(arg));
124 for(j = 0; j < LENGTH(arg);) {
134 eprint("syntax error\n");
151 mover(0, j ? arg[0] : 1);
154 mover(0, j ? -arg[0] : -1);
157 mover(j ? arg[0] : 1, 0);
160 mover(j ? -arg[0] : -1, 0);
163 /* movel(j ? arg[0] : 1); */
166 /* movel(j ? -arg[0] : -1); */
170 movea(j ? arg[0] : 1, cy);
174 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
176 /* insline(j ? arg[0] : 1); */
179 /* delline(j ? arg[0] : 1); */
184 scroll(j ? arg[0] : 1);
187 scroll(j ? -arg[0] : -1);
190 movea(cx, j ? arg[0] : 1);
193 for(i = 0; i < j; i++) {
194 if(arg[i] >= 30 && arg[i] <= 37)
195 cmd("#%d", arg[i] - 30);
196 if(arg[i] >= 40 && arg[i] <= 47)
197 cmd("|%d", arg[i] - 40);
198 /* xterm bright colors */
199 if(arg[i] >= 90 && arg[i] <= 97)
200 cmd("#%d", arg[i] - 90);
201 if(arg[i] >= 100 && arg[i] <= 107)
202 cmd("|%d", arg[i] - 100);
225 cmd("s %d, %d", cx, cy + l);
230 static char *shell = NULL;
232 if(!shell && !(shell = getenv("SHELL")))
237 eprint("error, cannot fork\n");
240 dup2(pts, STDIN_FILENO);
241 dup2(pts, STDOUT_FILENO);
242 dup2(pts, STDERR_FILENO);
244 putenv("TERM=vt102");
249 signal(SIGCHLD, sigchld);
257 if(waitpid(pid, &ret, 0) == -1)
258 eprintn("error, waiting for child failed");
260 exit(WEXITSTATUS(ret));
269 c = buf.data[buf.s++];
270 buf.s %= LENGTH(buf.data);
276 main(int argc, char *argv[]) {
277 if(argc == 2 && !strcmp("-v", argv[1]))
278 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
280 eprint("usage: st [-v]\n");
283 fptm = fdopen(ptm, "r+");
285 eprintn("cannot open slave pty");