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