JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
add missing header file
[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 #if !(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600)
10 #include <pty.h>
11 #endif
12 #include <signal.h>
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #define LENGTH(x)       (sizeof(x) / sizeof((x)[0]))
20 #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
21 #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
22
23 void buffer(char c);
24 void cmd(const char *cmdstr, ...);
25 void *emallocz(unsigned int size);
26 void eprint(const char *errstr, ...);
27 void eprintn(const char *errstr, ...);
28 void getpty(void);
29 void movea(int x, int y);
30 void mover(int x, int y);
31 void parse(void);
32 void scroll(int l);
33 void shell(void);
34 void sigchld(int n);
35 char unbuffer(void);
36
37 typedef struct {
38         unsigned char data[BUFSIZ];
39         int s, e;
40         int n;
41 } RingBuffer;
42
43 int cols = 80, lines = 25;
44 int cx = 0, cy = 0;
45 int c;
46 FILE *fptm = NULL;
47 int ptm, pts;
48 _Bool bold, digit, qmark;
49 pid_t pid;
50 RingBuffer buf;
51
52 void
53 buffer(char c) {
54         if(buf.n < LENGTH(buf.data))
55                 buf.n++;
56         else
57                 buf.s = (buf.s + 1) % LENGTH(buf.data);
58         buf.data[buf.e++] = c;
59         buf.e %= LENGTH(buf.data);
60 }
61
62 void
63 cmd(const char *cmdstr, ...) {
64         va_list ap;
65
66         putchar('\n');
67         putchar(':');
68         va_start(ap, cmdstr);
69         vfprintf(stdout, cmdstr, ap);
70         va_end(ap);
71 }
72
73 void *
74 emallocz(unsigned int size) {
75         void *res = calloc(1, size);
76
77         if(!res)
78                 eprint("fatal: could not malloc() %u bytes\n", size);
79         return res;
80 }
81
82 void
83 eprint(const char *errstr, ...) {
84         va_list ap;
85
86         va_start(ap, errstr);
87         vfprintf(stderr, errstr, ap);
88         va_end(ap);
89         exit(EXIT_FAILURE);
90 }
91
92 void
93 eprintn(const char *errstr, ...) {
94         va_list ap;
95
96         va_start(ap, errstr);
97         vfprintf(stderr, errstr, ap);
98         va_end(ap);
99         fprintf(stderr, ": %s\n", strerror(errno));
100         exit(EXIT_FAILURE);
101 }
102
103 void
104 getpty(void) {
105         char *ptsdev;
106
107 #if defined(_GNU_SOURCE)
108         ptm = getpt();
109 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
110         ptm = posix_openpt(O_RDWR);
111 #else
112         ptm = open("/dev/ptmx", O_RDWR);
113         if(ptm == -1)
114                 if(openpty(&ptm, &pts, NULL, NULL, NULL) == -1)
115                         eprintn("error, cannot open pty");
116 #endif
117 #if defined(_XOPEN_SOURCE)
118         if(ptm != -1) {
119                 if(grantpt(ptm) == -1)
120                         eprintn("error, cannot grant access to pty");
121                 if(unlockpt(ptm) == -1)
122                         eprintn("error, cannot unlock pty");
123                 ptsdev = ptsname(ptm);
124                 if(!ptsdev)
125                         eprintn("error, slave pty name undefined");
126                 pts = open(ptsdev, O_RDWR);
127                 if(pts == -1)
128                         eprintn("error, cannot open slave pty");
129         }
130         else
131                 eprintn("error, cannot open pty");
132 #endif
133 }
134
135 void
136 movea(int x, int y) {
137         x = MAX(x, cols);
138         y = MAX(y, lines);
139         cx = x;
140         cy = y;
141         cmd("s %d,%d", x, y);
142 }
143
144 void
145 mover(int x, int y) {
146         movea(cx + x, cy + y);
147 }
148
149 void
150 parseesc(void) {
151         int i, j;
152         int arg[16];
153
154         memset(arg, 0, LENGTH(arg));
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                                 digit = 1;
162                                 arg[j] *= 10;
163                                 arg[j] += c - '0';
164                         }
165                         else if(c == '?')
166                                 qmark = 1;
167                         else if(c == ';') {
168                                 if(!digit)
169                                         eprint("syntax error\n");
170                                 digit = 0;
171                                 j++;
172                         }
173                         else {
174                                 if(digit) {
175                                         digit = 0;
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         if(argc == 2 && !strcmp("-v", argv[1]))
313                 eprint("std-"VERSION", © 2008 Matthias-Christian Ott\n");
314         else if(argc == 1)
315                 eprint("usage: st [-v]\n");
316         getpty();
317         shell();
318         fptm = fdopen(ptm, "r+");
319         if(!fptm)
320                 eprintn("cannot open slave pty");
321         for(;;) {
322                 c = getc(fptm);
323                 switch(c) {
324                 case '\033':
325                         parseesc();
326                         break;
327                 default:
328                         putchar(c);
329                 }
330         }
331         return 0;
332 }