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