1139747Simp/*-
24Srgrimes * Mach Operating System
34Srgrimes * Copyright (c) 1991,1990 Carnegie Mellon University
44Srgrimes * All Rights Reserved.
58876Srgrimes *
64Srgrimes * Permission to use, copy, modify and distribute this software and its
74Srgrimes * documentation is hereby granted, provided that both the copyright
84Srgrimes * notice and this permission notice appear in all copies of the
94Srgrimes * software, derivative works or modified versions, and any portions
104Srgrimes * thereof, and that both notices appear in supporting documentation.
118876Srgrimes *
128876Srgrimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
134Srgrimes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
144Srgrimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
158876Srgrimes *
164Srgrimes * Carnegie Mellon requests users of this software to return to
178876Srgrimes *
184Srgrimes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
194Srgrimes *  School of Computer Science
204Srgrimes *  Carnegie Mellon University
214Srgrimes *  Pittsburgh PA 15213-3890
228876Srgrimes *
234Srgrimes * any improvements or extensions that they make and grant Carnegie the
244Srgrimes * rights to redistribute these changes.
254Srgrimes */
264Srgrimes/*
274Srgrimes *	Author: David B. Golub, Carnegie Mellon University
284Srgrimes *	Date:	7/90
294Srgrimes */
304Srgrimes
31116176Sobrien#include <sys/cdefs.h>
32116176Sobrien__FBSDID("$FreeBSD$");
33116176Sobrien
342056Swollman#include <sys/param.h>
352056Swollman#include <sys/systm.h>
3649558Sphk#include <sys/cons.h>
3712734Sbde
382056Swollman#include <ddb/ddb.h>
392056Swollman#include <ddb/db_output.h>
404Srgrimes
414Srgrimes/*
424Srgrimes * Character input and editing.
434Srgrimes */
444Srgrimes
454Srgrimes/*
464Srgrimes * We don't track output position while editing input,
474Srgrimes * since input always ends with a new-line.  We just
484Srgrimes * reset the line position at the end.
494Srgrimes */
5012720Sphkstatic char *	db_lbuf_start;	/* start of input line buffer */
5112720Sphkstatic char *	db_lbuf_end;	/* end of input line buffer */
5212720Sphkstatic char *	db_lc;		/* current character */
5312720Sphkstatic char *	db_le;		/* one past last character */
544Srgrimes
5517495Sjoerg/*
5617495Sjoerg * Simple input line history support.
5717495Sjoerg */
5831314Sbdestatic char	db_lhistory[2048];
5917495Sjoergstatic int	db_lhistlsize, db_lhistidx, db_lhistcur;
6031062Smsmithstatic int	db_lhist_nlines;
6117495Sjoerg
624Srgrimes#define	CTRL(c)		((c) & 0x1f)
634Srgrimes#define	BLANK		' '
644Srgrimes#define	BACKUP		'\b'
654Srgrimes
6692756Salfredstatic int	cnmaygetc(void);
6792756Salfredstatic void	db_delete(int n, int bwd);
6892756Salfredstatic int	db_inputchar(int c);
6992756Salfredstatic void	db_putnchars(int c, int count);
7092756Salfredstatic void	db_putstring(char *s, int count);
7112473Sbde
72104094Sphkstatic void
734Srgrimesdb_putstring(s, count)
744Srgrimes	char	*s;
754Srgrimes	int	count;
764Srgrimes{
774Srgrimes	while (--count >= 0)
784Srgrimes	    cnputc(*s++);
794Srgrimes}
804Srgrimes
81104094Sphkstatic void
824Srgrimesdb_putnchars(c, count)
834Srgrimes	int	c;
844Srgrimes	int	count;
854Srgrimes{
864Srgrimes	while (--count >= 0)
874Srgrimes	    cnputc(c);
884Srgrimes}
894Srgrimes
904Srgrimes/*
914Srgrimes * Delete N characters, forward or backward
924Srgrimes */
934Srgrimes#define	DEL_FWD		0
944Srgrimes#define	DEL_BWD		1
95104094Sphkstatic void
964Srgrimesdb_delete(n, bwd)
974Srgrimes	int	n;
984Srgrimes	int	bwd;
994Srgrimes{
1004Srgrimes	register char *p;
1014Srgrimes
1024Srgrimes	if (bwd) {
1034Srgrimes	    db_lc -= n;
1044Srgrimes	    db_putnchars(BACKUP, n);
1054Srgrimes	}
1064Srgrimes	for (p = db_lc; p < db_le-n; p++) {
1074Srgrimes	    *p = *(p+n);
1084Srgrimes	    cnputc(*p);
1094Srgrimes	}
1104Srgrimes	db_putnchars(BLANK, n);
1114Srgrimes	db_putnchars(BACKUP, db_le - db_lc);
1124Srgrimes	db_le -= n;
1134Srgrimes}
1144Srgrimes
1154Srgrimes/* returns TRUE at end-of-line */
116104094Sphkstatic int
1174Srgrimesdb_inputchar(c)
1184Srgrimes	int	c;
1194Srgrimes{
12024840Sjoerg	static int escstate;
12124840Sjoerg
12224840Sjoerg	if (escstate == 1) {
12324840Sjoerg		/* ESC seen, look for [ or O */
12424840Sjoerg		if (c == '[' || c == 'O')
12524840Sjoerg			escstate++;
12624840Sjoerg		else
12724840Sjoerg			escstate = 0; /* re-init state machine */
12824840Sjoerg		return (0);
12924840Sjoerg	} else if (escstate == 2) {
13024840Sjoerg		escstate = 0;
13124840Sjoerg		/*
13224840Sjoerg		 * If a valid cursor key has been found, translate
13324840Sjoerg		 * into an emacs-style control key, and fall through.
13424840Sjoerg		 * Otherwise, drop off.
13524840Sjoerg		 */
13624840Sjoerg		switch (c) {
13724840Sjoerg		case 'A':	/* up */
13824840Sjoerg			c = CTRL('p');
13924840Sjoerg			break;
14024840Sjoerg		case 'B':	/* down */
14124840Sjoerg			c = CTRL('n');
14224840Sjoerg			break;
14324840Sjoerg		case 'C':	/* right */
14424840Sjoerg			c = CTRL('f');
14524840Sjoerg			break;
14624840Sjoerg		case 'D':	/* left */
14724840Sjoerg			c = CTRL('b');
14824840Sjoerg			break;
14924840Sjoerg		default:
15024840Sjoerg			return (0);
15124840Sjoerg		}
15224840Sjoerg	}
15324840Sjoerg
1544Srgrimes	switch (c) {
15524840Sjoerg	    case CTRL('['):
15624840Sjoerg		escstate = 1;
15724840Sjoerg		break;
1584Srgrimes	    case CTRL('b'):
1594Srgrimes		/* back up one character */
1604Srgrimes		if (db_lc > db_lbuf_start) {
1614Srgrimes		    cnputc(BACKUP);
1624Srgrimes		    db_lc--;
1634Srgrimes		}
1644Srgrimes		break;
1654Srgrimes	    case CTRL('f'):
1664Srgrimes		/* forward one character */
1674Srgrimes		if (db_lc < db_le) {
1684Srgrimes		    cnputc(*db_lc);
1694Srgrimes		    db_lc++;
1704Srgrimes		}
1714Srgrimes		break;
1724Srgrimes	    case CTRL('a'):
1734Srgrimes		/* beginning of line */
1744Srgrimes		while (db_lc > db_lbuf_start) {
1754Srgrimes		    cnputc(BACKUP);
1764Srgrimes		    db_lc--;
1774Srgrimes		}
1784Srgrimes		break;
1794Srgrimes	    case CTRL('e'):
1804Srgrimes		/* end of line */
1814Srgrimes		while (db_lc < db_le) {
1824Srgrimes		    cnputc(*db_lc);
1834Srgrimes		    db_lc++;
1844Srgrimes		}
1854Srgrimes		break;
1864Srgrimes	    case CTRL('h'):
1874Srgrimes	    case 0177:
1884Srgrimes		/* erase previous character */
1894Srgrimes		if (db_lc > db_lbuf_start)
1904Srgrimes		    db_delete(1, DEL_BWD);
1914Srgrimes		break;
1924Srgrimes	    case CTRL('d'):
1934Srgrimes		/* erase next character */
1944Srgrimes		if (db_lc < db_le)
1954Srgrimes		    db_delete(1, DEL_FWD);
1964Srgrimes		break;
19790591Syar	    case CTRL('u'):
19890591Syar		/* kill entire line: */
19990591Syar		/* at first, delete to beginning of line */
20090591Syar		if (db_lc > db_lbuf_start)
20190591Syar		    db_delete(db_lc - db_lbuf_start, DEL_BWD);
20290591Syar		/* FALLTHROUGH */
2034Srgrimes	    case CTRL('k'):
2044Srgrimes		/* delete to end of line */
2054Srgrimes		if (db_lc < db_le)
2064Srgrimes		    db_delete(db_le - db_lc, DEL_FWD);
2074Srgrimes		break;
2084Srgrimes	    case CTRL('t'):
2094Srgrimes		/* twiddle last 2 characters */
2104Srgrimes		if (db_lc >= db_lbuf_start + 2) {
2114Srgrimes		    c = db_lc[-2];
2124Srgrimes		    db_lc[-2] = db_lc[-1];
2134Srgrimes		    db_lc[-1] = c;
2144Srgrimes		    cnputc(BACKUP);
2154Srgrimes		    cnputc(BACKUP);
2164Srgrimes		    cnputc(db_lc[-2]);
2174Srgrimes		    cnputc(db_lc[-1]);
2184Srgrimes		}
2194Srgrimes		break;
2204Srgrimes	    case CTRL('r'):
2214Srgrimes		db_putstring("^R\n", 3);
22217495Sjoerg	    redraw:
2234Srgrimes		if (db_le > db_lbuf_start) {
2244Srgrimes		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
2254Srgrimes		    db_putnchars(BACKUP, db_le - db_lc);
2264Srgrimes		}
2274Srgrimes		break;
22817495Sjoerg	    case CTRL('p'):
22917495Sjoerg		/* Make previous history line the active one. */
23017495Sjoerg		if (db_lhistcur >= 0) {
23117495Sjoerg		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
23217495Sjoerg			  db_lbuf_start, db_lhistlsize);
23317495Sjoerg		    db_lhistcur--;
23417495Sjoerg		    goto hist_redraw;
23517495Sjoerg		}
23617495Sjoerg		break;
23717495Sjoerg	    case CTRL('n'):
23817495Sjoerg		/* Make next history line the active one. */
23917495Sjoerg		if (db_lhistcur < db_lhistidx - 1) {
24017495Sjoerg		    db_lhistcur += 2;
24117495Sjoerg		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
24217495Sjoerg			  db_lbuf_start, db_lhistlsize);
24317495Sjoerg		} else {
24417495Sjoerg		    /*
24517495Sjoerg		     * ^N through tail of history, reset the
24617495Sjoerg		     * buffer to zero length.
24717495Sjoerg		     */
24817495Sjoerg		    *db_lbuf_start = '\0';
24917495Sjoerg		    db_lhistcur = db_lhistidx;
25017495Sjoerg		}
25117495Sjoerg
25217495Sjoerg	    hist_redraw:
253176870Srwatson		db_putnchars(BACKUP, db_lc - db_lbuf_start);
25417495Sjoerg		db_putnchars(BLANK, db_le - db_lbuf_start);
25517495Sjoerg		db_putnchars(BACKUP, db_le - db_lbuf_start);
256229272Sed		db_le = strchr(db_lbuf_start, '\0');
25717495Sjoerg		if (db_le[-1] == '\r' || db_le[-1] == '\n')
25817495Sjoerg		    *--db_le = '\0';
25917495Sjoerg		db_lc = db_le;
26017495Sjoerg		goto redraw;
26117495Sjoerg
26219268Sjulian	    case -1:
26319268Sjulian		/*
26419268Sjulian		 * eek! the console returned eof.
26519268Sjulian		 * probably that means we HAVE no console.. we should try bail
26619268Sjulian		 * XXX
26719268Sjulian		 */
26819268Sjulian		c = '\r';
2694Srgrimes	    case '\n':
270115495Sphk		/* FALLTHROUGH */
2714Srgrimes	    case '\r':
2724Srgrimes		*db_le++ = c;
2734Srgrimes		return (1);
2744Srgrimes	    default:
2754Srgrimes		if (db_le == db_lbuf_end) {
2764Srgrimes		    cnputc('\007');
2774Srgrimes		}
2784Srgrimes		else if (c >= ' ' && c <= '~') {
2794Srgrimes		    register char *p;
2804Srgrimes
2814Srgrimes		    for (p = db_le; p > db_lc; p--)
2824Srgrimes			*p = *(p-1);
2834Srgrimes		    *db_lc++ = c;
2844Srgrimes		    db_le++;
2854Srgrimes		    cnputc(c);
2864Srgrimes		    db_putstring(db_lc, db_le - db_lc);
2874Srgrimes		    db_putnchars(BACKUP, db_le - db_lc);
2884Srgrimes		}
2894Srgrimes		break;
2904Srgrimes	}
2914Srgrimes	return (0);
2924Srgrimes}
2934Srgrimes
294104094Sphkstatic int
29512473Sbdecnmaygetc()
2962112Swollman{
2972112Swollman	return (-1);
2982112Swollman}
2992112Swollman
3002112Swollmanint
3014Srgrimesdb_readline(lstart, lsize)
3024Srgrimes	char *	lstart;
3034Srgrimes	int	lsize;
3044Srgrimes{
305176895Srwatson
306176895Srwatson	if (lsize < 2)
307176895Srwatson		return (0);
30831314Sbde	if (lsize != db_lhistlsize) {
30931314Sbde		/*
31031314Sbde		 * (Re)initialize input line history.  Throw away any
31131314Sbde		 * existing history.
31231314Sbde		 */
31331314Sbde		db_lhist_nlines = sizeof(db_lhistory) / lsize;
31417495Sjoerg		db_lhistlsize = lsize;
31517495Sjoerg		db_lhistidx = -1;
31617495Sjoerg	}
31717495Sjoerg	db_lhistcur = db_lhistidx;
31817495Sjoerg
3194Srgrimes	db_force_whitespace();	/* synch output position */
3204Srgrimes
3214Srgrimes	db_lbuf_start = lstart;
322176895Srwatson	db_lbuf_end   = lstart + lsize - 2;	/* Will append NL and NUL. */
3234Srgrimes	db_lc = lstart;
3244Srgrimes	db_le = lstart;
3254Srgrimes
3264Srgrimes	while (!db_inputchar(cngetc()))
3274Srgrimes	    continue;
3284Srgrimes
329174910Srwatson	db_capture_write(lstart, db_le - db_lbuf_start);
33015680Sgpalmer	db_printf("\n");	/* synch output position */
33117495Sjoerg	*db_le = 0;
3324Srgrimes
33331314Sbde	if (db_le - db_lbuf_start > 1) {
33417495Sjoerg	    /* Maintain input line history for non-empty lines. */
33531062Smsmith	    if (++db_lhistidx == db_lhist_nlines) {
33617495Sjoerg		/* Rotate history. */
337113071Sdes		bcopy(db_lhistory + db_lhistlsize, db_lhistory,
338113071Sdes		      db_lhistlsize * (db_lhist_nlines - 1));
33917495Sjoerg		db_lhistidx--;
34017495Sjoerg	    }
34131314Sbde	    bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
34217495Sjoerg		  db_lhistlsize);
34317495Sjoerg	}
34417495Sjoerg
3454Srgrimes	return (db_le - db_lbuf_start);
3464Srgrimes}
3474Srgrimes
3484Srgrimesvoid
3494Srgrimesdb_check_interrupt()
3504Srgrimes{
3514Srgrimes	register int	c;
3524Srgrimes
3534Srgrimes	c = cnmaygetc();
3544Srgrimes	switch (c) {
3554Srgrimes	    case -1:		/* no character */
3564Srgrimes		return;
3574Srgrimes
3584Srgrimes	    case CTRL('c'):
3594Srgrimes		db_error((char *)0);
3604Srgrimes		/*NOTREACHED*/
3614Srgrimes
3624Srgrimes	    case CTRL('s'):
3634Srgrimes		do {
3644Srgrimes		    c = cnmaygetc();
3654Srgrimes		    if (c == CTRL('c'))
3664Srgrimes			db_error((char *)0);
3674Srgrimes		} while (c != CTRL('q'));
3684Srgrimes		break;
3694Srgrimes
3704Srgrimes	    default:
3714Srgrimes		/* drop on floor */
3724Srgrimes		break;
3734Srgrimes	}
3744Srgrimes}
375