| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
greg |
2.9 |
static const char RCSid[] = "$Id: editline.c,v 2.8 2004/03/30 16:13:01 schorsch Exp $"; |
| 3 |
greg |
1.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* editline.c - routine for editing raw input for rview. |
| 6 |
|
|
* |
| 7 |
greg |
2.4 |
* External symbols declared in driver.h |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
greg |
2.5 |
#include "copyright.h" |
| 11 |
greg |
2.6 |
|
| 12 |
|
|
#include <stdio.h> |
| 13 |
schorsch |
2.8 |
#include <string.h> |
| 14 |
greg |
1.1 |
|
| 15 |
greg |
2.4 |
#include "color.h" |
| 16 |
|
|
|
| 17 |
|
|
#include "driver.h" |
| 18 |
|
|
|
| 19 |
greg |
1.2 |
#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 |
greg |
1.1 |
|
| 25 |
|
|
|
| 26 |
greg |
2.9 |
void |
| 27 |
schorsch |
2.8 |
editline( /* edit input line */ |
| 28 |
|
|
char *buf, |
| 29 |
|
|
dr_getchf_t *c_get, |
| 30 |
|
|
dr_comoutf_t *s_put |
| 31 |
|
|
) |
| 32 |
greg |
1.1 |
{ |
| 33 |
|
|
static char erases[] = "\b \b"; |
| 34 |
|
|
static char obuf[4]; |
| 35 |
greg |
2.9 |
int i; |
| 36 |
|
|
int c; |
| 37 |
greg |
1.1 |
|
| 38 |
|
|
i = 0; |
| 39 |
|
|
while ((c = (*c_get)()&0177) != '\n' && c != '\r') |
| 40 |
greg |
1.2 |
if (iserase(c)) { /* single char erase */ |
| 41 |
greg |
1.1 |
if (i > 0) { |
| 42 |
|
|
(*s_put)(erases); |
| 43 |
|
|
--i; |
| 44 |
|
|
} |
| 45 |
greg |
1.2 |
} 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 |
greg |
1.1 |
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 |
greg |
1.3 |
|
| 74 |
|
|
|
| 75 |
|
|
static char mybuf[512]; |
| 76 |
|
|
|
| 77 |
|
|
|
| 78 |
greg |
2.4 |
void |
| 79 |
greg |
2.9 |
tocombuf( /* add command(s) to my buffer */ |
| 80 |
|
|
char *b, |
| 81 |
|
|
struct driver *d |
| 82 |
|
|
) |
| 83 |
greg |
1.3 |
{ |
| 84 |
greg |
2.9 |
char *cp; |
| 85 |
greg |
2.3 |
char *comstart; |
| 86 |
greg |
2.2 |
|
| 87 |
|
|
for (cp = mybuf; *cp; cp++) |
| 88 |
|
|
; |
| 89 |
greg |
2.3 |
comstart = cp; |
| 90 |
schorsch |
2.7 |
while ( (*cp++ = *b) ) |
| 91 |
greg |
2.3 |
if (cp >= mybuf+sizeof(mybuf)) { |
| 92 |
|
|
*comstart = '\0'; |
| 93 |
|
|
return; /* what should I do about this? */ |
| 94 |
|
|
} else if (*b++ == '\n') { |
| 95 |
greg |
2.2 |
d->inpready++; |
| 96 |
greg |
2.3 |
comstart = cp; |
| 97 |
|
|
} |
| 98 |
greg |
1.3 |
} |
| 99 |
|
|
|
| 100 |
|
|
|
| 101 |
greg |
2.4 |
int |
| 102 |
greg |
2.9 |
fromcombuf( /* get command from my buffer */ |
| 103 |
|
|
char *b, |
| 104 |
|
|
struct driver *d |
| 105 |
|
|
) |
| 106 |
greg |
1.3 |
{ |
| 107 |
greg |
2.9 |
char *cp, *cp1; |
| 108 |
greg |
1.3 |
/* get next command */ |
| 109 |
|
|
for (cp = mybuf; *cp != '\n'; cp++) |
| 110 |
|
|
if (!*cp) |
| 111 |
|
|
return(0); |
| 112 |
|
|
*cp++ = '\0'; |
| 113 |
greg |
1.4 |
#ifdef DEBUG |
| 114 |
greg |
1.3 |
(*d->comout)(mybuf); /* echo my command */ |
| 115 |
|
|
(*d->comout)("\n"); |
| 116 |
greg |
1.4 |
#endif |
| 117 |
greg |
1.3 |
/* send it as reply */ |
| 118 |
|
|
strcpy(b, mybuf); |
| 119 |
|
|
d->inpready--; |
| 120 |
greg |
2.3 |
/* advance commands */ |
| 121 |
greg |
2.9 |
cp1 = mybuf; |
| 122 |
|
|
while ((*cp1++ = *cp++)) |
| 123 |
|
|
; |
| 124 |
greg |
1.3 |
return(1); |
| 125 |
|
|
} |