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