1 /* See LICENSE file for copyright and license details. */
7 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
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))
21 static void buffer(char c);
22 static void cmd(const char *cmdstr, ...);
24 static void getpty(void);
25 static void movea(int x, int y);
26 static void mover(int x, int y);
27 static void parseesc(void);
28 static void scroll(int l);
29 static void shell(void);
30 static void sigchld(int n);
31 static char unbuffer(void);
32 static void ungetch(int c);
35 unsigned char data[BUFSIZ];
41 unsigned char data[BUFSIZ];
45 static int cols = 80, lines = 25;
46 static int cx = 0, cy = 0;
49 static _Bool bold, digit, qmark;
51 static RingBuffer buf;
52 static ReadBuffer rbuf;
56 if(buf.n < LENGTH(buf.data))
59 buf.s = (buf.s + 1) % LENGTH(buf.data);
60 buf.data[buf.e++] = c;
61 buf.e %= LENGTH(buf.data);
65 cmd(const char *cmdstr, ...) {
71 vfprintf(stdout, cmdstr, ap);
77 if(rbuf.i++ >= rbuf.n) {
78 rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data));
80 err(EXIT_FAILURE, "cannot read from slave pty");
83 return rbuf.data[rbuf.i];
92 cmd("seek(%d,%d)", x, y);
97 movea(cx + x, cy + y);
105 memset(arg, 0, LENGTH(arg));
110 for(j = 0; j < LENGTH(arg);) {
120 errx(EXIT_FAILURE, "syntax error");
137 mover(0, j ? arg[0] : 1);
140 mover(0, j ? -arg[0] : -1);
143 mover(j ? arg[0] : 1, 0);
146 mover(j ? -arg[0] : -1, 0);
149 /* movel(j ? arg[0] : 1); */
152 /* movel(j ? -arg[0] : -1); */
156 movea(j ? arg[0] : 1, cy);
160 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
162 /* insline(j ? arg[0] : 1); */
165 /* delline(j ? arg[0] : 1); */
170 scroll(j ? arg[0] : 1);
173 scroll(j ? -arg[0] : -1);
176 movea(cx, j ? arg[0] : 1);
179 for(i = 0; i < j; i++) {
180 if(arg[i] >= 30 && arg[i] <= 37)
181 cmd("#%d", arg[i] - 30);
182 if(arg[i] >= 40 && arg[i] <= 47)
183 cmd("|%d", arg[i] - 40);
184 /* xterm bright colors */
185 if(arg[i] >= 90 && arg[i] <= 97)
186 cmd("#%d", arg[i] - 90);
187 if(arg[i] >= 100 && arg[i] <= 107)
188 cmd("|%d", arg[i] - 100);
211 cmd("seek(%d,%d)", cx, cy + l);
218 #if defined(_GNU_SOURCE)
220 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
221 ptm = posix_openpt(O_RDWR);
223 ptm = open("/dev/ptmx", O_RDWR);
225 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
226 err(EXIT_FAILURE, "cannot open pty");
228 #if defined(_XOPEN_SOURCE)
230 if(grantpt(ptm) == -1)
231 err(EXIT_FAILURE, "cannot grant access to pty");
232 if(unlockpt(ptm) == -1)
233 err(EXIT_FAILURE, "cannot unlock pty");
234 ptsdev = ptsname(ptm);
236 err(EXIT_FAILURE, "slave pty name undefined");
237 pts = open(ptsdev, O_RDWR);
239 err(EXIT_FAILURE, "cannot open slave pty");
242 err(EXIT_FAILURE, "cannot open pty");
248 static char *shell = NULL;
250 if(!shell && !(shell = getenv("SHELL")))
255 err(EXIT_FAILURE, "cannot fork");
258 dup2(pts, STDIN_FILENO);
259 dup2(pts, STDOUT_FILENO);
260 dup2(pts, STDERR_FILENO);
262 putenv("TERM=vt102");
267 signal(SIGCHLD, sigchld);
275 if(waitpid(pid, &ret, 0) == -1)
276 err(EXIT_FAILURE, "waiting for child failed");
278 exit(WEXITSTATUS(ret));
287 c = buf.data[buf.s++];
288 buf.s %= LENGTH(buf.data);
295 if(rbuf.i + 1 >= rbuf.n)
296 errx(EXIT_FAILURE, "read buffer full");
297 rbuf.data[rbuf.i++] = c;
301 main(int argc, char *argv[]) {
304 if(argc == 2 && !strcmp("-v", argv[1]))
305 errx(EXIT_SUCCESS, "std-"VERSION", © 2008 Matthias-Christian Ott");
307 errx(EXIT_FAILURE, "usage: std [-v]");
311 FD_SET(STDIN_FILENO, &rfds);
314 if(select(ptm + 1, &rfds, NULL, NULL, NULL) == -1)
315 err(EXIT_FAILURE, "cannot select");
316 if(FD_ISSET(ptm, &rfds)) {
326 } while(rbuf.i < rbuf.n);