bsd.lib.mk: Add INTERNALLIBPROF knob.
[dragonfly.git] / sys / ddb / db_input.c
blob1dfb83474d575aa09d37425f9f85760620730ff6
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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.
26 * $FreeBSD: src/sys/ddb/db_input.c,v 1.28.2.1 2002/03/08 16:37:10 yar Exp $
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/cons.h>
38 #include <ddb/ddb.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)
63 #define BLANK ' '
64 #define BACKUP '\b'
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);
72 static void
73 db_putstring(char *s, int count)
75 while (--count >= 0)
76 cnputc(*s++);
79 static void
80 db_putnchars(int c, int count)
82 while (--count >= 0)
83 cnputc(c);
87 * Delete N characters, forward or backward
89 #define DEL_FWD 0
90 #define DEL_BWD 1
91 static void
92 db_delete(int n, int bwd)
94 char *p;
96 if (bwd) {
97 db_lc -= n;
98 db_putnchars(BACKUP, n);
100 for (p = db_lc; p < db_le-n; p++) {
101 *p = *(p+n);
102 cnputc(*p);
104 db_putnchars(BLANK, n);
105 db_putnchars(BACKUP, db_le - db_lc);
106 db_le -= n;
109 /* returns TRUE at end-of-line */
110 static int
111 db_inputchar(int c)
113 static int escstate;
115 if (escstate == 1) {
116 /* ESC seen, look for [ or O */
117 if (c == '[' || c == 'O')
118 escstate++;
119 else
120 escstate = 0; /* re-init state machine */
121 return (0);
122 } else if (escstate == 2) {
123 escstate = 0;
125 * If a valid cursor key has been found, translate
126 * into an emacs-style control key, and fall through.
127 * Otherwise, drop off.
129 switch (c) {
130 case 'A': /* up */
131 c = CTRL('p');
132 break;
133 case 'B': /* down */
134 c = CTRL('n');
135 break;
136 case 'C': /* right */
137 c = CTRL('f');
138 break;
139 case 'D': /* left */
140 c = CTRL('b');
141 break;
142 default:
143 return (0);
147 switch (c) {
148 case CTRL('['):
149 escstate = 1;
150 break;
151 case CTRL('b'):
152 /* back up one character */
153 if (db_lc > db_lbuf_start) {
154 cnputc(BACKUP);
155 db_lc--;
157 break;
158 case CTRL('f'):
159 /* forward one character */
160 if (db_lc < db_le) {
161 cnputc(*db_lc);
162 db_lc++;
164 break;
165 case CTRL('a'):
166 /* beginning of line */
167 while (db_lc > db_lbuf_start) {
168 cnputc(BACKUP);
169 db_lc--;
171 break;
172 case CTRL('e'):
173 /* end of line */
174 while (db_lc < db_le) {
175 cnputc(*db_lc);
176 db_lc++;
178 break;
179 case CTRL('h'):
180 case 0177:
181 /* erase previous character */
182 if (db_lc > db_lbuf_start)
183 db_delete(1, DEL_BWD);
184 break;
185 case CTRL('d'):
186 /* erase next character */
187 if (db_lc < db_le)
188 db_delete(1, DEL_FWD);
189 break;
190 case CTRL('u'):
191 /* kill entire line: */
192 /* at first, delete to beginning of line */
193 if (db_lc > db_lbuf_start)
194 db_delete(db_lc - db_lbuf_start, DEL_BWD);
195 /* FALLTHROUGH */
196 case CTRL('k'):
197 /* delete to end of line */
198 if (db_lc < db_le)
199 db_delete(db_le - db_lc, DEL_FWD);
200 break;
201 case CTRL('t'):
202 /* twiddle last 2 characters */
203 if (db_lc >= db_lbuf_start + 2) {
204 c = db_lc[-2];
205 db_lc[-2] = db_lc[-1];
206 db_lc[-1] = c;
207 cnputc(BACKUP);
208 cnputc(BACKUP);
209 cnputc(db_lc[-2]);
210 cnputc(db_lc[-1]);
212 break;
213 case CTRL('r'):
214 db_putstring("^R\n", 3);
215 redraw:
216 if (db_le > db_lbuf_start) {
217 db_putstring(db_lbuf_start, db_le - db_lbuf_start);
218 db_putnchars(BACKUP, db_le - db_lc);
220 break;
221 case CTRL('p'):
222 /* Make previous history line the active one. */
223 if (db_lhistcur >= 0) {
224 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
225 db_lbuf_start, db_lhistlsize);
226 db_lhistcur--;
227 goto hist_redraw;
229 break;
230 case CTRL('n'):
231 /* Make next history line the active one. */
232 if (db_lhistcur < db_lhistidx - 1) {
233 db_lhistcur += 2;
234 bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
235 db_lbuf_start, db_lhistlsize);
236 } else {
238 * ^N through tail of history, reset the
239 * buffer to zero length.
241 *db_lbuf_start = '\0';
242 db_lhistcur = db_lhistidx;
245 hist_redraw:
246 db_putnchars(BACKUP, db_le - db_lbuf_start);
247 db_putnchars(BLANK, db_le - db_lbuf_start);
248 db_putnchars(BACKUP, db_le - db_lbuf_start);
249 db_le = index(db_lbuf_start, '\0');
250 if (db_le[-1] == '\r' || db_le[-1] == '\n')
251 *--db_le = '\0';
252 db_lc = db_le;
253 goto redraw;
255 case -1:
257 * eek! the console returned eof.
258 * probably that means we HAVE no console.. we should try bail
259 * XXX
261 return (0);
262 case '\n':
263 case '\r':
264 *db_le++ = c;
265 return (1);
266 default:
267 if (db_le == db_lbuf_end) {
268 cnputc('\007');
270 else if (c >= ' ' && c <= '~') {
271 char *p;
273 for (p = db_le; p > db_lc; p--)
274 *p = *(p-1);
275 *db_lc++ = c;
276 db_le++;
277 cnputc(c);
278 db_putstring(db_lc, db_le - db_lc);
279 db_putnchars(BACKUP, db_le - db_lc);
281 break;
283 return (0);
286 static int
287 cnmaygetc(void)
289 return (-1);
293 db_readline(char *lstart, int lsize)
295 if (lsize != db_lhistlsize) {
297 * (Re)initialize input line history. Throw away any
298 * existing history.
300 db_lhist_nlines = sizeof(db_lhistory) / lsize;
301 db_lhistlsize = lsize;
302 db_lhistidx = -1;
304 db_lhistcur = db_lhistidx;
306 db_force_whitespace(); /* synch output position */
308 db_lbuf_start = lstart;
309 db_lbuf_end = lstart + lsize;
310 db_lc = lstart;
311 db_le = lstart;
313 while (!db_inputchar(cngetc()))
314 continue;
316 db_printf("\n"); /* synch output position */
317 *db_le = 0;
319 if (db_le - db_lbuf_start > 1) {
320 /* Maintain input line history for non-empty lines. */
321 if (++db_lhistidx == db_lhist_nlines) {
322 /* Rotate history. */
323 bcopy(db_lhistory + db_lhistlsize, db_lhistory,
324 db_lhistlsize * (db_lhist_nlines - 1));
325 db_lhistidx--;
327 bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
328 db_lhistlsize);
331 return (db_le - db_lbuf_start);
334 void
335 db_check_interrupt(void)
337 int c;
339 c = cnmaygetc();
340 switch (c) {
341 case -1: /* no character */
342 return;
344 case CTRL('c'):
345 db_error(NULL);
346 /*NOTREACHED*/
348 case CTRL('s'):
349 do {
350 c = cnmaygetc();
351 if (c == CTRL('c'))
352 db_error(NULL);
353 } while (c != CTRL('q'));
354 break;
356 default:
357 /* drop on floor */
358 break;