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 #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(char *s, int count)
80 db_putnchars(int c, int count)
87 * Delete N characters, forward or backward
92 db_delete(int n, int bwd)
98 db_putnchars(BACKUP, n);
100 for (p = db_lc; p < db_le-n; p++) {
104 db_putnchars(BLANK, n);
105 db_putnchars(BACKUP, db_le - db_lc);
109 /* returns true at end-of-line */
116 /* ESC seen, look for [ or O */
117 if (c == '[' || c == 'O')
120 escstate = 0; /* re-init state machine */
122 } else if (escstate == 2) {
125 * If a valid cursor key has been found, translate
126 * into an emacs-style control key, and fall through.
127 * Otherwise, drop off.
136 case 'C': /* right */
152 /* back up one character */
153 if (db_lc > db_lbuf_start) {
159 /* forward one character */
166 /* beginning of line */
167 while (db_lc > db_lbuf_start) {
174 while (db_lc < db_le) {
181 /* erase previous character */
182 if (db_lc > db_lbuf_start)
183 db_delete(1, DEL_BWD);
186 /* erase next character */
188 db_delete(1, DEL_FWD);
192 /* kill entire line: */
193 /* at first, delete to beginning of line */
194 if (db_lc > db_lbuf_start)
195 db_delete(db_lc - db_lbuf_start, DEL_BWD);
198 /* delete to end of line */
200 db_delete(db_le - db_lc, DEL_FWD);
203 /* twiddle last 2 characters */
204 if (db_lc >= db_lbuf_start + 2) {
206 db_lc[-2] = db_lc[-1];
215 /* erase previous word */
216 for (; db_lc > db_lbuf_start;) {
217 if (*(db_lc - 1) != ' ')
219 db_delete(1, DEL_BWD);
221 for (; db_lc > db_lbuf_start;) {
222 if (*(db_lc - 1) == ' ')
224 db_delete(1, DEL_BWD);
228 db_putstring("^R\n", 3);
230 if (db_le > db_lbuf_start) {
231 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
232 db_putnchars(BACKUP, db_le - db_lc);
236 /* Make previous history line the active one. */
237 if (db_lhistcur >= 0) {
238 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
239 db_lbuf_start, db_lhistlsize);
245 /* Make next history line the active one. */
246 if (db_lhistcur < db_lhistidx - 1) {
248 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
249 db_lbuf_start, db_lhistlsize);
252 * ^N through tail of history, reset the
253 * buffer to zero length.
255 *db_lbuf_start = '\0';
256 db_lhistcur = db_lhistidx;
260 db_putnchars(BACKUP, db_lc - db_lbuf_start);
261 db_putnchars(BLANK, db_le - db_lbuf_start);
262 db_putnchars(BACKUP, db_le - db_lbuf_start);
263 db_le = strchr(db_lbuf_start, '\0');
264 if (db_le[-1] == '\r' || db_le[-1] == '\n')
271 * eek! the console returned eof.
272 * probably that means we HAVE no console.. we should try bail
282 if (db_le == db_lbuf_end) {
285 else if (c >= ' ' && c <= '~') {
288 for (p = db_le; p > db_lc; p--)
293 db_putstring(db_lc, db_le - db_lc);
294 db_putnchars(BACKUP, db_le - db_lc);
308 db_readline(char *lstart, int lsize)
313 if (lsize != db_lhistlsize) {
315 * (Re)initialize input line history. Throw away any
318 db_lhist_nlines = sizeof(db_lhistory) / lsize;
319 db_lhistlsize = lsize;
322 db_lhistcur = db_lhistidx;
324 db_force_whitespace(); /* synch output position */
326 db_lbuf_start = lstart;
327 db_lbuf_end = lstart + lsize - 2; /* Will append NL and NUL. */
331 while (!db_inputchar(cngetc()))
334 db_capture_write(lstart, db_le - db_lbuf_start);
335 db_printf("\n"); /* synch output position */
338 if (db_le - db_lbuf_start > 1) {
339 /* Maintain input line history for non-empty lines. */
340 if (++db_lhistidx == db_lhist_nlines) {
341 /* Rotate history. */
342 bcopy(db_lhistory + db_lhistlsize, db_lhistory,
343 db_lhistlsize * (db_lhist_nlines - 1));
346 bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
350 return (db_le - db_lbuf_start);
354 db_check_interrupt(void)
360 case -1: /* no character */
372 } while (c != CTRL('q'));