1 /* $NetBSD: db_input.c,v 1.23 2009/03/07 22:02:17 ad Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 * 28 * Author: David B. Golub, Carnegie Mellon University 29 * Date: 7/90 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: db_input.c,v 1.23 2009/03/07 22:02:17 ad Exp $"); 34 35 #ifdef _KERNEL_OPT 36 #include "opt_ddbparam.h" 37 #endif 38 39 #include <sys/param.h> 40 #include <sys/proc.h> 41 #include <sys/cpu.h> 42 43 #include <ddb/ddb.h> 44 45 #include <dev/cons.h> 46 47 #ifndef DDB_HISTORY_SIZE 48 #define DDB_HISTORY_SIZE 0 49 #endif /* DDB_HISTORY_SIZE */ 50 51 /* 52 * Character input and editing. 53 */ 54 55 /* 56 * We don't track output position while editing input, 57 * since input always ends with a new-line. We just 58 * reset the line position at the end. 59 */ 60 static char *db_lbuf_start; /* start of input line buffer */ 61 static char *db_lbuf_end; /* end of input line buffer */ 62 static char *db_lc; /* current character */ 63 static char *db_le; /* one past last character */ 64 #if DDB_HISTORY_SIZE != 0 65 static char db_history[DDB_HISTORY_SIZE]; /* start of history buffer */ 66 static int db_history_size = DDB_HISTORY_SIZE;/* size of history buffer */ 67 static char *db_history_curr = db_history; /* start of current line */ 68 static char *db_history_last = db_history; /* start of last line */ 69 static char *db_history_prev = (char *) 0; /* start of previous line */ 70 #endif 71 72 73 #define CTRL(c) ((c) & 0x1f) 74 #define isspace(c) ((c) == ' ' || (c) == '\t') 75 #define BLANK ' ' 76 #define BACKUP '\b' 77 78 static int cnmaygetc(void); 79 static void db_putstring(const char *, int); 80 static void db_putnchars(int, int); 81 static void db_delete(int, int); 82 static void db_delete_line(void); 83 static int db_inputchar(int); 84 85 static void 86 db_putstring(const char *s, int count) 87 { 88 89 while (--count >= 0) 90 cnputc(*s++); 91 } 92 93 static void 94 db_putnchars(int c, int count) 95 { 96 97 while (--count >= 0) 98 cnputc(c); 99 } 100 101 /* 102 * Delete N characters, forward or backward 103 */ 104 #define DEL_FWD 0 105 #define DEL_BWD 1 106 static void 107 db_delete(int n, int bwd) 108 { 109 char *p; 110 111 if (bwd) { 112 db_lc -= n; 113 db_putnchars(BACKUP, n); 114 } 115 for (p = db_lc; p < db_le-n; p++) { 116 *p = *(p+n); 117 cnputc(*p); 118 } 119 db_putnchars(BLANK, n); 120 db_putnchars(BACKUP, db_le - db_lc); 121 db_le -= n; 122 } 123 124 static void 125 db_delete_line(void) 126 { 127 128 db_delete(db_le - db_lc, DEL_FWD); 129 db_delete(db_lc - db_lbuf_start, DEL_BWD); 130 db_le = db_lc = db_lbuf_start; 131 } 132 133 #if DDB_HISTORY_SIZE != 0 134 #define INC_DB_CURR() \ 135 do { \ 136 db_history_curr++; \ 137 if (db_history_curr > db_history + db_history_size - 1) \ 138 db_history_curr = db_history; \ 139 } while (/*CONSTCOND*/ 0) 140 #define DEC_DB_CURR() \ 141 do { \ 142 db_history_curr--; \ 143 if (db_history_curr < db_history) \ 144 db_history_curr = db_history + \ 145 db_history_size - 1; \ 146 } while (/*CONSTCOND*/ 0) 147 #endif 148 149 /* returns true at end-of-line */ 150 static int 151 db_inputchar(int c) 152 { 153 switch (c) { 154 case CTRL('b'): 155 /* back up one character */ 156 if (db_lc > db_lbuf_start) { 157 cnputc(BACKUP); 158 db_lc--; 159 } 160 break; 161 case CTRL('f'): 162 /* forward one character */ 163 if (db_lc < db_le) { 164 cnputc(*db_lc); 165 db_lc++; 166 } 167 break; 168 case CTRL('a'): 169 /* beginning of line */ 170 while (db_lc > db_lbuf_start) { 171 cnputc(BACKUP); 172 db_lc--; 173 } 174 break; 175 case CTRL('e'): 176 /* end of line */ 177 while (db_lc < db_le) { 178 cnputc(*db_lc); 179 db_lc++; 180 } 181 break; 182 case CTRL('h'): 183 case 0177: 184 /* erase previous character */ 185 if (db_lc > db_lbuf_start) 186 db_delete(1, DEL_BWD); 187 break; 188 case CTRL('d'): 189 /* erase next character */ 190 if (db_lc < db_le) 191 db_delete(1, DEL_FWD); 192 break; 193 case CTRL('k'): 194 /* delete to end of line */ 195 if (db_lc < db_le) 196 db_delete(db_le - db_lc, DEL_FWD); 197 break; 198 case CTRL('u'): 199 /* delete line */ 200 db_delete_line(); 201 break; 202 case CTRL('t'): 203 /* twiddle last 2 characters */ 204 if (db_lc >= db_lbuf_start + 1) { 205 if (db_lc < db_le) { 206 c = db_lc[-1]; 207 db_lc[-1] = db_lc[0]; 208 db_lc[0] = c; 209 cnputc(BACKUP); 210 cnputc(db_lc[-1]); 211 cnputc(db_lc[0]); 212 db_lc++; 213 } else 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 } 223 break; 224 #if DDB_HISTORY_SIZE != 0 225 case CTRL('p'): 226 DEC_DB_CURR(); 227 while (db_history_curr != db_history_last) { 228 DEC_DB_CURR(); 229 if (*db_history_curr == '\0') 230 break; 231 } 232 db_delete_line(); 233 if (db_history_curr == db_history_last) { 234 INC_DB_CURR(); 235 db_le = db_lc = db_lbuf_start; 236 } else { 237 char *p; 238 INC_DB_CURR(); 239 for (p = db_history_curr, db_le = db_lbuf_start; 240 *p; ) { 241 *db_le++ = *p++; 242 if (p == db_history + db_history_size) { 243 p = db_history; 244 } 245 } 246 db_lc = db_le; 247 } 248 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 249 break; 250 case CTRL('n'): 251 while (db_history_curr != db_history_last) { 252 if (*db_history_curr == '\0') 253 break; 254 INC_DB_CURR(); 255 } 256 if (db_history_curr != db_history_last) { 257 INC_DB_CURR(); 258 db_delete_line(); 259 if (db_history_curr != db_history_last) { 260 char *p; 261 for (p = db_history_curr, 262 db_le = db_lbuf_start; *p;) { 263 *db_le++ = *p++; 264 if (p == db_history + 265 db_history_size) { 266 p = db_history; 267 } 268 } 269 db_lc = db_le; 270 } 271 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 272 } 273 break; 274 #endif 275 case CTRL('r'): 276 db_putstring("^R\n", 3); 277 if (db_le > db_lbuf_start) { 278 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 279 db_putnchars(BACKUP, db_le - db_lc); 280 } 281 break; 282 case '\n': 283 case '\r': 284 #if DDB_HISTORY_SIZE != 0 285 /* Check if it same than previous line */ 286 if (db_history_curr == db_history_prev) { 287 char *pp, *pc; 288 289 /* Is it unmodified */ 290 for (pp = db_history_prev, pc = db_lbuf_start; 291 pc != db_le && *pp; pp++, pc++) { 292 if (*pp != *pc) 293 break; 294 if (++pp == db_history + db_history_size) { 295 pp = db_history; 296 } 297 if (++pc == db_history + db_history_size) { 298 pc = db_history; 299 } 300 } 301 if (!*pp && pc == db_le) { 302 /* Repeted previous line, not saved */ 303 db_history_curr = db_history_last; 304 *db_le++ = c; 305 return (true); 306 } 307 } 308 if (db_le != db_lbuf_start) { 309 char *p; 310 db_history_prev = db_history_last; 311 for (p = db_lbuf_start; p != db_le; p++) { 312 *db_history_last++ = *p; 313 if (db_history_last == db_history + 314 db_history_size) { 315 db_history_last = db_history; 316 } 317 } 318 *db_history_last++ = '\0'; 319 } 320 db_history_curr = db_history_last; 321 #endif 322 *db_le++ = c; 323 return (1); 324 default: 325 if (db_le == db_lbuf_end) { 326 cnputc('\007'); 327 } 328 else if (c >= ' ' && c <= '~') { 329 char *p; 330 331 for (p = db_le; p > db_lc; p--) 332 *p = *(p-1); 333 *db_lc++ = c; 334 db_le++; 335 cnputc(c); 336 db_putstring(db_lc, db_le - db_lc); 337 db_putnchars(BACKUP, db_le - db_lc); 338 } 339 break; 340 } 341 return (0); 342 } 343 344 int 345 db_readline(char *lstart, int lsize) 346 { 347 348 # ifdef MULTIPROCESSOR 349 db_printf("db{%ld}> ", (long)cpu_number()); 350 # else 351 db_printf("db> "); 352 # endif 353 db_force_whitespace(); /* synch output position */ 354 355 db_lbuf_start = lstart; 356 db_lbuf_end = lstart + lsize; 357 db_lc = lstart; 358 db_le = lstart; 359 360 while (!db_inputchar(cngetc())) 361 continue; 362 363 db_putchar('\n'); /* synch output position */ 364 365 *db_le = 0; 366 return (db_le - db_lbuf_start); 367 } 368 369 void 370 db_check_interrupt(void) 371 { 372 int c; 373 374 c = cnmaygetc(); 375 switch (c) { 376 case -1: /* no character */ 377 return; 378 379 case CTRL('c'): 380 db_error((char *)0); 381 /*NOTREACHED*/ 382 383 case CTRL('s'): 384 do { 385 c = cnmaygetc(); 386 if (c == CTRL('c')) { 387 db_error((char *)0); 388 /*NOTREACHED*/ 389 } 390 } while (c != CTRL('q')); 391 break; 392 393 default: 394 /* drop on floor */ 395 break; 396 } 397 } 398 399 static int 400 cnmaygetc(void) 401 { 402 403 return (-1); 404 } 405