db_input.c revision 90528
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 * $FreeBSD: head/sys/ddb/db_input.c 90528 2002-02-11 14:14:42Z yar $ 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/systm.h> 36#include <sys/cons.h> 37 38#include <ddb/ddb.h> 39#include <ddb/db_output.h> 40 41/* 42 * Character input and editing. 43 */ 44 45/* 46 * We don't track output position while editing input, 47 * since input always ends with a new-line. We just 48 * reset the line position at the end. 49 */ 50static char * db_lbuf_start; /* start of input line buffer */ 51static char * db_lbuf_end; /* end of input line buffer */ 52static char * db_lc; /* current character */ 53static char * db_le; /* one past last character */ 54 55/* 56 * Simple input line history support. 57 */ 58static char db_lhistory[2048]; 59static int db_lhistlsize, db_lhistidx, db_lhistcur; 60static int db_lhist_nlines; 61 62#define CTRL(c) ((c) & 0x1f) 63#define BLANK ' ' 64#define BACKUP '\b' 65 66static int cnmaygetc __P((void)); 67static void db_delete __P((int n, int bwd)); 68static int db_inputchar __P((int c)); 69static void db_putnchars __P((int c, int count)); 70static void db_putstring __P((char *s, int count)); 71 72void 73db_putstring(s, count) 74 char *s; 75 int count; 76{ 77 while (--count >= 0) 78 cnputc(*s++); 79} 80 81void 82db_putnchars(c, count) 83 int c; 84 int count; 85{ 86 while (--count >= 0) 87 cnputc(c); 88} 89 90/* 91 * Delete N characters, forward or backward 92 */ 93#define DEL_FWD 0 94#define DEL_BWD 1 95void 96db_delete(n, bwd) 97 int n; 98 int bwd; 99{ 100 register char *p; 101 102 if (bwd) { 103 db_lc -= n; 104 db_putnchars(BACKUP, n); 105 } 106 for (p = db_lc; p < db_le-n; p++) { 107 *p = *(p+n); 108 cnputc(*p); 109 } 110 db_putnchars(BLANK, n); 111 db_putnchars(BACKUP, db_le - db_lc); 112 db_le -= n; 113} 114 115/* returns TRUE at end-of-line */ 116int 117db_inputchar(c) 118 int c; 119{ 120 static int escstate; 121 122 if (escstate == 1) { 123 /* ESC seen, look for [ or O */ 124 if (c == '[' || c == 'O') 125 escstate++; 126 else 127 escstate = 0; /* re-init state machine */ 128 return (0); 129 } else if (escstate == 2) { 130 escstate = 0; 131 /* 132 * If a valid cursor key has been found, translate 133 * into an emacs-style control key, and fall through. 134 * Otherwise, drop off. 135 */ 136 switch (c) { 137 case 'A': /* up */ 138 c = CTRL('p'); 139 break; 140 case 'B': /* down */ 141 c = CTRL('n'); 142 break; 143 case 'C': /* right */ 144 c = CTRL('f'); 145 break; 146 case 'D': /* left */ 147 c = CTRL('b'); 148 break; 149 default: 150 return (0); 151 } 152 } 153 154 switch (c) { 155 case CTRL('['): 156 escstate = 1; 157 break; 158 case CTRL('b'): 159 /* back up one character */ 160 if (db_lc > db_lbuf_start) { 161 cnputc(BACKUP); 162 db_lc--; 163 } 164 break; 165 case CTRL('f'): 166 /* forward one character */ 167 if (db_lc < db_le) { 168 cnputc(*db_lc); 169 db_lc++; 170 } 171 break; 172 case CTRL('a'): 173 /* beginning of line */ 174 while (db_lc > db_lbuf_start) { 175 cnputc(BACKUP); 176 db_lc--; 177 } 178 break; 179 case CTRL('e'): 180 /* end of line */ 181 while (db_lc < db_le) { 182 cnputc(*db_lc); 183 db_lc++; 184 } 185 break; 186 case CTRL('h'): 187 case 0177: 188 /* erase previous character */ 189 if (db_lc > db_lbuf_start) 190 db_delete(1, DEL_BWD); 191 break; 192 case CTRL('u'): 193 /* delete to beginning of line */ 194 if (db_lc > db_lbuf_start) 195 db_delete(db_lc - db_lbuf_start, DEL_BWD); 196 break; 197 case CTRL('d'): 198 /* erase next character */ 199 if (db_lc < db_le) 200 db_delete(1, DEL_FWD); 201 break; 202 case CTRL('k'): 203 /* delete to end of line */ 204 if (db_lc < db_le) 205 db_delete(db_le - db_lc, DEL_FWD); 206 break; 207 case CTRL('t'): 208 /* twiddle last 2 characters */ 209 if (db_lc >= db_lbuf_start + 2) { 210 c = db_lc[-2]; 211 db_lc[-2] = db_lc[-1]; 212 db_lc[-1] = c; 213 cnputc(BACKUP); 214 cnputc(BACKUP); 215 cnputc(db_lc[-2]); 216 cnputc(db_lc[-1]); 217 } 218 break; 219 case CTRL('r'): 220 db_putstring("^R\n", 3); 221 redraw: 222 if (db_le > db_lbuf_start) { 223 db_putstring(db_lbuf_start, db_le - db_lbuf_start); 224 db_putnchars(BACKUP, db_le - db_lc); 225 } 226 break; 227 case CTRL('p'): 228 /* Make previous history line the active one. */ 229 if (db_lhistcur >= 0) { 230 bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 231 db_lbuf_start, db_lhistlsize); 232 db_lhistcur--; 233 goto hist_redraw; 234 } 235 break; 236 case CTRL('n'): 237 /* Make next history line the active one. */ 238 if (db_lhistcur < db_lhistidx - 1) { 239 db_lhistcur += 2; 240 bcopy(db_lhistory + db_lhistcur * db_lhistlsize, 241 db_lbuf_start, db_lhistlsize); 242 } else { 243 /* 244 * ^N through tail of history, reset the 245 * buffer to zero length. 246 */ 247 *db_lbuf_start = '\0'; 248 db_lhistcur = db_lhistidx; 249 } 250 251 hist_redraw: 252 db_putnchars(BACKUP, db_le - db_lbuf_start); 253 db_putnchars(BLANK, db_le - db_lbuf_start); 254 db_putnchars(BACKUP, db_le - db_lbuf_start); 255 db_le = index(db_lbuf_start, '\0'); 256 if (db_le[-1] == '\r' || db_le[-1] == '\n') 257 *--db_le = '\0'; 258 db_lc = db_le; 259 goto redraw; 260 261 case -1: 262 /* 263 * eek! the console returned eof. 264 * probably that means we HAVE no console.. we should try bail 265 * XXX 266 */ 267 c = '\r'; 268 case '\n': 269 case '\r': 270 *db_le++ = c; 271 return (1); 272 default: 273 if (db_le == db_lbuf_end) { 274 cnputc('\007'); 275 } 276 else if (c >= ' ' && c <= '~') { 277 register char *p; 278 279 for (p = db_le; p > db_lc; p--) 280 *p = *(p-1); 281 *db_lc++ = c; 282 db_le++; 283 cnputc(c); 284 db_putstring(db_lc, db_le - db_lc); 285 db_putnchars(BACKUP, db_le - db_lc); 286 } 287 break; 288 } 289 return (0); 290} 291 292int 293cnmaygetc() 294{ 295 return (-1); 296} 297 298int 299db_readline(lstart, lsize) 300 char * lstart; 301 int lsize; 302{ 303 if (lsize != db_lhistlsize) { 304 /* 305 * (Re)initialize input line history. Throw away any 306 * existing history. 307 */ 308 db_lhist_nlines = sizeof(db_lhistory) / lsize; 309 db_lhistlsize = lsize; 310 db_lhistidx = -1; 311 } 312 db_lhistcur = db_lhistidx; 313 314 db_force_whitespace(); /* synch output position */ 315 316 db_lbuf_start = lstart; 317 db_lbuf_end = lstart + lsize; 318 db_lc = lstart; 319 db_le = lstart; 320 321 while (!db_inputchar(cngetc())) 322 continue; 323 324 db_printf("\n"); /* synch output position */ 325 *db_le = 0; 326 327 if (db_le - db_lbuf_start > 1) { 328 /* Maintain input line history for non-empty lines. */ 329 if (++db_lhistidx == db_lhist_nlines) { 330 /* Rotate history. */ 331 ovbcopy(db_lhistory + db_lhistlsize, db_lhistory, 332 db_lhistlsize * (db_lhist_nlines - 1)); 333 db_lhistidx--; 334 } 335 bcopy(lstart, db_lhistory + db_lhistidx * db_lhistlsize, 336 db_lhistlsize); 337 } 338 339 return (db_le - db_lbuf_start); 340} 341 342void 343db_check_interrupt() 344{ 345 register int c; 346 347 c = cnmaygetc(); 348 switch (c) { 349 case -1: /* no character */ 350 return; 351 352 case CTRL('c'): 353 db_error((char *)0); 354 /*NOTREACHED*/ 355 356 case CTRL('s'): 357 do { 358 c = cnmaygetc(); 359 if (c == CTRL('c')) 360 db_error((char *)0); 361 } while (c != CTRL('q')); 362 break; 363 364 default: 365 /* drop on floor */ 366 break; 367 } 368} 369