aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias-Christian Ott <ott@enolink.de>2008-08-07 10:38:54 +0200
committerMatthias-Christian Ott <ott@enolink.de>2008-08-07 10:38:54 +0200
commitd61a2a8fce2638594644feb664ce04398825b0f3 (patch)
treeba5efcdb3c48bb2d915bfff3b8ee16b4c085c8b5
parent240411e7d08006f181afbf70930f748d7585ce85 (diff)
downloadst-d61a2a8fce2638594644feb664ce04398825b0f3.tar.gz
st-d61a2a8fce2638594644feb664ce04398825b0f3.zip
add prototype for command parsing
-rw-r--r--std.c81
1 files changed, 50 insertions, 31 deletions
diff --git a/std.c b/std.c
index ef9e57a..a9a4f8c 100644
--- a/std.c
+++ b/std.c
@@ -18,19 +18,6 @@
18#define MAX(a,b) (((a) > (b)) ? (a) : (b)) 18#define MAX(a,b) (((a) > (b)) ? (a) : (b))
19#define MIN(a,b) (((a) < (b)) ? (a) : (b)) 19#define MIN(a,b) (((a) < (b)) ? (a) : (b))
20 20
21static void buffer(char c);
22static void cmd(const char *cmdstr, ...);
23static int getch();
24static void getpty(void);
25static void movea(int x, int y);
26static void mover(int x, int y);
27static void parseesc(void);
28static void scroll(int l);
29static void shell(void);
30static void sigchld(int n);
31static char unbuffer(void);
32static void ungetch(int c);
33
34typedef struct { 21typedef struct {
35 unsigned char data[BUFSIZ]; 22 unsigned char data[BUFSIZ];
36 int s, e; 23 int s, e;
@@ -40,8 +27,23 @@ typedef struct {
40typedef struct { 27typedef struct {
41 unsigned char data[BUFSIZ]; 28 unsigned char data[BUFSIZ];
42 int i, n; 29 int i, n;
30 int fd;
43} ReadBuffer; 31} ReadBuffer;
44 32
33static void buffer(char c);
34static void cmd(const char *cmdstr, ...);
35static int getch(ReadBuffer *buf);
36static void getpty(void);
37static void movea(int x, int y);
38static void mover(int x, int y);
39static void parsecmd(void);
40static void parseesc(void);
41static void scroll(int l);
42static void shell(void);
43static void sigchld(int n);
44static char unbuffer(void);
45static void ungetch(ReadBuffer *buf, int c);
46
45static int cols = 80, lines = 25; 47static int cols = 80, lines = 25;
46static int cx = 0, cy = 0; 48static int cx = 0, cy = 0;
47static int c; 49static int c;
@@ -49,7 +51,7 @@ static int ptm, pts;
49static _Bool bold, digit, qmark; 51static _Bool bold, digit, qmark;
50static pid_t pid; 52static pid_t pid;
51static RingBuffer buf; 53static RingBuffer buf;
52static ReadBuffer rbuf; 54static ReadBuffer cmdbuf, ptmbuf;
53 55
54void 56void
55buffer(char c) { 57buffer(char c) {
@@ -73,14 +75,14 @@ cmd(const char *cmdstr, ...) {
73} 75}
74 76
75int 77int
76getch() { 78getch(ReadBuffer *buf) {
77 if(rbuf.i++ >= rbuf.n) { 79 if(buf->i++ >= buf->n) {
78 rbuf.n = read(ptm, rbuf.data, LENGTH(rbuf.data)); 80 buf->n = read(buf->fd, buf->data, BUFSIZ);
79 if(rbuf.n == -1) 81 if(buf->n == -1)
80 err(EXIT_FAILURE, "cannot read from slave pty"); 82 err(EXIT_FAILURE, "cannot read");
81 rbuf.i = 0; 83 buf->i = 0;
82 } 84 }
83 return rbuf.data[rbuf.i]; 85 return buf->data[buf->i];
84} 86}
85 87
86void 88void
@@ -98,15 +100,19 @@ mover(int x, int y) {
98} 100}
99 101
100void 102void
103parsecmd(void) {
104}
105
106void
101parseesc(void) { 107parseesc(void) {
102 int i, j; 108 int i, j;
103 int arg[16]; 109 int arg[16];
104 110
105 memset(arg, 0, LENGTH(arg)); 111 memset(arg, 0, LENGTH(arg));
106 c = getch(); 112 c = getch(&ptmbuf);
107 switch(c) { 113 switch(c) {
108 case '[': 114 case '[':
109 c = getch(); 115 c = getch(&ptmbuf);
110 for(j = 0; j < LENGTH(arg);) { 116 for(j = 0; j < LENGTH(arg);) {
111 if(isdigit(c)) { 117 if(isdigit(c)) {
112 digit = 1; 118 digit = 1;
@@ -128,7 +134,7 @@ parseesc(void) {
128 } 134 }
129 break; 135 break;
130 } 136 }
131 c = getch(); 137 c = getch(&ptmbuf);
132 } 138 }
133 switch(c) { 139 switch(c) {
134 case '@': 140 case '@':
@@ -202,7 +208,7 @@ parseesc(void) {
202 break; 208 break;
203 default: 209 default:
204 putchar('\033'); 210 putchar('\033');
205 ungetch(c); 211 ungetch(&ptmbuf, c);
206 } 212 }
207} 213}
208 214
@@ -291,10 +297,10 @@ unbuffer(void) {
291} 297}
292 298
293void 299void
294ungetch(int c) { 300ungetch(ReadBuffer *buf, int c) {
295 if(rbuf.i + 1 >= rbuf.n) 301 if(buf->i + 1 >= buf->n)
296 errx(EXIT_FAILURE, "read buffer full"); 302 errx(EXIT_FAILURE, "buffer full");
297 rbuf.data[rbuf.i++] = c; 303 buf->data[buf->i++] = c;
298} 304}
299 305
300int 306int
@@ -307,15 +313,28 @@ main(int argc, char *argv[]) {
307 errx(EXIT_FAILURE, "usage: std [-v]"); 313 errx(EXIT_FAILURE, "usage: std [-v]");
308 getpty(); 314 getpty();
309 shell(); 315 shell();
316 cmdbuf.fd = STDIN_FILENO;
317 ptmbuf.fd = ptm;
310 FD_ZERO(&rfds); 318 FD_ZERO(&rfds);
311 FD_SET(STDIN_FILENO, &rfds); 319 FD_SET(STDIN_FILENO, &rfds);
312 FD_SET(ptm, &rfds); 320 FD_SET(ptm, &rfds);
313 for(;;) { 321 for(;;) {
314 if(select(ptm + 1, &rfds, NULL, NULL, NULL) == -1) 322 if(select(ptm + 1, &rfds, NULL, NULL, NULL) == -1)
315 err(EXIT_FAILURE, "cannot select"); 323 err(EXIT_FAILURE, "cannot select");
324 if(FD_ISSET(STDIN_FILENO, &rfds))
325 do {
326 c = getch(&cmdbuf);
327 switch(c) {
328 case ':':
329 parsecmd();
330 break;
331 default:
332 break;
333 }
334 } while(cmdbuf.i < cmdbuf.n);
316 if(FD_ISSET(ptm, &rfds)) { 335 if(FD_ISSET(ptm, &rfds)) {
317 do { 336 do {
318 c = getch(); 337 c = getch(&ptmbuf);
319 switch(c) { 338 switch(c) {
320 case '\033': 339 case '\033':
321 parseesc(); 340 parseesc();
@@ -323,7 +342,7 @@ main(int argc, char *argv[]) {
323 default: 342 default:
324 putchar(c); 343 putchar(c);
325 } 344 }
326 } while(rbuf.i < rbuf.n); 345 } while(ptmbuf.i < ptmbuf.n);
327 fflush(stdout); 346 fflush(stdout);
328 } 347 }
329 } 348 }