db_input.c revision 24840
140496Sbde/*
250473Speter * Mach Operating System
31539Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University
41539Srgrimes * All Rights Reserved.
554351Smarcel *
654351Smarcel * Permission to use, copy, modify and distribute this software and its
754351Smarcel * documentation is hereby granted, provided that both the copyright
81539Srgrimes * notice and this permission notice appear in all copies of the
918420Sbde * software, derivative works or modified versions, and any portions
1096462Sru * thereof, and that both notices appear in supporting documentation.
11101138Smike *
12107046Smarcel * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13107046Smarcel * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14107046Smarcel * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15107046Smarcel *
16107046Smarcel * Carnegie Mellon requests users of this software to return to
17107046Smarcel *
18107046Smarcel *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19107046Smarcel *  School of Computer Science
20107046Smarcel *  Carnegie Mellon University
21107046Smarcel *  Pittsburgh PA 15213-3890
221539Srgrimes *
2388055Sru * any improvements or extensions that they make and grant Carnegie the
2434030Sdufault * rights to redistribute these changes.
25104219Smike *
26104634Smike *	$Id: db_input.c,v 1.17 1997/02/22 09:28:23 peter Exp $
2734030Sdufault */
2888055Sru
2964767Sjhb/*
301539Srgrimes *	Author: David B. Golub, Carnegie Mellon University
31105376Ssam *	Date:	7/90
3283653Speter */
3383653Speter
3417900Speter#include <sys/param.h>
35105400Stmm#include <sys/malloc.h>
36103589Speter#include <sys/systm.h>
3777223Sru
3877162Sru#include <machine/cons.h>
3993229Sru
40101204Srwatson#include <ddb/ddb.h>
41105875Srwatson#include <ddb/db_output.h>
4277031Sru
43107139Sjulian/*
44107139Sjulian * Character input and editing.
4593229Sru */
4693229Sru
4793229Sru/*
4877046Sru * We don't track output position while editing input,
4954351Smarcel * since input always ends with a new-line.  We just
5054351Smarcel * reset the line position at the end.
5154351Smarcel */
5254351Smarcelstatic char *	db_lbuf_start;	/* start of input line buffer */
5354351Smarcelstatic char *	db_lbuf_end;	/* end of input line buffer */
5454351Smarcelstatic char *	db_lc;		/* current character */
5554351Smarcelstatic char *	db_le;		/* one past last character */
5696462Sru
5725734Speter/*
5836283Seivind * Simple input line history support.
5978376Speter */
6078376Speterstatic char *	db_lhistory;
6125734Speterstatic int	db_lhistlsize, db_lhistidx, db_lhistcur;
6236283Seivind#define DB_LHIST_NLINES 10
6325734Speter
6425734Speter#define	CTRL(c)		((c) & 0x1f)
6572673Speter#define	isspace(c)	((c) == ' ' || (c) == '\t')
6678376Speter#define	BLANK		' '
6772673Speter#define	BACKUP		'\b'
6825734Speter
6972673Speterstatic int	cnmaygetc __P((void));
7072673Speterstatic void	db_delete __P((int n, int bwd));
7125734Speterstatic int	db_inputchar __P((int c));
7288055Srustatic void	db_putnchars __P((int c, int count));
7396462Srustatic void	db_putstring __P((char *s, int count));
7417900Speter
7588055Sruvoid
7696462Srudb_putstring(s, count)
7717900Speter	char	*s;
7888055Sru	int	count;
7996462Sru{
8034030Sdufault	while (--count >= 0)
8154351Smarcel	    cnputc(*s++);
8254351Smarcel}
83104489Ssam
84104288Sruvoid
8554351Smarceldb_putnchars(c, count)
8654351Smarcel	int	c;
8754351Smarcel	int	count;
8865885Sache{
8954351Smarcel	while (--count >= 0)
9054351Smarcel	    cnputc(c);
9154351Smarcel}
9254351Smarcel
9354351Smarcel/*
9454351Smarcel * Delete N characters, forward or backward
95104489Ssam */
96104489Ssam#define	DEL_FWD		0
97104489Ssam#define	DEL_BWD		1
9877857Sjlemonvoid
9977857Sjlemondb_delete(n, bwd)
10077857Sjlemon	int	n;
10177857Sjlemon	int	bwd;
10293229Sru{
10393229Sru	register char *p;
10493229Sru
10593229Sru	if (bwd) {
10677857Sjlemon	    db_lc -= n;
10793229Sru	    db_putnchars(BACKUP, n);
1081539Srgrimes	}
10954351Smarcel	for (p = db_lc; p < db_le-n; p++) {
11054351Smarcel	    *p = *(p+n);
11156645Speter	    cnputc(*p);
11254351Smarcel	}
11354351Smarcel	db_putnchars(BLANK, n);
11454351Smarcel	db_putnchars(BACKUP, db_le - db_lc);
115104489Ssam	db_le -= n;
116104489Ssam}
11793730Sru
11856645Speter/* returns TRUE at end-of-line */
11977046Sruint
12077046Srudb_inputchar(c)
12154351Smarcel	int	c;
12254351Smarcel{
12354351Smarcel	static int escstate;
12496668Sru
12596462Sru	if (escstate == 1) {
12696668Sru		/* 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