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