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