db_input.c revision 1.8
1/* $NetBSD: db_input.c,v 1.8 1997/11/16 22:24:47 christos 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 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/param.h> 33#include <sys/proc.h> 34 35#include <machine/db_machdep.h> 36 37#include <ddb/db_output.h> 38#include <ddb/db_command.h> 39#include <ddb/db_sym.h> 40#include <ddb/db_extern.h> 41 42#include <dev/cons.h> 43 44#ifndef DB_HISTORY_SIZE 45#define DB_HISTORY_SIZE 0 46#endif /* DB_HISTORY_SIZE */ 47 48/* 49 * Character input and editing. 50 */ 51 52/* 53 * We don't track output position while editing input, 54 * since input always ends with a new-line. We just 55 * reset the line position at the end. 56 */ 57char * db_lbuf_start; /* start of input line buffer */ 58char * db_lbuf_end; /* end of input line buffer */ 59char * db_lc; /* current character */ 60char * db_le; /* one past last character */ 61#if DB_HISTORY_SIZE != 0 62char db_history[DB_HISTORY_SIZE]; /* start of history buffer */ 63int db_history_size = DB_HISTORY_SIZE;/* size of history buffer */ 64char * db_history_curr = db_history; /* start of current line */ 65char * db_history_last = db_history; /* start of last line */ 66char * db_history_prev = (char *) 0; /* start of previous line */ 67#endif 68 69 70#define CTRL(c) ((c) & 0x1f) 71#define isspace(c) ((c) == ' ' || (c) == '\t') 72#define BLANK ' ' 73#define BACKUP '\b' 74 75static int cnmaygetc __P((void)); 76 77void 78db_putstring(s, count) 79 char *s; 80 int count; 81{ 82 while (--count >= 0) 83 cnputc(*s++); 84} 85 86void 87db_putnchars(c, count) 88 int c; 89 int count; 90{ 91 while (--count >= 0) 92 cnputc(c); 93} 94 95/* 96 * Delete N characters, forward or backward 97 */ 98#define DEL_FWD 0 99#define DEL_BWD 1 100void 101db_delete(n, bwd) 102 int n; 103 int bwd; 104{ 105 register char *p; 106 107 if (bwd) { 108 db_lc -= n; 109 db_putnchars(BACKUP, n); 110 } 111 for (p = db_lc; p < db_le-n; p++) { 112 *p = *(p+n); 113 cnputc(*p); 114 } 115 db_putnchars(BLANK, n); 116 db_putnchars(BACKUP, db_le - db_lc); 117 db_le -= n; 118} 119 120void 121db_delete_line() 122{ 123 db_delete(db_le - db_lc, DEL_FWD); 124 db_delete(db_lc - db_lbuf_start, DEL_BWD); 125 db_le = db_lc = db_lbuf_start; 126} 127 128#if DB_HISTORY_SIZE != 0 129#define INC_DB_CURR() \ 130 do { \ 131 db_history_curr++; \ 132 if (db_history_curr > \ 133 db_history + db_history_size - 1) \ 134 db_history_curr = db_history; \ 135 } while (0) 136#define DEC_DB_CURR() \ 137 do { \ 138 db_history_curr--; \ 139 if (db_history_curr < db_history) \ 140 db_history_curr = db_history + \ 141 db_history_size - 1; \ 142 } while (0) 143#endif 144 145/* returns TRUE at end-of-line */ 146int 147db_inputchar(c) 148 int c; 149{ 150 switch (c) { 151 case CTRL('b'): 152 /* back up one character */ 153 if (db_lc > db_lbuf_start) { 154 cnputc(BACKUP); 155 db_lc--; 156 } 157 break; 158 case CTRL('f'): 159 /* forward one character */ 160 if (db_lc < db_le) { 161 cnputc(*db_lc); 162 db_lc++; 163 } 164 break; 165 case CTRL('a'): 166 /* beginning of line */ 167 while (db_lc > db_lbuf_start) { 168 cnputc(BACKUP); 169 db_lc--; 170 } 171 break; 172 case CTRL('e'): 173 /* end of line */ 174 while (db_lc < db_le) { 175 cnputc(*db_lc); 176 db_lc++; 177 } 178 break; 179 case CTRL('h'): 180 case 0177: 181 /* erase previous character */ 182 if (db_lc > db_lbuf_start) 183 db_delete(1, DEL_BWD); 184 break; 185 case CTRL('d'): 186 /* erase next character */ 187 if (db_lc < db_le) 188 db_delete(1, DEL_FWD); 189 break; 190 case CTRL('k'): 191 /* delete to end of line */ 192 if (db_lc < db_le) 193 db_delete(db_le - db_lc, DEL_FWD); 194 break; 195 case CTRL('u'): 196 /* delete line */ 197 db_delete_line(); 198 break; 199 case CTRL('t'): 200 /* twiddle last 2 characters */ 201 if (db_lc >= db_lbuf_start + 2) { 202 c = db_lc[-2]; 203 db_lc[-2] = db_lc[-1]; 204 db_lc[-1] = c; 205 cnputc(BACKUP); 206 cnputc(BACKUP); 207 cnputc(db_lc[-2]); 208 cnputc(db_lc[-1]); 209 } 210 break; 211#if DB_HISTORY_SIZE != 0 212 case CTRL('p'): 213 DEC_DB_CURR(); 214 while (db_history_curr != db_history_last) { 215 DEC_DB_CURR(); 216 if (*db_history_curr == '\0') 217 break; 218 } 219 db_delete_line(); 220 if (db_history_curr == db_history_last) { 221 INC_DB_CURR(); 222 db_le = db_lc = db_lbuf_start; 223 } else { 224 register char *p; 225 INC_DB_CURR(); 226 for (p = db_history_curr, db_le = db_lbuf_start; 227 *p; ) { 228 *db_le++ = *p++; 229 if (p == db_history + db_history_size) { 230 p = db_history; 231 } 232 } 233 db_lc = db_le; 234 } 235 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 236 break; 237 case CTRL('n'): 238 while (db_history_curr != db_history_last) { 239 if (*db_history_curr == '\0') 240 break; 241 INC_DB_CURR(); 242 } 243 if (db_history_curr != db_history_last) { 244 INC_DB_CURR(); 245 db_delete_line(); 246 if (db_history_curr != db_history_last) { 247 register char *p; 248 for (p = db_history_curr, 249 db_le = db_lbuf_start; *p;) { 250 *db_le++ = *p++; 251 if (p == db_history + 252 db_history_size) { 253 p = db_history; 254 } 255 } 256 db_lc = db_le; 257 } 258 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 259 } 260 break; 261#endif 262 case CTRL('r'): 263 db_putstring("^R\n", 3); 264 if (db_le > db_lbuf_start) { 265 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 266 db_putnchars(BACKUP, db_le - db_lc); 267 } 268 break; 269 case '\n': 270 case '\r': 271#if DB_HISTORY_SIZE != 0 272 /* Check if it same than previous line */ 273 if (db_history_curr == db_history_prev) { 274 register char *pp, *pc; 275 276 /* Is it unmodified */ 277 for (pp = db_history_prev, pc = db_lbuf_start; 278 pc != db_le && *pp; pp++, pc++) { 279 if (*pp != *pc) 280 break; 281 if (++pp == db_history + db_history_size) { 282 pp = db_history; 283 } 284 if (++pc == db_history + db_history_size) { 285 pc = db_history; 286 } 287 } 288 if (!*pp && pc == db_le) { 289 /* Repeted previous line, not saved */ 290 db_history_curr = db_history_last; 291 *db_le++ = c; 292 return (TRUE); 293 } 294 } 295 if (db_le != db_lbuf_start) { 296 register char *p; 297 db_history_prev = db_history_last; 298 for (p = db_lbuf_start; p != db_le; p++) { 299 *db_history_last++ = *p; 300 if (db_history_last == db_history + 301 db_history_size) { 302 db_history_last = db_history; 303 } 304 } 305 *db_history_last++ = '\0'; 306 } 307 db_history_curr = db_history_last; 308#endif 309 *db_le++ = c; 310 return (1); 311 default: 312 if (db_le == db_lbuf_end) { 313 cnputc('\007'); 314 } 315 else if (c >= ' ' && c <= '~') { 316 register char *p; 317 318 for (p = db_le; p > db_lc; p--) 319 *p = *(p-1); 320 *db_lc++ = c; 321 db_le++; 322 cnputc(c); 323 db_putstring(db_lc, db_le - db_lc); 324 db_putnchars(BACKUP, db_le - db_lc); 325 } 326 break; 327 } 328 return (0); 329} 330 331int 332db_readline(lstart, lsize) 333 char * lstart; 334 int lsize; 335{ 336 db_force_whitespace(); /* synch output position */ 337 338 db_lbuf_start = lstart; 339 db_lbuf_end = lstart + lsize; 340 db_lc = lstart; 341 db_le = lstart; 342 343 while (!db_inputchar(cngetc())) 344 continue; 345 346 db_putchar('\n'); /* synch output position */ 347 348 *db_le = 0; 349 return (db_le - db_lbuf_start); 350} 351 352void 353db_check_interrupt() 354{ 355 register int c; 356 357 c = cnmaygetc(); 358 switch (c) { 359 case -1: /* no character */ 360 return; 361 362 case CTRL('c'): 363 db_error((char *)0); 364 /*NOTREACHED*/ 365 366 case CTRL('s'): 367 do { 368 c = cnmaygetc(); 369 if (c == CTRL('c')) { 370 db_error((char *)0); 371 /*NOTREACHED*/ 372 } 373 } while (c != CTRL('q')); 374 break; 375 376 default: 377 /* drop on floor */ 378 break; 379 } 380} 381 382static int 383cnmaygetc () 384{ 385 return (-1); 386} 387