xref: /freebsd/sys/ddb/db_input.c (revision 5ccbc3cc5aa70fee35bc061ab0e4fadcf67ad335)
15b81b6b3SRodney W. Grimes /*
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
250edf66ecSRodney W. Grimes  *
265ccbc3ccSBruce Evans  *	$Id: db_input.c,v 1.11 1995/12/10 13:32:37 phk Exp $
275b81b6b3SRodney W. Grimes  */
280edf66ecSRodney W. Grimes 
295b81b6b3SRodney W. Grimes /*
305b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
315b81b6b3SRodney W. Grimes  *	Date:	7/90
325b81b6b3SRodney W. Grimes  */
335b81b6b3SRodney W. Grimes 
34f540b106SGarrett Wollman #include <sys/param.h>
35f540b106SGarrett Wollman #include <sys/systm.h>
365ccbc3ccSBruce Evans 
375ccbc3ccSBruce Evans #include <machine/cons.h>
385ccbc3ccSBruce Evans 
39f540b106SGarrett Wollman #include <ddb/ddb.h>
40f540b106SGarrett Wollman #include <ddb/db_output.h>
415b81b6b3SRodney W. Grimes 
425b81b6b3SRodney W. Grimes /*
435b81b6b3SRodney W. Grimes  * Character input and editing.
445b81b6b3SRodney W. Grimes  */
455b81b6b3SRodney W. Grimes 
465b81b6b3SRodney W. Grimes /*
475b81b6b3SRodney W. Grimes  * We don't track output position while editing input,
485b81b6b3SRodney W. Grimes  * since input always ends with a new-line.  We just
495b81b6b3SRodney W. Grimes  * reset the line position at the end.
505b81b6b3SRodney W. Grimes  */
5125eb640dSPoul-Henning Kamp static char *	db_lbuf_start;	/* start of input line buffer */
5225eb640dSPoul-Henning Kamp static char *	db_lbuf_end;	/* end of input line buffer */
5325eb640dSPoul-Henning Kamp static char *	db_lc;		/* current character */
5425eb640dSPoul-Henning Kamp static char *	db_le;		/* one past last character */
555b81b6b3SRodney W. Grimes 
565b81b6b3SRodney W. Grimes #define	CTRL(c)		((c) & 0x1f)
575b81b6b3SRodney W. Grimes #define	isspace(c)	((c) == ' ' || (c) == '\t')
585b81b6b3SRodney W. Grimes #define	BLANK		' '
595b81b6b3SRodney W. Grimes #define	BACKUP		'\b'
605b81b6b3SRodney W. Grimes 
61f73a856dSPoul-Henning Kamp static int	cnmaygetc __P((void));
62f73a856dSPoul-Henning Kamp static void	db_delete __P((int n, int bwd));
63f73a856dSPoul-Henning Kamp static int	db_inputchar __P((int c));
64f73a856dSPoul-Henning Kamp static void	db_putnchars __P((int c, int count));
65f73a856dSPoul-Henning Kamp static void	db_putstring __P((char *s, int count));
66058284fcSBruce Evans 
675b81b6b3SRodney W. Grimes void
685b81b6b3SRodney W. Grimes db_putstring(s, count)
695b81b6b3SRodney W. Grimes 	char	*s;
705b81b6b3SRodney W. Grimes 	int	count;
715b81b6b3SRodney W. Grimes {
725b81b6b3SRodney W. Grimes 	while (--count >= 0)
735b81b6b3SRodney W. Grimes 	    cnputc(*s++);
745b81b6b3SRodney W. Grimes }
755b81b6b3SRodney W. Grimes 
765b81b6b3SRodney W. Grimes void
775b81b6b3SRodney W. Grimes db_putnchars(c, count)
785b81b6b3SRodney W. Grimes 	int	c;
795b81b6b3SRodney W. Grimes 	int	count;
805b81b6b3SRodney W. Grimes {
815b81b6b3SRodney W. Grimes 	while (--count >= 0)
825b81b6b3SRodney W. Grimes 	    cnputc(c);
835b81b6b3SRodney W. Grimes }
845b81b6b3SRodney W. Grimes 
855b81b6b3SRodney W. Grimes /*
865b81b6b3SRodney W. Grimes  * Delete N characters, forward or backward
875b81b6b3SRodney W. Grimes  */
885b81b6b3SRodney W. Grimes #define	DEL_FWD		0
895b81b6b3SRodney W. Grimes #define	DEL_BWD		1
905b81b6b3SRodney W. Grimes void
915b81b6b3SRodney W. Grimes db_delete(n, bwd)
925b81b6b3SRodney W. Grimes 	int	n;
935b81b6b3SRodney W. Grimes 	int	bwd;
945b81b6b3SRodney W. Grimes {
955b81b6b3SRodney W. Grimes 	register char *p;
965b81b6b3SRodney W. Grimes 
975b81b6b3SRodney W. Grimes 	if (bwd) {
985b81b6b3SRodney W. Grimes 	    db_lc -= n;
995b81b6b3SRodney W. Grimes 	    db_putnchars(BACKUP, n);
1005b81b6b3SRodney W. Grimes 	}
1015b81b6b3SRodney W. Grimes 	for (p = db_lc; p < db_le-n; p++) {
1025b81b6b3SRodney W. Grimes 	    *p = *(p+n);
1035b81b6b3SRodney W. Grimes 	    cnputc(*p);
1045b81b6b3SRodney W. Grimes 	}
1055b81b6b3SRodney W. Grimes 	db_putnchars(BLANK, n);
1065b81b6b3SRodney W. Grimes 	db_putnchars(BACKUP, db_le - db_lc);
1075b81b6b3SRodney W. Grimes 	db_le -= n;
1085b81b6b3SRodney W. Grimes }
1095b81b6b3SRodney W. Grimes 
1105b81b6b3SRodney W. Grimes /* returns TRUE at end-of-line */
1115b81b6b3SRodney W. Grimes int
1125b81b6b3SRodney W. Grimes db_inputchar(c)
1135b81b6b3SRodney W. Grimes 	int	c;
1145b81b6b3SRodney W. Grimes {
1155b81b6b3SRodney W. Grimes 	switch (c) {
1165b81b6b3SRodney W. Grimes 	    case CTRL('b'):
1175b81b6b3SRodney W. Grimes 		/* back up one character */
1185b81b6b3SRodney W. Grimes 		if (db_lc > db_lbuf_start) {
1195b81b6b3SRodney W. Grimes 		    cnputc(BACKUP);
1205b81b6b3SRodney W. Grimes 		    db_lc--;
1215b81b6b3SRodney W. Grimes 		}
1225b81b6b3SRodney W. Grimes 		break;
1235b81b6b3SRodney W. Grimes 	    case CTRL('f'):
1245b81b6b3SRodney W. Grimes 		/* forward one character */
1255b81b6b3SRodney W. Grimes 		if (db_lc < db_le) {
1265b81b6b3SRodney W. Grimes 		    cnputc(*db_lc);
1275b81b6b3SRodney W. Grimes 		    db_lc++;
1285b81b6b3SRodney W. Grimes 		}
1295b81b6b3SRodney W. Grimes 		break;
1305b81b6b3SRodney W. Grimes 	    case CTRL('a'):
1315b81b6b3SRodney W. Grimes 		/* beginning of line */
1325b81b6b3SRodney W. Grimes 		while (db_lc > db_lbuf_start) {
1335b81b6b3SRodney W. Grimes 		    cnputc(BACKUP);
1345b81b6b3SRodney W. Grimes 		    db_lc--;
1355b81b6b3SRodney W. Grimes 		}
1365b81b6b3SRodney W. Grimes 		break;
1375b81b6b3SRodney W. Grimes 	    case CTRL('e'):
1385b81b6b3SRodney W. Grimes 		/* end of line */
1395b81b6b3SRodney W. Grimes 		while (db_lc < db_le) {
1405b81b6b3SRodney W. Grimes 		    cnputc(*db_lc);
1415b81b6b3SRodney W. Grimes 		    db_lc++;
1425b81b6b3SRodney W. Grimes 		}
1435b81b6b3SRodney W. Grimes 		break;
1445b81b6b3SRodney W. Grimes 	    case CTRL('h'):
1455b81b6b3SRodney W. Grimes 	    case 0177:
1465b81b6b3SRodney W. Grimes 		/* erase previous character */
1475b81b6b3SRodney W. Grimes 		if (db_lc > db_lbuf_start)
1485b81b6b3SRodney W. Grimes 		    db_delete(1, DEL_BWD);
1495b81b6b3SRodney W. Grimes 		break;
1505b81b6b3SRodney W. Grimes 	    case CTRL('d'):
1515b81b6b3SRodney W. Grimes 		/* erase next character */
1525b81b6b3SRodney W. Grimes 		if (db_lc < db_le)
1535b81b6b3SRodney W. Grimes 		    db_delete(1, DEL_FWD);
1545b81b6b3SRodney W. Grimes 		break;
1555b81b6b3SRodney W. Grimes 	    case CTRL('k'):
1565b81b6b3SRodney W. Grimes 		/* delete to end of line */
1575b81b6b3SRodney W. Grimes 		if (db_lc < db_le)
1585b81b6b3SRodney W. Grimes 		    db_delete(db_le - db_lc, DEL_FWD);
1595b81b6b3SRodney W. Grimes 		break;
1605b81b6b3SRodney W. Grimes 	    case CTRL('t'):
1615b81b6b3SRodney W. Grimes 		/* twiddle last 2 characters */
1625b81b6b3SRodney W. Grimes 		if (db_lc >= db_lbuf_start + 2) {
1635b81b6b3SRodney W. Grimes 		    c = db_lc[-2];
1645b81b6b3SRodney W. Grimes 		    db_lc[-2] = db_lc[-1];
1655b81b6b3SRodney W. Grimes 		    db_lc[-1] = c;
1665b81b6b3SRodney W. Grimes 		    cnputc(BACKUP);
1675b81b6b3SRodney W. Grimes 		    cnputc(BACKUP);
1685b81b6b3SRodney W. Grimes 		    cnputc(db_lc[-2]);
1695b81b6b3SRodney W. Grimes 		    cnputc(db_lc[-1]);
1705b81b6b3SRodney W. Grimes 		}
1715b81b6b3SRodney W. Grimes 		break;
1725b81b6b3SRodney W. Grimes 	    case CTRL('r'):
1735b81b6b3SRodney W. Grimes 		db_putstring("^R\n", 3);
1745b81b6b3SRodney W. Grimes 		if (db_le > db_lbuf_start) {
1755b81b6b3SRodney W. Grimes 		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
1765b81b6b3SRodney W. Grimes 		    db_putnchars(BACKUP, db_le - db_lc);
1775b81b6b3SRodney W. Grimes 		}
1785b81b6b3SRodney W. Grimes 		break;
1795b81b6b3SRodney W. Grimes 	    case '\n':
1805b81b6b3SRodney W. Grimes 	    case '\r':
1815b81b6b3SRodney W. Grimes 		*db_le++ = c;
1825b81b6b3SRodney W. Grimes 		return (1);
1835b81b6b3SRodney W. Grimes 	    default:
1845b81b6b3SRodney W. Grimes 		if (db_le == db_lbuf_end) {
1855b81b6b3SRodney W. Grimes 		    cnputc('\007');
1865b81b6b3SRodney W. Grimes 		}
1875b81b6b3SRodney W. Grimes 		else if (c >= ' ' && c <= '~') {
1885b81b6b3SRodney W. Grimes 		    register char *p;
1895b81b6b3SRodney W. Grimes 
1905b81b6b3SRodney W. Grimes 		    for (p = db_le; p > db_lc; p--)
1915b81b6b3SRodney W. Grimes 			*p = *(p-1);
1925b81b6b3SRodney W. Grimes 		    *db_lc++ = c;
1935b81b6b3SRodney W. Grimes 		    db_le++;
1945b81b6b3SRodney W. Grimes 		    cnputc(c);
1955b81b6b3SRodney W. Grimes 		    db_putstring(db_lc, db_le - db_lc);
1965b81b6b3SRodney W. Grimes 		    db_putnchars(BACKUP, db_le - db_lc);
1975b81b6b3SRodney W. Grimes 		}
1985b81b6b3SRodney W. Grimes 		break;
1995b81b6b3SRodney W. Grimes 	}
2005b81b6b3SRodney W. Grimes 	return (0);
2015b81b6b3SRodney W. Grimes }
2025b81b6b3SRodney W. Grimes 
2035b81b6b3SRodney W. Grimes int
204058284fcSBruce Evans cnmaygetc()
205f23b4c91SGarrett Wollman {
206f23b4c91SGarrett Wollman 	return (-1);
207f23b4c91SGarrett Wollman }
208f23b4c91SGarrett Wollman 
209f23b4c91SGarrett Wollman int
2105b81b6b3SRodney W. Grimes db_readline(lstart, lsize)
2115b81b6b3SRodney W. Grimes 	char *	lstart;
2125b81b6b3SRodney W. Grimes 	int	lsize;
2135b81b6b3SRodney W. Grimes {
2145b81b6b3SRodney W. Grimes 	db_force_whitespace();	/* synch output position */
2155b81b6b3SRodney W. Grimes 
2165b81b6b3SRodney W. Grimes 	db_lbuf_start = lstart;
2175b81b6b3SRodney W. Grimes 	db_lbuf_end   = lstart + lsize;
2185b81b6b3SRodney W. Grimes 	db_lc = lstart;
2195b81b6b3SRodney W. Grimes 	db_le = lstart;
2205b81b6b3SRodney W. Grimes 
2215b81b6b3SRodney W. Grimes 	while (!db_inputchar(cngetc()))
2225b81b6b3SRodney W. Grimes 	    continue;
2235b81b6b3SRodney W. Grimes 
2245b81b6b3SRodney W. Grimes 	db_putchar('\n');	/* synch output position */
2255b81b6b3SRodney W. Grimes 
2265b81b6b3SRodney W. Grimes 	*db_le = 0;
2275b81b6b3SRodney W. Grimes 	return (db_le - db_lbuf_start);
2285b81b6b3SRodney W. Grimes }
2295b81b6b3SRodney W. Grimes 
2305b81b6b3SRodney W. Grimes void
2315b81b6b3SRodney W. Grimes db_check_interrupt()
2325b81b6b3SRodney W. Grimes {
2335b81b6b3SRodney W. Grimes 	register int	c;
2345b81b6b3SRodney W. Grimes 
2355b81b6b3SRodney W. Grimes 	c = cnmaygetc();
2365b81b6b3SRodney W. Grimes 	switch (c) {
2375b81b6b3SRodney W. Grimes 	    case -1:		/* no character */
2385b81b6b3SRodney W. Grimes 		return;
2395b81b6b3SRodney W. Grimes 
2405b81b6b3SRodney W. Grimes 	    case CTRL('c'):
2415b81b6b3SRodney W. Grimes 		db_error((char *)0);
2425b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
2435b81b6b3SRodney W. Grimes 
2445b81b6b3SRodney W. Grimes 	    case CTRL('s'):
2455b81b6b3SRodney W. Grimes 		do {
2465b81b6b3SRodney W. Grimes 		    c = cnmaygetc();
2475b81b6b3SRodney W. Grimes 		    if (c == CTRL('c'))
2485b81b6b3SRodney W. Grimes 			db_error((char *)0);
2495b81b6b3SRodney W. Grimes 		} while (c != CTRL('q'));
2505b81b6b3SRodney W. Grimes 		break;
2515b81b6b3SRodney W. Grimes 
2525b81b6b3SRodney W. Grimes 	    default:
2535b81b6b3SRodney W. Grimes 		/* drop on floor */
2545b81b6b3SRodney W. Grimes 		break;
2555b81b6b3SRodney W. Grimes 	}
2565b81b6b3SRodney W. Grimes }
2575b81b6b3SRodney W. Grimes 
2585b81b6b3SRodney W. Grimes /* called from kdb_trap in db_interface.c */
259381fe1aaSGarrett Wollman void
2605b81b6b3SRodney W. Grimes cnpollc (flag)
261381fe1aaSGarrett Wollman 	int flag;
2625b81b6b3SRodney W. Grimes {
2635b81b6b3SRodney W. Grimes }
264