Xinqi Bao's Git

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