db_input.c revision 28330
1301462Ssjg/* 2246149Ssjg * Mach Operating System 3246149Ssjg * Copyright (c) 1991,1990 Carnegie Mellon University 4246149Ssjg * All Rights Reserved. 5246149Ssjg * 6246149Ssjg * Permission to use, copy, modify and distribute this software and its 7246149Ssjg * documentation is hereby granted, provided that both the copyright 8246149Ssjg * notice and this permission notice appear in all copies of the 9246149Ssjg * software, derivative works or modified versions, and any portions 10246149Ssjg * thereof, and that both notices appear in supporting documentation. 11246149Ssjg * 12246149Ssjg * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13246149Ssjg * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14246149Ssjg * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15246149Ssjg * 16246149Ssjg * Carnegie Mellon requests users of this software to return to 17246149Ssjg * 18246149Ssjg * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19246149Ssjg * School of Computer Science 20246149Ssjg * Carnegie Mellon University 21246149Ssjg * Pittsburgh PA 15213-3890 22246149Ssjg * 23246149Ssjg * any improvements or extensions that they make and grant Carnegie the 24246149Ssjg * rights to redistribute these changes. 25246149Ssjg * 26289842Ssjg * $Id: db_input.c,v 1.18 1997/04/12 17:35:02 joerg Exp $ 27289842Ssjg */ 28246149Ssjg 29296637Ssjg/* 30246149Ssjg * Author: David B. Golub, Carnegie Mellon University 31246149Ssjg * Date: 7/90 32246149Ssjg */ 33246149Ssjg 34246149Ssjg#include <sys/param.h> 35246149Ssjg#include <sys/malloc.h> 36246149Ssjg#include <sys/systm.h> 37246149Ssjg 38246149Ssjg#include <machine/cons.h> 39246149Ssjg 40281812Ssjg#include <ddb/ddb.h> 41281812Ssjg#include <ddb/db_output.h> 42281812Ssjg 43281812Ssjg/* 44246149Ssjg * Character input and editing. 45281812Ssjg */ 46281812Ssjg 47246149Ssjg/* 48246149Ssjg * We don't track output position while editing input, 49246149Ssjg * since input always ends with a new-line. We just 50246149Ssjg * reset the line position at the end. 51246149Ssjg */ 52246149Ssjgstatic char * db_lbuf_start; /* start of input line buffer */ 53246149Ssjgstatic char * db_lbuf_end; /* end of input line buffer */ 54246149Ssjgstatic char * db_lc; /* current character */ 55246149Ssjgstatic char * db_le; /* one past last character */ 56246149Ssjg 57281812Ssjg/* 58246149Ssjg * Simple input line history support. 59249033Ssjg */ 60249033Ssjgstatic char * db_lhistory; 61297040Ssjgstatic int db_lhistlsize, db_lhistidx, db_lhistcur; 62249033Ssjg#define DB_LHIST_NLINES 10 63249033Ssjg 64289842Ssjg#define CTRL(c) ((c) & 0x1f) 65289842Ssjg#define isspace(c) ((c) == ' ' || (c) == '\t') 66289842Ssjg#define BLANK ' ' 67289842Ssjg#define BACKUP '\b' 68289842Ssjg 69289842Ssjgstatic int cnmaygetc __P((void)); 70289842Ssjgstatic void db_delete __P((int n, int bwd)); 71289842Ssjgstatic int db_inputchar __P((int c)); 72289842Ssjgstatic void db_putnchars __P((int c, int count)); 73246149Ssjgstatic void db_putstring __P((char *s, int count)); 74246149Ssjg 75249033Ssjgvoid 76246149Ssjgdb_putstring(s, count) 77246149Ssjg char *s; 78246149Ssjg int count; 79289842Ssjg{ 80289842Ssjg while (--count >= 0) 81246149Ssjg cnputc(*s++); 82249033Ssjg} 83246149Ssjg 84246149Ssjgvoid 85246149Ssjgdb_putnchars(c, count) 86246149Ssjg int c; 87246149Ssjg int count; 88249033Ssjg{ 89246149Ssjg while (--count >= 0) 90246149Ssjg cnputc(c); 91246149Ssjg} 92246149Ssjg 93246149Ssjg/* 94246149Ssjg * Delete N characters, forward or backward 95249033Ssjg */ 96249033Ssjg#define DEL_FWD 0 97246149Ssjg#define DEL_BWD 1 98246149Ssjgvoid 99246149Ssjgdb_delete(n, bwd) 100249033Ssjg int n; 101246149Ssjg int bwd; 102246149Ssjg{ 103246149Ssjg register char *p; 104246149Ssjg 105246149Ssjg if (bwd) { 106246149Ssjg db_lc -= n; 107246149Ssjg db_putnchars(BACKUP, n); 108246149Ssjg } 109246149Ssjg for (p = db_lc; p < db_le-n; p++) { 110246149Ssjg *p = *(p+n); 111249033Ssjg cnputc(*p); 112246149Ssjg } 113246149Ssjg db_putnchars(BLANK, n); 114246149Ssjg db_putnchars(BACKUP, db_le - db_lc); 115249033Ssjg db_le -= n; 116246149Ssjg} 117246149Ssjg 118246149Ssjg/* returns TRUE at end-of-line */ 119246149Ssjgint 120246149Ssjgdb_inputchar(c) 121246149Ssjg int c; 122246149Ssjg{ 123246149Ssjg static int escstate; 124246149Ssjg 125249033Ssjg if (escstate == 1) { 126249033Ssjg /* ESC seen, look for [ or O */ 127246149Ssjg if (c == '[' || c == 'O') 128246149Ssjg escstate++; 129246149Ssjg else 130246149Ssjg escstate = 0; /* re-init state machine */ 131246149Ssjg return (0); 132246149Ssjg } else if (escstate == 2) { 133281812Ssjg escstate = 0; 134281812Ssjg /* 135281812Ssjg * If a valid cursor key has been found, translate 136246149Ssjg * into an emacs-style control key, and fall through. 137249033Ssjg * Otherwise, drop off. 138289842Ssjg */ 139246149Ssjg switch (c) { 140281812Ssjg case 'A': /* up */ 141246149Ssjg c = CTRL('p'); 142246149Ssjg break; 143246149Ssjg case 'B': /* down */ 144246149Ssjg c = CTRL('n'); 145246149Ssjg break; 146246149Ssjg case 'C': /* right */ 147249033Ssjg c = CTRL('f'); 148246149Ssjg break; 149289842Ssjg case 'D': /* left */ 150246149Ssjg c = CTRL('b'); 151246149Ssjg break; 152246149Ssjg default: 153281812Ssjg return (0); 154246149Ssjg } 155246149Ssjg } 156246149Ssjg 157246149Ssjg switch (c) { 158291978Ssjg case CTRL('['): 159246149Ssjg escstate = 1; 160281812Ssjg break; 161246149Ssjg#if __i386__ && __FreeBSD__ 162246149Ssjg case 591: /* syscons's idea of an arrow key... */ 163246149Ssjg#endif 164246149Ssjg case CTRL('b'): 165246149Ssjg /* back up one character */ 166246149Ssjg if (db_lc > db_lbuf_start) { 167246149Ssjg cnputc(BACKUP); 168246149Ssjg db_lc--; 169246149Ssjg } 170246149Ssjg break; 171246149Ssjg#if __i386__ && __FreeBSD__ 172246149Ssjg case 593: /* syscons's idea of an arrow key... */ 173246149Ssjg#endif 174246149Ssjg case CTRL('f'): 175246149Ssjg /* forward one character */ 176246149Ssjg if (db_lc < db_le) { 177246149Ssjg cnputc(*db_lc); 178246149Ssjg db_lc++; 179246149Ssjg } 180246149Ssjg break; 181289842Ssjg case CTRL('a'): 182289842Ssjg /* beginning of line */ 183246149Ssjg while (db_lc > db_lbuf_start) { 184246149Ssjg cnputc(BACKUP); 185246149Ssjg db_lc--; 186249033Ssjg } 187246149Ssjg break; 188246149Ssjg case CTRL('e'): 189246149Ssjg /* end of line */ 190246149Ssjg while (db_lc < db_le) { 191246149Ssjg cnputc(*db_lc); 192246149Ssjg db_lc++; 193246149Ssjg } 194246149Ssjg break; 195246149Ssjg case CTRL('h'): 196249033Ssjg case 0177: 197246149Ssjg /* erase previous character */ 198246149Ssjg if (db_lc > db_lbuf_start) 199246149Ssjg db_delete(1, DEL_BWD); 200246149Ssjg break; 201246149Ssjg case CTRL('d'): 202246149Ssjg /* erase next character */ 203246149Ssjg if (db_lc < db_le) 204246149Ssjg db_delete(1, DEL_FWD); 205246149Ssjg break; 206249033Ssjg case CTRL('k'): 207246149Ssjg /* delete to end of line */ 208246149Ssjg if (db_lc < db_le) 209246149Ssjg db_delete(db_le - db_lc, DEL_FWD); 210246149Ssjg break; 211246149Ssjg case CTRL('t'): 212246149Ssjg /* twiddle last 2 characters */ 213246149Ssjg if (db_lc >= db_lbuf_start + 2) { 214246149Ssjg c = db_lc[-2]; 215246149Ssjg db_lc[-2] = db_lc[-1]; 216246149Ssjg db_lc[-1] = c; 217246149Ssjg cnputc(BACKUP); 218246149Ssjg cnputc(BACKUP); 219246149Ssjg cnputc(db_lc[-2]); 220246149Ssjg cnputc(db_lc[-1]); 221246149Ssjg } 222249033Ssjg break; 223249033Ssjg case CTRL('r'): 224246149Ssjg db_putstring("^R\n", 3); 225246149Ssjg redraw: 226246149Ssjg if (db_le > db_lbuf_start) { 227246149Ssjg db_putstring(db_lbuf_start, db_le - db_lbuf_start); 228289842Ssjg db_putnchars(BACKUP, db_le - db_lc); 229246149Ssjg } 230246149Ssjg break; 231246149Ssjg#if __i386__ && __FreeBSD__ 232284254Ssjg case 588: /* syscons's idea of an arrow key... */ 233246149Ssjg#endif 234246149Ssjg case CTRL('p'): 235246149Ssjg /* Make previous history line the active one. */ 236246149Ssjg if (db_lhistcur >= 0) { 237246149Ssjg bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 238281812Ssjg db_lbuf_start, db_lhistlsize); 239249033Ssjg db_lhistcur--; 240249033Ssjg goto hist_redraw; 241289842Ssjg } 242289842Ssjg break; 243289842Ssjg#if __i386__ && __FreeBSD__ 244296637Ssjg case 596: /* syscons's idea of an arrow key... */ 245289842Ssjg#endif 246249033Ssjg case CTRL('n'): 247249033Ssjg /* Make next history line the active one. */ 248249033Ssjg if (db_lhistcur < db_lhistidx - 1) { 249289842Ssjg db_lhistcur += 2; 250249033Ssjg bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 251249033Ssjg db_lbuf_start, db_lhistlsize); 252249033Ssjg } else { 253249033Ssjg /* 254284254Ssjg * ^N through tail of history, reset the 255249033Ssjg * buffer to zero length. 256284254Ssjg */ 257249033Ssjg *db_lbuf_start = '\0'; 258250837Ssjg db_lhistcur = db_lhistidx; 259250837Ssjg } 260250837Ssjg 261246149Ssjg hist_redraw: 262250837Ssjg db_putnchars(BACKUP, db_le - db_lbuf_start); 263250837Ssjg db_putnchars(BLANK, db_le - db_lbuf_start); 264250837Ssjg db_putnchars(BACKUP, db_le - db_lbuf_start); 265250837Ssjg db_le = index(db_lbuf_start, '\0'); 266250837Ssjg if (db_le[-1] == '\r' || db_le[-1] == '\n') 267250837Ssjg *--db_le = '\0'; 268250837Ssjg db_lc = db_le; 269250837Ssjg goto redraw; 270281812Ssjg 271250837Ssjg case -1: 272281812Ssjg /* 273301462Ssjg * eek! the console returned eof. 274301462Ssjg * probably that means we HAVE no console.. we should try bail 275281812Ssjg * XXX 276281812Ssjg */ 277281812Ssjg c = '\r'; 278281812Ssjg case '\n': 279281812Ssjg case '\r': 280281812Ssjg *db_le++ = c; 281281812Ssjg return (1); 282281812Ssjg default: 283281812Ssjg if (db_le == db_lbuf_end) { 284281812Ssjg cnputc('\007'); 285281812Ssjg } 286281812Ssjg else if (c >= ' ' && c <= '~') { 287281812Ssjg register char *p; 288281812Ssjg 289281812Ssjg for (p = db_le; p > db_lc; p--) 290281812Ssjg *p = *(p-1); 291281812Ssjg *db_lc++ = c; 292250837Ssjg db_le++; 293281812Ssjg cnputc(c); 294281812Ssjg db_putstring(db_lc, db_le - db_lc); 295 db_putnchars(BACKUP, db_le - db_lc); 296 } 297 break; 298 } 299 return (0); 300} 301 302int 303cnmaygetc() 304{ 305 return (-1); 306} 307 308int 309db_readline(lstart, lsize) 310 char * lstart; 311 int lsize; 312{ 313 if (db_lhistory && lsize != db_lhistlsize) { 314 /* Should not happen, but to be sane, throw history away. */ 315 FREE(db_lhistory, M_TEMP); 316 db_lhistory = 0; 317 } 318 if (db_lhistory == 0) { 319 /* Initialize input line history. */ 320 db_lhistlsize = lsize; 321 db_lhistidx = -1; 322 MALLOC(db_lhistory, char *, lsize * DB_LHIST_NLINES, 323 M_TEMP, M_NOWAIT); 324 } 325 db_lhistcur = db_lhistidx; 326 327 db_force_whitespace(); /* synch output position */ 328 329 db_lbuf_start = lstart; 330 db_lbuf_end = lstart + lsize; 331 db_lc = lstart; 332 db_le = lstart; 333 334 while (!db_inputchar(cngetc())) 335 continue; 336 337 db_printf("\n"); /* synch output position */ 338 *db_le = 0; 339 340 if (db_le - db_lbuf_start > 1) { 341 /* Maintain input line history for non-empty lines. */ 342 if (++db_lhistidx == DB_LHIST_NLINES) { 343 /* Rotate history. */ 344 ovbcopy(db_lhistory + db_lhistlsize, db_lhistory, 345 db_lhistlsize * (DB_LHIST_NLINES - 1)); 346 db_lhistidx--; 347 } 348 bcopy(lstart, db_lhistory + (db_lhistidx * db_lhistlsize), 349 db_lhistlsize); 350 } 351 352 return (db_le - db_lbuf_start); 353} 354 355void 356db_check_interrupt() 357{ 358 register int c; 359 360 c = cnmaygetc(); 361 switch (c) { 362 case -1: /* no character */ 363 return; 364 365 case CTRL('c'): 366 db_error((char *)0); 367 /*NOTREACHED*/ 368 369 case CTRL('s'): 370 do { 371 c = cnmaygetc(); 372 if (c == CTRL('c')) 373 db_error((char *)0); 374 } while (c != CTRL('q')); 375 break; 376 377 default: 378 /* drop on floor */ 379 break; 380 } 381} 382 383/* called from kdb_trap in db_interface.c */ 384void 385cnpollc (flag) 386 int flag; 387{ 388} 389