db_input.c revision 1.7 1 /* $NetBSD: db_input.c,v 1.7 1996/02/05 01:57:02 christos Exp $ */
2
3 /*
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 *
28 * Author: David B. Golub, Carnegie Mellon University
29 * Date: 7/90
30 */
31
32 #include <sys/param.h>
33 #include <sys/proc.h>
34
35 #include <machine/db_machdep.h>
36
37 #include <ddb/db_output.h>
38 #include <ddb/db_command.h>
39 #include <ddb/db_sym.h>
40 #include <ddb/db_extern.h>
41
42 #include <dev/cons.h>
43
44 /*
45 * Character input and editing.
46 */
47
48 /*
49 * We don't track output position while editing input,
50 * since input always ends with a new-line. We just
51 * reset the line position at the end.
52 */
53 char * db_lbuf_start; /* start of input line buffer */
54 char * db_lbuf_end; /* end of input line buffer */
55 char * db_lc; /* current character */
56 char * db_le; /* one past last character */
57
58 #define CTRL(c) ((c) & 0x1f)
59 #define isspace(c) ((c) == ' ' || (c) == '\t')
60 #define BLANK ' '
61 #define BACKUP '\b'
62
63 static int cnmaygetc __P((void));
64
65 void
66 db_putstring(s, count)
67 char *s;
68 int count;
69 {
70 while (--count >= 0)
71 cnputc(*s++);
72 }
73
74 void
75 db_putnchars(c, count)
76 int c;
77 int count;
78 {
79 while (--count >= 0)
80 cnputc(c);
81 }
82
83 /*
84 * Delete N characters, forward or backward
85 */
86 #define DEL_FWD 0
87 #define DEL_BWD 1
88 void
89 db_delete(n, bwd)
90 int n;
91 int bwd;
92 {
93 register char *p;
94
95 if (bwd) {
96 db_lc -= n;
97 db_putnchars(BACKUP, n);
98 }
99 for (p = db_lc; p < db_le-n; p++) {
100 *p = *(p+n);
101 cnputc(*p);
102 }
103 db_putnchars(BLANK, n);
104 db_putnchars(BACKUP, db_le - db_lc);
105 db_le -= n;
106 }
107
108 /* returns TRUE at end-of-line */
109 int
110 db_inputchar(c)
111 int c;
112 {
113 switch (c) {
114 case CTRL('b'):
115 /* back up one character */
116 if (db_lc > db_lbuf_start) {
117 cnputc(BACKUP);
118 db_lc--;
119 }
120 break;
121 case CTRL('f'):
122 /* forward one character */
123 if (db_lc < db_le) {
124 cnputc(*db_lc);
125 db_lc++;
126 }
127 break;
128 case CTRL('a'):
129 /* beginning of line */
130 while (db_lc > db_lbuf_start) {
131 cnputc(BACKUP);
132 db_lc--;
133 }
134 break;
135 case CTRL('e'):
136 /* end of line */
137 while (db_lc < db_le) {
138 cnputc(*db_lc);
139 db_lc++;
140 }
141 break;
142 case CTRL('h'):
143 case 0177:
144 /* erase previous character */
145 if (db_lc > db_lbuf_start)
146 db_delete(1, DEL_BWD);
147 break;
148 case CTRL('d'):
149 /* erase next character */
150 if (db_lc < db_le)
151 db_delete(1, DEL_FWD);
152 break;
153 case CTRL('k'):
154 /* delete to end of line */
155 if (db_lc < db_le)
156 db_delete(db_le - db_lc, DEL_FWD);
157 break;
158 case CTRL('t'):
159 /* twiddle last 2 characters */
160 if (db_lc >= db_lbuf_start + 2) {
161 c = db_lc[-2];
162 db_lc[-2] = db_lc[-1];
163 db_lc[-1] = c;
164 cnputc(BACKUP);
165 cnputc(BACKUP);
166 cnputc(db_lc[-2]);
167 cnputc(db_lc[-1]);
168 }
169 break;
170 case CTRL('r'):
171 db_putstring("^R\n", 3);
172 if (db_le > db_lbuf_start) {
173 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
174 db_putnchars(BACKUP, db_le - db_lc);
175 }
176 break;
177 case '\n':
178 case '\r':
179 *db_le++ = c;
180 return (1);
181 default:
182 if (db_le == db_lbuf_end) {
183 cnputc('\007');
184 }
185 else if (c >= ' ' && c <= '~') {
186 register char *p;
187
188 for (p = db_le; p > db_lc; p--)
189 *p = *(p-1);
190 *db_lc++ = c;
191 db_le++;
192 cnputc(c);
193 db_putstring(db_lc, db_le - db_lc);
194 db_putnchars(BACKUP, db_le - db_lc);
195 }
196 break;
197 }
198 return (0);
199 }
200
201 int
202 db_readline(lstart, lsize)
203 char * lstart;
204 int lsize;
205 {
206 db_force_whitespace(); /* synch output position */
207
208 db_lbuf_start = lstart;
209 db_lbuf_end = lstart + lsize;
210 db_lc = lstart;
211 db_le = lstart;
212
213 while (!db_inputchar(cngetc()))
214 continue;
215
216 db_putchar('\n'); /* synch output position */
217
218 *db_le = 0;
219 return (db_le - db_lbuf_start);
220 }
221
222 void
223 db_check_interrupt()
224 {
225 register int c;
226
227 c = cnmaygetc();
228 switch (c) {
229 case -1: /* no character */
230 return;
231
232 case CTRL('c'):
233 db_error((char *)0);
234 /*NOTREACHED*/
235
236 case CTRL('s'):
237 do {
238 c = cnmaygetc();
239 if (c == CTRL('c')) {
240 db_error((char *)0);
241 /*NOTREACHED*/
242 }
243 } while (c != CTRL('q'));
244 break;
245
246 default:
247 /* drop on floor */
248 break;
249 }
250 }
251
252 static int
253 cnmaygetc ()
254 {
255 return (-1);
256 }
257