db_input.c revision 1.13 1 /* $NetBSD: db_input.c,v 1.13 2000/03/30 11:31:27 augustss 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 "AS IS"
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 "opt_ddbparam.h"
33
34 #include <sys/param.h>
35 #include <sys/proc.h>
36
37 #include <machine/db_machdep.h>
38
39 #include <ddb/db_output.h>
40 #include <ddb/db_command.h>
41 #include <ddb/db_sym.h>
42 #include <ddb/db_extern.h>
43
44 #include <dev/cons.h>
45
46 #ifndef DDB_HISTORY_SIZE
47 #define DDB_HISTORY_SIZE 0
48 #endif /* DDB_HISTORY_SIZE */
49
50 /*
51 * Character input and editing.
52 */
53
54 /*
55 * We don't track output position while editing input,
56 * since input always ends with a new-line. We just
57 * reset the line position at the end.
58 */
59 char * db_lbuf_start; /* start of input line buffer */
60 char * db_lbuf_end; /* end of input line buffer */
61 char * db_lc; /* current character */
62 char * db_le; /* one past last character */
63 #if DDB_HISTORY_SIZE != 0
64 char db_history[DDB_HISTORY_SIZE]; /* start of history buffer */
65 int db_history_size = DDB_HISTORY_SIZE;/* size of history buffer */
66 char * db_history_curr = db_history; /* start of current line */
67 char * db_history_last = db_history; /* start of last line */
68 char * db_history_prev = (char *) 0; /* start of previous line */
69 #endif
70
71
72 #define CTRL(c) ((c) & 0x1f)
73 #define isspace(c) ((c) == ' ' || (c) == '\t')
74 #define BLANK ' '
75 #define BACKUP '\b'
76
77 static int cnmaygetc __P((void));
78
79 void
80 db_putstring(s, count)
81 char *s;
82 int count;
83 {
84 while (--count >= 0)
85 cnputc(*s++);
86 }
87
88 void
89 db_putnchars(c, count)
90 int c;
91 int count;
92 {
93 while (--count >= 0)
94 cnputc(c);
95 }
96
97 /*
98 * Delete N characters, forward or backward
99 */
100 #define DEL_FWD 0
101 #define DEL_BWD 1
102 void
103 db_delete(n, bwd)
104 int n;
105 int bwd;
106 {
107 char *p;
108
109 if (bwd) {
110 db_lc -= n;
111 db_putnchars(BACKUP, n);
112 }
113 for (p = db_lc; p < db_le-n; p++) {
114 *p = *(p+n);
115 cnputc(*p);
116 }
117 db_putnchars(BLANK, n);
118 db_putnchars(BACKUP, db_le - db_lc);
119 db_le -= n;
120 }
121
122 void
123 db_delete_line()
124 {
125 db_delete(db_le - db_lc, DEL_FWD);
126 db_delete(db_lc - db_lbuf_start, DEL_BWD);
127 db_le = db_lc = db_lbuf_start;
128 }
129
130 #if DDB_HISTORY_SIZE != 0
131 #define INC_DB_CURR() \
132 do { \
133 db_history_curr++; \
134 if (db_history_curr > \
135 db_history + db_history_size - 1) \
136 db_history_curr = db_history; \
137 } while (0)
138 #define DEC_DB_CURR() \
139 do { \
140 db_history_curr--; \
141 if (db_history_curr < db_history) \
142 db_history_curr = db_history + \
143 db_history_size - 1; \
144 } while (0)
145 #endif
146
147 /* returns TRUE at end-of-line */
148 int
149 db_inputchar(c)
150 int c;
151 {
152 switch (c) {
153 case CTRL('b'):
154 /* back up one character */
155 if (db_lc > db_lbuf_start) {
156 cnputc(BACKUP);
157 db_lc--;
158 }
159 break;
160 case CTRL('f'):
161 /* forward one character */
162 if (db_lc < db_le) {
163 cnputc(*db_lc);
164 db_lc++;
165 }
166 break;
167 case CTRL('a'):
168 /* beginning of line */
169 while (db_lc > db_lbuf_start) {
170 cnputc(BACKUP);
171 db_lc--;
172 }
173 break;
174 case CTRL('e'):
175 /* end of line */
176 while (db_lc < db_le) {
177 cnputc(*db_lc);
178 db_lc++;
179 }
180 break;
181 case CTRL('h'):
182 case 0177:
183 /* erase previous character */
184 if (db_lc > db_lbuf_start)
185 db_delete(1, DEL_BWD);
186 break;
187 case CTRL('d'):
188 /* erase next character */
189 if (db_lc < db_le)
190 db_delete(1, DEL_FWD);
191 break;
192 case CTRL('k'):
193 /* delete to end of line */
194 if (db_lc < db_le)
195 db_delete(db_le - db_lc, DEL_FWD);
196 break;
197 case CTRL('u'):
198 /* delete line */
199 db_delete_line();
200 break;
201 case CTRL('t'):
202 /* twiddle last 2 characters */
203 if (db_lc >= db_lbuf_start + 2) {
204 c = db_lc[-2];
205 db_lc[-2] = db_lc[-1];
206 db_lc[-1] = c;
207 cnputc(BACKUP);
208 cnputc(BACKUP);
209 cnputc(db_lc[-2]);
210 cnputc(db_lc[-1]);
211 }
212 break;
213 #if DDB_HISTORY_SIZE != 0
214 case CTRL('p'):
215 DEC_DB_CURR();
216 while (db_history_curr != db_history_last) {
217 DEC_DB_CURR();
218 if (*db_history_curr == '\0')
219 break;
220 }
221 db_delete_line();
222 if (db_history_curr == db_history_last) {
223 INC_DB_CURR();
224 db_le = db_lc = db_lbuf_start;
225 } else {
226 char *p;
227 INC_DB_CURR();
228 for (p = db_history_curr, db_le = db_lbuf_start;
229 *p; ) {
230 *db_le++ = *p++;
231 if (p == db_history + db_history_size) {
232 p = db_history;
233 }
234 }
235 db_lc = db_le;
236 }
237 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
238 break;
239 case CTRL('n'):
240 while (db_history_curr != db_history_last) {
241 if (*db_history_curr == '\0')
242 break;
243 INC_DB_CURR();
244 }
245 if (db_history_curr != db_history_last) {
246 INC_DB_CURR();
247 db_delete_line();
248 if (db_history_curr != db_history_last) {
249 char *p;
250 for (p = db_history_curr,
251 db_le = db_lbuf_start; *p;) {
252 *db_le++ = *p++;
253 if (p == db_history +
254 db_history_size) {
255 p = db_history;
256 }
257 }
258 db_lc = db_le;
259 }
260 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
261 }
262 break;
263 #endif
264 case CTRL('r'):
265 db_putstring("^R\n", 3);
266 if (db_le > db_lbuf_start) {
267 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
268 db_putnchars(BACKUP, db_le - db_lc);
269 }
270 break;
271 case '\n':
272 case '\r':
273 #if DDB_HISTORY_SIZE != 0
274 /* Check if it same than previous line */
275 if (db_history_curr == db_history_prev) {
276 char *pp, *pc;
277
278 /* Is it unmodified */
279 for (pp = db_history_prev, pc = db_lbuf_start;
280 pc != db_le && *pp; pp++, pc++) {
281 if (*pp != *pc)
282 break;
283 if (++pp == db_history + db_history_size) {
284 pp = db_history;
285 }
286 if (++pc == db_history + db_history_size) {
287 pc = db_history;
288 }
289 }
290 if (!*pp && pc == db_le) {
291 /* Repeted previous line, not saved */
292 db_history_curr = db_history_last;
293 *db_le++ = c;
294 return (TRUE);
295 }
296 }
297 if (db_le != db_lbuf_start) {
298 char *p;
299 db_history_prev = db_history_last;
300 for (p = db_lbuf_start; p != db_le; p++) {
301 *db_history_last++ = *p;
302 if (db_history_last == db_history +
303 db_history_size) {
304 db_history_last = db_history;
305 }
306 }
307 *db_history_last++ = '\0';
308 }
309 db_history_curr = db_history_last;
310 #endif
311 *db_le++ = c;
312 return (1);
313 default:
314 if (db_le == db_lbuf_end) {
315 cnputc('\007');
316 }
317 else if (c >= ' ' && c <= '~') {
318 char *p;
319
320 for (p = db_le; p > db_lc; p--)
321 *p = *(p-1);
322 *db_lc++ = c;
323 db_le++;
324 cnputc(c);
325 db_putstring(db_lc, db_le - db_lc);
326 db_putnchars(BACKUP, db_le - db_lc);
327 }
328 break;
329 }
330 return (0);
331 }
332
333 int
334 db_readline(lstart, lsize)
335 char * lstart;
336 int lsize;
337 {
338 db_force_whitespace(); /* synch output position */
339
340 db_lbuf_start = lstart;
341 db_lbuf_end = lstart + lsize;
342 db_lc = lstart;
343 db_le = lstart;
344
345 while (!db_inputchar(cngetc()))
346 continue;
347
348 db_putchar('\n'); /* synch output position */
349
350 *db_le = 0;
351 return (db_le - db_lbuf_start);
352 }
353
354 void
355 db_check_interrupt()
356 {
357 int c;
358
359 c = cnmaygetc();
360 switch (c) {
361 case -1: /* no character */
362 return;
363
364 case CTRL('c'):
365 db_error((char *)0);
366 /*NOTREACHED*/
367
368 case CTRL('s'):
369 do {
370 c = cnmaygetc();
371 if (c == CTRL('c')) {
372 db_error((char *)0);
373 /*NOTREACHED*/
374 }
375 } while (c != CTRL('q'));
376 break;
377
378 default:
379 /* drop on floor */
380 break;
381 }
382 }
383
384 static int
385 cnmaygetc ()
386 {
387 return (-1);
388 }
389