| 1 | #ifndef lint | 
| 2 | static const char       RCSid[] = "$Id: editline.c,v 2.7 2003/07/27 22:12:03 schorsch Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | *  editline.c - routine for editing raw input for rview. | 
| 6 | * | 
| 7 | *  External symbols declared in driver.h | 
| 8 | */ | 
| 9 |  | 
| 10 | #include "copyright.h" | 
| 11 |  | 
| 12 | #include <stdio.h> | 
| 13 | #include <string.h> | 
| 14 |  | 
| 15 | #include "color.h" | 
| 16 |  | 
| 17 | #include "driver.h" | 
| 18 |  | 
| 19 | #define iscntrl(c)      ((c) < ' ') | 
| 20 | #define isblank(c)      ((c) == ' ') | 
| 21 | #define iserase(c)      ((c) == '\b' || (c) == 127) | 
| 22 | #define iswerase(c)     ((c) == 'W'-'@') | 
| 23 | #define iskill(c)       ((c) == 'U'-'@' || (c) == 'X'-'@') | 
| 24 |  | 
| 25 |  | 
| 26 | extern void | 
| 27 | editline(       /* edit input line */ | 
| 28 | char  *buf, | 
| 29 | dr_getchf_t *c_get, | 
| 30 | dr_comoutf_t *s_put | 
| 31 | ) | 
| 32 | { | 
| 33 | static char  erases[] = "\b \b"; | 
| 34 | static char  obuf[4]; | 
| 35 | register int  i; | 
| 36 | register int  c; | 
| 37 |  | 
| 38 | i = 0; | 
| 39 | while ((c = (*c_get)()&0177) != '\n' && c != '\r') | 
| 40 | if (iserase(c)) {               /* single char erase */ | 
| 41 | if (i > 0) { | 
| 42 | (*s_put)(erases); | 
| 43 | --i; | 
| 44 | } | 
| 45 | } else if (iswerase(c)) {       /* word erase */ | 
| 46 | while (i > 0 && isblank(buf[i-1])) { | 
| 47 | (*s_put)(erases); | 
| 48 | --i; | 
| 49 | } | 
| 50 | while (i > 0 && !isblank(buf[i-1])) { | 
| 51 | (*s_put)(erases); | 
| 52 | --i; | 
| 53 | } | 
| 54 | } else if (iskill(c)) {         /* kill line */ | 
| 55 | while (i > 0) { | 
| 56 | (*s_put)(erases); | 
| 57 | --i; | 
| 58 | } | 
| 59 | } else if (iscntrl(c)) {                /* control char */ | 
| 60 | i = 0; | 
| 61 | buf[i++] = c; | 
| 62 | obuf[0] = '^'; obuf[1] = c|0100; obuf[2] = '\0'; | 
| 63 | (*s_put)(obuf); | 
| 64 | break; | 
| 65 | } else {                                /* regular char */ | 
| 66 | buf[i++] = c; | 
| 67 | obuf[0] = c; obuf[1] = '\0'; | 
| 68 | (*s_put)(obuf); | 
| 69 | } | 
| 70 | buf[i] = '\0'; | 
| 71 | (*s_put)("\n"); | 
| 72 | } | 
| 73 |  | 
| 74 |  | 
| 75 | static char  mybuf[512]; | 
| 76 |  | 
| 77 |  | 
| 78 | void | 
| 79 | tocombuf(b, d)                          /* add command(s) to my buffer */ | 
| 80 | register char  *b; | 
| 81 | register struct driver  *d; | 
| 82 | { | 
| 83 | register char  *cp; | 
| 84 | char  *comstart; | 
| 85 |  | 
| 86 | for (cp = mybuf; *cp; cp++) | 
| 87 | ; | 
| 88 | comstart = cp; | 
| 89 | while ( (*cp++ = *b) ) | 
| 90 | if (cp >= mybuf+sizeof(mybuf)) { | 
| 91 | *comstart = '\0'; | 
| 92 | return;         /* what should I do about this? */ | 
| 93 | } else if (*b++ == '\n') { | 
| 94 | d->inpready++; | 
| 95 | comstart = cp; | 
| 96 | } | 
| 97 | } | 
| 98 |  | 
| 99 |  | 
| 100 | int | 
| 101 | fromcombuf(b, d)                        /* get command from my buffer */ | 
| 102 | char  *b; | 
| 103 | struct driver  *d; | 
| 104 | { | 
| 105 | register char   *cp; | 
| 106 | /* get next command */ | 
| 107 | for (cp = mybuf; *cp != '\n'; cp++) | 
| 108 | if (!*cp) | 
| 109 | return(0); | 
| 110 | *cp++ = '\0'; | 
| 111 | #ifdef DEBUG | 
| 112 | (*d->comout)(mybuf);                    /* echo my command */ | 
| 113 | (*d->comout)("\n"); | 
| 114 | #endif | 
| 115 | /* send it as reply */ | 
| 116 | strcpy(b, mybuf); | 
| 117 | d->inpready--; | 
| 118 | /* advance commands */ | 
| 119 | strcpy(mybuf, cp); | 
| 120 | return(1); | 
| 121 | } |