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. */
366 if (cbp >= MB_LEN_MAX) {
371 /* Incomplete sequence, read another byte. */
374 /* Valid character, process it. */
381 * Pop a macro from the stack
384 read_pop(c_macro_t *ma)
388 el_free(ma->macro[0]);
389 for (i = 0; i < ma->level; i++)
390 ma->macro[i] = ma->macro[i + 1];
396 * Read a wide character
399 el_wgetc(EditLine *el, wchar_t *cp)
402 c_macro_t *ma = &el->el_chared.c_macro;
407 if (!read_preread(el))
414 if (ma->macro[0][ma->offset] == '\0') {
419 *cp = ma->macro[0][ma->offset++];
421 if (ma->macro[0][ma->offset] == '\0') {
422 /* Needed for QuoteMode On */
430 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
431 #endif /* DEBUG_READ */
432 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
436 (void) fprintf(el->el_errfile, "Reading a character\n");
437 #endif /* DEBUG_READ */
438 num_read = (*el->el_read.read_char)(el, cp);
440 el->el_errno = errno;
442 (void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
443 #endif /* DEBUG_READ */
448 read_prepare(EditLine *el)
450 if (el->el_flags & HANDLE_SIGNALS)
452 if (el->el_flags & NO_TTY)
454 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
457 /* This is relatively cheap, and things go terribly wrong if
458 we have the wrong size. */
460 re_clear_display(el); /* reset the display stuff */
462 re_refresh(el); /* print the prompt */
464 if (el->el_flags & UNBUFFERED)
469 read_finish(EditLine *el)
471 if ((el->el_flags & UNBUFFERED) == 0)
472 (void) tty_cookedmode(el);
473 if (el->el_flags & HANDLE_SIGNALS)
478 FUN(el,gets)(EditLine *el, int *nread)
481 el_action_t cmdnum = 0;
482 int num; /* how many chars we have read at NL */
488 c_macro_t *ma = &el->el_chared.c_macro;
489 #endif /* FIONREAD */
495 if (el->el_flags & NO_TTY) {
498 cp = el->el_line.buffer;
499 while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
501 /* make sure there is space for next character */
502 if (cp + 1 >= el->el_line.limit) {
503 idx = (size_t)(cp - el->el_line.buffer);
504 if (!ch_enlargebufs(el, (size_t)2))
506 cp = &el->el_line.buffer[idx];
509 if (el->el_flags & UNBUFFERED)
511 if (cp[-1] == '\r' || cp[-1] == '\n')
516 cp = el->el_line.buffer;
517 el->el_errno = errno;
525 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
528 (void) ioctl(el->el_infd, FIONREAD, &chrs);
530 if (tty_rawmode(el) < 0) {
537 #endif /* FIONREAD */
539 if ((el->el_flags & UNBUFFERED) == 0)
542 if (el->el_flags & EDIT_DISABLED) {
545 if ((el->el_flags & UNBUFFERED) == 0)
546 cp = el->el_line.buffer;
548 cp = el->el_line.lastchar;
552 while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
554 /* make sure there is space next character */
555 if (cp + 1 >= el->el_line.limit) {
556 idx = (size_t)(cp - el->el_line.buffer);
557 if (!ch_enlargebufs(el, (size_t)2))
559 cp = &el->el_line.buffer[idx];
562 crlf = cp[-1] == '\r' || cp[-1] == '\n';
563 if (el->el_flags & UNBUFFERED)
571 cp = el->el_line.buffer;
572 el->el_errno = errno;
578 for (num = OKCMD; num == OKCMD;) { /* while still editing this
582 #endif /* DEBUG_EDIT */
583 /* if EOF or error */
584 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
587 (void) fprintf(el->el_errfile,
588 "Returning from el_gets %d\n", num);
589 #endif /* DEBUG_READ */
592 if (el->el_errno == EINTR) {
593 el->el_line.buffer[0] = '\0';
594 el->el_line.lastchar =
595 el->el_line.cursor = el->el_line.buffer;
598 if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
600 (void) fprintf(el->el_errfile,
601 "ERROR: illegal command from key 0%o\r\n", ch);
602 #endif /* DEBUG_EDIT */
603 continue; /* try again */
605 /* now do the real command */
609 for (b = el->el_map.help; b->name; b++)
610 if (b->func == cmdnum)
613 (void) fprintf(el->el_errfile,
614 "Executing " FSTR "\n", b->name);
616 (void) fprintf(el->el_errfile,
617 "Error command = %d\n", cmdnum);
619 #endif /* DEBUG_READ */
620 /* vi redo needs these way down the levels... */
621 el->el_state.thiscmd = cmdnum;
622 el->el_state.thisch = ch;
623 if (el->el_map.type == MAP_VI &&
624 el->el_map.current == el->el_map.key &&
625 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
626 if (cmdnum == VI_DELETE_PREV_CHAR &&
627 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
628 && Isprint(el->el_chared.c_redo.pos[-1]))
629 el->el_chared.c_redo.pos--;
631 *el->el_chared.c_redo.pos++ = ch;
633 retval = (*el->el_map.func[cmdnum]) (el, ch);
635 (void) fprintf(el->el_errfile,
636 "Returned state %d\n", retval );
637 #endif /* DEBUG_READ */
639 /* save the last command here */
640 el->el_state.lastcmd = cmdnum;
642 /* use any return value */
645 re_refresh_cursor(el);
650 re_clear_display(el);
657 case CC_REFRESH_BEEP:
662 case CC_NORM: /* normal char */
665 case CC_ARGHACK: /* Suggested by Rich Salz */
666 /* <rsalz@pineapple.bbn.com> */
667 continue; /* keep going... */
669 case CC_EOF: /* end of file typed */
670 if ((el->el_flags & UNBUFFERED) == 0)
672 else if (num == -1) {
673 *el->el_line.lastchar++ = CONTROL('d');
674 el->el_line.cursor = el->el_line.lastchar;
679 case CC_NEWLINE: /* normal end of line */
680 num = (int)(el->el_line.lastchar - el->el_line.buffer);
683 case CC_FATAL: /* fatal error, reset to known state */
685 (void) fprintf(el->el_errfile,
686 "*** editor fatal ERROR ***\r\n\n");
687 #endif /* DEBUG_READ */
688 /* put (real) cursor in a known place */
689 re_clear_display(el); /* reset the display stuff */
690 ch_reset(el, 1); /* reset the input pointers */
691 re_refresh(el); /* print the prompt again */
695 default: /* functions we don't know about */
697 (void) fprintf(el->el_errfile,
698 "*** editor ERROR ***\r\n\n");
699 #endif /* DEBUG_READ */
704 el->el_state.argument = 1;
705 el->el_state.doingarg = 0;
706 el->el_chared.c_vcmd.action = NOP;
707 if (el->el_flags & UNBUFFERED)
711 terminal__flush(el); /* flush any buffered output */
712 /* make sure the tty is set up correctly */
713 if ((el->el_flags & UNBUFFERED) == 0) {
715 *nread = num != -1 ? num : 0;
717 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
721 el->el_line.cursor = el->el_line.lastchar = cp;
723 *nread = (int)(el->el_line.cursor - el->el_line.buffer);
728 errno = el->el_errno;
732 return el->el_line.buffer;