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