1 /* $NetBSD: read.c,v 1.20 2001/09/27 19:29:50 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 <sys/cdefs.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.20 2001/09/27 19:29:50 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 "sys.h" 53 #include <errno.h> 54 #include <unistd.h> 55 #include <stdlib.h> 56 #include "el.h" 57 58 #define OKCMD -1 59 60 private int read__fixio(int, int); 61 private int read_preread(EditLine *); 62 private int read_char(EditLine *, char *); 63 private int read_getcmd(EditLine *, el_action_t *, char *); 64 65 /* read_init(): 66 * Initialize the read stuff 67 */ 68 protected int 69 read_init(EditLine *el) 70 { 71 /* builtin read_char */ 72 el->el_read.read_char = read_char; 73 return 0; 74 } 75 76 77 /* el_read_setfn(): 78 * Set the read char function to the one provided. 79 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one. 80 */ 81 protected int 82 el_read_setfn(EditLine *el, el_rfunc_t rc) 83 { 84 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc; 85 return 0; 86 } 87 88 89 /* el_read_getfn(): 90 * return the current read char function, or EL_BUILTIN_GETCFN 91 * if it is the default one 92 */ 93 protected el_rfunc_t 94 el_read_getfn(EditLine *el) 95 { 96 return (el->el_read.read_char == read_char) ? 97 EL_BUILTIN_GETCFN : el->el_read.read_char; 98 } 99 100 101 #ifdef DEBUG_EDIT 102 private void 103 read_debug(EditLine *el) 104 { 105 106 if (el->el_line.cursor > el->el_line.lastchar) 107 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n"); 108 if (el->el_line.cursor < el->el_line.buffer) 109 (void) fprintf(el->el_errfile, "cursor < buffer\r\n"); 110 if (el->el_line.cursor > el->el_line.limit) 111 (void) fprintf(el->el_errfile, "cursor > limit\r\n"); 112 if (el->el_line.lastchar > el->el_line.limit) 113 (void) fprintf(el->el_errfile, "lastchar > limit\r\n"); 114 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2]) 115 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n"); 116 } 117 #endif /* DEBUG_EDIT */ 118 119 120 /* read__fixio(): 121 * Try to recover from a read error 122 */ 123 /* ARGSUSED */ 124 private int 125 read__fixio(int fd, int e) 126 { 127 128 switch (e) { 129 case -1: /* Make sure that the code is reachable */ 130 131 #ifdef EWOULDBLOCK 132 case EWOULDBLOCK: 133 #ifndef TRY_AGAIN 134 #define TRY_AGAIN 135 #endif 136 #endif /* EWOULDBLOCK */ 137 138 #if defined(POSIX) && defined(EAGAIN) 139 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 140 case EAGAIN: 141 #ifndef TRY_AGAIN 142 #define TRY_AGAIN 143 #endif 144 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */ 145 #endif /* POSIX && EAGAIN */ 146 147 e = 0; 148 #ifdef TRY_AGAIN 149 #if defined(F_SETFL) && defined(O_NDELAY) 150 if ((e = fcntl(fd, F_GETFL, 0)) == -1) 151 return (-1); 152 153 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1) 154 return (-1); 155 else 156 e = 1; 157 #endif /* F_SETFL && O_NDELAY */ 158 159 #ifdef FIONBIO 160 { 161 int zero = 0; 162 163 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1) 164 return (-1); 165 else 166 e = 1; 167 } 168 #endif /* FIONBIO */ 169 170 #endif /* TRY_AGAIN */ 171 return (e ? 0 : -1); 172 173 case EINTR: 174 return (0); 175 176 default: 177 return (-1); 178 } 179 } 180 181 182 /* read_preread(): 183 * Try to read the stuff in the input queue; 184 */ 185 private int 186 read_preread(EditLine *el) 187 { 188 int chrs = 0; 189 190 if (el->el_chared.c_macro.nline) { 191 el_free((ptr_t) el->el_chared.c_macro.nline); 192 el->el_chared.c_macro.nline = NULL; 193 } 194 if (el->el_tty.t_mode == ED_IO) 195 return (0); 196 197 #ifdef FIONREAD 198 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 199 if (chrs > 0) { 200 char buf[EL_BUFSIZ]; 201 202 chrs = read(el->el_infd, buf, 203 (size_t) MIN(chrs, EL_BUFSIZ - 1)); 204 if (chrs > 0) { 205 buf[chrs] = '\0'; 206 el->el_chared.c_macro.nline = strdup(buf); 207 el_push(el, el->el_chared.c_macro.nline); 208 } 209 } 210 #endif /* FIONREAD */ 211 212 return (chrs > 0); 213 } 214 215 216 /* el_push(): 217 * Push a macro 218 */ 219 public void 220 el_push(EditLine *el, const char *str) 221 { 222 c_macro_t *ma = &el->el_chared.c_macro; 223 224 if (str != NULL && ma->level + 1 < EL_MAXMACRO) { 225 ma->level++; 226 /* LINTED const cast */ 227 ma->macro[ma->level] = (char *) str; 228 } else { 229 term_beep(el); 230 term__flush(); 231 } 232 } 233 234 235 /* read_getcmd(): 236 * Return next command from the input stream. 237 */ 238 private int 239 read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch) 240 { 241 el_action_t cmd = ED_UNASSIGNED; 242 int num; 243 244 while (cmd == ED_UNASSIGNED || cmd == ED_SEQUENCE_LEAD_IN) { 245 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */ 246 return (num); 247 248 #ifdef KANJI 249 if ((*ch & 0200)) { 250 el->el_state.metanext = 0; 251 cmd = CcViMap[' ']; 252 break; 253 } else 254 #endif /* KANJI */ 255 256 if (el->el_state.metanext) { 257 el->el_state.metanext = 0; 258 *ch |= 0200; 259 } 260 cmd = el->el_map.current[(unsigned char) *ch]; 261 if (cmd == ED_SEQUENCE_LEAD_IN) { 262 key_value_t val; 263 switch (key_get(el, ch, &val)) { 264 case XK_CMD: 265 cmd = val.cmd; 266 break; 267 case XK_STR: 268 el_push(el, val.str); 269 break; 270 #ifdef notyet 271 case XK_EXE: 272 /* XXX: In the future to run a user function */ 273 RunCommand(val.str); 274 break; 275 #endif 276 default: 277 EL_ABORT((el->el_errfile, "Bad XK_ type \n")); 278 break; 279 } 280 } 281 if (el->el_map.alt == NULL) 282 el->el_map.current = el->el_map.key; 283 } 284 *cmdnum = cmd; 285 return (OKCMD); 286 } 287 288 289 /* read_char(): 290 * Read a character from the tty. 291 */ 292 private int 293 read_char(EditLine *el, char *cp) 294 { 295 int num_read; 296 int tried = 0; 297 298 while ((num_read = read(el->el_infd, cp, 1)) == -1) 299 if (!tried && read__fixio(el->el_infd, errno) == 0) 300 tried = 1; 301 else { 302 *cp = '\0'; 303 return (-1); 304 } 305 306 return (num_read); 307 } 308 309 310 /* el_getc(): 311 * Read a character 312 */ 313 public int 314 el_getc(EditLine *el, char *cp) 315 { 316 int num_read; 317 c_macro_t *ma = &el->el_chared.c_macro; 318 319 term__flush(); 320 for (;;) { 321 if (ma->level < 0) { 322 if (!read_preread(el)) 323 break; 324 } 325 if (ma->level < 0) 326 break; 327 328 if (*ma->macro[ma->level] == 0) { 329 ma->level--; 330 continue; 331 } 332 *cp = *ma->macro[ma->level]++ & 0377; 333 if (*ma->macro[ma->level] == 0) { /* Needed for QuoteMode 334 * On */ 335 ma->level--; 336 } 337 return (1); 338 } 339 340 #ifdef DEBUG_READ 341 (void) fprintf(el->el_errfile, "Turning raw mode on\n"); 342 #endif /* DEBUG_READ */ 343 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */ 344 return (0); 345 346 #ifdef DEBUG_READ 347 (void) fprintf(el->el_errfile, "Reading a character\n"); 348 #endif /* DEBUG_READ */ 349 num_read = (*el->el_read.read_char)(el, cp); 350 #ifdef DEBUG_READ 351 (void) fprintf(el->el_errfile, "Got it %c\n", *cp); 352 #endif /* DEBUG_READ */ 353 return (num_read); 354 } 355 356 357 public const char * 358 el_gets(EditLine *el, int *nread) 359 { 360 int retval; 361 el_action_t cmdnum = 0; 362 int num; /* how many chars we have read at NL */ 363 char ch; 364 #ifdef FIONREAD 365 c_macro_t *ma = &el->el_chared.c_macro; 366 #endif /* FIONREAD */ 367 368 if (el->el_flags & HANDLE_SIGNALS) 369 sig_set(el); 370 371 if (el->el_flags & NO_TTY) { 372 char *cp = el->el_line.buffer; 373 size_t idx; 374 375 while ((*el->el_read.read_char)(el, cp) == 1) { 376 /* make sure there is space for next character */ 377 if (cp + 1 >= el->el_line.limit) { 378 idx = (cp - el->el_line.buffer); 379 if (!ch_enlargebufs(el, 2)) 380 break; 381 cp = &el->el_line.buffer[idx]; 382 } 383 cp++; 384 if (cp[-1] == '\r' || cp[-1] == '\n') 385 break; 386 } 387 388 el->el_line.cursor = el->el_line.lastchar = cp; 389 *cp = '\0'; 390 if (nread) 391 *nread = el->el_line.cursor - el->el_line.buffer; 392 return (el->el_line.buffer); 393 } 394 re_clear_display(el); /* reset the display stuff */ 395 ch_reset(el); 396 397 #ifdef FIONREAD 398 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 399 long chrs = 0; 400 401 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs); 402 if (chrs == 0) { 403 if (tty_rawmode(el) < 0) { 404 if (nread) 405 *nread = 0; 406 return (NULL); 407 } 408 } 409 } 410 #endif /* FIONREAD */ 411 412 re_refresh(el); /* print the prompt */ 413 414 if (el->el_flags & EDIT_DISABLED) { 415 char *cp = el->el_line.buffer; 416 size_t idx; 417 418 term__flush(); 419 420 while ((*el->el_read.read_char)(el, cp) == 1) { 421 /* make sure there is space next character */ 422 if (cp + 1 >= el->el_line.limit) { 423 idx = (cp - el->el_line.buffer); 424 if (!ch_enlargebufs(el, 2)) 425 break; 426 cp = &el->el_line.buffer[idx]; 427 } 428 cp++; 429 if (cp[-1] == '\r' || cp[-1] == '\n') 430 break; 431 } 432 433 el->el_line.cursor = el->el_line.lastchar = cp; 434 *cp = '\0'; 435 if (nread) 436 *nread = el->el_line.cursor - el->el_line.buffer; 437 return (el->el_line.buffer); 438 } 439 for (num = OKCMD; num == OKCMD;) { /* while still editing this 440 * line */ 441 #ifdef DEBUG_EDIT 442 read_debug(el); 443 #endif /* DEBUG_EDIT */ 444 /* if EOF or error */ 445 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 446 #ifdef DEBUG_READ 447 (void) fprintf(el->el_errfile, 448 "Returning from el_gets %d\n", num); 449 #endif /* DEBUG_READ */ 450 break; 451 } 452 if ((int) cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */ 453 #ifdef DEBUG_EDIT 454 (void) fprintf(el->el_errfile, 455 "ERROR: illegal command from key 0%o\r\n", ch); 456 #endif /* DEBUG_EDIT */ 457 continue; /* try again */ 458 } 459 /* now do the real command */ 460 #ifdef DEBUG_READ 461 { 462 el_bindings_t *b; 463 for (b = el->el_map.help; b->name; b++) 464 if (b->func == cmdnum) 465 break; 466 if (b->name) 467 (void) fprintf(el->el_errfile, 468 "Executing %s\n", b->name); 469 else 470 (void) fprintf(el->el_errfile, 471 "Error command = %d\n", cmdnum); 472 } 473 #endif /* DEBUG_READ */ 474 retval = (*el->el_map.func[cmdnum]) (el, ch); 475 476 /* save the last command here */ 477 el->el_state.lastcmd = cmdnum; 478 479 /* use any return value */ 480 switch (retval) { 481 case CC_CURSOR: 482 el->el_state.argument = 1; 483 el->el_state.doingarg = 0; 484 re_refresh_cursor(el); 485 break; 486 487 case CC_REDISPLAY: 488 re_clear_lines(el); 489 re_clear_display(el); 490 /* FALLTHROUGH */ 491 492 case CC_REFRESH: 493 el->el_state.argument = 1; 494 el->el_state.doingarg = 0; 495 re_refresh(el); 496 break; 497 498 case CC_REFRESH_BEEP: 499 el->el_state.argument = 1; 500 el->el_state.doingarg = 0; 501 re_refresh(el); 502 term_beep(el); 503 break; 504 505 case CC_NORM: /* normal char */ 506 el->el_state.argument = 1; 507 el->el_state.doingarg = 0; 508 break; 509 510 case CC_ARGHACK: /* Suggested by Rich Salz */ 511 /* <rsalz@pineapple.bbn.com> */ 512 break; /* keep going... */ 513 514 case CC_EOF: /* end of file typed */ 515 num = 0; 516 break; 517 518 case CC_NEWLINE: /* normal end of line */ 519 num = el->el_line.lastchar - el->el_line.buffer; 520 break; 521 522 case CC_FATAL: /* fatal error, reset to known state */ 523 #ifdef DEBUG_READ 524 (void) fprintf(el->el_errfile, 525 "*** editor fatal ERROR ***\r\n\n"); 526 #endif /* DEBUG_READ */ 527 /* put (real) cursor in a known place */ 528 re_clear_display(el); /* reset the display stuff */ 529 ch_reset(el); /* reset the input pointers */ 530 re_refresh(el); /* print the prompt again */ 531 el->el_state.argument = 1; 532 el->el_state.doingarg = 0; 533 break; 534 535 case CC_ERROR: 536 default: /* functions we don't know about */ 537 #ifdef DEBUG_READ 538 (void) fprintf(el->el_errfile, 539 "*** editor ERROR ***\r\n\n"); 540 #endif /* DEBUG_READ */ 541 el->el_state.argument = 1; 542 el->el_state.doingarg = 0; 543 term_beep(el); 544 term__flush(); 545 break; 546 } 547 } 548 549 /* make sure the tty is set up correctly */ 550 (void) tty_cookedmode(el); 551 term__flush(); /* flush any buffered output */ 552 if (el->el_flags & HANDLE_SIGNALS) 553 sig_clr(el); 554 if (nread) 555 *nread = num; 556 return (num ? el->el_line.buffer : NULL); 557 } 558