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