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