1 /* $OpenBSD: read.c,v 1.6 1997/08/20 03:30:13 millert Exp $ */ 2 /* $NetBSD: read.c,v 1.4 1997/04/11 17:52:47 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1992, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Christos Zoulas of Cornell University. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #if !defined(lint) && !defined(SCCSID) 41 #if 0 42 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; 43 #else 44 static char rcsid[] = "$OpenBSD: read.c,v 1.6 1997/08/20 03:30:13 millert 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 <sys/errno.h> 54 #include <unistd.h> 55 #include <stdlib.h> 56 extern int errno; 57 #include "el.h" 58 59 #define OKCMD -1 60 61 private int read__fixio __P((int, int)); 62 private int read_preread __P((EditLine *)); 63 private int read_getcmd __P((EditLine *, el_action_t *, char *)); 64 65 #ifdef DEBUG_EDIT 66 private void 67 read_debug(el) 68 EditLine *el; 69 { 70 71 if (el->el_line.cursor > el->el_line.lastchar) 72 (void)fprintf(el->el_errfile, "cursor > lastchar\r\n"); 73 if (el->el_line.cursor < el->el_line.buffer) 74 (void)fprintf(el->el_errfile, "cursor < buffer\r\n"); 75 if (el->el_line.cursor > el->el_line.limit) 76 (void)fprintf(el->el_errfile, "cursor > limit\r\n"); 77 if (el->el_line.lastchar > el->el_line.limit) 78 (void)fprintf(el->el_errfile, "lastchar > limit\r\n"); 79 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2]) 80 (void)fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n"); 81 } 82 #endif /* DEBUG_EDIT */ 83 84 /* read__fixio(): 85 * Try to recover from a read error 86 */ 87 private int 88 read__fixio(fd, e) 89 int fd, e; 90 { 91 switch (e) { 92 case -1: /* Make sure that the code is reachable */ 93 94 #ifdef EWOULDBLOCK 95 case EWOULDBLOCK: 96 # ifndef TRY_AGAIN 97 # define TRY_AGAIN 98 # endif 99 #endif /* EWOULDBLOCK */ 100 101 #if defined(POSIX) && defined(EAGAIN) 102 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN 103 case EAGAIN: 104 # ifndef TRY_AGAIN 105 # define TRY_AGAIN 106 # endif 107 # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */ 108 #endif /* POSIX && EAGAIN */ 109 110 e = 0; 111 #ifdef TRY_AGAIN 112 # if defined(F_SETFL) && defined(O_NDELAY) 113 if ((e = fcntl(fd, F_GETFL, 0)) == -1) 114 return -1; 115 116 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1) 117 return -1; 118 else 119 e = 1; 120 # endif /* F_SETFL && O_NDELAY */ 121 122 # ifdef FIONBIO 123 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1) 124 return -1; 125 else 126 e = 1; 127 # endif /* FIONBIO */ 128 129 #endif /* TRY_AGAIN */ 130 return e ? 0 : -1; 131 132 case EINTR: 133 return 0; 134 135 default: 136 return -1; 137 } 138 } 139 140 141 /* read_preread(): 142 * Try to read the stuff in the input queue; 143 */ 144 private int 145 read_preread(el) 146 EditLine *el; 147 { 148 int chrs = 0; 149 150 if (el->el_chared.c_macro.nline) { 151 el_free((ptr_t) el->el_chared.c_macro.nline); 152 el->el_chared.c_macro.nline = NULL; 153 } 154 155 if (el->el_tty.t_mode == ED_IO) 156 return 0; 157 158 #ifdef FIONREAD 159 (void)ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs); 160 if (chrs > 0) { 161 char buf[EL_BUFSIZ]; 162 163 chrs = read(el->el_infd, buf, (size_t) MIN(chrs, EL_BUFSIZ - 1)); 164 if (chrs > 0) { 165 buf[chrs] = '\0'; 166 el->el_chared.c_macro.nline = strdup(buf); 167 el_push(el, el->el_chared.c_macro.nline); 168 } 169 } 170 #endif /* FIONREAD */ 171 172 return chrs > 0; 173 } 174 175 176 /* el_push(): 177 * Push a macro 178 */ 179 public void 180 el_push(el, str) 181 EditLine *el; 182 const char *str; 183 { 184 c_macro_t *ma = &el->el_chared.c_macro; 185 186 if (str != NULL && ma->level + 1 < EL_MAXMACRO) { 187 ma->level++; 188 ma->macro[ma->level] = (char *) str; 189 } 190 else { 191 term_beep(el); 192 term__flush(); 193 } 194 } 195 196 197 /* read_getcmd(): 198 * Return next command from the input stream. 199 */ 200 private int 201 read_getcmd(el, cmdnum, ch) 202 EditLine *el; 203 el_action_t *cmdnum; 204 char *ch; 205 { 206 el_action_t cmd = 0; 207 int num; 208 209 while (cmd == 0 || cmd == ED_SEQUENCE_LEAD_IN) { 210 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */ 211 return num; 212 213 #ifdef KANJI 214 if ((*ch & 0200)) { 215 el->el_state.metanext = 0; 216 cmd = CcViMap[' ']; 217 break; 218 } 219 else 220 #endif /* KANJI */ 221 222 if (el->el_state.metanext) { 223 el->el_state.metanext = 0; 224 *ch |= 0200; 225 } 226 cmd = el->el_map.current[(unsigned char) *ch]; 227 if (cmd == ED_SEQUENCE_LEAD_IN) { 228 key_value_t val; 229 switch (key_get(el, ch, &val)) { 230 case XK_CMD: 231 cmd = val.cmd; 232 break; 233 case XK_STR: 234 el_push(el, val.str); 235 break; 236 #ifdef notyet 237 case XK_EXE: 238 /* XXX: In the future to run a user function */ 239 RunCommand(val.str); 240 break; 241 #endif 242 default: 243 abort(); 244 break; 245 } 246 } 247 if (el->el_map.alt == NULL) 248 el->el_map.current = el->el_map.key; 249 } 250 *cmdnum = cmd; 251 return OKCMD; 252 } 253 254 255 /* el_getc(): 256 * Read a character 257 */ 258 public int 259 el_getc(el, cp) 260 EditLine *el; 261 char *cp; 262 { 263 int num_read; 264 unsigned char tcp; 265 int tried = 0; 266 267 c_macro_t *ma = &el->el_chared.c_macro; 268 269 term__flush(); 270 for (;;) { 271 if (ma->level < 0) { 272 if (!read_preread(el)) 273 break; 274 } 275 if (ma->level < 0) 276 break; 277 278 if (*ma->macro[ma->level] == 0) { 279 ma->level--; 280 continue; 281 } 282 *cp = *ma->macro[ma->level]++ & 0377; 283 if (*ma->macro[ma->level] == 0) { /* Needed for QuoteMode On */ 284 ma->level--; 285 } 286 return 1; 287 } 288 289 #ifdef DEBUG_READ 290 (void)fprintf(el->el_errfile, "Turning raw mode on\n"); 291 #endif /* DEBUG_READ */ 292 if (tty_rawmode(el) < 0) /* make sure the tty is set up correctly */ 293 return 0; 294 295 #ifdef DEBUG_READ 296 (void)fprintf(el->el_errfile, "Reading a character\n"); 297 #endif /* DEBUG_READ */ 298 while ((num_read = read(el->el_infd, (char *) &tcp, 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 #ifdef DEBUG_READ 306 (void)fprintf(el->el_errfile, "Got it %c\n", tcp); 307 #endif /* DEBUG_READ */ 308 *cp = tcp; 309 return num_read; 310 } 311 312 313 314 public const char * 315 el_gets(el, nread) 316 EditLine *el; 317 int *nread; 318 { 319 int retval; 320 el_action_t cmdnum = 0; 321 int num; /* how many chars we have read at NL */ 322 char ch; 323 #ifdef FIONREAD 324 c_macro_t *ma = &el->el_chared.c_macro; 325 #endif /* FIONREAD */ 326 327 if (el->el_flags & HANDLE_SIGNALS) 328 sig_set(el); 329 330 re_clear_display(el); /* reset the display stuff */ 331 ch_reset(el); 332 333 #ifdef FIONREAD 334 if (el->el_tty.t_mode == EX_IO && ma->level < 0) { 335 int chrs = 0; 336 337 (void)ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs); 338 if (chrs == 0) { 339 if (tty_rawmode(el) < 0) { 340 if (nread) 341 *nread = 0; 342 return NULL; 343 } 344 } 345 } 346 #endif /* FIONREAD */ 347 348 re_refresh(el); /* print the prompt */ 349 350 for (num = OKCMD; num == OKCMD;) { /* while still editing this line */ 351 #ifdef DEBUG_EDIT 352 read_debug(el); 353 #endif /* DEBUG_EDIT */ 354 /* if EOF or error */ 355 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) { 356 #ifdef DEBUG_READ 357 (void)fprintf(el->el_errfile, "Returning from el_gets %d\n", num); 358 #endif /* DEBUG_READ */ 359 break; 360 } 361 362 if (cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */ 363 #ifdef DEBUG_EDIT 364 (void)fprintf(el->el_errfile, 365 "ERROR: illegal command from key 0%o\r\n", ch); 366 #endif /* DEBUG_EDIT */ 367 continue; /* try again */ 368 } 369 370 /* now do the real command */ 371 #ifdef DEBUG_READ 372 { 373 el_bindings_t *b; 374 for (b = el->el_map.help; b->name; b++) 375 if (b->func == cmdnum) 376 break; 377 if (b->name) 378 (void)fprintf(el->el_errfile, "Executing %s\n", b->name); 379 else 380 (void)fprintf(el->el_errfile, "Error command = %d\n", cmdnum); 381 } 382 #endif /* DEBUG_READ */ 383 retval = (*el->el_map.func[cmdnum])(el, ch); 384 385 /* save the last command here */ 386 el->el_state.lastcmd = cmdnum; 387 388 /* use any return value */ 389 switch (retval) { 390 case CC_CURSOR: 391 el->el_state.argument = 1; 392 el->el_state.doingarg = 0; 393 re_refresh_cursor(el); 394 break; 395 396 case CC_REDISPLAY: 397 re_clear_lines(el); 398 re_clear_display(el); 399 /* FALLTHROUGH */ 400 401 case CC_REFRESH: 402 el->el_state.argument = 1; 403 el->el_state.doingarg = 0; 404 re_refresh(el); 405 break; 406 407 case CC_NORM: /* normal char */ 408 el->el_state.argument = 1; 409 el->el_state.doingarg = 0; 410 break; 411 412 case CC_ARGHACK: /* Suggested by Rich Salz */ 413 /* <rsalz@pineapple.bbn.com> */ 414 break; /* keep going... */ 415 416 case CC_EOF: /* end of file typed */ 417 num = 0; 418 break; 419 420 case CC_NEWLINE: /* normal end of line */ 421 num = el->el_line.lastchar - el->el_line.buffer; 422 break; 423 424 case CC_FATAL: /* fatal error, reset to known state */ 425 #ifdef DEBUG_READ 426 (void)fprintf(el->el_errfile, "*** editor fatal ERROR ***\r\n\n"); 427 #endif /* DEBUG_READ */ 428 /* put (real) cursor in a known place */ 429 re_clear_display(el); /* reset the display stuff */ 430 ch_reset(el); /* reset the input pointers */ 431 re_refresh(el); /* print the prompt again */ 432 el->el_state.argument = 1; 433 el->el_state.doingarg = 0; 434 break; 435 436 case CC_ERROR: 437 default: /* functions we don't know about */ 438 #ifdef DEBUG_READ 439 (void)fprintf(el->el_errfile, "*** editor ERROR ***\r\n\n"); 440 #endif /* DEBUG_READ */ 441 el->el_state.argument = 1; 442 el->el_state.doingarg = 0; 443 term_beep(el); 444 term__flush(); 445 break; 446 } 447 } 448 449 (void)tty_cookedmode(el); /* make sure the tty is set up correctly */ 450 term__flush(); /* flush any buffered output */ 451 if (el->el_flags & HANDLE_SIGNALS) 452 sig_clr(el); 453 if (nread) 454 *nread = num; 455 return num ? el->el_line.buffer : NULL; 456 } 457