Xinqi Bao's Git
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>
20 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
30 #define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
31 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
32 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
35 unsigned char data
[BUFSIZ
];
40 static void buffer(char c
);
41 static void cmd(const char *cmdstr
, ...);
42 static void getpty(void);
43 static void movea(int x
, int y
);
44 static void mover(int x
, int y
);
45 static void parsecmd(void);
46 static void parseesc(void);
47 static void scroll(int l
);
48 static void shell(void);
49 static void sigchld(int n
);
50 static char unbuffer(void);
52 static int cols
= 80, lines
= 25;
53 static int cx
= 0, cy
= 0;
56 static _Bool bold
, digit
, qmark
;
58 static RingBuffer buf
;
63 if(buf
.n
< LENGTH(buf
.data
))
66 buf
.s
= (buf
.s
+ 1) % LENGTH(buf
.data
);
67 buf
.data
[buf
.e
++] = c
;
68 buf
.e
%= LENGTH(buf
.data
);
72 cmd(const char *cmdstr
, ...) {
78 vfprintf(stdout
, cmdstr
, ap
);
88 cmd("seek(%d,%d)", x
, y
);
93 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
);
294 main(int argc
, char *argv
[]) {
297 if(argc
== 2 && !strcmp("-v", argv
[1]))
298 errx(EXIT_SUCCESS
, "std-"VERSION
", © 2008 Matthias-Christian Ott");
300 errx(EXIT_FAILURE
, "usage: std [-v]");
304 FD_SET(STDIN_FILENO
, &rfds
);
306 if(!(fptm
= fdopen(ptm
, "r+")))
307 err(EXIT_FAILURE
, "cannot open pty");
308 if(fcntl(ptm
, F_SETFL
, O_NONBLOCK
) == -1)
309 err(EXIT_FAILURE
, "cannot set pty to non-blocking mode");
310 if(fcntl(STDIN_FILENO
, F_SETFL
, O_NONBLOCK
) == -1)
311 err(EXIT_FAILURE
, "cannot set stdin to non-blocking mode");
313 if(select(MAX(ptm
, STDIN_FILENO
) + 1, &rfds
, NULL
, NULL
, NULL
) == -1)
314 err(EXIT_FAILURE
, "cannot select");
315 if(FD_ISSET(ptm
, &rfds
)) {
317 if((c
= getc(fptm
)) == EOF
) {
324 err(EXIT_FAILURE
, "cannot read from pty");
338 if(FD_ISSET(STDIN_FILENO
, &rfds
)) {
340 if((c
= getchar()) == EOF
) {
342 FD_CLR(STDIN_FILENO
, &rfds
);
347 err(EXIT_FAILURE
, "cannot read from stdin");