1 /* $NetBSD: read.c,v 1.102 2016/12/11 15:47:06 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.102 2016/12/11 15:47:06 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * read.c: Terminal read functions
59 #define EL_MAXMACRO 10
69 el_rfunc_t read_char; /* Function to read a character. */
73 static int read__fixio(int, int);
74 static int read_char(EditLine *, wchar_t *);
75 static int read_getcmd(EditLine *, el_action_t *, wchar_t *);
76 static void read_clearmacros(struct macros *);
77 static void read_pop(struct macros *);
78 static const wchar_t *noedit_wgets(EditLine *, int *);
81 * Initialize the read stuff
84 read_init(EditLine *el)
88 if ((el->el_read = el_malloc(sizeof(*el->el_read))) == NULL)
91 ma = &el->el_read->macros;
92 if ((ma->macro = el_malloc(EL_MAXMACRO *
93 sizeof(*ma->macro))) == NULL) {
100 /* builtin read_char */
101 el->el_read->read_char = read_char;
106 * Free the data structures used by the read stuff.
109 read_end(struct el_read_t *el_read)
111 read_clearmacros(&el_read->macros);
112 el_free(el_read->macros.macro);
113 el_read->macros.macro = NULL;
118 * Set the read char function to the one provided.
119 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
122 el_read_setfn(struct el_read_t *el_read, el_rfunc_t rc)
124 el_read->read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
130 * return the current read char function, or EL_BUILTIN_GETCFN
131 * if it is the default one
133 libedit_private el_rfunc_t
134 el_read_getfn(struct el_read_t *el_read)
136 return el_read->read_char == read_char ?
137 EL_BUILTIN_GETCFN : el_read->read_char;
142 * Try to recover from a read error
146 read__fixio(int fd __attribute__((__unused__)), int e)
150 case -1: /* Make sure that the code is reachable */
157 #endif /* EWOULDBLOCK */
159 #if defined(POSIX) && defined(EAGAIN)
160 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
165 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
166 #endif /* POSIX && EAGAIN */
170 #if defined(F_SETFL) && defined(O_NDELAY)
171 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
174 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
178 #endif /* F_SETFL && O_NDELAY */
184 if (ioctl(fd, FIONBIO, &zero) == -1)
191 #endif /* TRY_AGAIN */
207 el_wpush(EditLine *el, const wchar_t *str)
209 struct macros *ma = &el->el_read->macros;
211 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
213 if ((ma->macro[ma->level] = wcsdup(str)) != NULL)
223 * Get next command from the input stream,
224 * return 0 on success or -1 on EOF or error.
225 * Character values > 255 are not looked up in the map, but inserted.
228 read_getcmd(EditLine *el, el_action_t *cmdnum, wchar_t *ch)
230 static const wchar_t meta = (wchar_t)0x80;
235 if ((num = el_wgetc(el, ch)) != 1)
240 el->el_state.metanext = 0;
246 if (el->el_state.metanext) {
247 el->el_state.metanext = 0;
253 cmd = el->el_map.current[(unsigned char) *ch];
254 if (cmd == ED_SEQUENCE_LEAD_IN) {
255 keymacro_value_t val;
256 switch (keymacro_get(el, ch, &val)) {
261 el_wpush(el, val.str);
266 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
270 } while (cmd == ED_SEQUENCE_LEAD_IN);
276 * Read a character from the tty.
279 read_char(EditLine *el, wchar_t *cp)
283 char cbuf[MB_LEN_MAX];
285 int save_errno = errno;
288 el->el_signal->sig_no = 0;
289 while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
291 switch (el->el_signal->sig_no) {
293 el_wset(el, EL_REFRESH);
301 if (!tried && read__fixio(el->el_infd, e) == 0) {
321 /* This only works because UTF8 is stateless. */
322 memset(&mbs, 0, sizeof(mbs));
323 switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
327 * Invalid sequence, discard all bytes
328 * except the last one.
330 cbuf[0] = cbuf[cbp - 1];
334 /* Invalid byte, discard it. */
340 * We don't support other multibyte charsets.
341 * The second condition shouldn't happen
342 * and is here merely for additional safety.
344 if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
350 /* Incomplete sequence, read another byte. */
353 /* Valid character, process it. */
360 * Pop a macro from the stack
363 read_pop(struct macros *ma)
367 el_free(ma->macro[0]);
368 for (i = 0; i < ma->level; i++)
369 ma->macro[i] = ma->macro[i + 1];
375 read_clearmacros(struct macros *ma)
377 while (ma->level >= 0)
378 el_free(ma->macro[ma->level--]);
383 * Read a wide character
386 el_wgetc(EditLine *el, wchar_t *cp)
388 struct macros *ma = &el->el_read->macros;
396 if (ma->macro[0][ma->offset] == '\0') {
401 *cp = ma->macro[0][ma->offset++];
403 if (ma->macro[0][ma->offset] == '\0') {
404 /* Needed for QuoteMode On */
411 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
414 num_read = (*el->el_read->read_char)(el, cp);
417 * Remember the original reason of a read failure
418 * such that el_wgets() can restore it after doing
419 * various cleanup operation that might change errno.
422 el->el_read->read_errno = errno;
428 read_prepare(EditLine *el)
430 if (el->el_flags & HANDLE_SIGNALS)
432 if (el->el_flags & NO_TTY)
434 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
437 /* This is relatively cheap, and things go terribly wrong if
438 we have the wrong size. */
440 re_clear_display(el); /* reset the display stuff */
442 re_refresh(el); /* print the prompt */
444 if (el->el_flags & UNBUFFERED)
449 read_finish(EditLine *el)
451 if ((el->el_flags & UNBUFFERED) == 0)
452 (void) tty_cookedmode(el);
453 if (el->el_flags & HANDLE_SIGNALS)
457 static const wchar_t *
458 noedit_wgets(EditLine *el, int *nread)
460 el_line_t *lp = &el->el_line;
463 while ((num = (*el->el_read->read_char)(el, lp->lastchar)) == 1) {
464 if (lp->lastchar + 1 >= lp->limit &&
465 !ch_enlargebufs(el, (size_t)2))
468 if (el->el_flags & UNBUFFERED ||
469 lp->lastchar[-1] == '\r' ||
470 lp->lastchar[-1] == '\n')
473 if (num == -1 && errno == EINTR)
474 lp->lastchar = lp->buffer;
475 lp->cursor = lp->lastchar;
476 *lp->lastchar = '\0';
477 *nread = (int)(lp->lastchar - lp->buffer);
478 return *nread ? lp->buffer : NULL;
482 el_wgets(EditLine *el, int *nread)
485 el_action_t cmdnum = 0;
486 int num; /* how many chars we have read at NL */
493 el->el_read->read_errno = 0;
495 if (el->el_flags & NO_TTY) {
496 el->el_line.lastchar = el->el_line.buffer;
497 return noedit_wgets(el, nread);
501 if (el->el_tty.t_mode == EX_IO && el->el_read->macros.level < 0) {
504 (void) ioctl(el->el_infd, FIONREAD, &chrs);
506 if (tty_rawmode(el) < 0) {
513 #endif /* FIONREAD */
515 if ((el->el_flags & UNBUFFERED) == 0)
518 if (el->el_flags & EDIT_DISABLED) {
519 if ((el->el_flags & UNBUFFERED) == 0)
520 el->el_line.lastchar = el->el_line.buffer;
522 return noedit_wgets(el, nread);
525 for (num = -1; num == -1;) { /* while still editing this line */
526 /* if EOF or error */
527 if (read_getcmd(el, &cmdnum, &ch) == -1)
529 if ((size_t)cmdnum >= el->el_map.nfunc) /* BUG CHECK command */
530 continue; /* try again */
531 /* now do the real command */
532 /* vi redo needs these way down the levels... */
533 el->el_state.thiscmd = cmdnum;
534 el->el_state.thisch = ch;
535 if (el->el_map.type == MAP_VI &&
536 el->el_map.current == el->el_map.key &&
537 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
538 if (cmdnum == VI_DELETE_PREV_CHAR &&
539 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
540 && iswprint(el->el_chared.c_redo.pos[-1]))
541 el->el_chared.c_redo.pos--;
543 *el->el_chared.c_redo.pos++ = ch;
545 retval = (*el->el_map.func[cmdnum]) (el, ch);
547 /* save the last command here */
548 el->el_state.lastcmd = cmdnum;
550 /* use any return value */
553 re_refresh_cursor(el);
558 re_clear_display(el);
565 case CC_REFRESH_BEEP:
570 case CC_NORM: /* normal char */
573 case CC_ARGHACK: /* Suggested by Rich Salz */
574 /* <rsalz@pineapple.bbn.com> */
575 continue; /* keep going... */
577 case CC_EOF: /* end of file typed */
578 if ((el->el_flags & UNBUFFERED) == 0)
580 else if (num == -1) {
581 *el->el_line.lastchar++ = CONTROL('d');
582 el->el_line.cursor = el->el_line.lastchar;
587 case CC_NEWLINE: /* normal end of line */
588 num = (int)(el->el_line.lastchar - el->el_line.buffer);
591 case CC_FATAL: /* fatal error, reset to known state */
592 /* put (real) cursor in a known place */
593 re_clear_display(el); /* reset the display stuff */
594 ch_reset(el); /* reset the input pointers */
595 read_clearmacros(&el->el_read->macros);
596 re_refresh(el); /* print the prompt again */
600 default: /* functions we don't know about */
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 terminal__flush(el); /* flush any buffered output */
613 /* make sure the tty is set up correctly */
614 if ((el->el_flags & UNBUFFERED) == 0) {
616 *nread = num != -1 ? num : 0;
618 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
623 if (el->el_read->read_errno)
624 errno = el->el_read->read_errno;
628 return el->el_line.buffer;