Xinqi Bao's Git

terminate error message with newline
[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 #endif
116 #if defined(_XOPEN_SOURCE)
117 if(ptm != -1) {
118 if(grantpt(ptm) == -1)
119 eprintn("error, cannot grant access to pty");
120 if(unlockpt(ptm) == -1)
121 eprintn("error, cannot unlock pty");
122 ptsdev = ptsname(ptm);
123 if(!ptsdev)
124 eprintn("error, slave pty name undefined");
125 pts = open(ptsdev, O_RDWR);
126 if(pts == -1)
127 eprintn("error, cannot open slave pty");
128 }
129 else
130 eprintn("error, cannot open pty");
131 #endif
132 }
133
134 void
135 movea(int x, int y) {
136 x = MAX(x, cols);
137 y = MAX(y, lines);
138 cx = x;
139 cy = y;
140 cmd("s %d,%d", x, y);
141 }
142
143 void
144 mover(int x, int y) {
145 movea(cx + x, cy + y);
146 }
147
148 void
149 parseesc(void) {
150 int i, j;
151 int arg[16];
152
153 memset(arg, 0, LENGTH(arg));
154 s = 0;
155 c = getc(fptm);
156 switch(c) {
157 case '[':
158 c = getc(fptm);
159 for(j = 0; j < LENGTH(arg);) {
160 if(isdigit(c)) {
161 s |= Digit;
162 arg[j] *= 10;
163 arg[j] += c - '0';
164 }
165 else if(c == '?')
166 s |= QuestionMark;
167 else if(c == ';') {
168 if(!(s & Digit))
169 eprint("syntax error\n");
170 s &= ~Digit;
171 j++;
172 }
173 else {
174 if(s & Digit) {
175 s &= ~Digit;
176 j++;
177 }
178 break;
179 }
180 c = getc(fptm);
181 }
182 switch(c) {
183 case '@':
184 break;
185 case 'A':
186 mover(0, j ? arg[0] : 1);
187 break;
188 case 'B':
189 mover(0, j ? -arg[0] : -1);
190 break;
191 case 'C':
192 mover(j ? arg[0] : 1, 0);
193 break;
194 case 'D':
195 mover(j ? -arg[0] : -1, 0);
196 break;
197 case 'E':
198 /* movel(j ? arg[0] : 1); */
199 break;
200 case 'F':
201 /* movel(j ? -arg[0] : -1); */
202 break;
203 case '`':
204 case 'G':
205 movea(j ? arg[0] : 1, cy);
206 break;
207 case 'f':
208 case 'H':
209 movea(arg[1] ? arg[1] : 1, arg[0] ? arg[0] : 1);
210 case 'L':
211 /* insline(j ? arg[0] : 1); */
212 break;
213 case 'M':
214 /* delline(j ? arg[0] : 1); */
215 break;
216 case 'P':
217 break;
218 case 'S':
219 scroll(j ? arg[0] : 1);
220 break;
221 case 'T':
222 scroll(j ? -arg[0] : -1);
223 break;
224 case 'd':
225 movea(cx, j ? arg[0] : 1);
226 break;
227 case 'm':
228 for(i = 0; i < j; i++) {
229 if(arg[i] >= 30 && arg[i] <= 37)
230 cmd("#%d", arg[i] - 30);
231 if(arg[i] >= 40 && arg[i] <= 47)
232 cmd("|%d", arg[i] - 40);
233 /* xterm bright colors */
234 if(arg[i] >= 90 && arg[i] <= 97)
235 cmd("#%d", arg[i] - 90);
236 if(arg[i] >= 100 && arg[i] <= 107)
237 cmd("|%d", arg[i] - 100);
238 switch(arg[i]) {
239 case 0:
240 case 22:
241 if(bold)
242 cmd("b");
243 case 1:
244 if(!bold)
245 cmd("b");
246 break;
247 }
248 }
249 break;
250 }
251 break;
252 default:
253 putchar('\033');
254 ungetc(c, fptm);
255 }
256 }
257
258 void
259 scroll(int l) {
260 cmd("s %d, %d", cx, cy + l);
261 }
262
263 void
264 shell(void) {
265 static char *shell = NULL;
266
267 if(!shell && !(shell = getenv("SHELL")))
268 shell = "/bin/sh";
269 pid = fork();
270 switch(pid) {
271 case -1:
272 eprint("error, cannot fork\n");
273 case 0:
274 setsid();
275 dup2(pts, STDIN_FILENO);
276 dup2(pts, STDOUT_FILENO);
277 dup2(pts, STDERR_FILENO);
278 close(ptm);
279 putenv("TERM=vt102");
280 execvp(shell, NULL);
281 break;
282 default:
283 close(pts);
284 signal(SIGCHLD, sigchld);
285 }
286 }
287
288 void
289 sigchld(int n) {
290 int ret;
291
292 if(waitpid(pid, &ret, 0) == -1)
293 eprintn("error, waiting for child failed");
294 if(WIFEXITED(ret))
295 exit(WEXITSTATUS(ret));
296 else
297 exit(EXIT_SUCCESS);
298 }
299
300 char
301 unbuffer(void) {
302 char c;
303
304 c = buf.data[buf.s++];
305 buf.s %= LENGTH(buf.data);
306 buf.n--;
307 return c;
308 }
309
310 int
311 main(int argc, char *argv[]) {
312 fd_set rd;
313 if(argc == 2 && !strcmp("-v", argv[1]))
314 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
315 else if(argc == 1)
316 eprint("usage: st [-v]\n");
317 getpty();
318 shell();
319 return 0;
320 }