1dd3cb568SWarner Losh /*- 25b81b6b3SRodney W. Grimes * Mach Operating System 35b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University 45b81b6b3SRodney W. Grimes * All Rights Reserved. 55b81b6b3SRodney W. Grimes * 65b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its 75b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright 85b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the 95b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions 105b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 115b81b6b3SRodney W. Grimes * 125b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 135b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 145b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 155b81b6b3SRodney W. Grimes * 165b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to 175b81b6b3SRodney W. Grimes * 185b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 195b81b6b3SRodney W. Grimes * School of Computer Science 205b81b6b3SRodney W. Grimes * Carnegie Mellon University 215b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890 225b81b6b3SRodney W. Grimes * 235b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 245b81b6b3SRodney W. Grimes * rights to redistribute these changes. 255b81b6b3SRodney W. Grimes */ 265b81b6b3SRodney W. Grimes /* 275b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University 285b81b6b3SRodney W. Grimes * Date: 7/90 295b81b6b3SRodney W. Grimes */ 305b81b6b3SRodney W. Grimes 31753960f7SDavid E. O'Brien #include <sys/cdefs.h> 32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$"); 33753960f7SDavid E. O'Brien 34f540b106SGarrett Wollman #include <sys/param.h> 35f540b106SGarrett Wollman #include <sys/systm.h> 36ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 375ccbc3ccSBruce Evans 38f540b106SGarrett Wollman #include <ddb/ddb.h> 39f540b106SGarrett Wollman #include <ddb/db_output.h> 405b81b6b3SRodney W. Grimes 415b81b6b3SRodney W. Grimes /* 425b81b6b3SRodney W. Grimes * Character input and editing. 435b81b6b3SRodney W. Grimes */ 445b81b6b3SRodney W. Grimes 455b81b6b3SRodney W. Grimes /* 465b81b6b3SRodney W. Grimes * We don't track output position while editing input, 475b81b6b3SRodney W. Grimes * since input always ends with a new-line. We just 485b81b6b3SRodney W. Grimes * reset the line position at the end. 495b81b6b3SRodney W. Grimes */ 5025eb640dSPoul-Henning Kamp static char * db_lbuf_start; /* start of input line buffer */ 5125eb640dSPoul-Henning Kamp static char * db_lbuf_end; /* end of input line buffer */ 5225eb640dSPoul-Henning Kamp static char * db_lc; /* current character */ 5325eb640dSPoul-Henning Kamp static char * db_le; /* one past last character */ 545b81b6b3SRodney W. Grimes 55b4630773SJoerg Wunsch /* 56b4630773SJoerg Wunsch * Simple input line history support. 57b4630773SJoerg Wunsch */ 5841630a01SBruce Evans static char db_lhistory[2048]; 59b4630773SJoerg Wunsch static int db_lhistlsize, db_lhistidx, db_lhistcur; 60d984ae1eSMike Smith static int db_lhist_nlines; 61b4630773SJoerg Wunsch 625b81b6b3SRodney W. Grimes #define CTRL(c) ((c) & 0x1f) 635b81b6b3SRodney W. Grimes #define BLANK ' ' 645b81b6b3SRodney W. Grimes #define BACKUP '\b' 655b81b6b3SRodney W. Grimes 6614e10f99SAlfred Perlstein static int cnmaygetc(void); 6714e10f99SAlfred Perlstein static void db_delete(int n, int bwd); 6814e10f99SAlfred Perlstein static int db_inputchar(int c); 6914e10f99SAlfred Perlstein static void db_putnchars(int c, int count); 7014e10f99SAlfred Perlstein static void db_putstring(char *s, int count); 71058284fcSBruce Evans 7237c84183SPoul-Henning Kamp static void 735b81b6b3SRodney W. Grimes db_putstring(s, count) 745b81b6b3SRodney W. Grimes char *s; 755b81b6b3SRodney W. Grimes int count; 765b81b6b3SRodney W. Grimes { 775b81b6b3SRodney W. Grimes while (--count >= 0) 785b81b6b3SRodney W. Grimes cnputc(*s++); 795b81b6b3SRodney W. Grimes } 805b81b6b3SRodney W. Grimes 8137c84183SPoul-Henning Kamp static void 825b81b6b3SRodney W. Grimes db_putnchars(c, count) 835b81b6b3SRodney W. Grimes int c; 845b81b6b3SRodney W. Grimes int count; 855b81b6b3SRodney W. Grimes { 865b81b6b3SRodney W. Grimes while (--count >= 0) 875b81b6b3SRodney W. Grimes cnputc(c); 885b81b6b3SRodney W. Grimes } 895b81b6b3SRodney W. Grimes 905b81b6b3SRodney W. Grimes /* 915b81b6b3SRodney W. Grimes * Delete N characters, forward or backward 925b81b6b3SRodney W. Grimes */ 935b81b6b3SRodney W. Grimes #define DEL_FWD 0 945b81b6b3SRodney W. Grimes #define DEL_BWD 1 9537c84183SPoul-Henning Kamp static void 965b81b6b3SRodney W. Grimes db_delete(n, bwd) 975b81b6b3SRodney W. Grimes int n; 985b81b6b3SRodney W. Grimes int bwd; 995b81b6b3SRodney W. Grimes { 1005b81b6b3SRodney W. Grimes register char *p; 1015b81b6b3SRodney W. Grimes 1025b81b6b3SRodney W. Grimes if (bwd) { 1035b81b6b3SRodney W. Grimes db_lc -= n; 1045b81b6b3SRodney W. Grimes db_putnchars(BACKUP, n); 1055b81b6b3SRodney W. Grimes } 1065b81b6b3SRodney W. Grimes for (p = db_lc; p < db_le-n; p++) { 1075b81b6b3SRodney W. Grimes *p = *(p+n); 1085b81b6b3SRodney W. Grimes cnputc(*p); 1095b81b6b3SRodney W. Grimes } 1105b81b6b3SRodney W. Grimes db_putnchars(BLANK, n); 1115b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 1125b81b6b3SRodney W. Grimes db_le -= n; 1135b81b6b3SRodney W. Grimes } 1145b81b6b3SRodney W. Grimes 1155b81b6b3SRodney W. Grimes /* returns TRUE at end-of-line */ 11637c84183SPoul-Henning Kamp static int 1175b81b6b3SRodney W. Grimes db_inputchar(c) 1185b81b6b3SRodney W. Grimes int c; 1195b81b6b3SRodney W. Grimes { 120eae6ab5eSJoerg Wunsch static int escstate; 121eae6ab5eSJoerg Wunsch 122eae6ab5eSJoerg Wunsch if (escstate == 1) { 123eae6ab5eSJoerg Wunsch /* ESC seen, look for [ or O */ 124eae6ab5eSJoerg Wunsch if (c == '[' || c == 'O') 125eae6ab5eSJoerg Wunsch escstate++; 126eae6ab5eSJoerg Wunsch else 127eae6ab5eSJoerg Wunsch escstate = 0; /* re-init state machine */ 128eae6ab5eSJoerg Wunsch return (0); 129eae6ab5eSJoerg Wunsch } else if (escstate == 2) { 130eae6ab5eSJoerg Wunsch escstate = 0; 131eae6ab5eSJoerg Wunsch /* 132eae6ab5eSJoerg Wunsch * If a valid cursor key has been found, translate 133eae6ab5eSJoerg Wunsch * into an emacs-style control key, and fall through. 134eae6ab5eSJoerg Wunsch * Otherwise, drop off. 135eae6ab5eSJoerg Wunsch */ 1365b81b6b3SRodney W. Grimes switch (c) { 137eae6ab5eSJoerg Wunsch case 'A': /* up */ 138eae6ab5eSJoerg Wunsch c = CTRL('p'); 139eae6ab5eSJoerg Wunsch break; 140eae6ab5eSJoerg Wunsch case 'B': /* down */ 141eae6ab5eSJoerg Wunsch c = CTRL('n'); 142eae6ab5eSJoerg Wunsch break; 143eae6ab5eSJoerg Wunsch case 'C': /* right */ 144eae6ab5eSJoerg Wunsch c = CTRL('f'); 145eae6ab5eSJoerg Wunsch break; 146eae6ab5eSJoerg Wunsch case 'D': /* left */ 147eae6ab5eSJoerg Wunsch c = CTRL('b'); 148eae6ab5eSJoerg Wunsch break; 149eae6ab5eSJoerg Wunsch default: 150eae6ab5eSJoerg Wunsch return (0); 151eae6ab5eSJoerg Wunsch } 152eae6ab5eSJoerg Wunsch } 153eae6ab5eSJoerg Wunsch 154eae6ab5eSJoerg Wunsch switch (c) { 155eae6ab5eSJoerg Wunsch case CTRL('['): 156eae6ab5eSJoerg Wunsch escstate = 1; 157eae6ab5eSJoerg Wunsch break; 1585b81b6b3SRodney W. Grimes case CTRL('b'): 1595b81b6b3SRodney W. Grimes /* back up one character */ 1605b81b6b3SRodney W. Grimes if (db_lc > db_lbuf_start) { 1615b81b6b3SRodney W. Grimes cnputc(BACKUP); 1625b81b6b3SRodney W. Grimes db_lc--; 1635b81b6b3SRodney W. Grimes } 1645b81b6b3SRodney W. Grimes break; 1655b81b6b3SRodney W. Grimes case CTRL('f'): 1665b81b6b3SRodney W. Grimes /* forward one character */ 1675b81b6b3SRodney W. Grimes if (db_lc < db_le) { 1685b81b6b3SRodney W. Grimes cnputc(*db_lc); 1695b81b6b3SRodney W. Grimes db_lc++; 1705b81b6b3SRodney W. Grimes } 1715b81b6b3SRodney W. Grimes break; 1725b81b6b3SRodney W. Grimes case CTRL('a'): 1735b81b6b3SRodney W. Grimes /* beginning of line */ 1745b81b6b3SRodney W. Grimes while (db_lc > db_lbuf_start) { 1755b81b6b3SRodney W. Grimes cnputc(BACKUP); 1765b81b6b3SRodney W. Grimes db_lc--; 1775b81b6b3SRodney W. Grimes } 1785b81b6b3SRodney W. Grimes break; 1795b81b6b3SRodney W. Grimes case CTRL('e'): 1805b81b6b3SRodney W. Grimes /* end of line */ 1815b81b6b3SRodney W. Grimes while (db_lc < db_le) { 1825b81b6b3SRodney W. Grimes cnputc(*db_lc); 1835b81b6b3SRodney W. Grimes db_lc++; 1845b81b6b3SRodney W. Grimes } 1855b81b6b3SRodney W. Grimes break; 1865b81b6b3SRodney W. Grimes case CTRL('h'): 1875b81b6b3SRodney W. Grimes case 0177: 1885b81b6b3SRodney W. Grimes /* erase previous character */ 1895b81b6b3SRodney W. Grimes if (db_lc > db_lbuf_start) 1905b81b6b3SRodney W. Grimes db_delete(1, DEL_BWD); 1915b81b6b3SRodney W. Grimes break; 1925b81b6b3SRodney W. Grimes case CTRL('d'): 1935b81b6b3SRodney W. Grimes /* erase next character */ 1945b81b6b3SRodney W. Grimes if (db_lc < db_le) 1955b81b6b3SRodney W. Grimes db_delete(1, DEL_FWD); 1965b81b6b3SRodney W. Grimes break; 19709ffa9adSYaroslav Tykhiy case CTRL('u'): 19809ffa9adSYaroslav Tykhiy /* kill entire line: */ 19909ffa9adSYaroslav Tykhiy /* at first, delete to beginning of line */ 20009ffa9adSYaroslav Tykhiy if (db_lc > db_lbuf_start) 20109ffa9adSYaroslav Tykhiy db_delete(db_lc - db_lbuf_start, DEL_BWD); 20209ffa9adSYaroslav Tykhiy /* FALLTHROUGH */ 2035b81b6b3SRodney W. Grimes case CTRL('k'): 2045b81b6b3SRodney W. Grimes /* delete to end of line */ 2055b81b6b3SRodney W. Grimes if (db_lc < db_le) 2065b81b6b3SRodney W. Grimes db_delete(db_le - db_lc, DEL_FWD); 2075b81b6b3SRodney W. Grimes break; 2085b81b6b3SRodney W. Grimes case CTRL('t'): 2095b81b6b3SRodney W. Grimes /* twiddle last 2 characters */ 2105b81b6b3SRodney W. Grimes if (db_lc >= db_lbuf_start + 2) { 2115b81b6b3SRodney W. Grimes c = db_lc[-2]; 2125b81b6b3SRodney W. Grimes db_lc[-2] = db_lc[-1]; 2135b81b6b3SRodney W. Grimes db_lc[-1] = c; 2145b81b6b3SRodney W. Grimes cnputc(BACKUP); 2155b81b6b3SRodney W. Grimes cnputc(BACKUP); 2165b81b6b3SRodney W. Grimes cnputc(db_lc[-2]); 2175b81b6b3SRodney W. Grimes cnputc(db_lc[-1]); 2185b81b6b3SRodney W. Grimes } 2195b81b6b3SRodney W. Grimes break; 2205b81b6b3SRodney W. Grimes case CTRL('r'): 2215b81b6b3SRodney W. Grimes db_putstring("^R\n", 3); 222b4630773SJoerg Wunsch redraw: 2235b81b6b3SRodney W. Grimes if (db_le > db_lbuf_start) { 2245b81b6b3SRodney W. Grimes db_putstring(db_lbuf_start, db_le - db_lbuf_start); 2255b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 2265b81b6b3SRodney W. Grimes } 2275b81b6b3SRodney W. Grimes break; 228b4630773SJoerg Wunsch case CTRL('p'): 229b4630773SJoerg Wunsch /* Make previous history line the active one. */ 230b4630773SJoerg Wunsch if (db_lhistcur >= 0) { 231b4630773SJoerg Wunsch bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 232b4630773SJoerg Wunsch db_lbuf_start, db_lhistlsize); 233b4630773SJoerg Wunsch db_lhistcur--; 234b4630773SJoerg Wunsch goto hist_redraw; 235b4630773SJoerg Wunsch } 236b4630773SJoerg Wunsch break; 237b4630773SJoerg Wunsch case CTRL('n'): 238b4630773SJoerg Wunsch /* Make next history line the active one. */ 239b4630773SJoerg Wunsch if (db_lhistcur < db_lhistidx - 1) { 240b4630773SJoerg Wunsch db_lhistcur += 2; 241b4630773SJoerg Wunsch bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 242b4630773SJoerg Wunsch db_lbuf_start, db_lhistlsize); 243b4630773SJoerg Wunsch } else { 244b4630773SJoerg Wunsch /* 245b4630773SJoerg Wunsch * ^N through tail of history, reset the 246b4630773SJoerg Wunsch * buffer to zero length. 247b4630773SJoerg Wunsch */ 248b4630773SJoerg Wunsch *db_lbuf_start = '\0'; 249b4630773SJoerg Wunsch db_lhistcur = db_lhistidx; 250b4630773SJoerg Wunsch } 251b4630773SJoerg Wunsch 252b4630773SJoerg Wunsch hist_redraw: 253ea1c6a39SRobert Watson db_putnchars(BACKUP, db_lc - db_lbuf_start); 254b4630773SJoerg Wunsch db_putnchars(BLANK, db_le - db_lbuf_start); 255b4630773SJoerg Wunsch db_putnchars(BACKUP, db_le - db_lbuf_start); 256b4630773SJoerg Wunsch db_le = index(db_lbuf_start, '\0'); 257b4630773SJoerg Wunsch if (db_le[-1] == '\r' || db_le[-1] == '\n') 258b4630773SJoerg Wunsch *--db_le = '\0'; 259b4630773SJoerg Wunsch db_lc = db_le; 260b4630773SJoerg Wunsch goto redraw; 261b4630773SJoerg Wunsch 26275680b05SJulian Elischer case -1: 26375680b05SJulian Elischer /* 26475680b05SJulian Elischer * eek! the console returned eof. 26575680b05SJulian Elischer * probably that means we HAVE no console.. we should try bail 26675680b05SJulian Elischer * XXX 26775680b05SJulian Elischer */ 26875680b05SJulian Elischer c = '\r'; 2695b81b6b3SRodney W. Grimes case '\n': 2709a4b535cSPoul-Henning Kamp /* FALLTHROUGH */ 2715b81b6b3SRodney W. Grimes case '\r': 2725b81b6b3SRodney W. Grimes *db_le++ = c; 2735b81b6b3SRodney W. Grimes return (1); 2745b81b6b3SRodney W. Grimes default: 2755b81b6b3SRodney W. Grimes if (db_le == db_lbuf_end) { 2765b81b6b3SRodney W. Grimes cnputc('\007'); 2775b81b6b3SRodney W. Grimes } 2785b81b6b3SRodney W. Grimes else if (c >= ' ' && c <= '~') { 2795b81b6b3SRodney W. Grimes register char *p; 2805b81b6b3SRodney W. Grimes 2815b81b6b3SRodney W. Grimes for (p = db_le; p > db_lc; p--) 2825b81b6b3SRodney W. Grimes *p = *(p-1); 2835b81b6b3SRodney W. Grimes *db_lc++ = c; 2845b81b6b3SRodney W. Grimes db_le++; 2855b81b6b3SRodney W. Grimes cnputc(c); 2865b81b6b3SRodney W. Grimes db_putstring(db_lc, db_le - db_lc); 2875b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 2885b81b6b3SRodney W. Grimes } 2895b81b6b3SRodney W. Grimes break; 2905b81b6b3SRodney W. Grimes } 2915b81b6b3SRodney W. Grimes return (0); 2925b81b6b3SRodney W. Grimes } 2935b81b6b3SRodney W. Grimes 29437c84183SPoul-Henning Kamp static int 295058284fcSBruce Evans cnmaygetc() 296f23b4c91SGarrett Wollman { 297f23b4c91SGarrett Wollman return (-1); 298f23b4c91SGarrett Wollman } 299f23b4c91SGarrett Wollman 300f23b4c91SGarrett Wollman int 3015b81b6b3SRodney W. Grimes db_readline(lstart, lsize) 3025b81b6b3SRodney W. Grimes char * lstart; 3035b81b6b3SRodney W. Grimes int lsize; 3045b81b6b3SRodney W. Grimes { 30541630a01SBruce Evans if (lsize != db_lhistlsize) { 30641630a01SBruce Evans /* 30741630a01SBruce Evans * (Re)initialize input line history. Throw away any 30841630a01SBruce Evans * existing history. 30941630a01SBruce Evans */ 31041630a01SBruce Evans db_lhist_nlines = sizeof(db_lhistory) / lsize; 311b4630773SJoerg Wunsch db_lhistlsize = lsize; 312b4630773SJoerg Wunsch db_lhistidx = -1; 313b4630773SJoerg Wunsch } 314b4630773SJoerg Wunsch db_lhistcur = db_lhistidx; 315b4630773SJoerg Wunsch 3165b81b6b3SRodney W. Grimes db_force_whitespace(); /* synch output position */ 3175b81b6b3SRodney W. Grimes 3185b81b6b3SRodney W. Grimes db_lbuf_start = lstart; 3195b81b6b3SRodney W. Grimes db_lbuf_end = lstart + lsize; 3205b81b6b3SRodney W. Grimes db_lc = lstart; 3215b81b6b3SRodney W. Grimes db_le = lstart; 3225b81b6b3SRodney W. Grimes 3235b81b6b3SRodney W. Grimes while (!db_inputchar(cngetc())) 3245b81b6b3SRodney W. Grimes continue; 3255b81b6b3SRodney W. Grimes 326086fec57SRobert Watson db_capture_write(lstart, db_le - db_lbuf_start); 3276ddbf1e2SGary Palmer db_printf("\n"); /* synch output position */ 3285b81b6b3SRodney W. Grimes *db_le = 0; 329b4630773SJoerg Wunsch 33041630a01SBruce Evans if (db_le - db_lbuf_start > 1) { 331b4630773SJoerg Wunsch /* Maintain input line history for non-empty lines. */ 332d984ae1eSMike Smith if (++db_lhistidx == db_lhist_nlines) { 333b4630773SJoerg Wunsch /* Rotate history. */ 3348543efaeSDag-Erling Smørgrav bcopy(db_lhistory + db_lhistlsize, db_lhistory, 335d984ae1eSMike Smith db_lhistlsize * (db_lhist_nlines - 1)); 336b4630773SJoerg Wunsch db_lhistidx--; 337b4630773SJoerg Wunsch } 33841630a01SBruce Evans bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize, 339b4630773SJoerg Wunsch db_lhistlsize); 340b4630773SJoerg Wunsch } 341b4630773SJoerg Wunsch 3425b81b6b3SRodney W. Grimes return (db_le - db_lbuf_start); 3435b81b6b3SRodney W. Grimes } 3445b81b6b3SRodney W. Grimes 3455b81b6b3SRodney W. Grimes void 3465b81b6b3SRodney W. Grimes db_check_interrupt() 3475b81b6b3SRodney W. Grimes { 3485b81b6b3SRodney W. Grimes register int c; 3495b81b6b3SRodney W. Grimes 3505b81b6b3SRodney W. Grimes c = cnmaygetc(); 3515b81b6b3SRodney W. Grimes switch (c) { 3525b81b6b3SRodney W. Grimes case -1: /* no character */ 3535b81b6b3SRodney W. Grimes return; 3545b81b6b3SRodney W. Grimes 3555b81b6b3SRodney W. Grimes case CTRL('c'): 3565b81b6b3SRodney W. Grimes db_error((char *)0); 3575b81b6b3SRodney W. Grimes /*NOTREACHED*/ 3585b81b6b3SRodney W. Grimes 3595b81b6b3SRodney W. Grimes case CTRL('s'): 3605b81b6b3SRodney W. Grimes do { 3615b81b6b3SRodney W. Grimes c = cnmaygetc(); 3625b81b6b3SRodney W. Grimes if (c == CTRL('c')) 3635b81b6b3SRodney W. Grimes db_error((char *)0); 3645b81b6b3SRodney W. Grimes } while (c != CTRL('q')); 3655b81b6b3SRodney W. Grimes break; 3665b81b6b3SRodney W. Grimes 3675b81b6b3SRodney W. Grimes default: 3685b81b6b3SRodney W. Grimes /* drop on floor */ 3695b81b6b3SRodney W. Grimes break; 3705b81b6b3SRodney W. Grimes } 3715b81b6b3SRodney W. Grimes } 372