]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ddb/db_input.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / sys / ddb / db_input.c
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  * $FreeBSD$
27  */
28
29 /*
30  *      Author: David B. Golub, Carnegie Mellon University
31  *      Date:   7/90
32  */
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 int      cnmaygetc __P((void));
67 static void     db_delete __P((int n, int bwd));
68 static int      db_inputchar __P((int c));
69 static void     db_putnchars __P((int c, int count));
70 static void     db_putstring __P((char *s, int count));
71
72 void
73 db_putstring(s, count)
74         char    *s;
75         int     count;
76 {
77         while (--count >= 0)
78             cnputc(*s++);
79 }
80
81 void
82 db_putnchars(c, count)
83         int     c;
84         int     count;
85 {
86         while (--count >= 0)
87             cnputc(c);
88 }
89
90 /*
91  * Delete N characters, forward or backward
92  */
93 #define DEL_FWD         0
94 #define DEL_BWD         1
95 void
96 db_delete(n, bwd)
97         int     n;
98         int     bwd;
99 {
100         register char *p;
101
102         if (bwd) {
103             db_lc -= n;
104             db_putnchars(BACKUP, n);
105         }
106         for (p = db_lc; p < db_le-n; p++) {
107             *p = *(p+n);
108             cnputc(*p);
109         }
110         db_putnchars(BLANK, n);
111         db_putnchars(BACKUP, db_le - db_lc);
112         db_le -= n;
113 }
114
115 /* returns TRUE at end-of-line */
116 int
117 db_inputchar(c)
118         int     c;
119 {
120         static int escstate;
121
122         if (escstate == 1) {
123                 /* ESC seen, look for [ or O */
124                 if (c == '[' || c == 'O')
125                         escstate++;
126                 else
127                         escstate = 0; /* re-init state machine */
128                 return (0);
129         } else if (escstate == 2) {
130                 escstate = 0;
131                 /*
132                  * If a valid cursor key has been found, translate
133                  * into an emacs-style control key, and fall through.
134                  * Otherwise, drop off.
135                  */
136                 switch (c) {
137                 case 'A':       /* up */
138                         c = CTRL('p');
139                         break;
140                 case 'B':       /* down */
141                         c = CTRL('n');
142                         break;
143                 case 'C':       /* right */
144                         c = CTRL('f');
145                         break;
146                 case 'D':       /* left */
147                         c = CTRL('b');
148                         break;
149                 default:
150                         return (0);
151                 }
152         }
153
154         switch (c) {
155             case CTRL('['):
156                 escstate = 1;
157                 break;
158             case CTRL('b'):
159                 /* back up one character */
160                 if (db_lc > db_lbuf_start) {
161                     cnputc(BACKUP);
162                     db_lc--;
163                 }
164                 break;
165             case CTRL('f'):
166                 /* forward one character */
167                 if (db_lc < db_le) {
168                     cnputc(*db_lc);
169                     db_lc++;
170                 }
171                 break;
172             case CTRL('a'):
173                 /* beginning of line */
174                 while (db_lc > db_lbuf_start) {
175                     cnputc(BACKUP);
176                     db_lc--;
177                 }
178                 break;
179             case CTRL('e'):
180                 /* end of line */
181                 while (db_lc < db_le) {
182                     cnputc(*db_lc);
183                     db_lc++;
184                 }
185                 break;
186             case CTRL('h'):
187             case 0177:
188                 /* erase previous character */
189                 if (db_lc > db_lbuf_start)
190                     db_delete(1, DEL_BWD);
191                 break;
192             case CTRL('d'):
193                 /* erase next character */
194                 if (db_lc < db_le)
195                     db_delete(1, DEL_FWD);
196                 break;
197             case CTRL('k'):
198                 /* delete to end of line */
199                 if (db_lc < db_le)
200                     db_delete(db_le - db_lc, DEL_FWD);
201                 break;
202             case CTRL('t'):
203                 /* twiddle last 2 characters */
204                 if (db_lc >= db_lbuf_start + 2) {
205                     c = db_lc[-2];
206                     db_lc[-2] = db_lc[-1];
207                     db_lc[-1] = c;
208                     cnputc(BACKUP);
209                     cnputc(BACKUP);
210                     cnputc(db_lc[-2]);
211                     cnputc(db_lc[-1]);
212                 }
213                 break;
214             case CTRL('r'):
215                 db_putstring("^R\n", 3);
216             redraw:
217                 if (db_le > db_lbuf_start) {
218                     db_putstring(db_lbuf_start, db_le - db_lbuf_start);
219                     db_putnchars(BACKUP, db_le - db_lc);
220                 }
221                 break;
222             case CTRL('p'):
223                 /* Make previous history line the active one. */
224                 if (db_lhistcur >= 0) {
225                     bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
226                           db_lbuf_start, db_lhistlsize);
227                     db_lhistcur--;
228                     goto hist_redraw;
229                 }
230                 break;
231             case CTRL('n'):
232                 /* Make next history line the active one. */
233                 if (db_lhistcur < db_lhistidx - 1) {
234                     db_lhistcur += 2;
235                     bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
236                           db_lbuf_start, db_lhistlsize);
237                 } else {
238                     /*
239                      * ^N through tail of history, reset the
240                      * buffer to zero length.
241                      */
242                     *db_lbuf_start = '\0';
243                     db_lhistcur = db_lhistidx;
244                 }
245
246             hist_redraw:
247                 db_putnchars(BACKUP, db_le - db_lbuf_start);
248                 db_putnchars(BLANK, db_le - db_lbuf_start);
249                 db_putnchars(BACKUP, db_le - db_lbuf_start);
250                 db_le = index(db_lbuf_start, '\0');
251                 if (db_le[-1] == '\r' || db_le[-1] == '\n')
252                     *--db_le = '\0';
253                 db_lc = db_le;
254                 goto redraw;
255
256             case -1:
257                 /*
258                  * eek! the console returned eof.
259                  * probably that means we HAVE no console.. we should try bail
260                  * XXX
261                  */
262                 c = '\r';
263             case '\n':
264             case '\r':
265                 *db_le++ = c;
266                 return (1);
267             default:
268                 if (db_le == db_lbuf_end) {
269                     cnputc('\007');
270                 }
271                 else if (c >= ' ' && c <= '~') {
272                     register char *p;
273
274                     for (p = db_le; p > db_lc; p--)
275                         *p = *(p-1);
276                     *db_lc++ = c;
277                     db_le++;
278                     cnputc(c);
279                     db_putstring(db_lc, db_le - db_lc);
280                     db_putnchars(BACKUP, db_le - db_lc);
281                 }
282                 break;
283         }
284         return (0);
285 }
286
287 int
288 cnmaygetc()
289 {
290         return (-1);
291 }
292
293 int
294 db_readline(lstart, lsize)
295         char *  lstart;
296         int     lsize;
297 {
298         if (lsize != db_lhistlsize) {
299                 /*
300                  * (Re)initialize input line history.  Throw away any
301                  * existing history.
302                  */
303                 db_lhist_nlines = sizeof(db_lhistory) / lsize;
304                 db_lhistlsize = lsize;
305                 db_lhistidx = -1;
306         }
307         db_lhistcur = db_lhistidx;
308
309         db_force_whitespace();  /* synch output position */
310
311         db_lbuf_start = lstart;
312         db_lbuf_end   = lstart + lsize;
313         db_lc = lstart;
314         db_le = lstart;
315
316         while (!db_inputchar(cngetc()))
317             continue;
318
319         db_printf("\n");        /* synch output position */
320         *db_le = 0;
321
322         if (db_le - db_lbuf_start > 1) {
323             /* Maintain input line history for non-empty lines. */
324             if (++db_lhistidx == db_lhist_nlines) {
325                 /* Rotate history. */
326                 ovbcopy(db_lhistory + db_lhistlsize, db_lhistory,
327                         db_lhistlsize * (db_lhist_nlines - 1));
328                 db_lhistidx--;
329             }
330             bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
331                   db_lhistlsize);
332         }
333
334         return (db_le - db_lbuf_start);
335 }
336
337 void
338 db_check_interrupt()
339 {
340         register int    c;
341
342         c = cnmaygetc();
343         switch (c) {
344             case -1:            /* no character */
345                 return;
346
347             case CTRL('c'):
348                 db_error((char *)0);
349                 /*NOTREACHED*/
350
351             case CTRL('s'):
352                 do {
353                     c = cnmaygetc();
354                     if (c == CTRL('c'))
355                         db_error((char *)0);
356                 } while (c != CTRL('q'));
357                 break;
358
359             default:
360                 /* drop on floor */
361                 break;
362         }
363 }