123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #include <sys/param.h>
- #include <sys/systm.h>
- #include <machine/db_machdep.h>
- #include <ddb/db_var.h>
- #include <ddb/db_output.h>
- #include <ddb/db_sym.h>
- #include <ddb/db_extern.h>
- #include <dev/cons.h>
- char * db_lbuf_start;
- char * db_lbuf_end;
- char * db_lc;
- char * db_le;
- #if DB_HISTORY_SIZE != 0
- char db_history[DB_HISTORY_SIZE];
- int db_history_size = DB_HISTORY_SIZE;
- char * db_history_curr = db_history;
- char * db_history_last = db_history;
- char * db_history_prev = (char *) 0;
- #endif
-
- #define CTRL(c) ((c) & 0x1f)
- #define isspace(c) ((c) == ' ' || (c) == '\t')
- #define BLANK ' '
- #define BACKUP '\b'
- void
- db_putstring(char *s, int count)
- {
- while (--count >= 0)
- cnputc(*s++);
- }
- void
- db_putnchars(int c, int count)
- {
- while (--count >= 0)
- cnputc(c);
- }
- #define DEL_FWD 0
- #define DEL_BWD 1
- void
- db_delete(int n, int bwd)
- {
- char *p;
- if (bwd) {
- db_lc -= n;
- db_putnchars(BACKUP, n);
- }
- for (p = db_lc; p < db_le-n; p++) {
- *p = *(p+n);
- cnputc(*p);
- }
- db_putnchars(BLANK, n);
- db_putnchars(BACKUP, db_le - db_lc);
- db_le -= n;
- }
- void
- db_delete_line(void)
- {
- db_delete(db_le - db_lc, DEL_FWD);
- db_delete(db_lc - db_lbuf_start, DEL_BWD);
- db_le = db_lc = db_lbuf_start;
- }
- #if DB_HISTORY_SIZE != 0
- #define INC_DB_CURR() \
- do { \
- db_history_curr++; \
- if (db_history_curr > \
- db_history + db_history_size - 1) \
- db_history_curr = db_history; \
- } while (0)
- #define DEC_DB_CURR() \
- do { \
- db_history_curr--; \
- if (db_history_curr < db_history) \
- db_history_curr = db_history + \
- db_history_size - 1; \
- } while (0)
- #endif
-
- /* returns TRUE at end-of-line */
- int
- db_inputchar(int c)
- {
- switch (c) {
- case CTRL('b'):
- /* back up one character */
- if (db_lc > db_lbuf_start) {
- cnputc(BACKUP);
- db_lc--;
- }
- break;
- case CTRL('f'):
-
- if (db_lc < db_le) {
- cnputc(*db_lc);
- db_lc++;
- }
- break;
- case CTRL('a'):
-
- while (db_lc > db_lbuf_start) {
- cnputc(BACKUP);
- db_lc--;
- }
- break;
- case CTRL('e'):
-
- while (db_lc < db_le) {
- cnputc(*db_lc);
- db_lc++;
- }
- break;
- case CTRL('w'):
-
- while (db_lc > db_lbuf_start && db_lc[-1] != BLANK)
- db_delete(1, DEL_BWD);
- break;
- case CTRL('h'):
- case 0177:
-
- if (db_lc > db_lbuf_start)
- db_delete(1, DEL_BWD);
- break;
- case CTRL('d'):
-
- if (db_lc < db_le)
- db_delete(1, DEL_FWD);
- break;
- case CTRL('k'):
-
- if (db_lc < db_le)
- db_delete(db_le - db_lc, DEL_FWD);
- break;
- case CTRL('u'):
-
- db_delete_line();
- break;
- case CTRL('t'):
-
- if (db_lc >= db_lbuf_start + 2) {
- c = db_lc[-2];
- db_lc[-2] = db_lc[-1];
- db_lc[-1] = c;
- cnputc(BACKUP);
- cnputc(BACKUP);
- cnputc(db_lc[-2]);
- cnputc(db_lc[-1]);
- }
- break;
- #if DB_HISTORY_SIZE != 0
- case CTRL('p'):
- DEC_DB_CURR();
- while (db_history_curr != db_history_last) {
- DEC_DB_CURR();
- if (*db_history_curr == '\0')
- break;
- }
- db_delete_line();
- if (db_history_curr == db_history_last) {
- INC_DB_CURR();
- db_le = db_lc = db_lbuf_start;
- } else {
- char *p;
- INC_DB_CURR();
- for (p = db_history_curr, db_le = db_lbuf_start;*p; ) {
- *db_le++ = *p++;
- if (p == db_history + db_history_size)
- p = db_history;
- }
- db_lc = db_le;
- }
- db_putstring(db_lbuf_start, db_le - db_lbuf_start);
- break;
- case CTRL('n'):
- while (db_history_curr != db_history_last) {
- if (*db_history_curr == '\0')
- break;
- INC_DB_CURR();
- }
- if (db_history_curr != db_history_last) {
- INC_DB_CURR();
- db_delete_line();
- if (db_history_curr != db_history_last) {
- char *p;
- for (p = db_history_curr,
- db_le = db_lbuf_start; *p;) {
- *db_le++ = *p++;
- if (p == db_history + db_history_size)
- p = db_history;
- }
- db_lc = db_le;
- }
- db_putstring(db_lbuf_start, db_le - db_lbuf_start);
- }
- break;
- #endif
- case CTRL('r'):
- db_putstring("^R\n", 3);
- if (db_le > db_lbuf_start) {
- db_putstring(db_lbuf_start, db_le - db_lbuf_start);
- db_putnchars(BACKUP, db_le - db_lc);
- }
- break;
- case '\n':
- case '\r':
- #if DB_HISTORY_SIZE != 0
-
- if (db_history_curr == db_history_prev) {
- char *pp, *pc;
-
- for (pp = db_history_prev, pc = db_lbuf_start;
- pc != db_le && *pp; ) {
- if (*pp != *pc)
- break;
- if (++pp == db_history + db_history_size)
- pp = db_history;
- pc++;
- }
- if (!*pp && pc == db_le) {
-
- db_history_curr = db_history_last;
- *db_le++ = c;
- return TRUE;
- }
- }
- if (db_le != db_lbuf_start) {
- char *p;
- db_history_prev = db_history_last;
- for (p = db_lbuf_start; p != db_le; p++) {
- *db_history_last++ = *p;
- if (db_history_last ==
- db_history + db_history_size)
- db_history_last = db_history;
- }
- *db_history_last++ = '\0';
- }
- db_history_curr = db_history_last;
- #endif
- *db_le++ = c;
- return TRUE;
- default:
- if (db_le == db_lbuf_end) {
- cnputc('\007');
- }
- else if (c >= ' ' && c <= '~') {
- char *p;
- for (p = db_le; p > db_lc; p--)
- *p = *(p-1);
- *db_lc++ = c;
- db_le++;
- cnputc(c);
- db_putstring(db_lc, db_le - db_lc);
- db_putnchars(BACKUP, db_le - db_lc);
- }
- break;
- }
- return FALSE;
- }
- int
- db_readline(char *lstart, int lsize)
- {
- db_force_whitespace();
- db_lbuf_start = lstart;
- db_lbuf_end = lstart + lsize - 1;
- db_lc = lstart;
- db_le = lstart;
- while (!db_inputchar(cngetc()))
- continue;
- db_putchar('\n');
- *db_le = 0;
- return (db_le - db_lbuf_start);
- }
|