1dd3cb568SWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: MIT-CMU 3796df753SPedro F. Giffuni * 45b81b6b3SRodney W. Grimes * Mach Operating System 55b81b6b3SRodney W. Grimes * Copyright (c) 1991,1990 Carnegie Mellon University 65b81b6b3SRodney W. Grimes * All Rights Reserved. 75b81b6b3SRodney W. Grimes * 85b81b6b3SRodney W. Grimes * Permission to use, copy, modify and distribute this software and its 95b81b6b3SRodney W. Grimes * documentation is hereby granted, provided that both the copyright 105b81b6b3SRodney W. Grimes * notice and this permission notice appear in all copies of the 115b81b6b3SRodney W. Grimes * software, derivative works or modified versions, and any portions 125b81b6b3SRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 135b81b6b3SRodney W. Grimes * 145b81b6b3SRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 155b81b6b3SRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 165b81b6b3SRodney W. Grimes * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 175b81b6b3SRodney W. Grimes * 185b81b6b3SRodney W. Grimes * Carnegie Mellon requests users of this software to return to 195b81b6b3SRodney W. Grimes * 205b81b6b3SRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 215b81b6b3SRodney W. Grimes * School of Computer Science 225b81b6b3SRodney W. Grimes * Carnegie Mellon University 235b81b6b3SRodney W. Grimes * Pittsburgh PA 15213-3890 245b81b6b3SRodney W. Grimes * 255b81b6b3SRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 265b81b6b3SRodney W. Grimes * rights to redistribute these changes. 275b81b6b3SRodney W. Grimes */ 285b81b6b3SRodney W. Grimes /* 295b81b6b3SRodney W. Grimes * Author: David B. Golub, Carnegie Mellon University 305b81b6b3SRodney W. Grimes * Date: 7/90 315b81b6b3SRodney W. Grimes */ 325b81b6b3SRodney W. Grimes 33753960f7SDavid E. O'Brien #include <sys/cdefs.h> 34753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$"); 35753960f7SDavid E. O'Brien 36f540b106SGarrett Wollman #include <sys/param.h> 37f540b106SGarrett Wollman #include <sys/systm.h> 38ce9edcf5SPoul-Henning Kamp #include <sys/cons.h> 39*3e5e9939SRyan Libby #include <sys/sysctl.h> 405ccbc3ccSBruce Evans 41f540b106SGarrett Wollman #include <ddb/ddb.h> 42f540b106SGarrett Wollman #include <ddb/db_output.h> 435b81b6b3SRodney W. Grimes 445b81b6b3SRodney W. Grimes /* 455b81b6b3SRodney W. Grimes * Character input and editing. 465b81b6b3SRodney W. Grimes */ 475b81b6b3SRodney W. Grimes 485b81b6b3SRodney W. Grimes /* 495b81b6b3SRodney W. Grimes * We don't track output position while editing input, 505b81b6b3SRodney W. Grimes * since input always ends with a new-line. We just 515b81b6b3SRodney W. Grimes * reset the line position at the end. 525b81b6b3SRodney W. Grimes */ 5325eb640dSPoul-Henning Kamp static char * db_lbuf_start; /* start of input line buffer */ 5425eb640dSPoul-Henning Kamp static char * db_lbuf_end; /* end of input line buffer */ 5525eb640dSPoul-Henning Kamp static char * db_lc; /* current character */ 5625eb640dSPoul-Henning Kamp static char * db_le; /* one past last character */ 575b81b6b3SRodney W. Grimes 58b4630773SJoerg Wunsch /* 59*3e5e9939SRyan Libby * Raw input buffer, processed only for certain control characters. 60*3e5e9939SRyan Libby */ 61*3e5e9939SRyan Libby #define DB_RAW_SIZE 512 62*3e5e9939SRyan Libby static char db_raw[DB_RAW_SIZE]; 63*3e5e9939SRyan Libby static u_int db_raw_pos; 64*3e5e9939SRyan Libby static u_int db_raw_cnt; 65*3e5e9939SRyan Libby static int db_raw_warned; 66*3e5e9939SRyan Libby static int ddb_prioritize_control_input = 1; 67*3e5e9939SRyan Libby SYSCTL_INT(_debug_ddb, OID_AUTO, prioritize_control_input, CTLFLAG_RWTUN, 68*3e5e9939SRyan Libby &ddb_prioritize_control_input, 0, 69*3e5e9939SRyan Libby "Drop input when the buffer fills in order to keep servicing ^C/^S/^Q"); 70*3e5e9939SRyan Libby 71*3e5e9939SRyan Libby /* 72b4630773SJoerg Wunsch * Simple input line history support. 73b4630773SJoerg Wunsch */ 7441630a01SBruce Evans static char db_lhistory[2048]; 75b4630773SJoerg Wunsch static int db_lhistlsize, db_lhistidx, db_lhistcur; 76d984ae1eSMike Smith static int db_lhist_nlines; 77b4630773SJoerg Wunsch 785b81b6b3SRodney W. Grimes #define CTRL(c) ((c) & 0x1f) 795b81b6b3SRodney W. Grimes #define BLANK ' ' 805b81b6b3SRodney W. Grimes #define BACKUP '\b' 815b81b6b3SRodney W. Grimes 8214e10f99SAlfred Perlstein static void db_delete(int n, int bwd); 8314e10f99SAlfred Perlstein static int db_inputchar(int c); 8414e10f99SAlfred Perlstein static void db_putnchars(int c, int count); 8514e10f99SAlfred Perlstein static void db_putstring(char *s, int count); 86*3e5e9939SRyan Libby static int db_raw_pop(void); 87*3e5e9939SRyan Libby static void db_raw_push(int); 88*3e5e9939SRyan Libby static int db_raw_space(void); 89058284fcSBruce Evans 9037c84183SPoul-Henning Kamp static void 915b81b6b3SRodney W. Grimes db_putstring(s, count) 925b81b6b3SRodney W. Grimes char *s; 935b81b6b3SRodney W. Grimes int count; 945b81b6b3SRodney W. Grimes { 955b81b6b3SRodney W. Grimes while (--count >= 0) 965b81b6b3SRodney W. Grimes cnputc(*s++); 975b81b6b3SRodney W. Grimes } 985b81b6b3SRodney W. Grimes 9937c84183SPoul-Henning Kamp static void 1005b81b6b3SRodney W. Grimes db_putnchars(c, count) 1015b81b6b3SRodney W. Grimes int c; 1025b81b6b3SRodney W. Grimes int count; 1035b81b6b3SRodney W. Grimes { 1045b81b6b3SRodney W. Grimes while (--count >= 0) 1055b81b6b3SRodney W. Grimes cnputc(c); 1065b81b6b3SRodney W. Grimes } 1075b81b6b3SRodney W. Grimes 1085b81b6b3SRodney W. Grimes /* 1095b81b6b3SRodney W. Grimes * Delete N characters, forward or backward 1105b81b6b3SRodney W. Grimes */ 1115b81b6b3SRodney W. Grimes #define DEL_FWD 0 1125b81b6b3SRodney W. Grimes #define DEL_BWD 1 11337c84183SPoul-Henning Kamp static void 1145b81b6b3SRodney W. Grimes db_delete(n, bwd) 1155b81b6b3SRodney W. Grimes int n; 1165b81b6b3SRodney W. Grimes int bwd; 1175b81b6b3SRodney W. Grimes { 1180a95ab74SPedro F. Giffuni char *p; 1195b81b6b3SRodney W. Grimes 1205b81b6b3SRodney W. Grimes if (bwd) { 1215b81b6b3SRodney W. Grimes db_lc -= n; 1225b81b6b3SRodney W. Grimes db_putnchars(BACKUP, n); 1235b81b6b3SRodney W. Grimes } 1245b81b6b3SRodney W. Grimes for (p = db_lc; p < db_le-n; p++) { 1255b81b6b3SRodney W. Grimes *p = *(p+n); 1265b81b6b3SRodney W. Grimes cnputc(*p); 1275b81b6b3SRodney W. Grimes } 1285b81b6b3SRodney W. Grimes db_putnchars(BLANK, n); 1295b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 1305b81b6b3SRodney W. Grimes db_le -= n; 1315b81b6b3SRodney W. Grimes } 1325b81b6b3SRodney W. Grimes 1332b490bc7SPedro F. Giffuni /* returns true at end-of-line */ 13437c84183SPoul-Henning Kamp static int 1355b81b6b3SRodney W. Grimes db_inputchar(c) 1365b81b6b3SRodney W. Grimes int c; 1375b81b6b3SRodney W. Grimes { 138eae6ab5eSJoerg Wunsch static int escstate; 139eae6ab5eSJoerg Wunsch 140eae6ab5eSJoerg Wunsch if (escstate == 1) { 141eae6ab5eSJoerg Wunsch /* ESC seen, look for [ or O */ 142eae6ab5eSJoerg Wunsch if (c == '[' || c == 'O') 143eae6ab5eSJoerg Wunsch escstate++; 144eae6ab5eSJoerg Wunsch else 145eae6ab5eSJoerg Wunsch escstate = 0; /* re-init state machine */ 146eae6ab5eSJoerg Wunsch return (0); 147eae6ab5eSJoerg Wunsch } else if (escstate == 2) { 148eae6ab5eSJoerg Wunsch escstate = 0; 149eae6ab5eSJoerg Wunsch /* 150eae6ab5eSJoerg Wunsch * If a valid cursor key has been found, translate 151eae6ab5eSJoerg Wunsch * into an emacs-style control key, and fall through. 152eae6ab5eSJoerg Wunsch * Otherwise, drop off. 153eae6ab5eSJoerg Wunsch */ 1545b81b6b3SRodney W. Grimes switch (c) { 155eae6ab5eSJoerg Wunsch case 'A': /* up */ 156eae6ab5eSJoerg Wunsch c = CTRL('p'); 157eae6ab5eSJoerg Wunsch break; 158eae6ab5eSJoerg Wunsch case 'B': /* down */ 159eae6ab5eSJoerg Wunsch c = CTRL('n'); 160eae6ab5eSJoerg Wunsch break; 161eae6ab5eSJoerg Wunsch case 'C': /* right */ 162eae6ab5eSJoerg Wunsch c = CTRL('f'); 163eae6ab5eSJoerg Wunsch break; 164eae6ab5eSJoerg Wunsch case 'D': /* left */ 165eae6ab5eSJoerg Wunsch c = CTRL('b'); 166eae6ab5eSJoerg Wunsch break; 167eae6ab5eSJoerg Wunsch default: 168eae6ab5eSJoerg Wunsch return (0); 169eae6ab5eSJoerg Wunsch } 170eae6ab5eSJoerg Wunsch } 171eae6ab5eSJoerg Wunsch 172eae6ab5eSJoerg Wunsch switch (c) { 173eae6ab5eSJoerg Wunsch case CTRL('['): 174eae6ab5eSJoerg Wunsch escstate = 1; 175eae6ab5eSJoerg Wunsch break; 1765b81b6b3SRodney W. Grimes case CTRL('b'): 1775b81b6b3SRodney W. Grimes /* back up one character */ 1785b81b6b3SRodney W. Grimes if (db_lc > db_lbuf_start) { 1795b81b6b3SRodney W. Grimes cnputc(BACKUP); 1805b81b6b3SRodney W. Grimes db_lc--; 1815b81b6b3SRodney W. Grimes } 1825b81b6b3SRodney W. Grimes break; 1835b81b6b3SRodney W. Grimes case CTRL('f'): 1845b81b6b3SRodney W. Grimes /* forward one character */ 1855b81b6b3SRodney W. Grimes if (db_lc < db_le) { 1865b81b6b3SRodney W. Grimes cnputc(*db_lc); 1875b81b6b3SRodney W. Grimes db_lc++; 1885b81b6b3SRodney W. Grimes } 1895b81b6b3SRodney W. Grimes break; 1905b81b6b3SRodney W. Grimes case CTRL('a'): 1915b81b6b3SRodney W. Grimes /* beginning of line */ 1925b81b6b3SRodney W. Grimes while (db_lc > db_lbuf_start) { 1935b81b6b3SRodney W. Grimes cnputc(BACKUP); 1945b81b6b3SRodney W. Grimes db_lc--; 1955b81b6b3SRodney W. Grimes } 1965b81b6b3SRodney W. Grimes break; 1975b81b6b3SRodney W. Grimes case CTRL('e'): 1985b81b6b3SRodney W. Grimes /* end of line */ 1995b81b6b3SRodney W. Grimes while (db_lc < db_le) { 2005b81b6b3SRodney W. Grimes cnputc(*db_lc); 2015b81b6b3SRodney W. Grimes db_lc++; 2025b81b6b3SRodney W. Grimes } 2035b81b6b3SRodney W. Grimes break; 2045b81b6b3SRodney W. Grimes case CTRL('h'): 2055b81b6b3SRodney W. Grimes case 0177: 2065b81b6b3SRodney W. Grimes /* erase previous character */ 2075b81b6b3SRodney W. Grimes if (db_lc > db_lbuf_start) 2085b81b6b3SRodney W. Grimes db_delete(1, DEL_BWD); 2095b81b6b3SRodney W. Grimes break; 2105b81b6b3SRodney W. Grimes case CTRL('d'): 2115b81b6b3SRodney W. Grimes /* erase next character */ 2125b81b6b3SRodney W. Grimes if (db_lc < db_le) 2135b81b6b3SRodney W. Grimes db_delete(1, DEL_FWD); 2145b81b6b3SRodney W. Grimes break; 21509ffa9adSYaroslav Tykhiy case CTRL('u'): 21681e12dd1SEdward Tomasz Napierala case CTRL('c'): 21709ffa9adSYaroslav Tykhiy /* kill entire line: */ 21809ffa9adSYaroslav Tykhiy /* at first, delete to beginning of line */ 21909ffa9adSYaroslav Tykhiy if (db_lc > db_lbuf_start) 22009ffa9adSYaroslav Tykhiy db_delete(db_lc - db_lbuf_start, DEL_BWD); 22109ffa9adSYaroslav Tykhiy /* FALLTHROUGH */ 2225b81b6b3SRodney W. Grimes case CTRL('k'): 2235b81b6b3SRodney W. Grimes /* delete to end of line */ 2245b81b6b3SRodney W. Grimes if (db_lc < db_le) 2255b81b6b3SRodney W. Grimes db_delete(db_le - db_lc, DEL_FWD); 2265b81b6b3SRodney W. Grimes break; 2275b81b6b3SRodney W. Grimes case CTRL('t'): 2285b81b6b3SRodney W. Grimes /* twiddle last 2 characters */ 2295b81b6b3SRodney W. Grimes if (db_lc >= db_lbuf_start + 2) { 2305b81b6b3SRodney W. Grimes c = db_lc[-2]; 2315b81b6b3SRodney W. Grimes db_lc[-2] = db_lc[-1]; 2325b81b6b3SRodney W. Grimes db_lc[-1] = c; 2335b81b6b3SRodney W. Grimes cnputc(BACKUP); 2345b81b6b3SRodney W. Grimes cnputc(BACKUP); 2355b81b6b3SRodney W. Grimes cnputc(db_lc[-2]); 2365b81b6b3SRodney W. Grimes cnputc(db_lc[-1]); 2375b81b6b3SRodney W. Grimes } 2385b81b6b3SRodney W. Grimes break; 239eeb6101eSEdward Tomasz Napierala case CTRL('w'): 240eeb6101eSEdward Tomasz Napierala /* erase previous word */ 241eeb6101eSEdward Tomasz Napierala for (; db_lc > db_lbuf_start;) { 242eeb6101eSEdward Tomasz Napierala if (*(db_lc - 1) != ' ') 243eeb6101eSEdward Tomasz Napierala break; 244eeb6101eSEdward Tomasz Napierala db_delete(1, DEL_BWD); 245eeb6101eSEdward Tomasz Napierala } 246eeb6101eSEdward Tomasz Napierala for (; db_lc > db_lbuf_start;) { 247eeb6101eSEdward Tomasz Napierala if (*(db_lc - 1) == ' ') 248eeb6101eSEdward Tomasz Napierala break; 249eeb6101eSEdward Tomasz Napierala db_delete(1, DEL_BWD); 250eeb6101eSEdward Tomasz Napierala } 251eeb6101eSEdward Tomasz Napierala break; 2525b81b6b3SRodney W. Grimes case CTRL('r'): 2535b81b6b3SRodney W. Grimes db_putstring("^R\n", 3); 254b4630773SJoerg Wunsch redraw: 2555b81b6b3SRodney W. Grimes if (db_le > db_lbuf_start) { 2565b81b6b3SRodney W. Grimes db_putstring(db_lbuf_start, db_le - db_lbuf_start); 2575b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 2585b81b6b3SRodney W. Grimes } 2595b81b6b3SRodney W. Grimes break; 260b4630773SJoerg Wunsch case CTRL('p'): 261b4630773SJoerg Wunsch /* Make previous history line the active one. */ 262b4630773SJoerg Wunsch if (db_lhistcur >= 0) { 263b4630773SJoerg Wunsch bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 264b4630773SJoerg Wunsch db_lbuf_start, db_lhistlsize); 265b4630773SJoerg Wunsch db_lhistcur--; 266b4630773SJoerg Wunsch goto hist_redraw; 267b4630773SJoerg Wunsch } 268b4630773SJoerg Wunsch break; 269b4630773SJoerg Wunsch case CTRL('n'): 270b4630773SJoerg Wunsch /* Make next history line the active one. */ 271b4630773SJoerg Wunsch if (db_lhistcur < db_lhistidx - 1) { 272b4630773SJoerg Wunsch db_lhistcur += 2; 273b4630773SJoerg Wunsch bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 274b4630773SJoerg Wunsch db_lbuf_start, db_lhistlsize); 275b4630773SJoerg Wunsch } else { 276b4630773SJoerg Wunsch /* 277b4630773SJoerg Wunsch * ^N through tail of history, reset the 278b4630773SJoerg Wunsch * buffer to zero length. 279b4630773SJoerg Wunsch */ 280b4630773SJoerg Wunsch *db_lbuf_start = '\0'; 281b4630773SJoerg Wunsch db_lhistcur = db_lhistidx; 282b4630773SJoerg Wunsch } 283b4630773SJoerg Wunsch 284b4630773SJoerg Wunsch hist_redraw: 285ea1c6a39SRobert Watson db_putnchars(BACKUP, db_lc - db_lbuf_start); 286b4630773SJoerg Wunsch db_putnchars(BLANK, db_le - db_lbuf_start); 287b4630773SJoerg Wunsch db_putnchars(BACKUP, db_le - db_lbuf_start); 288dc15eac0SEd Schouten db_le = strchr(db_lbuf_start, '\0'); 289b4630773SJoerg Wunsch if (db_le[-1] == '\r' || db_le[-1] == '\n') 290b4630773SJoerg Wunsch *--db_le = '\0'; 291b4630773SJoerg Wunsch db_lc = db_le; 292b4630773SJoerg Wunsch goto redraw; 293b4630773SJoerg Wunsch 29475680b05SJulian Elischer case -1: 29575680b05SJulian Elischer /* 29675680b05SJulian Elischer * eek! the console returned eof. 29775680b05SJulian Elischer * probably that means we HAVE no console.. we should try bail 29875680b05SJulian Elischer * XXX 29975680b05SJulian Elischer */ 30075680b05SJulian Elischer c = '\r'; 3015b81b6b3SRodney W. Grimes case '\n': 3029a4b535cSPoul-Henning Kamp /* FALLTHROUGH */ 3035b81b6b3SRodney W. Grimes case '\r': 3045b81b6b3SRodney W. Grimes *db_le++ = c; 3055b81b6b3SRodney W. Grimes return (1); 3065b81b6b3SRodney W. Grimes default: 3075b81b6b3SRodney W. Grimes if (db_le == db_lbuf_end) { 3085b81b6b3SRodney W. Grimes cnputc('\007'); 3095b81b6b3SRodney W. Grimes } 3105b81b6b3SRodney W. Grimes else if (c >= ' ' && c <= '~') { 3110a95ab74SPedro F. Giffuni char *p; 3125b81b6b3SRodney W. Grimes 3135b81b6b3SRodney W. Grimes for (p = db_le; p > db_lc; p--) 3145b81b6b3SRodney W. Grimes *p = *(p-1); 3155b81b6b3SRodney W. Grimes *db_lc++ = c; 3165b81b6b3SRodney W. Grimes db_le++; 3175b81b6b3SRodney W. Grimes cnputc(c); 3185b81b6b3SRodney W. Grimes db_putstring(db_lc, db_le - db_lc); 3195b81b6b3SRodney W. Grimes db_putnchars(BACKUP, db_le - db_lc); 3205b81b6b3SRodney W. Grimes } 3215b81b6b3SRodney W. Grimes break; 3225b81b6b3SRodney W. Grimes } 3235b81b6b3SRodney W. Grimes return (0); 3245b81b6b3SRodney W. Grimes } 3255b81b6b3SRodney W. Grimes 326*3e5e9939SRyan Libby /* Get a character from the console, first checking the raw input buffer. */ 327*3e5e9939SRyan Libby int 328*3e5e9939SRyan Libby db_getc(void) 329c2718b42SMark Johnston { 330*3e5e9939SRyan Libby int c; 331*3e5e9939SRyan Libby 332*3e5e9939SRyan Libby if (db_raw_cnt == 0) { 333*3e5e9939SRyan Libby c = cngetc(); 334*3e5e9939SRyan Libby } else { 335*3e5e9939SRyan Libby c = db_raw_pop(); 336*3e5e9939SRyan Libby if (c == '\r') 337*3e5e9939SRyan Libby c = '\n'; 338*3e5e9939SRyan Libby } 339*3e5e9939SRyan Libby return (c); 340*3e5e9939SRyan Libby } 341*3e5e9939SRyan Libby 342*3e5e9939SRyan Libby /* Whether the raw input buffer has space to accept another character. */ 343*3e5e9939SRyan Libby static int 344*3e5e9939SRyan Libby db_raw_space(void) 345*3e5e9939SRyan Libby { 346*3e5e9939SRyan Libby 347*3e5e9939SRyan Libby return (db_raw_cnt < DB_RAW_SIZE); 348*3e5e9939SRyan Libby } 349*3e5e9939SRyan Libby 350*3e5e9939SRyan Libby /* Un-get a character from the console by buffering it. */ 351*3e5e9939SRyan Libby static void 352*3e5e9939SRyan Libby db_raw_push(int c) 353*3e5e9939SRyan Libby { 354*3e5e9939SRyan Libby 355*3e5e9939SRyan Libby if (!db_raw_space()) 356*3e5e9939SRyan Libby db_error(NULL); 357*3e5e9939SRyan Libby db_raw[(db_raw_pos + db_raw_cnt++) % DB_RAW_SIZE] = c; 358*3e5e9939SRyan Libby } 359*3e5e9939SRyan Libby 360*3e5e9939SRyan Libby /* Drain a character from the raw input buffer. */ 361*3e5e9939SRyan Libby static int 362*3e5e9939SRyan Libby db_raw_pop(void) 363*3e5e9939SRyan Libby { 364*3e5e9939SRyan Libby 365*3e5e9939SRyan Libby if (db_raw_cnt == 0) 366c2718b42SMark Johnston return (-1); 367*3e5e9939SRyan Libby db_raw_cnt--; 368*3e5e9939SRyan Libby db_raw_warned = 0; 369*3e5e9939SRyan Libby return (db_raw[db_raw_pos++ % DB_RAW_SIZE]); 370c2718b42SMark Johnston } 371c2718b42SMark Johnston 372f23b4c91SGarrett Wollman int 3735b81b6b3SRodney W. Grimes db_readline(lstart, lsize) 3745b81b6b3SRodney W. Grimes char * lstart; 3755b81b6b3SRodney W. Grimes int lsize; 3765b81b6b3SRodney W. Grimes { 377233f8184SRobert Watson 378233f8184SRobert Watson if (lsize < 2) 379233f8184SRobert Watson return (0); 38041630a01SBruce Evans if (lsize != db_lhistlsize) { 38141630a01SBruce Evans /* 38241630a01SBruce Evans * (Re)initialize input line history. Throw away any 38341630a01SBruce Evans * existing history. 38441630a01SBruce Evans */ 38541630a01SBruce Evans db_lhist_nlines = sizeof(db_lhistory) / lsize; 386b4630773SJoerg Wunsch db_lhistlsize = lsize; 387b4630773SJoerg Wunsch db_lhistidx = -1; 388b4630773SJoerg Wunsch } 389b4630773SJoerg Wunsch db_lhistcur = db_lhistidx; 390b4630773SJoerg Wunsch 3915b81b6b3SRodney W. Grimes db_force_whitespace(); /* synch output position */ 3925b81b6b3SRodney W. Grimes 3935b81b6b3SRodney W. Grimes db_lbuf_start = lstart; 394233f8184SRobert Watson db_lbuf_end = lstart + lsize - 2; /* Will append NL and NUL. */ 3955b81b6b3SRodney W. Grimes db_lc = lstart; 3965b81b6b3SRodney W. Grimes db_le = lstart; 3975b81b6b3SRodney W. Grimes 398*3e5e9939SRyan Libby while (!db_inputchar(db_getc())) 3995b81b6b3SRodney W. Grimes continue; 4005b81b6b3SRodney W. Grimes 401086fec57SRobert Watson db_capture_write(lstart, db_le - db_lbuf_start); 4026ddbf1e2SGary Palmer db_printf("\n"); /* synch output position */ 4035b81b6b3SRodney W. Grimes *db_le = 0; 404b4630773SJoerg Wunsch 40541630a01SBruce Evans if (db_le - db_lbuf_start > 1) { 406b4630773SJoerg Wunsch /* Maintain input line history for non-empty lines. */ 407d984ae1eSMike Smith if (++db_lhistidx == db_lhist_nlines) { 408b4630773SJoerg Wunsch /* Rotate history. */ 4098543efaeSDag-Erling Smørgrav bcopy(db_lhistory + db_lhistlsize, db_lhistory, 410d984ae1eSMike Smith db_lhistlsize * (db_lhist_nlines - 1)); 411b4630773SJoerg Wunsch db_lhistidx--; 412b4630773SJoerg Wunsch } 41341630a01SBruce Evans bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize, 414b4630773SJoerg Wunsch db_lhistlsize); 415b4630773SJoerg Wunsch } 416b4630773SJoerg Wunsch 4175b81b6b3SRodney W. Grimes return (db_le - db_lbuf_start); 4185b81b6b3SRodney W. Grimes } 4195b81b6b3SRodney W. Grimes 420*3e5e9939SRyan Libby static void 421*3e5e9939SRyan Libby db_do_interrupt(const char *reason) 422*3e5e9939SRyan Libby { 423*3e5e9939SRyan Libby 424*3e5e9939SRyan Libby /* Do a pager quit too because some commands have jmpbuf handling. */ 425*3e5e9939SRyan Libby db_disable_pager(); 426*3e5e9939SRyan Libby db_pager_quit = 1; 427*3e5e9939SRyan Libby db_error(reason); 428*3e5e9939SRyan Libby } 429*3e5e9939SRyan Libby 4305b81b6b3SRodney W. Grimes void 431a41dd031SPedro F. Giffuni db_check_interrupt(void) 4325b81b6b3SRodney W. Grimes { 4330a95ab74SPedro F. Giffuni int c; 4345b81b6b3SRodney W. Grimes 435*3e5e9939SRyan Libby /* 436*3e5e9939SRyan Libby * Check console input for control characters. Non-control input is 437*3e5e9939SRyan Libby * buffered. When buffer space is exhausted, either stop responding to 438*3e5e9939SRyan Libby * control input or drop further non-control input on the floor. 439*3e5e9939SRyan Libby */ 440*3e5e9939SRyan Libby for (;;) { 441*3e5e9939SRyan Libby if (!ddb_prioritize_control_input && !db_raw_space()) 442*3e5e9939SRyan Libby return; 443*3e5e9939SRyan Libby c = cncheckc(); 4445b81b6b3SRodney W. Grimes switch (c) { 4455b81b6b3SRodney W. Grimes case -1: /* no character */ 4465b81b6b3SRodney W. Grimes return; 4475b81b6b3SRodney W. Grimes 4485b81b6b3SRodney W. Grimes case CTRL('c'): 449*3e5e9939SRyan Libby db_do_interrupt("^C"); 4505b81b6b3SRodney W. Grimes /*NOTREACHED*/ 4515b81b6b3SRodney W. Grimes 4525b81b6b3SRodney W. Grimes case CTRL('s'): 4535b81b6b3SRodney W. Grimes do { 454*3e5e9939SRyan Libby c = cncheckc(); 4555b81b6b3SRodney W. Grimes if (c == CTRL('c')) 456*3e5e9939SRyan Libby db_do_interrupt("^C"); 4575b81b6b3SRodney W. Grimes } while (c != CTRL('q')); 4585b81b6b3SRodney W. Grimes break; 4595b81b6b3SRodney W. Grimes 4605b81b6b3SRodney W. Grimes default: 461*3e5e9939SRyan Libby if (db_raw_space()) { 462*3e5e9939SRyan Libby db_raw_push(c); 463*3e5e9939SRyan Libby } else if (!db_raw_warned) { 464*3e5e9939SRyan Libby db_raw_warned = 1; 465*3e5e9939SRyan Libby db_printf("\n--Exceeded input buffer--\n"); 466*3e5e9939SRyan Libby } 4675b81b6b3SRodney W. Grimes break; 4685b81b6b3SRodney W. Grimes } 4695b81b6b3SRodney W. Grimes } 470*3e5e9939SRyan Libby } 471