]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/ddb/db_input.c
zfs: merge openzfs/zfs@eb62221ff (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / sys / ddb / db_input.c
1 /*-
2  * SPDX-License-Identifier: MIT-CMU
3  *
4  * Mach Operating System
5  * Copyright (c) 1991,1990 Carnegie Mellon University
6  * All Rights Reserved.
7  *
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.
13  *
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.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie the
26  * rights to redistribute these changes.
27  */
28 /*
29  *      Author: David B. Golub, Carnegie Mellon University
30  *      Date:   7/90
31  */
32
33 #include <sys/cdefs.h>
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(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);
71
72 static void
73 db_putstring(char *s, int count)
74 {
75         while (--count >= 0)
76             cnputc(*s++);
77 }
78
79 static void
80 db_putnchars(int c, int count)
81 {
82         while (--count >= 0)
83             cnputc(c);
84 }
85
86 /*
87  * Delete N characters, forward or backward
88  */
89 #define DEL_FWD         0
90 #define DEL_BWD         1
91 static void
92 db_delete(int n, int bwd)
93 {
94         char *p;
95
96         if (bwd) {
97             db_lc -= n;
98             db_putnchars(BACKUP, n);
99         }
100         for (p = db_lc; p < db_le-n; p++) {
101             *p = *(p+n);
102             cnputc(*p);
103         }
104         db_putnchars(BLANK, n);
105         db_putnchars(BACKUP, db_le - db_lc);
106         db_le -= n;
107 }
108
109 /* returns true at end-of-line */
110 static int
111 db_inputchar(int c)
112 {
113         static int escstate;
114
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;
124                 /*
125                  * If a valid cursor key has been found, translate
126                  * into an emacs-style control key, and fall through.
127                  * Otherwise, drop off.
128                  */
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);
144                 }
145         }
146
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--;
156                 }
157                 break;
158             case CTRL('f'):
159                 /* forward one character */
160                 if (db_lc < db_le) {
161                     cnputc(*db_lc);
162                     db_lc++;
163                 }
164                 break;
165             case CTRL('a'):
166                 /* beginning of line */
167                 while (db_lc > db_lbuf_start) {
168                     cnputc(BACKUP);
169                     db_lc--;
170                 }
171                 break;
172             case CTRL('e'):
173                 /* end of line */
174                 while (db_lc < db_le) {
175                     cnputc(*db_lc);
176                     db_lc++;
177                 }
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             case CTRL('c'):
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);
196                 /* FALLTHROUGH */
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('w'):
215                 /* erase previous word */
216                 for (; db_lc > db_lbuf_start;) {
217                     if (*(db_lc - 1) != ' ')
218                         break;
219                     db_delete(1, DEL_BWD);
220                 }
221                 for (; db_lc > db_lbuf_start;) {
222                     if (*(db_lc - 1) == ' ')
223                         break;
224                     db_delete(1, DEL_BWD);
225                 }
226                 break;
227             case CTRL('r'):
228                 db_putstring("^R\n", 3);
229             redraw:
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);
233                 }
234                 break;
235             case CTRL('p'):
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);
240                     db_lhistcur--;
241                     goto hist_redraw;
242                 }
243                 break;
244             case CTRL('n'):
245                 /* Make next history line the active one. */
246                 if (db_lhistcur < db_lhistidx - 1) {
247                     db_lhistcur += 2;
248                     bcopy(db_lhistory + db_lhistcur * db_lhistlsize,
249                           db_lbuf_start, db_lhistlsize);
250                 } else {
251                     /*
252                      * ^N through tail of history, reset the
253                      * buffer to zero length.
254                      */
255                     *db_lbuf_start = '\0';
256                     db_lhistcur = db_lhistidx;
257                 }
258
259             hist_redraw:
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')
265                     *--db_le = '\0';
266                 db_lc = db_le;
267                 goto redraw;
268
269             case -1:
270                 /*
271                  * eek! the console returned eof.
272                  * probably that means we HAVE no console.. we should try bail
273                  * XXX
274                  */
275                 c = '\r';
276             case '\n':
277                 /* FALLTHROUGH */
278             case '\r':
279                 *db_le++ = c;
280                 return (1);
281             default:
282                 if (db_le == db_lbuf_end) {
283                     cnputc('\007');
284                 }
285                 else if (c >= ' ' && c <= '~') {
286                     char *p;
287
288                     for (p = db_le; p > db_lc; p--)
289                         *p = *(p-1);
290                     *db_lc++ = c;
291                     db_le++;
292                     cnputc(c);
293                     db_putstring(db_lc, db_le - db_lc);
294                     db_putnchars(BACKUP, db_le - db_lc);
295                 }
296                 break;
297         }
298         return (0);
299 }
300
301 static int
302 cnmaygetc()
303 {
304         return (-1);
305 }
306
307 int
308 db_readline(char *lstart, int lsize)
309 {
310
311         if (lsize < 2)
312                 return (0);
313         if (lsize != db_lhistlsize) {
314                 /*
315                  * (Re)initialize input line history.  Throw away any
316                  * existing history.
317                  */
318                 db_lhist_nlines = sizeof(db_lhistory) / lsize;
319                 db_lhistlsize = lsize;
320                 db_lhistidx = -1;
321         }
322         db_lhistcur = db_lhistidx;
323
324         db_force_whitespace();  /* synch output position */
325
326         db_lbuf_start = lstart;
327         db_lbuf_end   = lstart + lsize - 2;     /* Will append NL and NUL. */
328         db_lc = lstart;
329         db_le = lstart;
330
331         while (!db_inputchar(cngetc()))
332             continue;
333
334         db_capture_write(lstart, db_le - db_lbuf_start);
335         db_printf("\n");        /* synch output position */
336         *db_le = 0;
337
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));
344                 db_lhistidx--;
345             }
346             bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize,
347                   db_lhistlsize);
348         }
349
350         return (db_le - db_lbuf_start);
351 }
352
353 void
354 db_check_interrupt(void)
355 {
356         int     c;
357
358         c = cnmaygetc();
359         switch (c) {
360             case -1:            /* no character */
361                 return;
362
363             case CTRL('c'):
364                 db_error((char *)0);
365                 /*NOTREACHED*/
366
367             case CTRL('s'):
368                 do {
369                     c = cnmaygetc();
370                     if (c == CTRL('c'))
371                         db_error((char *)0);
372                 } while (c != CTRL('q'));
373                 break;
374
375             default:
376                 /* drop on floor */
377                 break;
378         }
379 }