xref: /freebsd/sys/ddb/db_input.c (revision 792bbaba989533a1fc93823df1720c8c4aaf0442)
1 /*-
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
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.
11  *
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.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  */
26 /*
27  *	Author: David B. Golub, Carnegie Mellon University
28  *	Date:	7/90
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/cons.h>
37 
38 #include <ddb/ddb.h>
39 #include <ddb/db_output.h>
40 
41 /*
42  * Character input and editing.
43  */
44 
45 /*
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.
49  */
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 */
54 
55 /*
56  * Simple input line history support.
57  */
58 static char	db_lhistory[2048];
59 static int	db_lhistlsize, db_lhistidx, db_lhistcur;
60 static int	db_lhist_nlines;
61 
62 #define	CTRL(c)		((c) & 0x1f)
63 #define	BLANK		' '
64 #define	BACKUP		'\b'
65 
66 static void	db_delete(int n, int bwd);
67 static int	db_inputchar(int c);
68 static void	db_putnchars(int c, int count);
69 static void	db_putstring(char *s, int count);
70 
71 static void
72 db_putstring(s, count)
73 	char	*s;
74 	int	count;
75 {
76 	while (--count >= 0)
77 	    cnputc(*s++);
78 }
79 
80 static void
81 db_putnchars(c, count)
82 	int	c;
83 	int	count;
84 {
85 	while (--count >= 0)
86 	    cnputc(c);
87 }
88 
89 /*
90  * Delete N characters, forward or backward
91  */
92 #define	DEL_FWD		0
93 #define	DEL_BWD		1
94 static void
95 db_delete(n, bwd)
96 	int	n;
97 	int	bwd;
98 {
99 	char *p;
100 
101 	if (bwd) {
102 	    db_lc -= n;
103 	    db_putnchars(BACKUP, n);
104 	}
105 	for (p = db_lc; p < db_le-n; p++) {
106 	    *p = *(p+n);
107 	    cnputc(*p);
108 	}
109 	db_putnchars(BLANK, n);
110 	db_putnchars(BACKUP, db_le - db_lc);
111 	db_le -= n;
112 }
113 
114 /* returns true at end-of-line */
115 static int
116 db_inputchar(c)
117 	int	c;
118 {
119 	static int escstate;
120 
121 	if (escstate == 1) {
122 		/* ESC seen, look for [ or O */
123 		if (c == '[' || c == 'O')
124 			escstate++;
125 		else
126 			escstate = 0; /* re-init state machine */
127 		return (0);
128 	} else if (escstate == 2) {
129 		escstate = 0;
130 		/*
131 		 * If a valid cursor key has been found, translate
132 		 * into an emacs-style control key, and fall through.
133 		 * Otherwise, drop off.
134 		 */
135 		switch (c) {
136 		case 'A':	/* up */
137 			c = CTRL('p');
138 			break;
139 		case 'B':	/* down */
140 			c = CTRL('n');
141 			break;
142 		case 'C':	/* right */
143 			c = CTRL('f');
144 			break;
145 		case 'D':	/* left */
146 			c = CTRL('b');
147 			break;
148 		default:
149 			return (0);
150 		}
151 	}
152 
153 	switch (c) {
154 	    case CTRL('['):
155 		escstate = 1;
156 		break;
157 	    case CTRL('b'):
158 		/* back up one character */
159 		if (db_lc > db_lbuf_start) {
160 		    cnputc(BACKUP);
161 		    db_lc--;
162 		}
163 		break;
164 	    case CTRL('f'):
165 		/* forward one character */
166 		if (db_lc < db_le) {
167 		    cnputc(*db_lc);
168 		    db_lc++;
169 		}
170 		break;
171 	    case CTRL('a'):
172 		/* beginning of line */
173 		while (db_lc > db_lbuf_start) {
174 		    cnputc(BACKUP);
175 		    db_lc--;
176 		}
177 		break;
178 	    case CTRL('e'):
179 		/* end of line */
180 		while (db_lc < db_le) {
181 		    cnputc(*db_lc);
182 		    db_lc++;
183 		}
184 		break;
185 	    case CTRL('h'):
186 	    case 0177:
187 		/* erase previous character */
188 		if (db_lc > db_lbuf_start)
189 		    db_delete(1, DEL_BWD);
190 		break;
191 	    case CTRL('d'):
192 		/* erase next character */
193 		if (db_lc < db_le)
194 		    db_delete(1, DEL_FWD);
195 		break;
196 	    case CTRL('u'):
197 		/* kill entire line: */
198 		/* at first, delete to beginning of line */
199 		if (db_lc > db_lbuf_start)
200 		    db_delete(db_lc - db_lbuf_start, DEL_BWD);
201 		/* FALLTHROUGH */
202 	    case CTRL('k'):
203 		/* delete to end of line */
204 		if (db_lc < db_le)
205 		    db_delete(db_le - db_lc, DEL_FWD);
206 		break;
207 	    case CTRL('t'):
208 		/* twiddle last 2 characters */
209 		if (db_lc >= db_lbuf_start + 2) {
210 		    c = db_lc[-2];
211 		    db_lc[-2] = db_lc[-1];
212 		    db_lc[-1] = c;
213 		    cnputc(BACKUP);
214 		    cnputc(BACKUP);
215 		    cnputc(db_lc[-2]);
216 		    cnputc(db_lc[-1]);
217 		}
218 		break;
219 	    case CTRL('r'):
220 		db_putstring("^R\n", 3);
221 	    redraw:
222 		if (db_le > db_lbuf_start) {
223 		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
224 		    db_putnchars(BACKUP, db_le - db_lc);
225 		}
226 		break;
227 	    case CTRL('p'):
228 		/* Make previous history line the active one. */
229 		if (db_lhistcur >= 0) {
230 		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
231 			  db_lbuf_start, db_lhistlsize);
232 		    db_lhistcur--;
233 		    goto hist_redraw;
234 		}
235 		break;
236 	    case CTRL('n'):
237 		/* Make next history line the active one. */
238 		if (db_lhistcur < db_lhistidx - 1) {
239 		    db_lhistcur += 2;
240 		    bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
241 			  db_lbuf_start, db_lhistlsize);
242 		} else {
243 		    /*
244 		     * ^N through tail of history, reset the
245 		     * buffer to zero length.
246 		     */
247 		    *db_lbuf_start = '\0';
248 		    db_lhistcur = db_lhistidx;
249 		}
250 
251 	    hist_redraw:
252 		db_putnchars(BACKUP, db_lc - db_lbuf_start);
253 		db_putnchars(BLANK, db_le - db_lbuf_start);
254 		db_putnchars(BACKUP, db_le - db_lbuf_start);
255 		db_le = strchr(db_lbuf_start, '\0');
256 		if (db_le[-1] == '\r' || db_le[-1] == '\n')
257 		    *--db_le = '\0';
258 		db_lc = db_le;
259 		goto redraw;
260 
261 	    case -1:
262 		/*
263 		 * eek! the console returned eof.
264 		 * probably that means we HAVE no console.. we should try bail
265 		 * XXX
266 		 */
267 		c = '\r';
268 	    case '\n':
269 		/* FALLTHROUGH */
270 	    case '\r':
271 		*db_le++ = c;
272 		return (1);
273 	    default:
274 		if (db_le == db_lbuf_end) {
275 		    cnputc('\007');
276 		}
277 		else if (c >= ' ' && c <= '~') {
278 		    char *p;
279 
280 		    for (p = db_le; p > db_lc; p--)
281 			*p = *(p-1);
282 		    *db_lc++ = c;
283 		    db_le++;
284 		    cnputc(c);
285 		    db_putstring(db_lc, db_le - db_lc);
286 		    db_putnchars(BACKUP, db_le - db_lc);
287 		}
288 		break;
289 	}
290 	return (0);
291 }
292 
293 int
294 db_readline(lstart, lsize)
295 	char *	lstart;
296 	int	lsize;
297 {
298 
299 	if (lsize < 2)
300 		return (0);
301 	if (lsize != db_lhistlsize) {
302 		/*
303 		 * (Re)initialize input line history.  Throw away any
304 		 * existing history.
305 		 */
306 		db_lhist_nlines = sizeof(db_lhistory) / lsize;
307 		db_lhistlsize = lsize;
308 		db_lhistidx = -1;
309 	}
310 	db_lhistcur = db_lhistidx;
311 
312 	db_force_whitespace();	/* synch output position */
313 
314 	db_lbuf_start = lstart;
315 	db_lbuf_end   = lstart + lsize - 2;	/* Will append NL and NUL. */
316 	db_lc = lstart;
317 	db_le = lstart;
318 
319 	while (!db_inputchar(cngetc()))
320 	    continue;
321 
322 	db_capture_write(lstart, db_le - db_lbuf_start);
323 	db_printf("\n");	/* synch output position */
324 	*db_le = 0;
325 
326 	if (db_le - db_lbuf_start > 1) {
327 	    /* Maintain input line history for non-empty lines. */
328 	    if (++db_lhistidx == db_lhist_nlines) {
329 		/* Rotate history. */
330 		bcopy(db_lhistory + db_lhistlsize, db_lhistory,
331 		      db_lhistlsize * (db_lhist_nlines - 1));
332 		db_lhistidx--;
333 	    }
334 	    bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
335 		  db_lhistlsize);
336 	}
337 
338 	return (db_le - db_lbuf_start);
339 }
340 
341 void
342 db_check_interrupt(void)
343 {
344 	int	c;
345 
346 	c = cncheckc();
347 	switch (c) {
348 	    case -1:		/* no character */
349 		return;
350 
351 	    case CTRL('c'):
352 		db_error((char *)0);
353 		/*NOTREACHED*/
354 
355 	    case CTRL('s'):
356 		do {
357 		    c = cncheckc();
358 		    if (c == CTRL('c'))
359 			db_error((char *)0);
360 		} while (c != CTRL('q'));
361 		break;
362 
363 	    default:
364 		/* drop on floor */
365 		break;
366 	}
367 }
368