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