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