db_input.c revision 24840
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.17 1997/02/22 09:28:23 peter 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/malloc.h>
36#include <sys/systm.h>
37
38#include <machine/cons.h>
39
40#include <ddb/ddb.h>
41#include <ddb/db_output.h>
42
43/*
44 * Character input and editing.
45 */
46
47/*
48 * We don't track output position while editing input,
49 * since input always ends with a new-line.  We just
50 * reset the line position at the end.
51 */
52static char *	db_lbuf_start;	/* start of input line buffer */
53static char *	db_lbuf_end;	/* end of input line buffer */
54static char *	db_lc;		/* current character */
55static char *	db_le;		/* one past last character */
56
57/*
58 * Simple input line history support.
59 */
60static char *	db_lhistory;
61static int	db_lhistlsize, db_lhistidx, db_lhistcur;
62#define DB_LHIST_NLINES 10
63
64#define	CTRL(c)		((c) & 0x1f)
65#define	isspace(c)	((c) == ' ' || (c) == '\t')
66#define	BLANK		' '
67#define	BACKUP		'\b'
68
69static int	cnmaygetc __P((void));
70static void	db_delete __P((int n, int bwd));
71static int	db_inputchar __P((int c));
72static void	db_putnchars __P((int c, int count));
73static void	db_putstring __P((char *s, int count));
74
75void
76db_putstring(s, count)
77	char	*s;
78	int	count;
79{
80	while (--count >= 0)
81	    cnputc(*s++);
82}
83
84void
85db_putnchars(c, count)
86	int	c;
87	int	count;
88{
89	while (--count >= 0)
90	    cnputc(c);
91}
92
93/*
94 * Delete N characters, forward or backward
95 */
96#define	DEL_FWD		0
97#define	DEL_BWD		1
98void
99db_delete(n, bwd)
100	int	n;
101	int	bwd;
102{
103	register char *p;
104
105	if (bwd) {
106	    db_lc -= n;
107	    db_putnchars(BACKUP, n);
108	}
109	for (p = db_lc; p < db_le-n; p++) {
110	    *p = *(p+n);
111	    cnputc(*p);
112	}
113	db_putnchars(BLANK, n);
114	db_putnchars(BACKUP, db_le - db_lc);
115	db_le -= n;
116}
117
118/* returns TRUE at end-of-line */
119int
120db_inputchar(c)
121	int	c;
122{
123	static int escstate;
124
125	if (escstate == 1) {
126		/* ESC seen, look for [ or O */
127		if (c == '[' || c == 'O')
128			escstate++;
129		else
130			escstate = 0; /* re-init state machine */
131		return (0);
132	} else if (escstate == 2) {
133		escstate = 0;
134		/*
135		 * If a valid cursor key has been found, translate
136		 * into an emacs-style control key, and fall through.
137		 * Otherwise, drop off.
138		 */
139		switch (c) {
140		case 'A':	/* up */
141			c = CTRL('p');
142			break;
143		case 'B':	/* down */
144			c = CTRL('n');
145			break;
146		case 'C':	/* right */
147			c = CTRL('f');
148			break;
149		case 'D':	/* left */
150			c = CTRL('b');
151			break;
152		default:
153			return (0);
154		}
155	}
156
157	switch (c) {
158	    case CTRL('['):
159		escstate = 1;
160		break;
161	    case CTRL('b'):
162		/* back up one character */
163		if (db_lc > db_lbuf_start) {
164		    cnputc(BACKUP);
165		    db_lc--;
166		}
167		break;
168	    case CTRL('f'):
169		/* forward one character */
170		if (db_lc < db_le) {
171		    cnputc(*db_lc);
172		    db_lc++;
173		}
174		break;
175	    case CTRL('a'):
176		/* beginning of line */
177		while (db_lc > db_lbuf_start) {
178		    cnputc(BACKUP);
179		    db_lc--;
180		}
181		break;
182	    case CTRL('e'):
183		/* end of line */
184		while (db_lc < db_le) {
185		    cnputc(*db_lc);
186		    db_lc++;
187		}
188		break;
189	    case CTRL('h'):
190	    case 0177:
191		/* erase previous character */
192		if (db_lc > db_lbuf_start)
193		    db_delete(1, DEL_BWD);
194		break;
195	    case CTRL('d'):
196		/* erase next character */
197		if (db_lc < db_le)
198		    db_delete(1, DEL_FWD);
199		break;
200	    case CTRL('k'):
201		/* delete to end of line */
202		if (db_lc < db_le)
203		    db_delete(db_le - db_lc, DEL_FWD);
204		break;
205	    case CTRL('t'):
206		/* twiddle last 2 characters */
207		if (db_lc >= db_lbuf_start + 2) {
208		    c = db_lc[-2];
209		    db_lc[-2] = db_lc[-1];
210		    db_lc[-1] = c;
211		    cnputc(BACKUP);
212		    cnputc(BACKUP);
213		    cnputc(db_lc[-2]);
214		    cnputc(db_lc[-1]);
215		}
216		break;
217	    case CTRL('r'):
218		db_putstring("^R\n", 3);
219	    redraw:
220		if (db_le > db_lbuf_start) {
221		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
222		    db_putnchars(BACKUP, db_le - db_lc);
223		}
224		break;
225	    case CTRL('p'):
226		/* Make previous history line the active one. */
227		if (db_lhistcur >= 0) {
228		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
229			  db_lbuf_start, db_lhistlsize);
230		    db_lhistcur--;
231		    goto hist_redraw;
232		}
233		break;
234	    case CTRL('n'):
235		/* Make next history line the active one. */
236		if (db_lhistcur < db_lhistidx - 1) {
237		    db_lhistcur += 2;
238		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
239			  db_lbuf_start, db_lhistlsize);
240		} else {
241		    /*
242		     * ^N through tail of history, reset the
243		     * buffer to zero length.
244		     */
245		    *db_lbuf_start = '\0';
246		    db_lhistcur = db_lhistidx;
247		}
248
249	    hist_redraw:
250		db_putnchars(BACKUP, db_le - db_lbuf_start);
251		db_putnchars(BLANK, db_le - db_lbuf_start);
252		db_putnchars(BACKUP, db_le - db_lbuf_start);
253		db_le = index(db_lbuf_start, '\0');
254		if (db_le[-1] == '\r' || db_le[-1] == '\n')
255		    *--db_le = '\0';
256		db_lc = db_le;
257		goto redraw;
258
259	    case -1:
260		/*
261		 * eek! the console returned eof.
262		 * probably that means we HAVE no console.. we should try bail
263		 * XXX
264		 */
265		c = '\r';
266	    case '\n':
267	    case '\r':
268		*db_le++ = c;
269		return (1);
270	    default:
271		if (db_le == db_lbuf_end) {
272		    cnputc('\007');
273		}
274		else if (c >= ' ' && c <= '~') {
275		    register char *p;
276
277		    for (p = db_le; p > db_lc; p--)
278			*p = *(p-1);
279		    *db_lc++ = c;
280		    db_le++;
281		    cnputc(c);
282		    db_putstring(db_lc, db_le - db_lc);
283		    db_putnchars(BACKUP, db_le - db_lc);
284		}
285		break;
286	}
287	return (0);
288}
289
290int
291cnmaygetc()
292{
293	return (-1);
294}
295
296int
297db_readline(lstart, lsize)
298	char *	lstart;
299	int	lsize;
300{
301	if (db_lhistory && lsize != db_lhistlsize) {
302		/* Should not happen, but to be sane, throw history away. */
303		FREE(db_lhistory, M_TEMP);
304		db_lhistory = 0;
305	}
306	if (db_lhistory == 0) {
307		/* Initialize input line history. */
308		db_lhistlsize = lsize;
309		db_lhistidx = -1;
310		MALLOC(db_lhistory, char *, lsize * DB_LHIST_NLINES,
311		       M_TEMP, M_NOWAIT);
312	}
313	db_lhistcur = db_lhistidx;
314
315	db_force_whitespace();	/* synch output position */
316
317	db_lbuf_start = lstart;
318	db_lbuf_end   = lstart + lsize;
319	db_lc = lstart;
320	db_le = lstart;
321
322	while (!db_inputchar(cngetc()))
323	    continue;
324
325	db_printf("\n");	/* synch output position */
326	*db_le = 0;
327
328	if (db_le - db_lbuf_start > 1) {
329	    /* Maintain input line history for non-empty lines. */
330	    if (++db_lhistidx == DB_LHIST_NLINES) {
331		/* Rotate history. */
332		ovbcopy(db_lhistory + db_lhistlsize, db_lhistory,
333			db_lhistlsize * (DB_LHIST_NLINES - 1));
334		db_lhistidx--;
335	    }
336	    bcopy(lstart, db_lhistory + (db_lhistidx * db_lhistlsize),
337		  db_lhistlsize);
338	}
339
340	return (db_le - db_lbuf_start);
341}
342
343void
344db_check_interrupt()
345{
346	register int	c;
347
348	c = cnmaygetc();
349	switch (c) {
350	    case -1:		/* no character */
351		return;
352
353	    case CTRL('c'):
354		db_error((char *)0);
355		/*NOTREACHED*/
356
357	    case CTRL('s'):
358		do {
359		    c = cnmaygetc();
360		    if (c == CTRL('c'))
361			db_error((char *)0);
362		} while (c != CTRL('q'));
363		break;
364
365	    default:
366		/* drop on floor */
367		break;
368	}
369}
370
371/* called from kdb_trap in db_interface.c */
372void
373cnpollc (flag)
374	int flag;
375{
376}
377