2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)read.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: read.c,v 1.40 2007/03/01 21:41:45 christos Exp $
34 * $DragonFly: src/lib/libedit/read.c,v 1.5 2007/05/05 00:27:39 pavalos Exp $
40 * read.c: Clean this junk up! This is horrible code.
41 * Terminal read functions
51 private int read__fixio(int, int);
52 private int read_preread(EditLine *);
53 private int read_char(EditLine *, char *);
54 private int read_getcmd(EditLine *, el_action_t *, char *);
55 private void read_pop(c_macro_t *);
58 * Initialize the read stuff
61 read_init(EditLine *el)
63 /* builtin read_char */
64 el->el_read.read_char = read_char;
70 * Set the read char function to the one provided.
71 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
74 el_read_setfn(EditLine *el, el_rfunc_t rc)
76 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
82 * return the current read char function, or EL_BUILTIN_GETCFN
83 * if it is the default one
86 el_read_getfn(EditLine *el)
88 return (el->el_read.read_char == read_char) ?
89 EL_BUILTIN_GETCFN : el->el_read.read_char;
94 #define MIN(A,B) ((A) < (B) ? (A) : (B))
99 read_debug(EditLine *el)
102 if (el->el_line.cursor > el->el_line.lastchar)
103 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
104 if (el->el_line.cursor < el->el_line.buffer)
105 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
106 if (el->el_line.cursor > el->el_line.limit)
107 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
108 if (el->el_line.lastchar > el->el_line.limit)
109 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
110 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
111 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
113 #endif /* DEBUG_EDIT */
117 * Try to recover from a read error
121 read__fixio(int fd __attribute__((__unused__)), int e)
125 case -1: /* Make sure that the code is reachable */
132 #endif /* EWOULDBLOCK */
134 #if defined(POSIX) && defined(EAGAIN)
135 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
140 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
141 #endif /* POSIX && EAGAIN */
145 #if defined(F_SETFL) && defined(O_NDELAY)
146 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
149 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
153 #endif /* F_SETFL && O_NDELAY */
159 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
166 #endif /* TRY_AGAIN */
179 * Try to read the stuff in the input queue;
182 read_preread(EditLine *el)
186 if (el->el_tty.t_mode == ED_IO)
190 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
194 chrs = read(el->el_infd, buf,
195 (size_t) MIN(chrs, EL_BUFSIZ - 1));
201 #endif /* FIONREAD */
211 el_push(EditLine *el, char *str)
213 c_macro_t *ma = &el->el_chared.c_macro;
215 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
217 if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
227 * Return next command from the input stream.
230 read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
236 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
241 el->el_state.metanext = 0;
247 if (el->el_state.metanext) {
248 el->el_state.metanext = 0;
251 cmd = el->el_map.current[(unsigned char) *ch];
252 if (cmd == ED_SEQUENCE_LEAD_IN) {
254 switch (key_get(el, ch, &val)) {
259 el_push(el, val.str);
263 /* XXX: In the future to run a user function */
268 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
272 if (el->el_map.alt == NULL)
273 el->el_map.current = el->el_map.key;
274 } while (cmd == ED_SEQUENCE_LEAD_IN);
281 * Read a character from the tty.
284 read_char(EditLine *el, char *cp)
289 while ((num_read = read(el->el_infd, cp, 1)) == -1)
290 if (!tried && read__fixio(el->el_infd, errno) == 0)
301 * Pop a macro from the stack
304 read_pop(c_macro_t *ma)
308 el_free(ma->macro[0]);
309 for (i = ma->level--; i > 0; i--)
310 ma->macro[i - 1] = ma->macro[i];
318 el_getc(EditLine *el, char *cp)
321 c_macro_t *ma = &el->el_chared.c_macro;
326 if (!read_preread(el))
333 if (ma->macro[0][ma->offset] == '\0') {
338 *cp = ma->macro[0][ma->offset++] & 0377;
340 if (ma->macro[0][ma->offset] == '\0') {
341 /* Needed for QuoteMode On */
349 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
350 #endif /* DEBUG_READ */
351 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
355 (void) fprintf(el->el_errfile, "Reading a character\n");
356 #endif /* DEBUG_READ */
357 num_read = (*el->el_read.read_char)(el, cp);
359 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
360 #endif /* DEBUG_READ */
365 read_prepare(EditLine *el)
367 if (el->el_flags & HANDLE_SIGNALS)
369 if (el->el_flags & NO_TTY)
371 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
374 /* This is relatively cheap, and things go terribly wrong if
375 we have the wrong size. */
377 re_clear_display(el); /* reset the display stuff */
379 re_refresh(el); /* print the prompt */
381 if (el->el_flags & UNBUFFERED)
386 read_finish(EditLine *el)
388 if ((el->el_flags & UNBUFFERED) == 0)
389 (void) tty_cookedmode(el);
390 if (el->el_flags & HANDLE_SIGNALS)
395 el_gets(EditLine *el, int *nread)
398 el_action_t cmdnum = 0;
399 int num; /* how many chars we have read at NL */
403 c_macro_t *ma = &el->el_chared.c_macro;
404 #endif /* FIONREAD */
406 if (el->el_flags & NO_TTY) {
407 char *cp = el->el_line.buffer;
410 while ((*el->el_read.read_char)(el, cp) == 1) {
411 /* make sure there is space for next character */
412 if (cp + 1 >= el->el_line.limit) {
413 idx = (cp - el->el_line.buffer);
414 if (!ch_enlargebufs(el, 2))
416 cp = &el->el_line.buffer[idx];
419 if (el->el_flags & UNBUFFERED)
421 if (cp[-1] == '\r' || cp[-1] == '\n')
425 el->el_line.cursor = el->el_line.lastchar = cp;
428 *nread = el->el_line.cursor - el->el_line.buffer;
429 return (el->el_line.buffer);
434 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
437 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
439 if (tty_rawmode(el) < 0) {
446 #endif /* FIONREAD */
448 if ((el->el_flags & UNBUFFERED) == 0)
451 if (el->el_flags & EDIT_DISABLED) {
454 if ((el->el_flags & UNBUFFERED) == 0)
455 cp = el->el_line.buffer;
457 cp = el->el_line.lastchar;
461 while ((*el->el_read.read_char)(el, cp) == 1) {
462 /* make sure there is space next character */
463 if (cp + 1 >= el->el_line.limit) {
464 idx = (cp - el->el_line.buffer);
465 if (!ch_enlargebufs(el, 2))
467 cp = &el->el_line.buffer[idx];
469 if (*cp == 4) /* ought to be stty eof */
472 crlf = cp[-1] == '\r' || cp[-1] == '\n';
473 if (el->el_flags & UNBUFFERED)
479 el->el_line.cursor = el->el_line.lastchar = cp;
482 *nread = el->el_line.cursor - el->el_line.buffer;
483 return (el->el_line.buffer);
486 for (num = OKCMD; num == OKCMD;) { /* while still editing this
490 #endif /* DEBUG_EDIT */
491 /* if EOF or error */
492 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
494 (void) fprintf(el->el_errfile,
495 "Returning from el_gets %d\n", num);
496 #endif /* DEBUG_READ */
499 if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
501 (void) fprintf(el->el_errfile,
502 "ERROR: illegal command from key 0%o\r\n", ch);
503 #endif /* DEBUG_EDIT */
504 continue; /* try again */
506 /* now do the real command */
510 for (b = el->el_map.help; b->name; b++)
511 if (b->func == cmdnum)
514 (void) fprintf(el->el_errfile,
515 "Executing %s\n", b->name);
517 (void) fprintf(el->el_errfile,
518 "Error command = %d\n", cmdnum);
520 #endif /* DEBUG_READ */
521 /* vi redo needs these way down the levels... */
522 el->el_state.thiscmd = cmdnum;
523 el->el_state.thisch = ch;
524 if (el->el_map.type == MAP_VI &&
525 el->el_map.current == el->el_map.key &&
526 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
527 if (cmdnum == VI_DELETE_PREV_CHAR &&
528 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
529 && isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
530 el->el_chared.c_redo.pos--;
532 *el->el_chared.c_redo.pos++ = ch;
534 retval = (*el->el_map.func[cmdnum]) (el, ch);
536 (void) fprintf(el->el_errfile,
537 "Returned state %d\n", retval );
538 #endif /* DEBUG_READ */
540 /* save the last command here */
541 el->el_state.lastcmd = cmdnum;
543 /* use any return value */
546 re_refresh_cursor(el);
551 re_clear_display(el);
558 case CC_REFRESH_BEEP:
563 case CC_NORM: /* normal char */
566 case CC_ARGHACK: /* Suggested by Rich Salz */
567 /* <rsalz@pineapple.bbn.com> */
568 continue; /* keep going... */
570 case CC_EOF: /* end of file typed */
571 if ((el->el_flags & UNBUFFERED) == 0)
573 else if (num == -1) {
574 *el->el_line.lastchar++ = CONTROL('d');
575 el->el_line.cursor = el->el_line.lastchar;
580 case CC_NEWLINE: /* normal end of line */
581 num = el->el_line.lastchar - el->el_line.buffer;
584 case CC_FATAL: /* fatal error, reset to known state */
586 (void) fprintf(el->el_errfile,
587 "*** editor fatal ERROR ***\r\n\n");
588 #endif /* DEBUG_READ */
589 /* put (real) cursor in a known place */
590 re_clear_display(el); /* reset the display stuff */
591 ch_reset(el, 1); /* reset the input pointers */
592 re_refresh(el); /* print the prompt again */
596 default: /* functions we don't know about */
598 (void) fprintf(el->el_errfile,
599 "*** editor ERROR ***\r\n\n");
600 #endif /* DEBUG_READ */
605 el->el_state.argument = 1;
606 el->el_state.doingarg = 0;
607 el->el_chared.c_vcmd.action = NOP;
608 if (el->el_flags & UNBUFFERED)
612 term__flush(); /* flush any buffered output */
613 /* make sure the tty is set up correctly */
614 if ((el->el_flags & UNBUFFERED) == 0) {
620 *nread = el->el_line.lastchar - el->el_line.buffer;
622 return (num ? el->el_line.buffer : NULL);