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