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