1 /* See LICENSE file for copyright and license details. */
3 #include <sys/select.h>
17 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
18 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
19 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
22 void cmd(const char *cmdstr, ...);
23 void *emallocz(unsigned int size);
24 void eprint(const char *errstr, ...);
25 void eprintn(const char *errstr, ...);
27 void movea(int x, int y);
28 void mover(int x, int y);
36 unsigned char data[BUFSIZ];
41 int cols = 80, lines = 25;
46 _Bool bold, digit, qmark;
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);
72 emallocz(unsigned int size) {
73 void *res = calloc(1, size);
76 eprint("fatal: could not malloc() %u bytes\n", size);
81 eprint(const char *errstr, ...) {
85 vfprintf(stderr, errstr, ap);
91 eprintn(const char *errstr, ...) {
95 vfprintf(stderr, errstr, ap);
97 fprintf(stderr, ": %s\n", strerror(errno));
102 movea(int x, int y) {
107 cmd("s %d,%d", x, y);
111 mover(int x, int y) {
112 movea(cx + x, cy + y);
120 memset(arg, 0, LENGTH(arg));
125 for(j = 0; j < LENGTH(arg);) {
135 eprint("syntax error\n");
152 mover(0, j ? arg[0] : 1);
155 mover(0, j ? -arg[0] : -1);
158 mover(j ? arg[0] : 1, 0);
161 mover(j ? -arg[0] : -1, 0);
164 /* movel(j ? arg[0] : 1); */
167 /* movel(j ? -arg[0] : -1); */
171 movea(j ? arg[0] : 1, cy);
175 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
177 /* insline(j ? arg[0] : 1); */
180 /* delline(j ? arg[0] : 1); */
185 scroll(j ? arg[0] : 1);
188 scroll(j ? -arg[0] : -1);
191 movea(cx, j ? arg[0] : 1);
194 for(i = 0; i < j; i++) {
195 if(arg[i] >= 30 && arg[i] <= 37)
196 cmd("#%d", arg[i] - 30);
197 if(arg[i] >= 40 && arg[i] <= 47)
198 cmd("|%d", arg[i] - 40);
199 /* xterm bright colors */
200 if(arg[i] >= 90 && arg[i] <= 97)
201 cmd("#%d", arg[i] - 90);
202 if(arg[i] >= 100 && arg[i] <= 107)
203 cmd("|%d", arg[i] - 100);
226 cmd("s %d, %d", cx, cy + l);
231 static char *shell = NULL;
233 if(!shell && !(shell = getenv("SHELL")))
238 eprint("error, cannot fork\n");
241 dup2(pts, STDIN_FILENO);
242 dup2(pts, STDOUT_FILENO);
243 dup2(pts, STDERR_FILENO);
245 putenv("TERM=vt102");
250 signal(SIGCHLD, sigchld);
258 if(waitpid(pid, &ret, 0) == -1)
259 eprintn("error, waiting for child failed");
261 exit(WEXITSTATUS(ret));
270 c = buf.data[buf.s++];
271 buf.s %= LENGTH(buf.data);
277 main(int argc, char *argv[]) {
278 if(argc == 2 && !strcmp("-v", argv[1]))
279 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
281 eprint("usage: st [-v]\n");
284 fptm = fdopen(ptm, "r+");
286 eprintn("cannot open slave pty");