2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 * Author: David B. Golub, Carnegie Mellon University
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <ddb/db_output.h>
42 * Character input and editing.
46 * We don't track output position while editing input,
47 * since input always ends with a new-line. We just
48 * reset the line position at the end.
50 static char * db_lbuf_start; /* start of input line buffer */
51 static char * db_lbuf_end; /* end of input line buffer */
52 static char * db_lc; /* current character */
53 static char * db_le; /* one past last character */
56 * Simple input line history support.
58 static char db_lhistory[2048];
59 static int db_lhistlsize, db_lhistidx, db_lhistcur;
60 static int db_lhist_nlines;
62 #define CTRL(c) ((c) & 0x1f)
66 static int cnmaygetc(void);
67 static void db_delete(int n, int bwd);
68 static int db_inputchar(int c);
69 static void db_putnchars(int c, int count);
70 static void db_putstring(char *s, int count);
73 db_putstring(s, count)
82 db_putnchars(c, count)
91 * Delete N characters, forward or backward
104 db_putnchars(BACKUP, n);
106 for (p = db_lc; p < db_le-n; p++) {
110 db_putnchars(BLANK, n);
111 db_putnchars(BACKUP, db_le - db_lc);
115 /* returns true at end-of-line */
123 /* ESC seen, look for [ or O */
124 if (c == '[' || c == 'O')
127 escstate = 0; /* re-init state machine */
129 } else if (escstate == 2) {
132 * If a valid cursor key has been found, translate
133 * into an emacs-style control key, and fall through.
134 * Otherwise, drop off.
143 case 'C': /* right */
159 /* back up one character */
160 if (db_lc > db_lbuf_start) {
166 /* forward one character */
173 /* beginning of line */
174 while (db_lc > db_lbuf_start) {
181 while (db_lc < db_le) {
188 /* erase previous character */
189 if (db_lc > db_lbuf_start)
190 db_delete(1, DEL_BWD);
193 /* erase next character */
195 db_delete(1, DEL_FWD);
199 /* kill entire line: */
200 /* at first, delete to beginning of line */
201 if (db_lc > db_lbuf_start)
202 db_delete(db_lc - db_lbuf_start, DEL_BWD);
205 /* delete to end of line */
207 db_delete(db_le - db_lc, DEL_FWD);
210 /* twiddle last 2 characters */
211 if (db_lc >= db_lbuf_start + 2) {
213 db_lc[-2] = db_lc[-1];
222 /* erase previous word */
223 for (; db_lc > db_lbuf_start;) {
224 if (*(db_lc - 1) != ' ')
226 db_delete(1, DEL_BWD);
228 for (; db_lc > db_lbuf_start;) {
229 if (*(db_lc - 1) == ' ')
231 db_delete(1, DEL_BWD);
235 db_putstring("^R\n", 3);
237 if (db_le > db_lbuf_start) {
238 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
239 db_putnchars(BACKUP, db_le - db_lc);
243 /* Make previous history line the active one. */
244 if (db_lhistcur >= 0) {
245 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
246 db_lbuf_start, db_lhistlsize);
252 /* Make next history line the active one. */
253 if (db_lhistcur < db_lhistidx - 1) {
255 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
256 db_lbuf_start, db_lhistlsize);
259 * ^N through tail of history, reset the
260 * buffer to zero length.
262 *db_lbuf_start = '\0';
263 db_lhistcur = db_lhistidx;
267 db_putnchars(BACKUP, db_lc - db_lbuf_start);
268 db_putnchars(BLANK, db_le - db_lbuf_start);
269 db_putnchars(BACKUP, db_le - db_lbuf_start);
270 db_le = strchr(db_lbuf_start, '\0');
271 if (db_le[-1] == '\r' || db_le[-1] == '\n')
278 * eek! the console returned eof.
279 * probably that means we HAVE no console.. we should try bail
289 if (db_le == db_lbuf_end) {
292 else if (c >= ' ' && c <= '~') {
295 for (p = db_le; p > db_lc; p--)
300 db_putstring(db_lc, db_le - db_lc);
301 db_putnchars(BACKUP, db_le - db_lc);
315 db_readline(lstart, lsize)
322 if (lsize != db_lhistlsize) {
324 * (Re)initialize input line history. Throw away any
327 db_lhist_nlines = sizeof(db_lhistory) / lsize;
328 db_lhistlsize = lsize;
331 db_lhistcur = db_lhistidx;
333 db_force_whitespace(); /* synch output position */
335 db_lbuf_start = lstart;
336 db_lbuf_end = lstart + lsize - 2; /* Will append NL and NUL. */
340 while (!db_inputchar(cngetc()))
343 db_capture_write(lstart, db_le - db_lbuf_start);
344 db_printf("\n"); /* synch output position */
347 if (db_le - db_lbuf_start > 1) {
348 /* Maintain input line history for non-empty lines. */
349 if (++db_lhistidx == db_lhist_nlines) {
350 /* Rotate history. */
351 bcopy(db_lhistory + db_lhistlsize, db_lhistory,
352 db_lhistlsize * (db_lhist_nlines - 1));
355 bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
359 return (db_le - db_lbuf_start);
363 db_check_interrupt(void)
369 case -1: /* no character */
381 } while (c != CTRL('q'));