Home | History | Annotate | Line # | Download | only in ddb
db_input.c revision 1.14.2.3
      1  1.14.2.3   nathanw /*	$NetBSD: db_input.c,v 1.14.2.3 2002/02/28 04:13:05 nathanw Exp $	*/
      2       1.4       cgd 
      3  1.14.2.3   nathanw /*
      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.14.2.3   nathanw  *
      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.14.2.3   nathanw  *
     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.14.2.3   nathanw  *
     18       1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.14.2.3   nathanw  *
     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.14.2.3   nathanw  *
     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.3   nathanw __KERNEL_RCSID(0, "$NetBSD: db_input.c,v 1.14.2.3 2002/02/28 04:13:05 nathanw Exp $");
     34      1.10  jonathan 
     35  1.14.2.2   nathanw #include "opt_ddb.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.14.2.3   nathanw static char    *db_lbuf_start;	/* start of input line buffer */
     63  1.14.2.3   nathanw static char    *db_lbuf_end;	/* end of input line buffer */
     64  1.14.2.3   nathanw static char    *db_lc;		/* current character */
     65  1.14.2.3   nathanw static char    *db_le;		/* one past last character */
     66       1.9     lukem #if DDB_HISTORY_SIZE != 0
     67  1.14.2.3   nathanw static char	db_history[DDB_HISTORY_SIZE];	/* start of history buffer */
     68  1.14.2.3   nathanw static int	db_history_size = DDB_HISTORY_SIZE;/* size of history buffer */
     69  1.14.2.3   nathanw static char    *db_history_curr = db_history;	/* start of current line */
     70  1.14.2.3   nathanw static char    *db_history_last = db_history;	/* start of last line */
     71  1.14.2.3   nathanw static char    *db_history_prev = (char *) 0;	/* start of previous line */
     72       1.8  christos #endif
     73  1.14.2.3   nathanw 
     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.14.2.3   nathanw static int	cnmaygetc(void);
     81  1.14.2.3   nathanw static void	db_putstring(char *, int);
     82  1.14.2.3   nathanw static void	db_putnchars(int, int);
     83  1.14.2.3   nathanw static void	db_delete(int, int);
     84  1.14.2.3   nathanw static void	db_delete_line(void);
     85  1.14.2.3   nathanw static int	db_inputchar(int);
     86       1.7  christos 
     87  1.14.2.3   nathanw static void
     88  1.14.2.3   nathanw db_putstring(char *s, int count)
     89       1.1       cgd {
     90  1.14.2.3   nathanw 
     91       1.1       cgd 	while (--count >= 0)
     92  1.14.2.3   nathanw 		cnputc(*s++);
     93       1.1       cgd }
     94       1.1       cgd 
     95  1.14.2.3   nathanw static void
     96  1.14.2.3   nathanw db_putnchars(int c, int count)
     97       1.1       cgd {
     98  1.14.2.3   nathanw 
     99       1.1       cgd 	while (--count >= 0)
    100  1.14.2.3   nathanw 		cnputc(c);
    101       1.1       cgd }
    102       1.1       cgd 
    103       1.1       cgd /*
    104       1.1       cgd  * Delete N characters, forward or backward
    105       1.1       cgd  */
    106       1.1       cgd #define	DEL_FWD		0
    107       1.1       cgd #define	DEL_BWD		1
    108  1.14.2.3   nathanw static void
    109  1.14.2.3   nathanw db_delete(int n, int bwd)
    110       1.1       cgd {
    111      1.13  augustss 	char *p;
    112       1.1       cgd 
    113       1.1       cgd 	if (bwd) {
    114  1.14.2.3   nathanw 		db_lc -= n;
    115  1.14.2.3   nathanw 		db_putnchars(BACKUP, n);
    116       1.1       cgd 	}
    117       1.1       cgd 	for (p = db_lc; p < db_le-n; p++) {
    118  1.14.2.3   nathanw 		*p = *(p+n);
    119  1.14.2.3   nathanw 		cnputc(*p);
    120       1.1       cgd 	}
    121       1.1       cgd 	db_putnchars(BLANK, n);
    122       1.1       cgd 	db_putnchars(BACKUP, db_le - db_lc);
    123       1.1       cgd 	db_le -= n;
    124       1.1       cgd }
    125       1.1       cgd 
    126  1.14.2.3   nathanw static void
    127  1.14.2.3   nathanw db_delete_line(void)
    128       1.8  christos {
    129  1.14.2.3   nathanw 
    130       1.8  christos 	db_delete(db_le - db_lc, DEL_FWD);
    131       1.8  christos 	db_delete(db_lc - db_lbuf_start, DEL_BWD);
    132       1.8  christos 	db_le = db_lc = db_lbuf_start;
    133       1.8  christos }
    134       1.8  christos 
    135       1.9     lukem #if DDB_HISTORY_SIZE != 0
    136       1.8  christos #define INC_DB_CURR() \
    137  1.14.2.3   nathanw 	do { \
    138  1.14.2.3   nathanw 		 db_history_curr++; \
    139  1.14.2.3   nathanw 		 if (db_history_curr > db_history + db_history_size - 1) \
    140  1.14.2.3   nathanw 			 db_history_curr = db_history; \
    141  1.14.2.3   nathanw 	} while (0)
    142       1.8  christos #define DEC_DB_CURR() \
    143  1.14.2.3   nathanw 	do { \
    144  1.14.2.3   nathanw 		 db_history_curr--; \
    145  1.14.2.3   nathanw 		 if (db_history_curr < db_history) \
    146  1.14.2.3   nathanw 		     db_history_curr = db_history + \
    147  1.14.2.3   nathanw 		     db_history_size - 1; \
    148  1.14.2.3   nathanw 	} while (0)
    149       1.8  christos #endif
    150  1.14.2.3   nathanw 
    151       1.1       cgd /* returns TRUE at end-of-line */
    152  1.14.2.3   nathanw static int
    153  1.14.2.3   nathanw db_inputchar(int c)
    154       1.1       cgd {
    155       1.1       cgd 	switch (c) {
    156  1.14.2.3   nathanw 	case CTRL('b'):
    157       1.1       cgd 		/* back up one character */
    158       1.1       cgd 		if (db_lc > db_lbuf_start) {
    159  1.14.2.3   nathanw 			cnputc(BACKUP);
    160  1.14.2.3   nathanw 			db_lc--;
    161       1.1       cgd 		}
    162       1.1       cgd 		break;
    163  1.14.2.3   nathanw 	case CTRL('f'):
    164       1.1       cgd 		/* forward one character */
    165       1.1       cgd 		if (db_lc < db_le) {
    166  1.14.2.3   nathanw 			cnputc(*db_lc);
    167  1.14.2.3   nathanw 			db_lc++;
    168       1.1       cgd 		}
    169       1.1       cgd 		break;
    170  1.14.2.3   nathanw 	case CTRL('a'):
    171       1.1       cgd 		/* beginning of line */
    172       1.1       cgd 		while (db_lc > db_lbuf_start) {
    173  1.14.2.3   nathanw 			cnputc(BACKUP);
    174  1.14.2.3   nathanw 			db_lc--;
    175       1.1       cgd 		}
    176       1.1       cgd 		break;
    177  1.14.2.3   nathanw 	case CTRL('e'):
    178       1.1       cgd 		/* end of line */
    179       1.1       cgd 		while (db_lc < db_le) {
    180  1.14.2.3   nathanw 			cnputc(*db_lc);
    181  1.14.2.3   nathanw 			db_lc++;
    182       1.1       cgd 		}
    183       1.1       cgd 		break;
    184  1.14.2.3   nathanw 	case CTRL('h'):
    185  1.14.2.3   nathanw 	case 0177:
    186       1.1       cgd 		/* erase previous character */
    187       1.1       cgd 		if (db_lc > db_lbuf_start)
    188  1.14.2.3   nathanw 			db_delete(1, DEL_BWD);
    189       1.1       cgd 		break;
    190  1.14.2.3   nathanw 	case CTRL('d'):
    191       1.1       cgd 		/* erase next character */
    192       1.1       cgd 		if (db_lc < db_le)
    193  1.14.2.3   nathanw 			db_delete(1, DEL_FWD);
    194       1.1       cgd 		break;
    195  1.14.2.3   nathanw 	case CTRL('k'):
    196       1.1       cgd 		/* delete to end of line */
    197       1.1       cgd 		if (db_lc < db_le)
    198  1.14.2.3   nathanw 			db_delete(db_le - db_lc, DEL_FWD);
    199       1.1       cgd 		break;
    200  1.14.2.3   nathanw 	case CTRL('u'):
    201       1.8  christos 		/* delete line */
    202  1.14.2.3   nathanw 		db_delete_line();
    203       1.8  christos 		break;
    204  1.14.2.3   nathanw 	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.2.3   nathanw 			if (db_lc < db_le) {
    208  1.14.2.3   nathanw 				c = db_lc[-1];
    209  1.14.2.3   nathanw 				db_lc[-1] = db_lc[0];
    210  1.14.2.3   nathanw 				db_lc[0] = c;
    211  1.14.2.3   nathanw 				cnputc(BACKUP);
    212  1.14.2.3   nathanw 				cnputc(db_lc[-1]);
    213  1.14.2.3   nathanw 				cnputc(db_lc[0]);
    214  1.14.2.3   nathanw 				db_lc++;
    215  1.14.2.3   nathanw 			} else if (db_lc >= db_lbuf_start + 2) {
    216  1.14.2.3   nathanw 				c = db_lc[-2];
    217  1.14.2.3   nathanw 				db_lc[-2] = db_lc[-1];
    218  1.14.2.3   nathanw 				db_lc[-1] = c;
    219  1.14.2.3   nathanw 				cnputc(BACKUP);
    220  1.14.2.3   nathanw 				cnputc(BACKUP);
    221  1.14.2.3   nathanw 				cnputc(db_lc[-2]);
    222  1.14.2.3   nathanw 				cnputc(db_lc[-1]);
    223  1.14.2.3   nathanw 			}
    224       1.1       cgd 		}
    225       1.1       cgd 		break;
    226       1.9     lukem #if DDB_HISTORY_SIZE != 0
    227  1.14.2.3   nathanw 	case CTRL('p'):
    228  1.14.2.3   nathanw 		DEC_DB_CURR();
    229  1.14.2.3   nathanw 		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.14.2.3   nathanw 				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.14.2.3   nathanw 	case CTRL('n'):
    253  1.14.2.3   nathanw 		while (db_history_curr != db_history_last) {
    254       1.8  christos 			if (*db_history_curr == '\0')
    255  1.14.2.3   nathanw 				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.14.2.3   nathanw 	case CTRL('r'):
    278       1.1       cgd 		db_putstring("^R\n", 3);
    279       1.1       cgd 		if (db_le > db_lbuf_start) {
    280  1.14.2.3   nathanw 			db_putstring(db_lbuf_start, db_le - db_lbuf_start);
    281  1.14.2.3   nathanw 			db_putnchars(BACKUP, db_le - db_lc);
    282       1.1       cgd 		}
    283       1.1       cgd 		break;
    284  1.14.2.3   nathanw 	case '\n':
    285  1.14.2.3   nathanw 	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.14.2.3   nathanw 					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.14.2.3   nathanw 	default:
    327       1.1       cgd 		if (db_le == db_lbuf_end) {
    328  1.14.2.3   nathanw 			cnputc('\007');
    329       1.1       cgd 		}
    330       1.1       cgd 		else if (c >= ' ' && c <= '~') {
    331  1.14.2.3   nathanw 			char *p;
    332       1.1       cgd 
    333  1.14.2.3   nathanw 			for (p = db_le; p > db_lc; p--)
    334  1.14.2.3   nathanw 				*p = *(p-1);
    335  1.14.2.3   nathanw 			*db_lc++ = c;
    336  1.14.2.3   nathanw 			db_le++;
    337  1.14.2.3   nathanw 			cnputc(c);
    338  1.14.2.3   nathanw 			db_putstring(db_lc, db_le - db_lc);
    339  1.14.2.3   nathanw 			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.14.2.3   nathanw db_readline(char *lstart, int lsize)
    348       1.1       cgd {
    349       1.1       cgd 	db_force_whitespace();	/* synch output position */
    350       1.1       cgd 
    351       1.1       cgd 	db_lbuf_start = lstart;
    352       1.1       cgd 	db_lbuf_end   = lstart + lsize;
    353       1.1       cgd 	db_lc = lstart;
    354       1.1       cgd 	db_le = lstart;
    355       1.1       cgd 
    356       1.1       cgd 	while (!db_inputchar(cngetc()))
    357  1.14.2.3   nathanw 		continue;
    358       1.1       cgd 
    359       1.1       cgd 	db_putchar('\n');	/* synch output position */
    360       1.1       cgd 
    361       1.1       cgd 	*db_le = 0;
    362       1.1       cgd 	return (db_le - db_lbuf_start);
    363       1.1       cgd }
    364       1.1       cgd 
    365       1.1       cgd void
    366  1.14.2.3   nathanw db_check_interrupt(void)
    367       1.1       cgd {
    368      1.13  augustss 	int	c;
    369       1.1       cgd 
    370       1.1       cgd 	c = cnmaygetc();
    371       1.1       cgd 	switch (c) {
    372  1.14.2.3   nathanw 	case -1:		/* no character */
    373       1.1       cgd 		return;
    374       1.1       cgd 
    375  1.14.2.3   nathanw 	case CTRL('c'):
    376       1.1       cgd 		db_error((char *)0);
    377       1.1       cgd 		/*NOTREACHED*/
    378       1.1       cgd 
    379  1.14.2.3   nathanw 	case CTRL('s'):
    380       1.1       cgd 		do {
    381  1.14.2.3   nathanw 			c = cnmaygetc();
    382  1.14.2.3   nathanw 			if (c == CTRL('c')) {
    383  1.14.2.3   nathanw 				db_error((char *)0);
    384  1.14.2.3   nathanw 				/*NOTREACHED*/
    385  1.14.2.3   nathanw 			}
    386       1.1       cgd 		} while (c != CTRL('q'));
    387       1.1       cgd 		break;
    388       1.1       cgd 
    389  1.14.2.3   nathanw 	default:
    390       1.1       cgd 		/* drop on floor */
    391       1.1       cgd 		break;
    392       1.1       cgd 	}
    393       1.1       cgd }
    394       1.1       cgd 
    395       1.7  christos static int
    396  1.14.2.3   nathanw cnmaygetc(void)
    397       1.1       cgd {
    398  1.14.2.3   nathanw 
    399       1.1       cgd 	return (-1);
    400       1.1       cgd }
    401