diff options
| author | Matthias-Christian Ott <ott@enolink.de> | 2008-08-07 10:38:54 +0200 |
|---|---|---|
| committer | Matthias-Christian Ott <ott@enolink.de> | 2008-08-07 10:38:54 +0200 |
| commit | d61a2a8fce2638594644feb664ce04398825b0f3 (patch) | |
| tree | ba5efcdb3c48bb2d915bfff3b8ee16b4c085c8b5 | |
| parent | 240411e7d08006f181afbf70930f748d7585ce85 (diff) | |
| download | st-d61a2a8fce2638594644feb664ce04398825b0f3.tar.gz st-d61a2a8fce2638594644feb664ce04398825b0f3.zip | |
add prototype for command parsing
| -rw-r--r-- | std.c | 81 |
1 files changed, 50 insertions, 31 deletions
| @@ -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 | ||
| 21 | static void buffer(char c); | ||
| 22 | static void cmd(const char *cmdstr, ...); | ||
| 23 | static int getch(); | ||
| 24 | static void getpty(void); | ||
| 25 | static void movea(int x, int y); | ||
| 26 | static void mover(int x, int y); | ||
| 27 | static void parseesc(void); | ||
| 28 | static void scroll(int l); | ||
| 29 | static void shell(void); | ||
| 30 | static void sigchld(int n); | ||
| 31 | static char unbuffer(void); | ||
| 32 | static void ungetch(int c); | ||
| 33 | |||
| 34 | typedef struct { | 21 | typedef 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 { | |||
| 40 | typedef struct { | 27 | typedef 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 | ||
| 33 | static void buffer(char c); | ||
| 34 | static void cmd(const char *cmdstr, ...); | ||
| 35 | static int getch(ReadBuffer *buf); | ||
| 36 | static void getpty(void); | ||
| 37 | static void movea(int x, int y); | ||
| 38 | static void mover(int x, int y); | ||
| 39 | static void parsecmd(void); | ||
| 40 | static void parseesc(void); | ||
| 41 | static void scroll(int l); | ||
| 42 | static void shell(void); | ||
| 43 | static void sigchld(int n); | ||
| 44 | static char unbuffer(void); | ||
| 45 | static void ungetch(ReadBuffer *buf, int c); | ||
| 46 | |||
| 45 | static int cols = 80, lines = 25; | 47 | static int cols = 80, lines = 25; |
| 46 | static int cx = 0, cy = 0; | 48 | static int cx = 0, cy = 0; |
| 47 | static int c; | 49 | static int c; |
| @@ -49,7 +51,7 @@ static int ptm, pts; | |||
| 49 | static _Bool bold, digit, qmark; | 51 | static _Bool bold, digit, qmark; |
| 50 | static pid_t pid; | 52 | static pid_t pid; |
| 51 | static RingBuffer buf; | 53 | static RingBuffer buf; |
| 52 | static ReadBuffer rbuf; | 54 | static ReadBuffer cmdbuf, ptmbuf; |
| 53 | 55 | ||
| 54 | void | 56 | void |
| 55 | buffer(char c) { | 57 | buffer(char c) { |
| @@ -73,14 +75,14 @@ cmd(const char *cmdstr, ...) { | |||
| 73 | } | 75 | } |
| 74 | 76 | ||
| 75 | int | 77 | int |
| 76 | getch() { | 78 | getch(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 | ||
| 86 | void | 88 | void |
| @@ -98,15 +100,19 @@ mover(int x, int y) { | |||
| 98 | } | 100 | } |
| 99 | 101 | ||
| 100 | void | 102 | void |
| 103 | parsecmd(void) { | ||
| 104 | } | ||
| 105 | |||
| 106 | void | ||
| 101 | parseesc(void) { | 107 | parseesc(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 | ||
| 293 | void | 299 | void |
| 294 | ungetch(int c) { | 300 | ungetch(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 | ||
| 300 | int | 306 | int |
| @@ -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 | } |
