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");
118 #if defined(_XOPEN_SOURCE)
120 if(grantpt(ptm) == -1)
121 eprintn("error, cannot grant access to pty");
122 if(unlockpt(ptm) == -1)
123 eprintn("error, cannot unlock pty");
124 ptsdev = ptsname(ptm);
126 eprintn("error, slave pty name undefined");
127 pts = open(ptsdev, O_RDWR);
129 eprintn("error, cannot open slave pty");
132 eprintn("error, cannot open pty");
137 movea(int x, int y) {
142 cmd("s %d,%d", x, y);
146 mover(int x, int y) {
147 movea(cx + x, cy + y);
155 memset(arg, 0, LENGTH(arg));
161 for(j = 0; j < LENGTH(arg);) {
171 eprint("syntax error");
188 mover(0, j ? arg[0] : 1);
191 mover(0, j ? -arg[0] : -1);
194 mover(j ? arg[0] : 1, 0);
197 mover(j ? -arg[0] : -1, 0);
200 /* movel(j ? arg[0] : 1); */
203 /* movel(j ? -arg[0] : -1); */
207 movea(j ? arg[0] : 1, cy);
211 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
213 /* insline(j ? arg[0] : 1); */
216 /* delline(j ? arg[0] : 1); */
221 scroll(j ? arg[0] : 1);
224 scroll(j ? -arg[0] : -1);
227 movea(cx, j ? arg[0] : 1);
230 for(i = 0; i < j; i++) {
231 if(arg[i] >= 30 && arg[i] <= 37)
232 cmd("#%d", arg[i] - 30);
233 if(arg[i] >= 40 && arg[i] <= 47)
234 cmd("|%d", arg[i] - 40);
235 /* xterm bright colors */
236 if(arg[i] >= 90 && arg[i] <= 97)
237 cmd("#%d", arg[i] - 90);
238 if(arg[i] >= 100 && arg[i] <= 107)
239 cmd("|%d", arg[i] - 100);
262 cmd("s %d, %d", cx, cy + l);
267 static char *shell = NULL;
269 if(!shell && !(shell = getenv("SHELL")))
274 eprint("error, cannot fork\n");
277 dup2(pts, STDIN_FILENO);
278 dup2(pts, STDOUT_FILENO);
279 dup2(pts, STDERR_FILENO);
281 putenv("TERM=vt102");
286 signal(SIGCHLD, sigchld);
294 if(waitpid(pid, &ret, 0) == -1)
295 eprintn("error, waiting for child failed");
297 exit(WEXITSTATUS(ret));
306 c = buf.data[buf.s++];
307 buf.s %= LENGTH(buf.data);
313 main(int argc, char *argv[]) {
315 if(argc == 2 && !strcmp("-v", argv[1]))
316 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
318 eprint("usage: st [-v]\n");