1 /* 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify and distribute this software and its 7 * documentation is hereby granted, provided that both the copyright 8 * notice and this permission notice appear in all copies of the 9 * software, derivative works or modified versions, and any portions 10 * thereof, and that both notices appear in supporting documentation. 11 * 12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 * 26 * $Id: db_input.c,v 1.18 1997/04/12 17:35:02 joerg Exp $ 27 */ 28 29 /* 30 * Author: David B. Golub, Carnegie Mellon University 31 * Date: 7/90 32 */ 33 34 #include <sys/param.h> 35 #include <sys/malloc.h> 36 #include <sys/systm.h> 37 38 #include <machine/cons.h> 39 40 #include <ddb/ddb.h> 41 #include <ddb/db_output.h> 42 43 /* 44 * Character input and editing. 45 */ 46 47 /* 48 * We don't track output position while editing input, 49 * since input always ends with a new-line. We just 50 * reset the line position at the end. 51 */ 52 static char * db_lbuf_start; /* start of input line buffer */ 53 static char * db_lbuf_end; /* end of input line buffer */ 54 static char * db_lc; /* current character */ 55 static char * db_le; /* one past last character */ 56 57 /* 58 * Simple input line history support. 59 */ 60 static char * db_lhistory; 61 static int db_lhistlsize, db_lhistidx, db_lhistcur; 62 #define DB_LHIST_NLINES 10 63 64 #define CTRL(c) ((c) & 0x1f) 65 #define isspace(c) ((c) == ' ' || (c) == '\t') 66 #define BLANK ' ' 67 #define BACKUP '\b' 68 69 static int cnmaygetc __P((void)); 70 static void db_delete __P((int n, int bwd)); 71 static int db_inputchar __P((int c)); 72 static void db_putnchars __P((int c, int count)); 73 static void db_putstring __P((char *s, int count)); 74 75 void 76 db_putstring(s, count) 77 char *s; 78 int count; 79 { 80 while (--count >= 0) 81 cnputc(*s++); 82 } 83 84 void 85 db_putnchars(c, count) 86 int c; 87 int count; 88 { 89 while (--count >= 0) 90 cnputc(c); 91 } 92 93 /* 94 * Delete N characters, forward or backward 95 */ 96 #define DEL_FWD 0 97 #define DEL_BWD 1 98 void 99 db_delete(n, bwd) 100 int n; 101 int bwd; 102 { 103 register char *p; 104 105 if (bwd) { 106 db_lc -= n; 107 db_putnchars(BACKUP, n); 108 } 109 for (p = db_lc; p < db_le-n; p++) { 110 *p = *(p+n); 111 cnputc(*p); 112 } 113 db_putnchars(BLANK, n); 114 db_putnchars(BACKUP, db_le - db_lc); 115 db_le -= n; 116 } 117 118 /* returns TRUE at end-of-line */ 119 int 120 db_inputchar(c) 121 int c; 122 { 123 static int escstate; 124 125 if (escstate == 1) { 126 /* ESC seen, look for [ or O */ 127 if (c == '[' || c == 'O') 128 escstate++; 129 else 130 escstate = 0; /* re-init state machine */ 131 return (0); 132 } else if (escstate == 2) { 133 escstate = 0; 134 /* 135 * If a valid cursor key has been found, translate 136 * into an emacs-style control key, and fall through. 137 * Otherwise, drop off. 138 */ 139 switch (c) { 140 case 'A': /* up */ 141 c = CTRL('p'); 142 break; 143 case 'B': /* down */ 144 c = CTRL('n'); 145 break; 146 case 'C': /* right */ 147 c = CTRL('f'); 148 break; 149 case 'D': /* left */ 150 c = CTRL('b'); 151 break; 152 default: 153 return (0); 154 } 155 } 156 157 switch (c) { 158 case CTRL('['): 159 escstate = 1; 160 break; 161 #if __i386__ && __FreeBSD__ 162 case 591: /* syscons's idea of an arrow key... */ 163 #endif 164 case CTRL('b'): 165 /* back up one character */ 166 if (db_lc > db_lbuf_start) { 167 cnputc(BACKUP); 168 db_lc--; 169 } 170 break; 171 #if __i386__ && __FreeBSD__ 172 case 593: /* syscons's idea of an arrow key... */ 173 #endif 174 case CTRL('f'): 175 /* forward one character */ 176 if (db_lc < db_le) { 177 cnputc(*db_lc); 178 db_lc++; 179 } 180 break; 181 case CTRL('a'): 182 /* beginning of line */ 183 while (db_lc > db_lbuf_start) { 184 cnputc(BACKUP); 185 db_lc--; 186 } 187 break; 188 case CTRL('e'): 189 /* end of line */ 190 while (db_lc < db_le) { 191 cnputc(*db_lc); 192 db_lc++; 193 } 194 break; 195 case CTRL('h'): 196 case 0177: 197 /* erase previous character */ 198 if (db_lc > db_lbuf_start) 199 db_delete(1, DEL_BWD); 200 break; 201 case CTRL('d'): 202 /* erase next character */ 203 if (db_lc < db_le) 204 db_delete(1, DEL_FWD); 205 break; 206 case CTRL('k'): 207 /* delete to end of line */ 208 if (db_lc < db_le) 209 db_delete(db_le - db_lc, DEL_FWD); 210 break; 211 case CTRL('t'): 212 /* twiddle last 2 characters */ 213 if (db_lc >= db_lbuf_start + 2) { 214 c = db_lc[-2]; 215 db_lc[-2] = db_lc[-1]; 216 db_lc[-1] = c; 217 cnputc(BACKUP); 218 cnputc(BACKUP); 219 cnputc(db_lc[-2]); 220 cnputc(db_lc[-1]); 221 } 222 break; 223 case CTRL('r'): 224 db_putstring("^R\n", 3); 225 redraw: 226 if (db_le > db_lbuf_start) { 227 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 228 db_putnchars(BACKUP, db_le - db_lc); 229 } 230 break; 231 #if __i386__ && __FreeBSD__ 232 case 588: /* syscons's idea of an arrow key... */ 233 #endif 234 case CTRL('p'): 235 /* Make previous history line the active one. */ 236 if (db_lhistcur >= 0) { 237 bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 238 db_lbuf_start, db_lhistlsize); 239 db_lhistcur--; 240 goto hist_redraw; 241 } 242 break; 243 #if __i386__ && __FreeBSD__ 244 case 596: /* syscons's idea of an arrow key... */ 245 #endif 246 case CTRL('n'): 247 /* Make next history line the active one. */ 248 if (db_lhistcur < db_lhistidx - 1) { 249 db_lhistcur += 2; 250 bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 251 db_lbuf_start, db_lhistlsize); 252 } else { 253 /* 254 * ^N through tail of history, reset the 255 * buffer to zero length. 256 */ 257 *db_lbuf_start = '\0'; 258 db_lhistcur = db_lhistidx; 259 } 260 261 hist_redraw: 262 db_putnchars(BACKUP, db_le - db_lbuf_start); 263 db_putnchars(BLANK, db_le - db_lbuf_start); 264 db_putnchars(BACKUP, db_le - db_lbuf_start); 265 db_le = index(db_lbuf_start, '\0'); 266 if (db_le[-1] == '\r' || db_le[-1] == '\n') 267 *--db_le = '\0'; 268 db_lc = db_le; 269 goto redraw; 270 271 case -1: 272 /* 273 * eek! the console returned eof. 274 * probably that means we HAVE no console.. we should try bail 275 * XXX 276 */ 277 c = '\r'; 278 case '\n': 279 case '\r': 280 *db_le++ = c; 281 return (1); 282 default: 283 if (db_le == db_lbuf_end) { 284 cnputc('\007'); 285 } 286 else if (c >= ' ' && c <= '~') { 287 register char *p; 288 289 for (p = db_le; p > db_lc; p--) 290 *p = *(p-1); 291 *db_lc++ = c; 292 db_le++; 293 cnputc(c); 294 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 302 int 303 cnmaygetc() 304 { 305 return (-1); 306 } 307 308 int 309 db_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 355 void 356 db_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 */ 384 void 385 cnpollc (flag) 386 int flag; 387 { 388 } 389