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