1 /* $NetBSD: db_input.c,v 1.22 2007/02/22 06:41:01 thorpej Exp $ */
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 "AS IS"
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.
28 * Author: David B. Golub, Carnegie Mellon University
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_input.c,v 1.22 2007/02/22 06:41:01 thorpej Exp $");
36 #include "opt_ddbparam.h"
39 #include <sys/param.h>
47 #ifndef DDB_HISTORY_SIZE
48 #define DDB_HISTORY_SIZE 0
49 #endif /* DDB_HISTORY_SIZE */
52 * Character input and editing.
56 * We don't track output position while editing input,
57 * since input always ends with a new-line. We just
58 * reset the line position at the end.
60 static char *db_lbuf_start
; /* start of input line buffer */
61 static char *db_lbuf_end
; /* end of input line buffer */
62 static char *db_lc
; /* current character */
63 static char *db_le
; /* one past last character */
64 #if DDB_HISTORY_SIZE != 0
65 static char db_history
[DDB_HISTORY_SIZE
]; /* start of history buffer */
66 static int db_history_size
= DDB_HISTORY_SIZE
;/* size of history buffer */
67 static char *db_history_curr
= db_history
; /* start of current line */
68 static char *db_history_last
= db_history
; /* start of last line */
69 static char *db_history_prev
= (char *) 0; /* start of previous line */
73 #define CTRL(c) ((c) & 0x1f)
74 #define isspace(c) ((c) == ' ' || (c) == '\t')
78 static int cnmaygetc(void);
79 static void db_putstring(const char *, int);
80 static void db_putnchars(int, int);
81 static void db_delete(int, int);
82 static void db_delete_line(void);
83 static int db_inputchar(int);
86 db_putstring(const char *s
, int count
)
94 db_putnchars(int c
, int count
)
102 * Delete N characters, forward or backward
107 db_delete(int n
, int bwd
)
113 db_putnchars(BACKUP
, n
);
115 for (p
= db_lc
; p
< db_le
-n
; p
++) {
119 db_putnchars(BLANK
, n
);
120 db_putnchars(BACKUP
, db_le
- db_lc
);
128 db_delete(db_le
- db_lc
, DEL_FWD
);
129 db_delete(db_lc
- db_lbuf_start
, DEL_BWD
);
130 db_le
= db_lc
= db_lbuf_start
;
133 #if DDB_HISTORY_SIZE != 0
134 #define INC_DB_CURR() \
137 if (db_history_curr > db_history + db_history_size - 1) \
138 db_history_curr = db_history; \
139 } while (/*CONSTCOND*/ 0)
140 #define DEC_DB_CURR() \
143 if (db_history_curr < db_history) \
144 db_history_curr = db_history + \
145 db_history_size - 1; \
146 } while (/*CONSTCOND*/ 0)
149 /* returns true at end-of-line */
155 /* back up one character */
156 if (db_lc
> db_lbuf_start
) {
162 /* forward one character */
169 /* beginning of line */
170 while (db_lc
> db_lbuf_start
) {
177 while (db_lc
< db_le
) {
184 /* erase previous character */
185 if (db_lc
> db_lbuf_start
)
186 db_delete(1, DEL_BWD
);
189 /* erase next character */
191 db_delete(1, DEL_FWD
);
194 /* delete to end of line */
196 db_delete(db_le
- db_lc
, DEL_FWD
);
203 /* twiddle last 2 characters */
204 if (db_lc
>= db_lbuf_start
+ 1) {
207 db_lc
[-1] = db_lc
[0];
213 } else if (db_lc
>= db_lbuf_start
+ 2) {
215 db_lc
[-2] = db_lc
[-1];
224 #if DDB_HISTORY_SIZE != 0
227 while (db_history_curr
!= db_history_last
) {
229 if (*db_history_curr
== '\0')
233 if (db_history_curr
== db_history_last
) {
235 db_le
= db_lc
= db_lbuf_start
;
239 for (p
= db_history_curr
, db_le
= db_lbuf_start
;
242 if (p
== db_history
+ db_history_size
) {
248 db_putstring(db_lbuf_start
, db_le
- db_lbuf_start
);
251 while (db_history_curr
!= db_history_last
) {
252 if (*db_history_curr
== '\0')
256 if (db_history_curr
!= db_history_last
) {
259 if (db_history_curr
!= db_history_last
) {
261 for (p
= db_history_curr
,
262 db_le
= db_lbuf_start
; *p
;) {
264 if (p
== db_history
+
271 db_putstring(db_lbuf_start
, db_le
- db_lbuf_start
);
276 db_putstring("^R\n", 3);
277 if (db_le
> db_lbuf_start
) {
278 db_putstring(db_lbuf_start
, db_le
- db_lbuf_start
);
279 db_putnchars(BACKUP
, db_le
- db_lc
);
284 #if DDB_HISTORY_SIZE != 0
285 /* Check if it same than previous line */
286 if (db_history_curr
== db_history_prev
) {
289 /* Is it unmodified */
290 for (pp
= db_history_prev
, pc
= db_lbuf_start
;
291 pc
!= db_le
&& *pp
; pp
++, pc
++) {
294 if (++pp
== db_history
+ db_history_size
) {
297 if (++pc
== db_history
+ db_history_size
) {
301 if (!*pp
&& pc
== db_le
) {
302 /* Repeted previous line, not saved */
303 db_history_curr
= db_history_last
;
308 if (db_le
!= db_lbuf_start
) {
310 db_history_prev
= db_history_last
;
311 for (p
= db_lbuf_start
; p
!= db_le
; p
++) {
312 *db_history_last
++ = *p
;
313 if (db_history_last
== db_history
+
315 db_history_last
= db_history
;
318 *db_history_last
++ = '\0';
320 db_history_curr
= db_history_last
;
325 if (db_le
== db_lbuf_end
) {
328 else if (c
>= ' ' && c
<= '~') {
331 for (p
= db_le
; p
> db_lc
; p
--)
336 db_putstring(db_lc
, db_le
- db_lc
);
337 db_putnchars(BACKUP
, db_le
- db_lc
);
345 db_readline(char *lstart
, int lsize
)
348 # ifdef MULTIPROCESSOR
349 db_printf("db{%ld}> ", (long)cpu_number());
353 db_force_whitespace(); /* synch output position */
355 db_lbuf_start
= lstart
;
356 db_lbuf_end
= lstart
+ lsize
;
360 while (!db_inputchar(cngetc()))
363 db_putchar('\n'); /* synch output position */
366 return (db_le
- db_lbuf_start
);
370 db_check_interrupt(void)
376 case -1: /* no character */
386 if (c
== CTRL('c')) {
390 } while (c
!= CTRL('q'));