1 /* $NetBSD: read.c,v 1.86 2016/03/02 19:24:20 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.86 2016/03/02 19:24:20 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
60 #define OKCMD -1 /* must be -1! */
62 private int read__fixio(int, int);
63 private int read_preread(EditLine *);
64 private int read_char(EditLine *, wchar_t *);
65 private int read_getcmd(EditLine *, el_action_t *, Char *);
66 private void read_pop(c_macro_t *);
69 * Initialize the read stuff
72 read_init(EditLine *el)
74 /* builtin read_char */
75 el->el_read.read_char = read_char;
81 * Set the read char function to the one provided.
82 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
85 el_read_setfn(EditLine *el, el_rfunc_t rc)
87 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
93 * return the current read char function, or EL_BUILTIN_GETCFN
94 * if it is the default one
97 el_read_getfn(EditLine *el)
99 return el->el_read.read_char == read_char ?
100 EL_BUILTIN_GETCFN : el->el_read.read_char;
105 #define MIN(A,B) ((A) < (B) ? (A) : (B))
110 read_debug(EditLine *el)
113 if (el->el_line.cursor > el->el_line.lastchar)
114 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
115 if (el->el_line.cursor < el->el_line.buffer)
116 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
117 if (el->el_line.cursor > el->el_line.limit)
118 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
119 if (el->el_line.lastchar > el->el_line.limit)
120 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
121 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
122 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
124 #endif /* DEBUG_EDIT */
128 * Try to recover from a read error
132 read__fixio(int fd __attribute__((__unused__)), int e)
136 case -1: /* Make sure that the code is reachable */
143 #endif /* EWOULDBLOCK */
145 #if defined(POSIX) && defined(EAGAIN)
146 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
151 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
152 #endif /* POSIX && EAGAIN */
156 #if defined(F_SETFL) && defined(O_NDELAY)
157 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
160 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
164 #endif /* F_SETFL && O_NDELAY */
170 if (ioctl(fd, FIONBIO, &zero) == -1)
177 #endif /* TRY_AGAIN */
190 * Try to read the stuff in the input queue;
193 read_preread(EditLine *el)
197 if (el->el_tty.t_mode == ED_IO)
201 /* FIONREAD attempts to buffer up multiple bytes, and to make that work
202 * properly with partial wide/UTF-8 characters would need some careful work. */
204 (void) ioctl(el->el_infd, FIONREAD, &chrs);
208 chrs = read(el->el_infd, buf,
209 (size_t) MIN(chrs, EL_BUFSIZ - 1));
215 #endif /* FIONREAD */
225 FUN(el,push)(EditLine *el, const Char *str)
227 c_macro_t *ma = &el->el_chared.c_macro;
229 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
231 if ((ma->macro[ma->level] = Strdup(str)) != NULL)
241 * Get next command from the input stream, return OKCMD on success.
242 * Character values > 255 are not looked up in the map, but inserted.
245 read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
247 static const Char meta = (Char)0x80;
254 if ((num = el_wgetc(el, &wc)) != 1) {/* if EOF or error */
255 el->el_errno = num == 0 ? 0 : errno;
256 return 0; /* not OKCMD */
262 el->el_state.metanext = 0;
268 if (el->el_state.metanext) {
269 el->el_state.metanext = 0;
277 cmd = el->el_map.current[(unsigned char) *ch];
278 if (cmd == ED_SEQUENCE_LEAD_IN) {
279 keymacro_value_t val;
280 switch (keymacro_get(el, ch, &val)) {
285 FUN(el,push)(el, val.str);
289 /* XXX: In the future to run a user function */
294 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
298 if (el->el_map.alt == NULL)
299 el->el_map.current = el->el_map.key;
300 } while (cmd == ED_SEQUENCE_LEAD_IN);
306 * Read a character from the tty.
309 read_char(EditLine *el, wchar_t *cp)
313 char cbuf[MB_LEN_MAX];
315 int save_errno = errno;
318 el->el_signal->sig_no = 0;
319 while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
321 switch (el->el_signal->sig_no) {
323 FUN(el,set)(el, EL_REFRESH);
331 if (!tried && read__fixio(el->el_infd, e) == 0) {
350 switch (ct_mbrtowc(cp, cbuf, cbp)) {
354 * Invalid sequence, discard all bytes
355 * except the last one.
357 cbuf[0] = cbuf[cbp - 1];
361 /* Invalid byte, discard it. */
367 * We don't support other multibyte charsets.
368 * The second condition shouldn't happen
369 * and is here merely for additional safety.
371 if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
377 /* Incomplete sequence, read another byte. */
380 /* Valid character, process it. */
387 * Pop a macro from the stack
390 read_pop(c_macro_t *ma)
394 el_free(ma->macro[0]);
395 for (i = 0; i < ma->level; i++)
396 ma->macro[i] = ma->macro[i + 1];
402 * Read a wide character
405 el_wgetc(EditLine *el, wchar_t *cp)
408 c_macro_t *ma = &el->el_chared.c_macro;
413 if (!read_preread(el))
420 if (ma->macro[0][ma->offset] == '\0') {
425 *cp = ma->macro[0][ma->offset++];
427 if (ma->macro[0][ma->offset] == '\0') {
428 /* Needed for QuoteMode On */
436 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
437 #endif /* DEBUG_READ */
438 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
442 (void) fprintf(el->el_errfile, "Reading a character\n");
443 #endif /* DEBUG_READ */
444 num_read = (*el->el_read.read_char)(el, cp);
446 el->el_errno = errno;
448 (void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
449 #endif /* DEBUG_READ */
454 read_prepare(EditLine *el)
456 if (el->el_flags & HANDLE_SIGNALS)
458 if (el->el_flags & NO_TTY)
460 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
463 /* This is relatively cheap, and things go terribly wrong if
464 we have the wrong size. */
466 re_clear_display(el); /* reset the display stuff */
468 re_refresh(el); /* print the prompt */
470 if (el->el_flags & UNBUFFERED)
475 read_finish(EditLine *el)
477 if ((el->el_flags & UNBUFFERED) == 0)
478 (void) tty_cookedmode(el);
479 if (el->el_flags & HANDLE_SIGNALS)
484 FUN(el,gets)(EditLine *el, int *nread)
487 el_action_t cmdnum = 0;
488 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, &wc)) == 1) {
507 /* make sure there is space for next character */
508 if (cp + 1 >= el->el_line.limit) {
509 idx = (size_t)(cp - el->el_line.buffer);
510 if (!ch_enlargebufs(el, (size_t)2))
512 cp = &el->el_line.buffer[idx];
515 if (el->el_flags & UNBUFFERED)
517 if (cp[-1] == '\r' || cp[-1] == '\n')
522 cp = el->el_line.buffer;
523 el->el_errno = errno;
531 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
534 (void) ioctl(el->el_infd, FIONREAD, &chrs);
536 if (tty_rawmode(el) < 0) {
543 #endif /* FIONREAD */
545 if ((el->el_flags & UNBUFFERED) == 0)
548 if (el->el_flags & EDIT_DISABLED) {
551 if ((el->el_flags & UNBUFFERED) == 0)
552 cp = el->el_line.buffer;
554 cp = el->el_line.lastchar;
558 while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
560 /* make sure there is space next character */
561 if (cp + 1 >= el->el_line.limit) {
562 idx = (size_t)(cp - el->el_line.buffer);
563 if (!ch_enlargebufs(el, (size_t)2))
565 cp = &el->el_line.buffer[idx];
568 crlf = cp[-1] == '\r' || cp[-1] == '\n';
569 if (el->el_flags & UNBUFFERED)
577 cp = el->el_line.buffer;
578 el->el_errno = errno;
584 for (num = OKCMD; num == OKCMD;) { /* while still editing this
588 #endif /* DEBUG_EDIT */
589 /* if EOF or error */
590 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
593 (void) fprintf(el->el_errfile,
594 "Returning from el_gets %d\n", num);
595 #endif /* DEBUG_READ */
598 if (el->el_errno == EINTR) {
599 el->el_line.buffer[0] = '\0';
600 el->el_line.lastchar =
601 el->el_line.cursor = el->el_line.buffer;
604 if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
606 (void) fprintf(el->el_errfile,
607 "ERROR: illegal command from key 0%o\r\n", ch);
608 #endif /* DEBUG_EDIT */
609 continue; /* try again */
611 /* now do the real command */
615 for (b = el->el_map.help; b->name; b++)
616 if (b->func == cmdnum)
619 (void) fprintf(el->el_errfile,
620 "Executing " FSTR "\n", b->name);
622 (void) fprintf(el->el_errfile,
623 "Error command = %d\n", cmdnum);
625 #endif /* DEBUG_READ */
626 /* vi redo needs these way down the levels... */
627 el->el_state.thiscmd = cmdnum;
628 el->el_state.thisch = ch;
629 if (el->el_map.type == MAP_VI &&
630 el->el_map.current == el->el_map.key &&
631 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
632 if (cmdnum == VI_DELETE_PREV_CHAR &&
633 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
634 && Isprint(el->el_chared.c_redo.pos[-1]))
635 el->el_chared.c_redo.pos--;
637 *el->el_chared.c_redo.pos++ = ch;
639 retval = (*el->el_map.func[cmdnum]) (el, ch);
641 (void) fprintf(el->el_errfile,
642 "Returned state %d\n", retval );
643 #endif /* DEBUG_READ */
645 /* save the last command here */
646 el->el_state.lastcmd = cmdnum;
648 /* use any return value */
651 re_refresh_cursor(el);
656 re_clear_display(el);
663 case CC_REFRESH_BEEP:
668 case CC_NORM: /* normal char */
671 case CC_ARGHACK: /* Suggested by Rich Salz */
672 /* <rsalz@pineapple.bbn.com> */
673 continue; /* keep going... */
675 case CC_EOF: /* end of file typed */
676 if ((el->el_flags & UNBUFFERED) == 0)
678 else if (num == -1) {
679 *el->el_line.lastchar++ = CONTROL('d');
680 el->el_line.cursor = el->el_line.lastchar;
685 case CC_NEWLINE: /* normal end of line */
686 num = (int)(el->el_line.lastchar - el->el_line.buffer);
689 case CC_FATAL: /* fatal error, reset to known state */
691 (void) fprintf(el->el_errfile,
692 "*** editor fatal ERROR ***\r\n\n");
693 #endif /* DEBUG_READ */
694 /* put (real) cursor in a known place */
695 re_clear_display(el); /* reset the display stuff */
696 ch_reset(el, 1); /* reset the input pointers */
697 re_refresh(el); /* print the prompt again */
701 default: /* functions we don't know about */
703 (void) fprintf(el->el_errfile,
704 "*** editor ERROR ***\r\n\n");
705 #endif /* DEBUG_READ */
710 el->el_state.argument = 1;
711 el->el_state.doingarg = 0;
712 el->el_chared.c_vcmd.action = NOP;
713 if (el->el_flags & UNBUFFERED)
717 terminal__flush(el); /* flush any buffered output */
718 /* make sure the tty is set up correctly */
719 if ((el->el_flags & UNBUFFERED) == 0) {
721 *nread = num != -1 ? num : 0;
723 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
727 el->el_line.cursor = el->el_line.lastchar = cp;
729 *nread = (int)(el->el_line.cursor - el->el_line.buffer);
734 errno = el->el_errno;
738 return el->el_line.buffer;