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