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);
34 enum { QuestionMark = 1, Digit = 2 };
37 unsigned char data[BUFSIZ];
42 int cols = 80, lines = 25;
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);
73 emallocz(unsigned int size) {
74 void *res = calloc(1, size);
77 eprint("fatal: could not malloc() %u bytes\n", size);
82 eprint(const char *errstr, ...) {
86 vfprintf(stderr, errstr, ap);
92 eprintn(const char *errstr, ...) {
96 vfprintf(stderr, errstr, ap);
98 fprintf(stderr, ": %s\n", strerror(errno));
106 #if defined(_GNU_SOURCE)
108 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
109 ptm = posix_openpt(O_RDWR);
111 ptm = open("/dev/ptmx", O_RDWR);
113 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
114 eprintn("error, cannot open pty");
116 #if defined(_XOPEN_SOURCE)
118 if(grantpt(ptm) == -1)
119 eprintn("error, cannot grant access to pty");
120 if(unlockpt(ptm) == -1)
121 eprintn("error, cannot unlock pty");
122 ptsdev = ptsname(ptm);
124 eprintn("error, slave pty name undefined");
125 pts = open(ptsdev, O_RDWR);
127 eprintn("error, cannot open slave pty");
130 eprintn("error, cannot open pty");
135 movea(int x, int y) {
140 cmd("s %d,%d", x, y);
144 mover(int x, int y) {
145 movea(cx + x, cy + y);
153 memset(arg, 0, LENGTH(arg));
159 for(j = 0; j < LENGTH(arg);) {
169 eprint("syntax error\n");
186 mover(0, j ? arg[0] : 1);
189 mover(0, j ? -arg[0] : -1);
192 mover(j ? arg[0] : 1, 0);
195 mover(j ? -arg[0] : -1, 0);
198 /* movel(j ? arg[0] : 1); */
201 /* movel(j ? -arg[0] : -1); */
205 movea(j ? arg[0] : 1, cy);
209 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
211 /* insline(j ? arg[0] : 1); */
214 /* delline(j ? arg[0] : 1); */
219 scroll(j ? arg[0] : 1);
222 scroll(j ? -arg[0] : -1);
225 movea(cx, j ? arg[0] : 1);
228 for(i = 0; i < j; i++) {
229 if(arg[i] >= 30 && arg[i] <= 37)
230 cmd("#%d", arg[i] - 30);
231 if(arg[i] >= 40 && arg[i] <= 47)
232 cmd("|%d", arg[i] - 40);
233 /* xterm bright colors */
234 if(arg[i] >= 90 && arg[i] <= 97)
235 cmd("#%d", arg[i] - 90);
236 if(arg[i] >= 100 && arg[i] <= 107)
237 cmd("|%d", arg[i] - 100);
260 cmd("s %d, %d", cx, cy + l);
265 static char *shell = NULL;
267 if(!shell && !(shell = getenv("SHELL")))
272 eprint("error, cannot fork\n");
275 dup2(pts, STDIN_FILENO);
276 dup2(pts, STDOUT_FILENO);
277 dup2(pts, STDERR_FILENO);
279 putenv("TERM=vt102");
284 signal(SIGCHLD, sigchld);
292 if(waitpid(pid, &ret, 0) == -1)
293 eprintn("error, waiting for child failed");
295 exit(WEXITSTATUS(ret));
304 c = buf.data[buf.s++];
305 buf.s %= LENGTH(buf.data);
311 main(int argc, char *argv[]) {
313 if(argc == 2 && !strcmp("-v", argv[1]))
314 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
316 eprint("usage: st [-v]\n");