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: stable/11/sys/ddb/db_input.c 324266 2017-10-04 11:54:15Z trasz $");
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{
100283315Spfg	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
115283088Spfg/* 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'):
198324264Strasz	    case CTRL('c'):
19990591Syar		/* kill entire line: */
20090591Syar		/* at first, delete to beginning of line */
20190591Syar		if (db_lc > db_lbuf_start)
20290591Syar		    db_delete(db_lc - db_lbuf_start, DEL_BWD);
20390591Syar		/* FALLTHROUGH */
2044Srgrimes	    case CTRL('k'):
2054Srgrimes		/* delete to end of line */
2064Srgrimes		if (db_lc < db_le)
2074Srgrimes		    db_delete(db_le - db_lc, DEL_FWD);
2084Srgrimes		break;
2094Srgrimes	    case CTRL('t'):
2104Srgrimes		/* twiddle last 2 characters */
2114Srgrimes		if (db_lc >= db_lbuf_start + 2) {
2124Srgrimes		    c = db_lc[-2];
2134Srgrimes		    db_lc[-2] = db_lc[-1];
2144Srgrimes		    db_lc[-1] = c;
2154Srgrimes		    cnputc(BACKUP);
2164Srgrimes		    cnputc(BACKUP);
2174Srgrimes		    cnputc(db_lc[-2]);
2184Srgrimes		    cnputc(db_lc[-1]);
2194Srgrimes		}
2204Srgrimes		break;
221324266Strasz	    case CTRL('w'):
222324266Strasz		/* erase previous word */
223324266Strasz		for (; db_lc > db_lbuf_start;) {
224324266Strasz		    if (*(db_lc - 1) != ' ')
225324266Strasz			break;
226324266Strasz		    db_delete(1, DEL_BWD);
227324266Strasz		}
228324266Strasz		for (; db_lc > db_lbuf_start;) {
229324266Strasz		    if (*(db_lc - 1) == ' ')
230324266Strasz			break;
231324266Strasz		    db_delete(1, DEL_BWD);
232324266Strasz		}
233324266Strasz		break;
2344Srgrimes	    case CTRL('r'):
2354Srgrimes		db_putstring("^R\n", 3);
23617495Sjoerg	    redraw:
2374Srgrimes		if (db_le > db_lbuf_start) {
2384Srgrimes		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
2394Srgrimes		    db_putnchars(BACKUP, db_le - db_lc);
2404Srgrimes		}
2414Srgrimes		break;
24217495Sjoerg	    case CTRL('p'):
24317495Sjoerg		/* Make previous history line the active one. */
24417495Sjoerg		if (db_lhistcur >= 0) {
24517495Sjoerg		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
24617495Sjoerg			  db_lbuf_start, db_lhistlsize);
24717495Sjoerg		    db_lhistcur--;
24817495Sjoerg		    goto hist_redraw;
24917495Sjoerg		}
25017495Sjoerg		break;
25117495Sjoerg	    case CTRL('n'):
25217495Sjoerg		/* Make next history line the active one. */
25317495Sjoerg		if (db_lhistcur < db_lhistidx - 1) {
25417495Sjoerg		    db_lhistcur += 2;
25517495Sjoerg		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
25617495Sjoerg			  db_lbuf_start, db_lhistlsize);
25717495Sjoerg		} else {
25817495Sjoerg		    /*
25917495Sjoerg		     * ^N through tail of history, reset the
26017495Sjoerg		     * buffer to zero length.
26117495Sjoerg		     */
26217495Sjoerg		    *db_lbuf_start = '\0';
26317495Sjoerg		    db_lhistcur = db_lhistidx;
26417495Sjoerg		}
26517495Sjoerg
26617495Sjoerg	    hist_redraw:
267176870Srwatson		db_putnchars(BACKUP, db_lc - db_lbuf_start);
26817495Sjoerg		db_putnchars(BLANK, db_le - db_lbuf_start);
26917495Sjoerg		db_putnchars(BACKUP, db_le - db_lbuf_start);
270229272Sed		db_le = strchr(db_lbuf_start, '\0');
27117495Sjoerg		if (db_le[-1] == '\r' || db_le[-1] == '\n')
27217495Sjoerg		    *--db_le = '\0';
27317495Sjoerg		db_lc = db_le;
27417495Sjoerg		goto redraw;
27517495Sjoerg
27619268Sjulian	    case -1:
27719268Sjulian		/*
27819268Sjulian		 * eek! the console returned eof.
27919268Sjulian		 * probably that means we HAVE no console.. we should try bail
28019268Sjulian		 * XXX
28119268Sjulian		 */
28219268Sjulian		c = '\r';
2834Srgrimes	    case '\n':
284115495Sphk		/* FALLTHROUGH */
2854Srgrimes	    case '\r':
2864Srgrimes		*db_le++ = c;
2874Srgrimes		return (1);
2884Srgrimes	    default:
2894Srgrimes		if (db_le == db_lbuf_end) {
2904Srgrimes		    cnputc('\007');
2914Srgrimes		}
2924Srgrimes		else if (c >= ' ' && c <= '~') {
293283315Spfg		    char *p;
2944Srgrimes
2954Srgrimes		    for (p = db_le; p > db_lc; p--)
2964Srgrimes			*p = *(p-1);
2974Srgrimes		    *db_lc++ = c;
2984Srgrimes		    db_le++;
2994Srgrimes		    cnputc(c);
3004Srgrimes		    db_putstring(db_lc, db_le - db_lc);
3014Srgrimes		    db_putnchars(BACKUP, db_le - db_lc);
3024Srgrimes		}
3034Srgrimes		break;
3044Srgrimes	}
3054Srgrimes	return (0);
3064Srgrimes}
3074Srgrimes
308104094Sphkstatic int
30912473Sbdecnmaygetc()
3102112Swollman{
3112112Swollman	return (-1);
3122112Swollman}
3132112Swollman
3142112Swollmanint
3154Srgrimesdb_readline(lstart, lsize)
3164Srgrimes	char *	lstart;
3174Srgrimes	int	lsize;
3184Srgrimes{
319176895Srwatson
320176895Srwatson	if (lsize < 2)
321176895Srwatson		return (0);
32231314Sbde	if (lsize != db_lhistlsize) {
32331314Sbde		/*
32431314Sbde		 * (Re)initialize input line history.  Throw away any
32531314Sbde		 * existing history.
32631314Sbde		 */
32731314Sbde		db_lhist_nlines = sizeof(db_lhistory) / lsize;
32817495Sjoerg		db_lhistlsize = lsize;
32917495Sjoerg		db_lhistidx = -1;
33017495Sjoerg	}
33117495Sjoerg	db_lhistcur = db_lhistidx;
33217495Sjoerg
3334Srgrimes	db_force_whitespace();	/* synch output position */
3344Srgrimes
3354Srgrimes	db_lbuf_start = lstart;
336176895Srwatson	db_lbuf_end   = lstart + lsize - 2;	/* Will append NL and NUL. */
3374Srgrimes	db_lc = lstart;
3384Srgrimes	db_le = lstart;
3394Srgrimes
3404Srgrimes	while (!db_inputchar(cngetc()))
3414Srgrimes	    continue;
3424Srgrimes
343174910Srwatson	db_capture_write(lstart, db_le - db_lbuf_start);
34415680Sgpalmer	db_printf("\n");	/* synch output position */
34517495Sjoerg	*db_le = 0;
3464Srgrimes
34731314Sbde	if (db_le - db_lbuf_start > 1) {
34817495Sjoerg	    /* Maintain input line history for non-empty lines. */
34931062Smsmith	    if (++db_lhistidx == db_lhist_nlines) {
35017495Sjoerg		/* Rotate history. */
351113071Sdes		bcopy(db_lhistory + db_lhistlsize, db_lhistory,
352113071Sdes		      db_lhistlsize * (db_lhist_nlines - 1));
35317495Sjoerg		db_lhistidx--;
35417495Sjoerg	    }
35531314Sbde	    bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
35617495Sjoerg		  db_lhistlsize);
35717495Sjoerg	}
35817495Sjoerg
3594Srgrimes	return (db_le - db_lbuf_start);
3604Srgrimes}
3614Srgrimes
3624Srgrimesvoid
363273006Spfgdb_check_interrupt(void)
3644Srgrimes{
365283315Spfg	int	c;
3664Srgrimes
3674Srgrimes	c = cnmaygetc();
3684Srgrimes	switch (c) {
3694Srgrimes	    case -1:		/* no character */
3704Srgrimes		return;
3714Srgrimes
3724Srgrimes	    case CTRL('c'):
3734Srgrimes		db_error((char *)0);
3744Srgrimes		/*NOTREACHED*/
3754Srgrimes
3764Srgrimes	    case CTRL('s'):
3774Srgrimes		do {
3784Srgrimes		    c = cnmaygetc();
3794Srgrimes		    if (c == CTRL('c'))
3804Srgrimes			db_error((char *)0);
3814Srgrimes		} while (c != CTRL('q'));
3824Srgrimes		break;
3834Srgrimes
3844Srgrimes	    default:
3854Srgrimes		/* drop on floor */
3864Srgrimes		break;
3874Srgrimes	}
3884Srgrimes}
389