2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
41 #include <ddb/db_output.h>
44 * Character input and editing.
48 * We don't track output position while editing input,
49 * since input always ends with a new-line. We just
50 * reset the line position at the end.
52 static char * db_lbuf_start; /* start of input line buffer */
53 static char * db_lbuf_end; /* end of input line buffer */
54 static char * db_lc; /* current character */
55 static char * db_le; /* one past last character */
58 * Simple input line history support.
60 static char db_lhistory[2048];
61 static int db_lhistlsize, db_lhistidx, db_lhistcur;
62 static int db_lhist_nlines;
64 #define CTRL(c) ((c) & 0x1f)
68 static int cnmaygetc(void);
69 static void db_delete(int n, int bwd);
70 static int db_inputchar(int c);
71 static void db_putnchars(int c, int count);
72 static void db_putstring(char *s, int count);
75 db_putstring(s, count)
84 db_putnchars(c, count)
93 * Delete N characters, forward or backward
106 db_putnchars(BACKUP, n);
108 for (p = db_lc; p < db_le-n; p++) {
112 db_putnchars(BLANK, n);
113 db_putnchars(BACKUP, db_le - db_lc);
117 /* returns true at end-of-line */
125 /* ESC seen, look for [ or O */
126 if (c == '[' || c == 'O')
129 escstate = 0; /* re-init state machine */
131 } else if (escstate == 2) {
134 * If a valid cursor key has been found, translate
135 * into an emacs-style control key, and fall through.
136 * Otherwise, drop off.
145 case 'C': /* right */
161 /* back up one character */
162 if (db_lc > db_lbuf_start) {
168 /* forward one character */
175 /* beginning of line */
176 while (db_lc > db_lbuf_start) {
183 while (db_lc < db_le) {
190 /* erase previous character */
191 if (db_lc > db_lbuf_start)
192 db_delete(1, DEL_BWD);
195 /* erase next character */
197 db_delete(1, DEL_FWD);
201 /* kill entire line: */
202 /* at first, delete to beginning of line */
203 if (db_lc > db_lbuf_start)
204 db_delete(db_lc - db_lbuf_start, DEL_BWD);
207 /* delete to end of line */
209 db_delete(db_le - db_lc, DEL_FWD);
212 /* twiddle last 2 characters */
213 if (db_lc >= db_lbuf_start + 2) {
215 db_lc[-2] = db_lc[-1];
224 /* erase previous word */
225 for (; db_lc > db_lbuf_start;) {
226 if (*(db_lc - 1) != ' ')
228 db_delete(1, DEL_BWD);
230 for (; db_lc > db_lbuf_start;) {
231 if (*(db_lc - 1) == ' ')
233 db_delete(1, DEL_BWD);
237 db_putstring("^R\n", 3);
239 if (db_le > db_lbuf_start) {
240 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
241 db_putnchars(BACKUP, db_le - db_lc);
245 /* Make previous history line the active one. */
246 if (db_lhistcur >= 0) {
247 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
248 db_lbuf_start, db_lhistlsize);
254 /* Make next history line the active one. */
255 if (db_lhistcur < db_lhistidx - 1) {
257 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
258 db_lbuf_start, db_lhistlsize);
261 * ^N through tail of history, reset the
262 * buffer to zero length.
264 *db_lbuf_start = '\0';
265 db_lhistcur = db_lhistidx;
269 db_putnchars(BACKUP, db_lc - db_lbuf_start);
270 db_putnchars(BLANK, db_le - db_lbuf_start);
271 db_putnchars(BACKUP, db_le - db_lbuf_start);
272 db_le = strchr(db_lbuf_start, '\0');
273 if (db_le[-1] == '\r' || db_le[-1] == '\n')
280 * eek! the console returned eof.
281 * probably that means we HAVE no console.. we should try bail
291 if (db_le == db_lbuf_end) {
294 else if (c >= ' ' && c <= '~') {
297 for (p = db_le; p > db_lc; p--)
302 db_putstring(db_lc, db_le - db_lc);
303 db_putnchars(BACKUP, db_le - db_lc);
317 db_readline(lstart, lsize)
324 if (lsize != db_lhistlsize) {
326 * (Re)initialize input line history. Throw away any
329 db_lhist_nlines = sizeof(db_lhistory) / lsize;
330 db_lhistlsize = lsize;
333 db_lhistcur = db_lhistidx;
335 db_force_whitespace(); /* synch output position */
337 db_lbuf_start = lstart;
338 db_lbuf_end = lstart + lsize - 2; /* Will append NL and NUL. */
342 while (!db_inputchar(cngetc()))
345 db_capture_write(lstart, db_le - db_lbuf_start);
346 db_printf("\n"); /* synch output position */
349 if (db_le - db_lbuf_start > 1) {
350 /* Maintain input line history for non-empty lines. */
351 if (++db_lhistidx == db_lhist_nlines) {
352 /* Rotate history. */
353 bcopy(db_lhistory + db_lhistlsize, db_lhistory,
354 db_lhistlsize * (db_lhist_nlines - 1));
357 bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
361 return (db_le - db_lbuf_start);
365 db_check_interrupt(void)
371 case -1: /* no character */
383 } while (c != CTRL('q'));