1 /* $NetBSD: read.c,v 1.25 2003/06/19 15:55:06 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Christos Zoulas of Cornell University. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include "config.h" 40 #if !defined(lint) && !defined(SCCSID) 41 #if 0 42 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 __RCSID("$NetBSD: read.c,v 1.25 2003/06/19 15:55:06 christos Exp $"); 45 #endif 46 #endif /* not lint && not SCCSID */ 47 48 /* 49 * read.c: Clean this junk up! This is horrible code. 50 * Terminal read functions 51 */ 52 #include <errno.h> 53 #include <unistd.h> 54 #include <stdlib.h> 55 #include "el.h" 56 57 #define OKCMD -1 58 59 private int read__fixio(int, int); 60 private int read_preread(EditLine *); 61 private int read_char(EditLine *, char *); 62 private int read_getcmd(EditLine *, el_action_t *, char *); 63 64 /* read_init(): 65 * Initialize the read stuff 66 */ 67 protected int 68 read_init(EditLine *el) 69 { 70 /* builtin read_char */ 71 el->el_read.read_char = read_char; 72 return 0; 73 } 74 75 76 /* el_read_setfn(): 77 * Set the read char function to the one provided. 78 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one. 79 */ 80 protected int 81 el_read_setfn(EditLine *el, el_rfunc_t rc) 82 { 83 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc; 84 return 0; 85 } 86 87 88 /* el_read_getfn(): 89 * return the current read char function, or EL_BUILTIN_GETCFN 90 * if it is the default one 91 */ 92 protected el_rfunc_t 93 el_read_getfn(EditLine *el) 94 { 95 return (el->el_read.read_char == read_char) ? 96 EL_BUILTIN_GETCFN : el->el_read.read_char; 97 } 98 99 100 #ifndef MIN 101 #define MIN(A,B) ((A) < (B) ? (A) : (B)) 102 #endif 103 104 #ifdef DEBUG_EDIT 105 private void 106 read_debug(EditLine *el) 107 { 108 109 if (el->el_line.cursor > el->el_line.lastchar) 110 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n"); 111 if (el->el_line.cursor < el->el_line.buffer) 112 (void) fprintf(el->el_errfile, "cursor < buffer\r\n"); 113 if (el->el_line.cursor > el->el_line.limit) 114 (void) fprintf(el->el_errfile, "cursor > limit\r\n"); 115 if (el->el_line.lastchar > el->el_line.limit) 116 (void) fprintf(el->el_errfile, "lastchar > limit\r\n"); 117 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2]) 118 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n"); 119 } 120 #endif /* DEBUG_EDIT */ 121 122 123 /* read__fixio(): 124 * Try to recover from a read error 125 */ 126 /* ARGSUSED */ 127 private int 128 read__fixio(int fd __attribute__((__unused__)), int e) 129 { 130 131 switch (e) { 132 case -1: /* Make sure that the code is reachable */ 133 134 #ifdef EWOULDBLOCK 135 case EWOULDBLOCK: 136 #ifndef TRY_AGAIN 137 #define TRY_AGAIN 138 #endif 139 #endif /* EWOULDBLOCK */ 140 141 #if defined(POSIX) && defined(EAGAIN) 142 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 143 case EAGAIN: 144 #ifndef TRY_AGAIN 145 #define TRY_AGAIN 146 #endif 147 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */ 148 #endif /* POSIX && EAGAIN */ 149 150 e = 0; 151 #ifdef TRY_AGAIN 152 #if defined(F_SETFL) && defined(O_NDELAY) 153 if ((e = fcntl(fd, F_GETFL, 0)) == -1) 154 return (-1); 155 156 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1) 157 return (-1); 158 else 159 e = 1; 160 #endif /* F_SETFL && O_NDELAY */ 161 162 #ifdef FIONBIO 163 { 164 int zero = 0; 165 166 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1) 167 return (-1); 168 else 169 e = 1; 170 } 171 #endif /* FIONBIO */ 172 173 #endif /* TRY_AGAIN */ 174 return (e ? 0 : -1); 175 176 case EINTR: 177 return (0); 178 179 default: 180 return (-1); 181 } 182 } 183 184 185 /* read_preread(): 186 * Try to read the stuff in the input queue; 187 */ 188 private int 189 read_preread(EditLine *el) 190 { 191 int chrs = 0; 192 193 if (el->el_chared.c_macro.nline) { 194 el_free((ptr_t) el->el_chared.c_macro.nline); 195 el->el_chared.c_macro.nline = NULL; 196 } 197 if (el->el_tty.t_mode == ED_IO) 198 return (0); 199 200 #ifdef FIONREAD 201 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 202 if (chrs > 0) { 203 char buf[EL_BUFSIZ]; 204 205 chrs = read(el->el_infd, buf, 206 (size_t) MIN(chrs, EL_BUFSIZ - 1)); 207 if (chrs > 0) { 208 buf[chrs] = '\0'; 209 el->el_chared.c_macro.nline = strdup(buf); 210 el_push(el, el->el_chared.c_macro.nline); 211 } 212 } 213 #endif /* FIONREAD */ 214 215 return (chrs > 0); 216 } 217 218 219 /* el_push(): 220 * Push a macro 221 */ 222 public void 223 el_push(EditLine *el, char *str) 224 { 225 c_macro_t *ma = &el->el_chared.c_macro; 226 227 if (str != NULL && ma->level + 1 < EL_MAXMACRO) { 228 ma->level++; 229 ma->macro[ma->level] = str; 230 } else { 231 term_beep(el); 232 term__flush(); 233 } 234 } 235 236 237 /* read_getcmd(): 238 * Return next command from the input stream. 239 */ 240 private int 241 read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch) 242 { 243 el_action_t cmd; 244 int num; 245 246 do { 247 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */ 248 return (num); 249 250 #ifdef KANJI 251 if ((*ch & 0200)) { 252 el->el_state.metanext = 0; 253 cmd = CcViMap[' ']; 254 break; 255 } else 256 #endif /* KANJI */ 257 258 if (el->el_state.metanext) { 259 el->el_state.metanext = 0; 260 *ch |= 0200; 261 } 262 cmd = el->el_map.current[(unsigned char) *ch]; 263 if (cmd == ED_SEQUENCE_LEAD_IN) { 264 key_value_t val; 265 switch (key_get(el, ch, &val)) { 266 case XK_CMD: 267 cmd = val.cmd; 268 break; 269 case XK_STR: 270 el_push(el, val.str); 271 break; 272 #ifdef notyet 273 case XK_EXE: 274 /* XXX: In the future to run a user function */ 275 RunCommand(val.str); 276 break; 277 #endif 278 default: 279 EL_ABORT((el->el_errfile, "Bad XK_ type \n")); 280 break; 281 } 282 } 283 if (el->el_map.alt == NULL) 284 el->el_map.current = el->el_map.key; 285 } while (cmd == ED_SEQUENCE_LEAD_IN); 286 *cmdnum = cmd; 287 return (OKCMD); 288 } 289 290 291 /* read_char(): 292 * Read a character from the tty. 293 */ 294 private int 295 read_char(EditLine *el, char *cp) 296 { 297 int num_read; 298 int tried = 0; 299 300 while ((num_read = read(el->el_infd, cp, 1)) == -1) 301 if (!tried && read__fixio(el->el_infd, errno) == 0) 302 tried = 1; 303 else { 304 *cp = '\0'; 305 return (-1); 306 } 307 308 return (num_read); 309 } 310 311 312 /* el_getc(): 313 * Read a character 314 */ 315 public int 316 el_getc(EditLine *el, char *cp) 317 { 318 int num_read; 319 c_macro_t *ma = &el->el_chared.c_macro; 320 321 term__flush(); 322 for (;;) { 323 if (ma->level < 0) { 324 if (!read_preread(el)) 325 break; 326 } 327 if (ma->level < 0) 328 break; 329 330 if (*ma->macro[ma->level] == 0) { 331 ma->level--; 332 continue; 333 } 334 *cp = *ma->macro[ma->level]++ & 0377; 335 if (*ma->macro[ma->level] == 0) { /* Needed for QuoteMode 336 * On */ 337 ma->level--; 338 } 339 return (1); 340 } 341 342 #ifdef DEBUG_READ 343 (void) fprintf(el->el_errfile, "Turning raw mode on\n"); 344 #endif /* DEBUG_READ */ 345 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */ 346 return (0); 347 348 #ifdef DEBUG_READ 349 (void) fprintf(el->el_errfile, "Reading a character\n"); 350 #endif /* DEBUG_READ */ 351 num_read = (*el->el_read.read_char)(el, cp); 352 #ifdef DEBUG_READ 353 (void) fprintf(el->el_errfile, "Got it %c\n", *cp); 354 #endif /* DEBUG_READ */ 355 return (num_read); 356 } 357 358 359 public const char * 360 el_gets(EditLine *el, int *nread) 361 { 362 int retval; 363 el_action_t cmdnum = 0; 364 int num; /* how many chars we have read at NL */ 365 char ch; 366 #ifdef FIONREAD 367 c_macro_t *ma = &el->el_chared.c_macro; 368 #endif /* FIONREAD */ 369 370 if (el->el_flags & HANDLE_SIGNALS) 371 sig_set(el); 372 373 if (el->el_flags & NO_TTY) { 374 char *cp = el->el_line.buffer; 375 size_t idx; 376 377 while ((*el->el_read.read_char)(el, cp) == 1) { 378 /* make sure there is space for next character */ 379 if (cp + 1 >= el->el_line.limit) { 380 idx = (cp - el->el_line.buffer); 381 if (!ch_enlargebufs(el, 2)) 382 break; 383 cp = &el->el_line.buffer[idx]; 384 } 385 cp++; 386 if (cp[-1] == '\r' || cp[-1] == '\n') 387 break; 388 } 389 390 el->el_line.cursor = el->el_line.lastchar = cp; 391 *cp = '\0'; 392 if (nread) 393 *nread = el->el_line.cursor - el->el_line.buffer; 394 return (el->el_line.buffer); 395 } 396 397 /* This is relatively cheap, and things go terribly wrong if 398 we have the wrong size. */ 399 el_resize(el); 400 401 re_clear_display(el); /* reset the display stuff */ 402 ch_reset(el); 403 404 #ifdef FIONREAD 405 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 406 long chrs = 0; 407 408 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 409 if (chrs == 0) { 410 if (tty_rawmode(el) < 0) { 411 if (nread) 412 *nread = 0; 413 return (NULL); 414 } 415 } 416 } 417 #endif /* FIONREAD */ 418 419 re_refresh(el); /* print the prompt */ 420 421 if (el->el_flags & EDIT_DISABLED) { 422 char *cp = el->el_line.buffer; 423 size_t idx; 424 425 term__flush(); 426 427 while ((*el->el_read.read_char)(el, cp) == 1) { 428 /* make sure there is space next character */ 429 if (cp + 1 >= el->el_line.limit) { 430 idx = (cp - el->el_line.buffer); 431 if (!ch_enlargebufs(el, 2)) 432 break; 433 cp = &el->el_line.buffer[idx]; 434 } 435 if (*cp == 4) /* ought to be stty eof */ 436 break; 437 cp++; 438 if (cp[-1] == '\r' || cp[-1] == '\n') 439 break; 440 } 441 442 el->el_line.cursor = el->el_line.lastchar = cp; 443 *cp = '\0'; 444 if (nread) 445 *nread = el->el_line.cursor - el->el_line.buffer; 446 return (el->el_line.buffer); 447 } 448 449 for (num = OKCMD; num == OKCMD;) { /* while still editing this 450 * line */ 451 #ifdef DEBUG_EDIT 452 read_debug(el); 453 #endif /* DEBUG_EDIT */ 454 /* if EOF or error */ 455 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 456 #ifdef DEBUG_READ 457 (void) fprintf(el->el_errfile, 458 "Returning from el_gets %d\n", num); 459 #endif /* DEBUG_READ */ 460 break; 461 } 462 if ((uint)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */ 463 #ifdef DEBUG_EDIT 464 (void) fprintf(el->el_errfile, 465 "ERROR: illegal command from key 0%o\r\n", ch); 466 #endif /* DEBUG_EDIT */ 467 continue; /* try again */ 468 } 469 /* now do the real command */ 470 #ifdef DEBUG_READ 471 { 472 el_bindings_t *b; 473 for (b = el->el_map.help; b->name; b++) 474 if (b->func == cmdnum) 475 break; 476 if (b->name) 477 (void) fprintf(el->el_errfile, 478 "Executing %s\n", b->name); 479 else 480 (void) fprintf(el->el_errfile, 481 "Error command = %d\n", cmdnum); 482 } 483 #endif /* DEBUG_READ */ 484 /* vi redo needs these way down the levels... */ 485 el->el_state.thiscmd = cmdnum; 486 el->el_state.thisch = ch; 487 if (el->el_map.type == MAP_VI && 488 el->el_map.current == el->el_map.key && 489 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) { 490 if (cmdnum == VI_DELETE_PREV_CHAR && 491 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf 492 && isprint(el->el_chared.c_redo.pos[-1])) 493 el->el_chared.c_redo.pos--; 494 else 495 *el->el_chared.c_redo.pos++ = ch; 496 } 497 retval = (*el->el_map.func[cmdnum]) (el, ch); 498 #ifdef DEBUG_READ 499 (void) fprintf(el->el_errfile, 500 "Returned state %d\n", retval ); 501 #endif /* DEBUG_READ */ 502 503 /* save the last command here */ 504 el->el_state.lastcmd = cmdnum; 505 506 /* use any return value */ 507 switch (retval) { 508 case CC_CURSOR: 509 re_refresh_cursor(el); 510 break; 511 512 case CC_REDISPLAY: 513 re_clear_lines(el); 514 re_clear_display(el); 515 /* FALLTHROUGH */ 516 517 case CC_REFRESH: 518 re_refresh(el); 519 break; 520 521 case CC_REFRESH_BEEP: 522 re_refresh(el); 523 term_beep(el); 524 break; 525 526 case CC_NORM: /* normal char */ 527 break; 528 529 case CC_ARGHACK: /* Suggested by Rich Salz */ 530 /* <rsalz@pineapple.bbn.com> */ 531 continue; /* keep going... */ 532 533 case CC_EOF: /* end of file typed */ 534 num = 0; 535 break; 536 537 case CC_NEWLINE: /* normal end of line */ 538 num = el->el_line.lastchar - el->el_line.buffer; 539 break; 540 541 case CC_FATAL: /* fatal error, reset to known state */ 542 #ifdef DEBUG_READ 543 (void) fprintf(el->el_errfile, 544 "*** editor fatal ERROR ***\r\n\n"); 545 #endif /* DEBUG_READ */ 546 /* put (real) cursor in a known place */ 547 re_clear_display(el); /* reset the display stuff */ 548 ch_reset(el); /* reset the input pointers */ 549 re_refresh(el); /* print the prompt again */ 550 break; 551 552 case CC_ERROR: 553 default: /* functions we don't know about */ 554 #ifdef DEBUG_READ 555 (void) fprintf(el->el_errfile, 556 "*** editor ERROR ***\r\n\n"); 557 #endif /* DEBUG_READ */ 558 term_beep(el); 559 term__flush(); 560 break; 561 } 562 el->el_state.argument = 1; 563 el->el_state.doingarg = 0; 564 el->el_chared.c_vcmd.action = NOP; 565 } 566 567 term__flush(); /* flush any buffered output */ 568 /* make sure the tty is set up correctly */ 569 (void) tty_cookedmode(el); 570 if (el->el_flags & HANDLE_SIGNALS) 571 sig_clr(el); 572 if (nread) 573 *nread = num; 574 return (num ? el->el_line.buffer : NULL); 575 } 576