2 #include <sys/select.h>
9 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
19 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
20 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
21 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
24 void cmd(const char *cmdstr, ...);
25 void *emallocz(unsigned int size);
26 void eprint(const char *errstr, ...);
27 void eprintn(const char *errstr, ...);
29 void movea(int x, int y);
30 void mover(int x, int y);
38 unsigned char data[BUFSIZ];
43 int cols = 80, lines = 25;
48 _Bool bold, digit, qmark;
54 if(buf.n < LENGTH(buf.data))
57 buf.s = (buf.s + 1) % LENGTH(buf.data);
58 buf.data[buf.e++] = c;
59 buf.e %= LENGTH(buf.data);
63 cmd(const char *cmdstr, ...) {
69 vfprintf(stdout, cmdstr, ap);
74 emallocz(unsigned int size) {
75 void *res = calloc(1, size);
78 eprint("fatal: could not malloc() %u bytes\n", size);
83 eprint(const char *errstr, ...) {
87 vfprintf(stderr, errstr, ap);
93 eprintn(const char *errstr, ...) {
97 vfprintf(stderr, errstr, ap);
99 fprintf(stderr, ": %s\n", strerror(errno));
107 #if defined(_GNU_SOURCE)
109 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
110 ptm = posix_openpt(O_RDWR);
112 ptm = open("/dev/ptmx", O_RDWR);
114 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
115 eprintn("error, cannot open pty");
117 #if defined(_XOPEN_SOURCE)
119 if(grantpt(ptm) == -1)
120 eprintn("error, cannot grant access to pty");
121 if(unlockpt(ptm) == -1)
122 eprintn("error, cannot unlock pty");
123 ptsdev = ptsname(ptm);
125 eprintn("error, slave pty name undefined");
126 pts = open(ptsdev, O_RDWR);
128 eprintn("error, cannot open slave pty");
131 eprintn("error, cannot open pty");
136 movea(int x, int y) {
141 cmd("s %d,%d", x, y);
145 mover(int x, int y) {
146 movea(cx + x, cy + y);
154 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[]) {
312 if(argc == 2 && !strcmp("-v", argv[1]))
313 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
315 eprint("usage: st [-v]\n");
318 fptm = fdopen(ptm, "r+");
320 eprintn("cannot open slave pty");