Home | History | Annotate | Line # | Download | only in ddb
db_input.c revision 1.24
      1 /*	$NetBSD: db_input.c,v 1.24 2010/08/30 19:23:25 tls Exp $	*/
      2 
      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 "AS IS"
     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 (at) 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  *	Author: David B. Golub, Carnegie Mellon University
     29  *	Date:	7/90
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: db_input.c,v 1.24 2010/08/30 19:23:25 tls Exp $");
     34 
     35 #ifdef _KERNEL_OPT
     36 #include "opt_ddbparam.h"
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/proc.h>
     41 #include <sys/cpu.h>
     42 
     43 #include <ddb/ddb.h>
     44 
     45 #include <dev/cons.h>
     46 
     47 #ifndef DDB_HISTORY_SIZE
     48 #define DDB_HISTORY_SIZE 0
     49 #endif /* DDB_HISTORY_SIZE */
     50 
     51 /*
     52  * Character input and editing.
     53  */
     54 
     55 /*
     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.
     59  */
     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 char    *db_history_curr = db_history;	/* start of current line */
     67 static char    *db_history_last = db_history;	/* start of last line */
     68 static char    *db_history_prev = (char *) 0;	/* start of previous line */
     69 #endif
     70 
     71 
     72 #define	CTRL(c)		((c) & 0x1f)
     73 #define	isspace(c)	((c) == ' ' || (c) == '\t')
     74 #define	BLANK		' '
     75 #define	BACKUP		'\b'
     76 
     77 static int	cnmaygetc(void);
     78 static void	db_putstring(const char *, int);
     79 static void	db_putnchars(int, int);
     80 static void	db_delete(int, int);
     81 static void	db_delete_line(void);
     82 static int	db_inputchar(int);
     83 
     84 static void
     85 db_putstring(const char *s, int count)
     86 {
     87 
     88 	while (--count >= 0)
     89 		cnputc(*s++);
     90 }
     91 
     92 static void
     93 db_putnchars(int c, int count)
     94 {
     95 
     96 	while (--count >= 0)
     97 		cnputc(c);
     98 }
     99 
    100 /*
    101  * Delete N characters, forward or backward
    102  */
    103 #define	DEL_FWD		0
    104 #define	DEL_BWD		1
    105 static void
    106 db_delete(int n, int bwd)
    107 {
    108 	char *p;
    109 
    110 	if (bwd) {
    111 		db_lc -= n;
    112 		db_putnchars(BACKUP, n);
    113 	}
    114 	for (p = db_lc; p < db_le-n; p++) {
    115 		*p = *(p+n);
    116 		cnputc(*p);
    117 	}
    118 	db_putnchars(BLANK, n);
    119 	db_putnchars(BACKUP, db_le - db_lc);
    120 	db_le -= n;
    121 }
    122 
    123 static void
    124 db_delete_line(void)
    125 {
    126 
    127 	db_delete(db_le - db_lc, DEL_FWD);
    128 	db_delete(db_lc - db_lbuf_start, DEL_BWD);
    129 	db_le = db_lc = db_lbuf_start;
    130 }
    131 
    132 #if DDB_HISTORY_SIZE != 0
    133 
    134 #define INC_DB_CURR() do {						\
    135 	++db_history_curr;						\
    136 	if (db_history_curr > db_history + DDB_HISTORY_SIZE - 1)	\
    137 	    db_history_curr = db_history;				\
    138     } while (0)
    139 #define DEC_DB_CURR() do {						\
    140 	--db_history_curr;						\
    141 	if (db_history_curr < db_history)				\
    142 	    db_history_curr = db_history + DDB_HISTORY_SIZE - 1;	\
    143     } while (0)
    144 #endif
    145 
    146 static inline void db_hist_put(int c)
    147 {
    148 	KASSERT(&db_history[0]  <= db_history_last);
    149 	KASSERT(db_history_last <= &db_history[DDB_HISTORY_SIZE-1]);
    150 
    151 	*db_history_last++ = c;
    152 
    153 	if (db_history_last > &db_history[DDB_HISTORY_SIZE-1])
    154 	    db_history_last = db_history;
    155 }
    156 
    157 
    158 /* returns true at end-of-line */
    159 static int
    160 db_inputchar(int c)
    161 {
    162 	switch (c) {
    163 	case CTRL('b'):
    164 		/* back up one character */
    165 		if (db_lc > db_lbuf_start) {
    166 			cnputc(BACKUP);
    167 			db_lc--;
    168 		}
    169 		break;
    170 	case CTRL('f'):
    171 		/* forward one character */
    172 		if (db_lc < db_le) {
    173 			cnputc(*db_lc);
    174 			db_lc++;
    175 		}
    176 		break;
    177 	case CTRL('a'):
    178 		/* beginning of line */
    179 		while (db_lc > db_lbuf_start) {
    180 			cnputc(BACKUP);
    181 			db_lc--;
    182 		}
    183 		break;
    184 	case CTRL('e'):
    185 		/* end of line */
    186 		while (db_lc < db_le) {
    187 			cnputc(*db_lc);
    188 			db_lc++;
    189 		}
    190 		break;
    191 	case CTRL('h'):
    192 	case 0177:
    193 		/* erase previous character */
    194 		if (db_lc > db_lbuf_start)
    195 			db_delete(1, DEL_BWD);
    196 		break;
    197 	case CTRL('d'):
    198 		/* erase next character */
    199 		if (db_lc < db_le)
    200 			db_delete(1, DEL_FWD);
    201 		break;
    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('u'):
    208 		/* delete line */
    209 		db_delete_line();
    210 		break;
    211 	case CTRL('t'):
    212 		/* twiddle last 2 characters */
    213 		if (db_lc >= db_lbuf_start + 1) {
    214 			if (db_lc < db_le) {
    215 				c = db_lc[-1];
    216 				db_lc[-1] = db_lc[0];
    217 				db_lc[0] = c;
    218 				cnputc(BACKUP);
    219 				cnputc(db_lc[-1]);
    220 				cnputc(db_lc[0]);
    221 				db_lc++;
    222 			} else if (db_lc >= db_lbuf_start + 2) {
    223 				c = db_lc[-2];
    224 				db_lc[-2] = db_lc[-1];
    225 				db_lc[-1] = c;
    226 				cnputc(BACKUP);
    227 				cnputc(BACKUP);
    228 				cnputc(db_lc[-2]);
    229 				cnputc(db_lc[-1]);
    230 			}
    231 		}
    232 		break;
    233 #if DDB_HISTORY_SIZE != 0
    234 	case CTRL('p'):
    235 		DEC_DB_CURR();
    236 		while (db_history_curr != db_history_last) {
    237 			DEC_DB_CURR();
    238 			if (*db_history_curr == '\0')
    239 				break;
    240 		}
    241 		db_delete_line();
    242 		if (db_history_curr == db_history_last) {
    243 			INC_DB_CURR();
    244 			db_le = db_lc = db_lbuf_start;
    245 		} else {
    246 			char *p;
    247 			INC_DB_CURR();
    248 			for (p = db_history_curr, db_le = db_lbuf_start;
    249 			     *p; ) {
    250 				*db_le++ = *p++;
    251 				if (p >= db_history + DDB_HISTORY_SIZE) {
    252 					p = db_history;
    253 				}
    254 			}
    255 			db_lc = db_le;
    256 		}
    257 		db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    258 		break;
    259 	case CTRL('n'):
    260 		while (db_history_curr != db_history_last) {
    261 			if (*db_history_curr == '\0')
    262 				break;
    263 			INC_DB_CURR();
    264 		}
    265 		if (db_history_curr != db_history_last) {
    266 			INC_DB_CURR();
    267 			db_delete_line();
    268 			if (db_history_curr != db_history_last) {
    269 				char *p;
    270 				for (p = db_history_curr,
    271 				     db_le = db_lbuf_start; *p;) {
    272 					*db_le++ = *p++;
    273 					if (p >= db_history + DDB_HISTORY_SIZE) {
    274 						p = db_history;
    275 					}
    276 				}
    277 				db_lc = db_le;
    278 			}
    279 			db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    280 		}
    281 		break;
    282 #endif
    283 	case CTRL('r'):
    284 		db_putstring("^R\n", 3);
    285 		if (db_le > db_lbuf_start) {
    286 			db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    287 			db_putnchars(BACKUP, db_le - db_lc);
    288 		}
    289 		break;
    290 	case '\n':
    291 	case '\r':
    292 #if DDB_HISTORY_SIZE != 0
    293 		/* Check if it same than previous line */
    294 		if (db_history_curr == db_history_prev) {
    295 			char *pp, *pc;
    296 
    297 			/* Is it unmodified */
    298 			for (pp = db_history_prev, pc = db_lbuf_start;
    299 			     pc != db_le && *pp; pp++, pc++) {
    300 				if (*pp != *pc)
    301 					break;
    302 				if (++pp >= db_history + DDB_HISTORY_SIZE) {
    303 					pp = db_history;
    304 				}
    305 				if (++pc >= db_history + DDB_HISTORY_SIZE) {
    306 					pc = db_history;
    307 				}
    308 			}
    309 			if (!*pp && pc == db_le) {
    310 				/* Repeted previous line, not saved */
    311 				db_history_curr = db_history_last;
    312 				*db_le++ = c;
    313 				return (true);
    314 			}
    315 		}
    316 		if (db_le != db_lbuf_start) {
    317 			char *p;
    318 
    319 			db_history_prev = db_history_last;
    320 
    321 			for (p = db_lbuf_start; p != db_le; ) {
    322 				db_hist_put(*p++);
    323 			}
    324 			db_hist_put(0);
    325 		}
    326 		db_history_curr = db_history_last;
    327 #endif
    328 		*db_le++ = c;
    329 		return (1);
    330 	default:
    331 		if (db_le == db_lbuf_end) {
    332 			cnputc('\007');
    333 		}
    334 		else if (c >= ' ' && c <= '~') {
    335 			char *p;
    336 
    337 			for (p = db_le; p > db_lc; p--)
    338 				*p = *(p-1);
    339 			*db_lc++ = c;
    340 			db_le++;
    341 			cnputc(c);
    342 			db_putstring(db_lc, db_le - db_lc);
    343 			db_putnchars(BACKUP, db_le - db_lc);
    344 		}
    345 		break;
    346 	}
    347 	return (0);
    348 }
    349 
    350 int
    351 db_readline(char *lstart, int lsize)
    352 {
    353 
    354 # ifdef MULTIPROCESSOR
    355 	db_printf("db{%ld}> ", (long)cpu_number());
    356 # else
    357 	db_printf("db> ");
    358 # endif
    359 	db_force_whitespace();	/* synch output position */
    360 
    361 	db_lbuf_start = lstart;
    362 	db_lbuf_end   = lstart + lsize;
    363 	db_lc = lstart;
    364 	db_le = lstart;
    365 
    366 	while (!db_inputchar(cngetc()))
    367 		continue;
    368 
    369 	db_putchar('\n');	/* synch output position */
    370 
    371 	*db_le = 0;
    372 	return (db_le - db_lbuf_start);
    373 }
    374 
    375 void
    376 db_check_interrupt(void)
    377 {
    378 	int	c;
    379 
    380 	c = cnmaygetc();
    381 	switch (c) {
    382 	case -1:		/* no character */
    383 		return;
    384 
    385 	case CTRL('c'):
    386 		db_error((char *)0);
    387 		/*NOTREACHED*/
    388 
    389 	case CTRL('s'):
    390 		do {
    391 			c = cnmaygetc();
    392 			if (c == CTRL('c')) {
    393 				db_error((char *)0);
    394 				/*NOTREACHED*/
    395 			}
    396 		} while (c != CTRL('q'));
    397 		break;
    398 
    399 	default:
    400 		/* drop on floor */
    401 		break;
    402 	}
    403 }
    404 
    405 static int
    406 cnmaygetc(void)
    407 {
    408 
    409 	return (-1);
    410 }
    411