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