Xinqi Bao's Git
84a4b49c503eabcbe901f049ec0865a3f74f3751
1 /* See LICENSE file for copyright and license details.
3 * Simple terminal daemon is a terminal emulator. It can be used in
4 * combination with simple terminal to emulate a mostly VT100-compatible
7 * In this process std works like a filter. It reads data from a
8 * pseudo-terminal and parses the escape sequences and transforms
9 * them into an ed(1)-like. The resulting data is buffered and written
11 * Parallely it reads data from stdin and parses and executes the
12 * commands. The resulting data is written to the pseudo-terminal.
14 #include <sys/types.h>
19 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
29 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
30 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
31 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
34 unsigned char data
[BUFSIZ
];
40 unsigned char data
[BUFSIZ
];
45 static void buffer(char c
);
46 static void cmd(const char *cmdstr
, ...);
47 static int getch(ReadBuffer
*buf
);
48 static void getpty(void);
49 static void movea(int x
, int y
);
50 static void mover(int x
, int y
);
51 static void parsecmd(void);
52 static void parseesc(void);
53 static void scroll(int l
);
54 static void shell(void);
55 static void sigchld(int n
);
56 static char unbuffer(void);
57 static void ungetch(ReadBuffer
*buf
, int c
);
59 static int cols
= 80, lines
= 25;
60 static int cx
= 0, cy
= 0;
63 static _Bool bold
, digit
, qmark
;
65 static RingBuffer buf
;
66 static ReadBuffer cmdbuf
, ptmbuf
;
70 if(buf
.n
< LENGTH(buf
.data
))
73 buf
.s
= (buf
.s
+ 1) % LENGTH(buf
.data
);
74 buf
.data
[buf
.e
++] = c
;
75 buf
.e
%= LENGTH(buf
.data
);
79 cmd(const char *cmdstr
, ...) {
85 vfprintf(stdout
, cmdstr
, ap
);
90 getch(ReadBuffer
*buf
) {
91 if(buf
->i
++ >= buf
->n
) {
92 buf
->n
= read(buf
->fd
, buf
->data
, BUFSIZ
);
94 err(EXIT_FAILURE
, "cannot read");
97 return buf
->data
[buf
->i
];
101 movea(int x
, int y
) {
106 cmd("seek(%d,%d)", x
, y
);
110 mover(int x
, int y
) {
111 movea(cx
+ x
, cy
+ y
);
123 memset(arg
, 0, LENGTH(arg
));
128 for(j
= 0; j
< LENGTH(arg
);) {
138 errx(EXIT_FAILURE
, "syntax error");
155 mover(0, j
? arg
[0] : 1);
158 mover(0, j
? -arg
[0] : -1);
161 mover(j
? arg
[0] : 1, 0);
164 mover(j
? -arg
[0] : -1, 0);
167 /* movel(j ? arg[0] : 1); */
170 /* movel(j ? -arg[0] : -1); */
174 movea(j
? arg
[0] : 1, cy
);
178 movea(arg
[1] ? arg
[1] : 1, arg
[0] ? arg
[0] : 1);
180 /* insline(j ? arg[0] : 1); */
183 /* delline(j ? arg[0] : 1); */
188 scroll(j
? arg
[0] : 1);
191 scroll(j
? -arg
[0] : -1);
194 movea(cx
, j
? arg
[0] : 1);
197 for(i
= 0; i
< j
; i
++) {
198 if(arg
[i
] >= 30 && arg
[i
] <= 37)
199 cmd("#%d", arg
[i
] - 30);
200 if(arg
[i
] >= 40 && arg
[i
] <= 47)
201 cmd("|%d", arg
[i
] - 40);
202 /* xterm bright colors */
203 if(arg
[i
] >= 90 && arg
[i
] <= 97)
204 cmd("#%d", arg
[i
] - 90);
205 if(arg
[i
] >= 100 && arg
[i
] <= 107)
206 cmd("|%d", arg
[i
] - 100);
229 cmd("seek(%d,%d)", cx
, cy
+ l
);
236 #if defined(_GNU_SOURCE)
238 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
239 ptm
= posix_openpt(O_RDWR
);
241 ptm
= open("/dev/ptmx", O_RDWR
);
243 if(openpty(&ptm
, &pts
, NULL
, NULL
, NULL
) == -1)
244 err(EXIT_FAILURE
, "cannot open pty");
246 #if defined(_XOPEN_SOURCE)
248 if(grantpt(ptm
) == -1)
249 err(EXIT_FAILURE
, "cannot grant access to pty");
250 if(unlockpt(ptm
) == -1)
251 err(EXIT_FAILURE
, "cannot unlock pty");
252 ptsdev
= ptsname(ptm
);
254 err(EXIT_FAILURE
, "slave pty name undefined");
255 pts
= open(ptsdev
, O_RDWR
);
257 err(EXIT_FAILURE
, "cannot open slave pty");
260 err(EXIT_FAILURE
, "cannot open pty");
266 static char *shell
= NULL
;
268 if(!shell
&& !(shell
= getenv("SHELL")))
273 err(EXIT_FAILURE
, "cannot fork");
276 dup2(pts
, STDIN_FILENO
);
277 dup2(pts
, STDOUT_FILENO
);
278 dup2(pts
, STDERR_FILENO
);
280 putenv("TERM=vt102");
285 signal(SIGCHLD
, sigchld
);
293 if(waitpid(pid
, &ret
, 0) == -1)
294 err(EXIT_FAILURE
, "waiting for child failed");
296 exit(WEXITSTATUS(ret
));
305 c
= buf
.data
[buf
.s
++];
306 buf
.s
%= LENGTH(buf
.data
);
312 ungetch(ReadBuffer
*buf
, int c
) {
313 if(buf
->i
+ 1 >= buf
->n
)
314 errx(EXIT_FAILURE
, "buffer full");
315 buf
->data
[buf
->i
++] = c
;
319 main(int argc
, char *argv
[]) {
322 if(argc
== 2 && !strcmp("-v", argv
[1]))
323 errx(EXIT_SUCCESS
, "std-"VERSION
", © 2008 Matthias-Christian Ott");
325 errx(EXIT_FAILURE
, "usage: std [-v]");
328 cmdbuf
.fd
= STDIN_FILENO
;
331 FD_SET(STDIN_FILENO
, &rfds
);
334 if(select(ptm
+ 1, &rfds
, NULL
, NULL
, NULL
) == -1)
335 err(EXIT_FAILURE
, "cannot select");
336 if(FD_ISSET(STDIN_FILENO
, &rfds
))
346 } while(cmdbuf
.i
< cmdbuf
.n
);
347 if(FD_ISSET(ptm
, &rfds
)) {
357 } while(ptmbuf
.i
< ptmbuf
.n
);