1 /* $NetBSD: read.c,v 1.71 2014/07/06 18:15:34 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
40 __RCSID("$NetBSD: read.c,v 1.71 2014/07/06 18:15:34 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * read.c: Clean this junk up! This is horrible code.
48 * Terminal read functions
57 #define OKCMD -1 /* must be -1! */
59 private int read__fixio(int, int);
60 private int read_preread(EditLine *);
61 private int read_char(EditLine *, Char *);
62 private int read_getcmd(EditLine *, el_action_t *, Char *);
63 private void read_pop(c_macro_t *);
66 * Initialize the read stuff
69 read_init(EditLine *el)
71 /* builtin read_char */
72 el->el_read.read_char = read_char;
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.
82 el_read_setfn(EditLine *el, el_rfunc_t rc)
84 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
90 * return the current read char function, or EL_BUILTIN_GETCFN
91 * if it is the default one
94 el_read_getfn(EditLine *el)
96 return el->el_read.read_char == read_char ?
97 EL_BUILTIN_GETCFN : el->el_read.read_char;
102 #define MIN(A,B) ((A) < (B) ? (A) : (B))
107 read_debug(EditLine *el)
110 if (el->el_line.cursor > el->el_line.lastchar)
111 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
112 if (el->el_line.cursor < el->el_line.buffer)
113 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
114 if (el->el_line.cursor > el->el_line.limit)
115 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
116 if (el->el_line.lastchar > el->el_line.limit)
117 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
118 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
119 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
121 #endif /* DEBUG_EDIT */
125 * Try to recover from a read error
129 read__fixio(int fd __attribute__((__unused__)), int e)
133 case -1: /* Make sure that the code is reachable */
140 #endif /* EWOULDBLOCK */
142 #if defined(POSIX) && defined(EAGAIN)
143 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
148 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
149 #endif /* POSIX && EAGAIN */
153 #if defined(F_SETFL) && defined(O_NDELAY)
154 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
157 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
161 #endif /* F_SETFL && O_NDELAY */
167 if (ioctl(fd, FIONBIO, &zero) == -1)
174 #endif /* TRY_AGAIN */
187 * Try to read the stuff in the input queue;
190 read_preread(EditLine *el)
194 if (el->el_tty.t_mode == ED_IO)
198 /* FIONREAD attempts to buffer up multiple bytes, and to make that work
199 * properly with partial wide/UTF-8 characters would need some careful work. */
201 (void) ioctl(el->el_infd, FIONREAD, &chrs);
205 chrs = read(el->el_infd, buf,
206 (size_t) MIN(chrs, EL_BUFSIZ - 1));
212 #endif /* FIONREAD */
222 FUN(el,push)(EditLine *el, const Char *str)
224 c_macro_t *ma = &el->el_chared.c_macro;
226 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
228 if ((ma->macro[ma->level] = Strdup(str)) != NULL)
238 * Get next command from the input stream, return OKCMD on success.
239 * Character values > 255 are not looked up in the map, but inserted.
242 read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
249 if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
250 el->el_errno = num == 0 ? 0 : errno;
251 return 0; /* not OKCMD */
256 el->el_state.metanext = 0;
262 if (el->el_state.metanext) {
263 el->el_state.metanext = 0;
271 cmd = el->el_map.current[(unsigned char) *ch];
272 if (cmd == ED_SEQUENCE_LEAD_IN) {
273 keymacro_value_t val;
274 switch (keymacro_get(el, ch, &val)) {
279 FUN(el,push)(el, val.str);
283 /* XXX: In the future to run a user function */
288 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
292 if (el->el_map.alt == NULL)
293 el->el_map.current = el->el_map.key;
294 } while (cmd == ED_SEQUENCE_LEAD_IN);
301 * Test whether a byte is a leading byte of a UTF-8 sequence.
306 return c < 0x80 || /* single byte char */
307 (c >= 0xc2 && c <= 0xf4); /* start of multibyte sequence */
312 * Read a character from the tty.
315 read_char(EditLine *el, Char *cp)
319 char cbuf[MB_LEN_MAX];
324 el->el_signal->sig_no = 0;
325 while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
327 switch (el->el_signal->sig_no) {
329 FUN(el,set)(el, EL_REFRESH);
337 if (!tried && read__fixio(el->el_infd, e) == 0)
354 if (el->el_flags & CHARSET_IS_UTF8) {
355 if (!utf8_islead((unsigned char)cbuf[0]))
356 goto again; /* discard the byte we read and try again */
358 if ((bytes = ct_mbtowc(cp, cbuf, cbp)) == -1) {
360 if (cbp >= MB_LEN_MAX) { /* "shouldn't happen" */
367 } else if (isascii((unsigned char)cbuf[0]) ||
368 /* we don't support other multibyte charsets */
370 /* Try non-ASCII characters in a 8-bit character set */
371 (bytes = ct_mbtowc(cp, cbuf, cbp)) != 1)
373 *cp = (unsigned char)cbuf[0];
375 if ((el->el_flags & IGNORE_EXTCHARS) && bytes > 1) {
376 cbp = 0; /* skip this character */
380 return (int)num_read;
384 * Pop a macro from the stack
387 read_pop(c_macro_t *ma)
391 el_free(ma->macro[0]);
392 for (i = 0; i < ma->level; i++)
393 ma->macro[i] = ma->macro[i + 1];
402 FUN(el,getc)(EditLine *el, Char *cp)
405 c_macro_t *ma = &el->el_chared.c_macro;
410 if (!read_preread(el))
417 if (ma->macro[0][ma->offset] == '\0') {
422 *cp = ma->macro[0][ma->offset++];
424 if (ma->macro[0][ma->offset] == '\0') {
425 /* Needed for QuoteMode On */
433 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
434 #endif /* DEBUG_READ */
435 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
439 (void) fprintf(el->el_errfile, "Reading a character\n");
440 #endif /* DEBUG_READ */
441 num_read = (*el->el_read.read_char)(el, cp);
443 el->el_errno = errno;
445 if (el->el_flags & NARROW_READ)
446 *cp = *(char *)(void *)cp;
449 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
450 #endif /* DEBUG_READ */
455 read_prepare(EditLine *el)
457 if (el->el_flags & HANDLE_SIGNALS)
459 if (el->el_flags & NO_TTY)
461 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
464 /* This is relatively cheap, and things go terribly wrong if
465 we have the wrong size. */
467 re_clear_display(el); /* reset the display stuff */
469 re_refresh(el); /* print the prompt */
471 if (el->el_flags & UNBUFFERED)
476 read_finish(EditLine *el)
478 if ((el->el_flags & UNBUFFERED) == 0)
479 (void) tty_cookedmode(el);
480 if (el->el_flags & HANDLE_SIGNALS)
485 FUN(el,gets)(EditLine *el, int *nread)
488 el_action_t cmdnum = 0;
489 int num; /* how many chars we have read at NL */
494 c_macro_t *ma = &el->el_chared.c_macro;
495 #endif /* FIONREAD */
501 if (el->el_flags & NO_TTY) {
504 cp = el->el_line.buffer;
505 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
506 /* make sure there is space for next character */
507 if (cp + 1 >= el->el_line.limit) {
508 idx = (size_t)(cp - el->el_line.buffer);
509 if (!ch_enlargebufs(el, (size_t)2))
511 cp = &el->el_line.buffer[idx];
514 if (el->el_flags & UNBUFFERED)
516 if (cp[-1] == '\r' || cp[-1] == '\n')
521 cp = el->el_line.buffer;
522 el->el_errno = errno;
530 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
533 (void) ioctl(el->el_infd, FIONREAD, &chrs);
535 if (tty_rawmode(el) < 0) {
542 #endif /* FIONREAD */
544 if ((el->el_flags & UNBUFFERED) == 0)
547 if (el->el_flags & EDIT_DISABLED) {
550 if ((el->el_flags & UNBUFFERED) == 0)
551 cp = el->el_line.buffer;
553 cp = el->el_line.lastchar;
557 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
558 /* make sure there is space next character */
559 if (cp + 1 >= el->el_line.limit) {
560 idx = (size_t)(cp - el->el_line.buffer);
561 if (!ch_enlargebufs(el, (size_t)2))
563 cp = &el->el_line.buffer[idx];
566 crlf = cp[-1] == '\r' || cp[-1] == '\n';
567 if (el->el_flags & UNBUFFERED)
575 cp = el->el_line.buffer;
576 el->el_errno = errno;
582 for (num = OKCMD; num == OKCMD;) { /* while still editing this
586 #endif /* DEBUG_EDIT */
587 /* if EOF or error */
588 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
591 (void) fprintf(el->el_errfile,
592 "Returning from el_gets %d\n", num);
593 #endif /* DEBUG_READ */
596 if (el->el_errno == EINTR) {
597 el->el_line.buffer[0] = '\0';
598 el->el_line.lastchar =
599 el->el_line.cursor = el->el_line.buffer;
602 if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
604 (void) fprintf(el->el_errfile,
605 "ERROR: illegal command from key 0%o\r\n", ch);
606 #endif /* DEBUG_EDIT */
607 continue; /* try again */
609 /* now do the real command */
613 for (b = el->el_map.help; b->name; b++)
614 if (b->func == cmdnum)
617 (void) fprintf(el->el_errfile,
618 "Executing %s\n", b->name);
620 (void) fprintf(el->el_errfile,
621 "Error command = %d\n", cmdnum);
623 #endif /* DEBUG_READ */
624 /* vi redo needs these way down the levels... */
625 el->el_state.thiscmd = cmdnum;
626 el->el_state.thisch = ch;
627 if (el->el_map.type == MAP_VI &&
628 el->el_map.current == el->el_map.key &&
629 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
630 if (cmdnum == VI_DELETE_PREV_CHAR &&
631 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
632 && Isprint(el->el_chared.c_redo.pos[-1]))
633 el->el_chared.c_redo.pos--;
635 *el->el_chared.c_redo.pos++ = ch;
637 retval = (*el->el_map.func[cmdnum]) (el, ch);
639 (void) fprintf(el->el_errfile,
640 "Returned state %d\n", retval );
641 #endif /* DEBUG_READ */
643 /* save the last command here */
644 el->el_state.lastcmd = cmdnum;
646 /* use any return value */
649 re_refresh_cursor(el);
654 re_clear_display(el);
661 case CC_REFRESH_BEEP:
666 case CC_NORM: /* normal char */
669 case CC_ARGHACK: /* Suggested by Rich Salz */
670 /* <rsalz@pineapple.bbn.com> */
671 continue; /* keep going... */
673 case CC_EOF: /* end of file typed */
674 if ((el->el_flags & UNBUFFERED) == 0)
676 else if (num == -1) {
677 *el->el_line.lastchar++ = CONTROL('d');
678 el->el_line.cursor = el->el_line.lastchar;
683 case CC_NEWLINE: /* normal end of line */
684 num = (int)(el->el_line.lastchar - el->el_line.buffer);
687 case CC_FATAL: /* fatal error, reset to known state */
689 (void) fprintf(el->el_errfile,
690 "*** editor fatal ERROR ***\r\n\n");
691 #endif /* DEBUG_READ */
692 /* put (real) cursor in a known place */
693 re_clear_display(el); /* reset the display stuff */
694 ch_reset(el, 1); /* reset the input pointers */
695 re_refresh(el); /* print the prompt again */
699 default: /* functions we don't know about */
701 (void) fprintf(el->el_errfile,
702 "*** editor ERROR ***\r\n\n");
703 #endif /* DEBUG_READ */
708 el->el_state.argument = 1;
709 el->el_state.doingarg = 0;
710 el->el_chared.c_vcmd.action = NOP;
711 if (el->el_flags & UNBUFFERED)
715 terminal__flush(el); /* flush any buffered output */
716 /* make sure the tty is set up correctly */
717 if ((el->el_flags & UNBUFFERED) == 0) {
719 *nread = num != -1 ? num : 0;
721 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
725 el->el_line.cursor = el->el_line.lastchar = cp;
727 *nread = (int)(el->el_line.cursor - el->el_line.buffer);
732 errno = el->el_errno;
736 return el->el_line.buffer;