Xinqi Bao's Git

reunite pty.c with std.c
[st.git] / std.c
1 /* See LICENSE file for copyright and license details. */
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <ctype.h>
5 #include <err.h>
6 #include <fcntl.h>
7 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
8 #include <pty.h>
9 #endif
10 #include <signal.h>
11 #include <stdarg.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16
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))
20
21 static void buffer(char c);
22 static void cmd(const char *cmdstr, ...);
23 static int getch();
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);
33
34 typedef struct {
35 unsigned char data[BUFSIZ];
36 int s, e;
37 int n;
38 } RingBuffer;
39
40 typedef struct {
41 unsigned char data[BUFSIZ];
42 int i, n;
43 } ReadBuffer;
44
45 static int cols = 80, lines = 25;
46 static int cx = 0, cy = 0;
47 static int c;
48 static int ptm, pts;
49 static _Bool bold, digit, qmark;
50 static pid_t pid;
51 static RingBuffer buf;
52 static ReadBuffer rbuf;
53
54 void
55 buffer(char c) {
56 if(buf.n < LENGTH(buf.data))
57 buf.n++;
58 else
59 buf.s = (buf.s + 1) % LENGTH(buf.data);
60 buf.data[buf.e++] = c;
61 buf.e %= LENGTH(buf.data);
62 }
63
64 void
65 cmd(const char *cmdstr, ...) {
66 va_list ap;
67
68 putchar('\n');
69 putchar(':');
70 va_start(ap, cmdstr);
71 vfprintf(stdout, cmdstr, ap);
72 va_end(ap);
73 }
74
75 int
76 getch() {
77 if(rbuf.i++ >= rbuf.n) {
78 rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data));
79 if(rbuf.n == -1)
80 err(EXIT_FAILURE, "cannot read from slave pty");
81 rbuf.i = 0;
82 }
83 return rbuf.data[rbuf.i];
84 }
85
86 void
87 movea(int x, int y) {
88 x = MAX(x, cols);
89 y = MAX(y, lines);
90 cx = x;
91 cy = y;
92 cmd("seek(%d,%d)", x, y);
93 }
94
95 void
96 mover(int x, int y) {
97 movea(cx + x, cy + y);
98 }
99
100 void
101 parseesc(void) {
102 int i, j;
103 int arg[16];
104
105 memset(arg, 0, LENGTH(arg));
106 c = getch();
107 switch(c) {
108 case '[':
109 c = getch();
110 for(j = 0; j < LENGTH(arg);) {
111 if(isdigit(c)) {
112 digit = 1;
113 arg[j] *= 10;
114 arg[j] += c - '0';
115 }
116 else if(c == '?')
117 qmark = 1;
118 else if(c == ';') {
119 if(!digit)
120 errx(EXIT_FAILURE, "syntax error");
121 digit = 0;
122 j++;
123 }
124 else {
125 if(digit) {
126 digit = 0;
127 j++;
128 }
129 break;
130 }
131 c = getch();
132 }
133 switch(c) {
134 case '@':
135 break;
136 case 'A':
137 mover(0, j ? arg[0] : 1);
138 break;
139 case 'B':
140 mover(0, j ? -arg[0] : -1);
141 break;
142 case 'C':
143 mover(j ? arg[0] : 1, 0);
144 break;
145 case 'D':
146 mover(j ? -arg[0] : -1, 0);
147 break;
148 case 'E':
149 /* movel(j ? arg[0] : 1); */
150 break;
151 case 'F':
152 /* movel(j ? -arg[0] : -1); */
153 break;
154 case '`':
155 case 'G':
156 movea(j ? arg[0] : 1, cy);
157 break;
158 case 'f':
159 case 'H':
160 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
161 case 'L':
162 /* insline(j ? arg[0] : 1); */
163 break;
164 case 'M':
165 /* delline(j ? arg[0] : 1); */
166 break;
167 case 'P':
168 break;
169 case 'S':
170 scroll(j ? arg[0] : 1);
171 break;
172 case 'T':
173 scroll(j ? -arg[0] : -1);
174 break;
175 case 'd':
176 movea(cx, j ? arg[0] : 1);
177 break;
178 case 'm':
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);
189 switch(arg[i]) {
190 case 0:
191 case 22:
192 if(bold)
193 cmd("bold");
194 case 1:
195 if(!bold)
196 cmd("bold");
197 break;
198 }
199 }
200 break;
201 }
202 break;
203 default:
204 putchar('\033');
205 ungetch(c);
206 }
207 }
208
209 void
210 scroll(int l) {
211 cmd("seek(%d,%d)", cx, cy + l);
212 }
213
214 void
215 getpty(void) {
216 char *ptsdev;
217
218 #if defined(_GNU_SOURCE)
219 ptm = getpt();
220 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
221 ptm = posix_openpt(O_RDWR);
222 #else
223 ptm = open("/dev/ptmx", O_RDWR);
224 if(ptm == -1)
225 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
226 err(EXIT_FAILURE, "cannot open pty");
227 #endif
228 #if defined(_XOPEN_SOURCE)
229 if(ptm != -1) {
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);
235 if(!ptsdev)
236 err(EXIT_FAILURE, "slave pty name undefined");
237 pts = open(ptsdev, O_RDWR);
238 if(pts == -1)
239 err(EXIT_FAILURE, "cannot open slave pty");
240 }
241 else
242 err(EXIT_FAILURE, "cannot open pty");
243 #endif
244 }
245
246 void
247 shell(void) {
248 static char *shell = NULL;
249
250 if(!shell && !(shell = getenv("SHELL")))
251 shell = "/bin/sh";
252 pid = fork();
253 switch(pid) {
254 case -1:
255 err(EXIT_FAILURE, "cannot fork");
256 case 0:
257 setsid();
258 dup2(pts, STDIN_FILENO);
259 dup2(pts, STDOUT_FILENO);
260 dup2(pts, STDERR_FILENO);
261 close(ptm);
262 putenv("TERM=vt102");
263 execvp(shell, NULL);
264 break;
265 default:
266 close(pts);
267 signal(SIGCHLD, sigchld);
268 }
269 }
270
271 void
272 sigchld(int n) {
273 int ret;
274
275 if(waitpid(pid, &ret, 0) == -1)
276 err(EXIT_FAILURE, "waiting for child failed");
277 if(WIFEXITED(ret))
278 exit(WEXITSTATUS(ret));
279 else
280 exit(EXIT_SUCCESS);
281 }
282
283 char
284 unbuffer(void) {
285 char c;
286
287 c = buf.data[buf.s++];
288 buf.s %= LENGTH(buf.data);
289 buf.n--;
290 return c;
291 }
292
293 void
294 ungetch(int c) {
295 if(rbuf.i + 1 >= rbuf.n)
296 errx(EXIT_FAILURE, "read buffer full");
297 rbuf.data[rbuf.i++] = c;
298 }
299
300 int
301 main(int argc, char *argv[]) {
302 fd_set rfds;
303
304 if(argc == 2 && !strcmp("-v", argv[1]))
305 errx(EXIT_SUCCESS, "std-"VERSION", © 2008 Matthias-Christian Ott");
306 else if(argc == 1)
307 errx(EXIT_FAILURE, "usage: std [-v]");
308 getpty();
309 shell();
310 FD_ZERO(&rfds);
311 FD_SET(STDIN_FILENO, &rfds);
312 FD_SET(ptm, &rfds);
313 for(;;) {
314 if(select(ptm + 1, &rfds, NULL, NULL, NULL) == -1)
315 err(EXIT_FAILURE, "cannot select");
316 if(FD_ISSET(ptm, &rfds)) {
317 do {
318 c = getch();
319 switch(c) {
320 case '\033':
321 parseesc();
322 break;
323 default:
324 putchar(c);
325 }
326 } while(rbuf.i < rbuf.n);
327 fflush(stdout);
328 }
329 }
330 return 0;
331 }