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