Home | History | Annotate | Line # | Download | only in ddb
db_input.c revision 1.14.2.1
      1  1.14.2.1   nathanw /*	$NetBSD: db_input.c,v 1.14.2.1 2001/11/14 19:13:38 nathanw Exp $	*/
      2       1.4       cgd 
      3       1.1       cgd /*
      4       1.1       cgd  * Mach Operating System
      5       1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6       1.1       cgd  * All Rights Reserved.
      7       1.1       cgd  *
      8       1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9       1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10       1.1       cgd  * notice and this permission notice appear in all copies of the
     11       1.1       cgd  * software, derivative works or modified versions, and any portions
     12       1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13       1.1       cgd  *
     14      1.12        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15       1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16       1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17       1.1       cgd  *
     18       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19       1.1       cgd  *
     20       1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21       1.1       cgd  *  School of Computer Science
     22       1.1       cgd  *  Carnegie Mellon University
     23       1.1       cgd  *  Pittsburgh PA 15213-3890
     24       1.1       cgd  *
     25       1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26       1.1       cgd  * rights to redistribute these changes.
     27       1.1       cgd  *
     28       1.1       cgd  *	Author: David B. Golub, Carnegie Mellon University
     29       1.1       cgd  *	Date:	7/90
     30       1.1       cgd  */
     31  1.14.2.1   nathanw 
     32  1.14.2.1   nathanw #include <sys/cdefs.h>
     33  1.14.2.1   nathanw __KERNEL_RCSID(0, "$NetBSD: db_input.c,v 1.14.2.1 2001/11/14 19:13:38 nathanw Exp $");
     34      1.10  jonathan 
     35      1.10  jonathan #include "opt_ddbparam.h"
     36       1.1       cgd 
     37       1.3   mycroft #include <sys/param.h>
     38       1.3   mycroft #include <sys/proc.h>
     39       1.3   mycroft 
     40       1.7  christos #include <machine/db_machdep.h>
     41       1.7  christos 
     42       1.1       cgd #include <ddb/db_output.h>
     43       1.7  christos #include <ddb/db_command.h>
     44       1.7  christos #include <ddb/db_sym.h>
     45       1.7  christos #include <ddb/db_extern.h>
     46       1.7  christos 
     47       1.7  christos #include <dev/cons.h>
     48       1.1       cgd 
     49       1.9     lukem #ifndef DDB_HISTORY_SIZE
     50       1.9     lukem #define DDB_HISTORY_SIZE 0
     51       1.9     lukem #endif /* DDB_HISTORY_SIZE */
     52       1.8  christos 
     53       1.1       cgd /*
     54       1.1       cgd  * Character input and editing.
     55       1.1       cgd  */
     56       1.1       cgd 
     57       1.1       cgd /*
     58       1.1       cgd  * We don't track output position while editing input,
     59       1.1       cgd  * since input always ends with a new-line.  We just
     60       1.1       cgd  * reset the line position at the end.
     61       1.1       cgd  */
     62       1.1       cgd char *	db_lbuf_start;	/* start of input line buffer */
     63       1.1       cgd char *	db_lbuf_end;	/* end of input line buffer */
     64       1.1       cgd char *	db_lc;		/* current character */
     65       1.1       cgd char *	db_le;		/* one past last character */
     66       1.9     lukem #if DDB_HISTORY_SIZE != 0
     67       1.9     lukem char    db_history[DDB_HISTORY_SIZE];	/* start of history buffer */
     68       1.9     lukem int     db_history_size = DDB_HISTORY_SIZE;/* size of history buffer */
     69       1.8  christos char *  db_history_curr = db_history;	/* start of current line */
     70       1.8  christos char *  db_history_last = db_history;	/* start of last line */
     71       1.8  christos char *  db_history_prev = (char *) 0;	/* start of previous line */
     72       1.8  christos #endif
     73       1.8  christos 
     74       1.1       cgd 
     75       1.1       cgd #define	CTRL(c)		((c) & 0x1f)
     76       1.1       cgd #define	isspace(c)	((c) == ' ' || (c) == '\t')
     77       1.1       cgd #define	BLANK		' '
     78       1.1       cgd #define	BACKUP		'\b'
     79       1.1       cgd 
     80       1.7  christos static int cnmaygetc __P((void));
     81       1.7  christos 
     82       1.1       cgd void
     83       1.1       cgd db_putstring(s, count)
     84       1.1       cgd 	char	*s;
     85       1.1       cgd 	int	count;
     86       1.1       cgd {
     87       1.1       cgd 	while (--count >= 0)
     88       1.1       cgd 	    cnputc(*s++);
     89       1.1       cgd }
     90       1.1       cgd 
     91       1.1       cgd void
     92       1.1       cgd db_putnchars(c, count)
     93       1.1       cgd 	int	c;
     94       1.1       cgd 	int	count;
     95       1.1       cgd {
     96       1.1       cgd 	while (--count >= 0)
     97       1.1       cgd 	    cnputc(c);
     98       1.1       cgd }
     99       1.1       cgd 
    100       1.1       cgd /*
    101       1.1       cgd  * Delete N characters, forward or backward
    102       1.1       cgd  */
    103       1.1       cgd #define	DEL_FWD		0
    104       1.1       cgd #define	DEL_BWD		1
    105       1.1       cgd void
    106       1.1       cgd db_delete(n, bwd)
    107       1.1       cgd 	int	n;
    108       1.1       cgd 	int	bwd;
    109       1.1       cgd {
    110      1.13  augustss 	char *p;
    111       1.1       cgd 
    112       1.1       cgd 	if (bwd) {
    113       1.1       cgd 	    db_lc -= n;
    114       1.1       cgd 	    db_putnchars(BACKUP, n);
    115       1.1       cgd 	}
    116       1.1       cgd 	for (p = db_lc; p < db_le-n; p++) {
    117       1.1       cgd 	    *p = *(p+n);
    118       1.1       cgd 	    cnputc(*p);
    119       1.1       cgd 	}
    120       1.1       cgd 	db_putnchars(BLANK, n);
    121       1.1       cgd 	db_putnchars(BACKUP, db_le - db_lc);
    122       1.1       cgd 	db_le -= n;
    123       1.1       cgd }
    124       1.1       cgd 
    125       1.8  christos void
    126       1.8  christos db_delete_line()
    127       1.8  christos {
    128       1.8  christos 	db_delete(db_le - db_lc, DEL_FWD);
    129       1.8  christos 	db_delete(db_lc - db_lbuf_start, DEL_BWD);
    130       1.8  christos 	db_le = db_lc = db_lbuf_start;
    131       1.8  christos }
    132       1.8  christos 
    133       1.9     lukem #if DDB_HISTORY_SIZE != 0
    134       1.8  christos #define INC_DB_CURR() \
    135       1.8  christos     do { \
    136       1.8  christos 	     db_history_curr++; \
    137       1.8  christos 	     if (db_history_curr > \
    138       1.8  christos 		 db_history + db_history_size - 1) \
    139       1.8  christos 		     db_history_curr = db_history; \
    140       1.8  christos        } while (0)
    141       1.8  christos #define DEC_DB_CURR() \
    142       1.8  christos     do { \
    143       1.8  christos 	     db_history_curr--; \
    144       1.8  christos 	     if (db_history_curr < db_history) \
    145       1.8  christos 		 db_history_curr = db_history + \
    146       1.8  christos 		 db_history_size - 1; \
    147       1.8  christos        } while (0)
    148       1.8  christos #endif
    149       1.8  christos 
    150       1.1       cgd /* returns TRUE at end-of-line */
    151       1.1       cgd int
    152       1.1       cgd db_inputchar(c)
    153       1.1       cgd 	int	c;
    154       1.1       cgd {
    155       1.1       cgd 	switch (c) {
    156       1.1       cgd 	    case CTRL('b'):
    157       1.1       cgd 		/* back up one character */
    158       1.1       cgd 		if (db_lc > db_lbuf_start) {
    159       1.1       cgd 		    cnputc(BACKUP);
    160       1.1       cgd 		    db_lc--;
    161       1.1       cgd 		}
    162       1.1       cgd 		break;
    163       1.1       cgd 	    case CTRL('f'):
    164       1.1       cgd 		/* forward one character */
    165       1.1       cgd 		if (db_lc < db_le) {
    166       1.1       cgd 		    cnputc(*db_lc);
    167       1.1       cgd 		    db_lc++;
    168       1.1       cgd 		}
    169       1.1       cgd 		break;
    170       1.1       cgd 	    case CTRL('a'):
    171       1.1       cgd 		/* beginning of line */
    172       1.1       cgd 		while (db_lc > db_lbuf_start) {
    173       1.1       cgd 		    cnputc(BACKUP);
    174       1.1       cgd 		    db_lc--;
    175       1.1       cgd 		}
    176       1.1       cgd 		break;
    177       1.1       cgd 	    case CTRL('e'):
    178       1.1       cgd 		/* end of line */
    179       1.1       cgd 		while (db_lc < db_le) {
    180       1.1       cgd 		    cnputc(*db_lc);
    181       1.1       cgd 		    db_lc++;
    182       1.1       cgd 		}
    183       1.1       cgd 		break;
    184       1.1       cgd 	    case CTRL('h'):
    185       1.1       cgd 	    case 0177:
    186       1.1       cgd 		/* erase previous character */
    187       1.1       cgd 		if (db_lc > db_lbuf_start)
    188       1.1       cgd 		    db_delete(1, DEL_BWD);
    189       1.1       cgd 		break;
    190       1.1       cgd 	    case CTRL('d'):
    191       1.1       cgd 		/* erase next character */
    192       1.1       cgd 		if (db_lc < db_le)
    193       1.1       cgd 		    db_delete(1, DEL_FWD);
    194       1.1       cgd 		break;
    195       1.1       cgd 	    case CTRL('k'):
    196       1.1       cgd 		/* delete to end of line */
    197       1.1       cgd 		if (db_lc < db_le)
    198       1.1       cgd 		    db_delete(db_le - db_lc, DEL_FWD);
    199       1.1       cgd 		break;
    200       1.8  christos 	    case CTRL('u'):
    201       1.8  christos 		/* delete line */
    202       1.8  christos 	        db_delete_line();
    203       1.8  christos 		break;
    204       1.1       cgd 	    case CTRL('t'):
    205       1.1       cgd 		/* twiddle last 2 characters */
    206      1.14     jhawk 		if (db_lc >= db_lbuf_start + 1) {
    207      1.14     jhawk 		    if (db_lc < db_le) {
    208      1.14     jhawk 			    c = db_lc[-1];
    209      1.14     jhawk 			    db_lc[-1] = db_lc[0];
    210      1.14     jhawk 			    db_lc[0] = c;
    211      1.14     jhawk 			    cnputc(BACKUP);
    212      1.14     jhawk 			    cnputc(db_lc[-1]);
    213      1.14     jhawk 			    cnputc(db_lc[0]);
    214      1.14     jhawk 			    db_lc++;
    215      1.14     jhawk 		    } else if (db_lc >= db_lbuf_start + 2) {
    216      1.14     jhawk 			    c = db_lc[-2];
    217      1.14     jhawk 			    db_lc[-2] = db_lc[-1];
    218      1.14     jhawk 			    db_lc[-1] = c;
    219      1.14     jhawk 			    cnputc(BACKUP);
    220      1.14     jhawk 			    cnputc(BACKUP);
    221      1.14     jhawk 			    cnputc(db_lc[-2]);
    222      1.14     jhawk 			    cnputc(db_lc[-1]);
    223      1.14     jhawk 		    }
    224       1.1       cgd 		}
    225       1.1       cgd 		break;
    226       1.9     lukem #if DDB_HISTORY_SIZE != 0
    227       1.8  christos 	    case CTRL('p'):
    228       1.8  christos 	        DEC_DB_CURR();
    229       1.8  christos 	        while (db_history_curr != db_history_last) {
    230       1.8  christos 			DEC_DB_CURR();
    231       1.8  christos 			if (*db_history_curr == '\0')
    232       1.8  christos 			    break;
    233       1.8  christos 		}
    234       1.8  christos 		db_delete_line();
    235       1.8  christos 		if (db_history_curr == db_history_last) {
    236       1.8  christos 			INC_DB_CURR();
    237       1.8  christos 			db_le = db_lc = db_lbuf_start;
    238       1.8  christos 		} else {
    239      1.13  augustss 			char *p;
    240       1.8  christos 			INC_DB_CURR();
    241       1.8  christos 			for (p = db_history_curr, db_le = db_lbuf_start;
    242       1.8  christos 			     *p; ) {
    243       1.8  christos 				*db_le++ = *p++;
    244       1.8  christos 				if (p == db_history + db_history_size) {
    245       1.8  christos 					p = db_history;
    246       1.8  christos 				}
    247       1.8  christos 			}
    248       1.8  christos 			db_lc = db_le;
    249       1.8  christos 		}
    250       1.8  christos 		db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    251       1.8  christos 		break;
    252       1.8  christos 	    case CTRL('n'):
    253       1.8  christos 	        while (db_history_curr != db_history_last) {
    254       1.8  christos 			if (*db_history_curr == '\0')
    255       1.8  christos 			    break;
    256       1.8  christos 			INC_DB_CURR();
    257       1.8  christos 		}
    258       1.8  christos 		if (db_history_curr != db_history_last) {
    259       1.8  christos 			INC_DB_CURR();
    260       1.8  christos 			db_delete_line();
    261       1.8  christos 			if (db_history_curr != db_history_last) {
    262      1.13  augustss 				char *p;
    263       1.8  christos 				for (p = db_history_curr,
    264       1.8  christos 				     db_le = db_lbuf_start; *p;) {
    265       1.8  christos 					*db_le++ = *p++;
    266       1.8  christos 					if (p == db_history +
    267       1.8  christos 					    db_history_size) {
    268       1.8  christos 						p = db_history;
    269       1.8  christos 					}
    270       1.8  christos 				}
    271       1.8  christos 				db_lc = db_le;
    272       1.8  christos 			}
    273       1.8  christos 			db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    274       1.8  christos 		}
    275       1.8  christos 		break;
    276       1.8  christos #endif
    277       1.1       cgd 	    case CTRL('r'):
    278       1.1       cgd 		db_putstring("^R\n", 3);
    279       1.1       cgd 		if (db_le > db_lbuf_start) {
    280       1.1       cgd 		    db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    281       1.1       cgd 		    db_putnchars(BACKUP, db_le - db_lc);
    282       1.1       cgd 		}
    283       1.1       cgd 		break;
    284       1.1       cgd 	    case '\n':
    285       1.1       cgd 	    case '\r':
    286       1.9     lukem #if DDB_HISTORY_SIZE != 0
    287       1.8  christos 		/* Check if it same than previous line */
    288       1.8  christos 		if (db_history_curr == db_history_prev) {
    289      1.13  augustss 			char *pp, *pc;
    290       1.8  christos 
    291       1.8  christos 			/* Is it unmodified */
    292       1.8  christos 			for (pp = db_history_prev, pc = db_lbuf_start;
    293       1.8  christos 			     pc != db_le && *pp; pp++, pc++) {
    294       1.8  christos 				if (*pp != *pc)
    295       1.8  christos 				    break;
    296       1.8  christos 				if (++pp == db_history + db_history_size) {
    297       1.8  christos 					pp = db_history;
    298       1.8  christos 				}
    299       1.8  christos 				if (++pc == db_history + db_history_size) {
    300       1.8  christos 					pc = db_history;
    301       1.8  christos 				}
    302       1.8  christos 			}
    303       1.8  christos 			if (!*pp && pc == db_le) {
    304       1.8  christos 				/* Repeted previous line, not saved */
    305       1.8  christos 				db_history_curr = db_history_last;
    306       1.8  christos 				*db_le++ = c;
    307       1.8  christos 				return (TRUE);
    308       1.8  christos 			}
    309       1.8  christos 		}
    310       1.8  christos 		if (db_le != db_lbuf_start) {
    311      1.13  augustss 			char *p;
    312       1.8  christos 			db_history_prev = db_history_last;
    313       1.8  christos 			for (p = db_lbuf_start; p != db_le; p++) {
    314       1.8  christos 				*db_history_last++ = *p;
    315       1.8  christos 				if (db_history_last == db_history +
    316       1.8  christos 				    db_history_size) {
    317       1.8  christos 					db_history_last = db_history;
    318       1.8  christos 				}
    319       1.8  christos 			}
    320       1.8  christos 			*db_history_last++ = '\0';
    321       1.8  christos 		}
    322       1.8  christos 		db_history_curr = db_history_last;
    323       1.8  christos #endif
    324       1.1       cgd 		*db_le++ = c;
    325       1.1       cgd 		return (1);
    326       1.1       cgd 	    default:
    327       1.1       cgd 		if (db_le == db_lbuf_end) {
    328       1.1       cgd 		    cnputc('\007');
    329       1.1       cgd 		}
    330       1.1       cgd 		else if (c >= ' ' && c <= '~') {
    331      1.13  augustss 		    char *p;
    332       1.1       cgd 
    333       1.1       cgd 		    for (p = db_le; p > db_lc; p--)
    334       1.1       cgd 			*p = *(p-1);
    335       1.1       cgd 		    *db_lc++ = c;
    336       1.1       cgd 		    db_le++;
    337       1.1       cgd 		    cnputc(c);
    338       1.1       cgd 		    db_putstring(db_lc, db_le - db_lc);
    339       1.1       cgd 		    db_putnchars(BACKUP, db_le - db_lc);
    340       1.1       cgd 		}
    341       1.1       cgd 		break;
    342       1.1       cgd 	}
    343       1.1       cgd 	return (0);
    344       1.1       cgd }
    345       1.1       cgd 
    346       1.1       cgd int
    347       1.1       cgd db_readline(lstart, lsize)
    348       1.1       cgd 	char *	lstart;
    349       1.1       cgd 	int	lsize;
    350       1.1       cgd {
    351       1.1       cgd 	db_force_whitespace();	/* synch output position */
    352       1.1       cgd 
    353       1.1       cgd 	db_lbuf_start = lstart;
    354       1.1       cgd 	db_lbuf_end   = lstart + lsize;
    355       1.1       cgd 	db_lc = lstart;
    356       1.1       cgd 	db_le = lstart;
    357       1.1       cgd 
    358       1.1       cgd 	while (!db_inputchar(cngetc()))
    359       1.1       cgd 	    continue;
    360       1.1       cgd 
    361       1.1       cgd 	db_putchar('\n');	/* synch output position */
    362       1.1       cgd 
    363       1.1       cgd 	*db_le = 0;
    364       1.1       cgd 	return (db_le - db_lbuf_start);
    365       1.1       cgd }
    366       1.1       cgd 
    367       1.1       cgd void
    368       1.1       cgd db_check_interrupt()
    369       1.1       cgd {
    370      1.13  augustss 	int	c;
    371       1.1       cgd 
    372       1.1       cgd 	c = cnmaygetc();
    373       1.1       cgd 	switch (c) {
    374       1.1       cgd 	    case -1:		/* no character */
    375       1.1       cgd 		return;
    376       1.1       cgd 
    377       1.1       cgd 	    case CTRL('c'):
    378       1.1       cgd 		db_error((char *)0);
    379       1.1       cgd 		/*NOTREACHED*/
    380       1.1       cgd 
    381       1.1       cgd 	    case CTRL('s'):
    382       1.1       cgd 		do {
    383       1.1       cgd 		    c = cnmaygetc();
    384       1.5   mycroft 		    if (c == CTRL('c')) {
    385       1.1       cgd 			db_error((char *)0);
    386       1.5   mycroft 			/*NOTREACHED*/
    387       1.5   mycroft 		    }
    388       1.1       cgd 		} while (c != CTRL('q'));
    389       1.1       cgd 		break;
    390       1.1       cgd 
    391       1.1       cgd 	    default:
    392       1.1       cgd 		/* drop on floor */
    393       1.1       cgd 		break;
    394       1.1       cgd 	}
    395       1.1       cgd }
    396       1.1       cgd 
    397       1.7  christos static int
    398       1.1       cgd cnmaygetc ()
    399       1.1       cgd {
    400       1.1       cgd 	return (-1);
    401       1.1       cgd }
    402