Home | History | Annotate | Line # | Download | only in libedit
read.c revision 1.101
      1  1.101  christos /*	$NetBSD: read.c,v 1.101 2016/05/25 13:01:11 christos Exp $	*/
      2    1.2     lukem 
      3    1.1       cgd /*-
      4    1.1       cgd  * Copyright (c) 1992, 1993
      5    1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      6    1.1       cgd  *
      7    1.1       cgd  * This code is derived from software contributed to Berkeley by
      8    1.1       cgd  * Christos Zoulas of Cornell University.
      9    1.1       cgd  *
     10    1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11    1.1       cgd  * modification, are permitted provided that the following conditions
     12    1.1       cgd  * are met:
     13    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18   1.26       agc  * 3. Neither the name of the University nor the names of its contributors
     19    1.1       cgd  *    may be used to endorse or promote products derived from this software
     20    1.1       cgd  *    without specific prior written permission.
     21    1.1       cgd  *
     22    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23    1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24    1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25    1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26    1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27    1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28    1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29    1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30    1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31    1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32    1.1       cgd  * SUCH DAMAGE.
     33    1.1       cgd  */
     34    1.1       cgd 
     35   1.21  christos #include "config.h"
     36    1.1       cgd #if !defined(lint) && !defined(SCCSID)
     37    1.2     lukem #if 0
     38    1.1       cgd static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/4/93";
     39    1.2     lukem #else
     40  1.101  christos __RCSID("$NetBSD: read.c,v 1.101 2016/05/25 13:01:11 christos Exp $");
     41    1.2     lukem #endif
     42    1.2     lukem #endif /* not lint && not SCCSID */
     43    1.1       cgd 
     44    1.1       cgd /*
     45  1.101  christos  * read.c: Terminal read functions
     46    1.1       cgd  */
     47   1.81  christos #include <ctype.h>
     48   1.11    kleink #include <errno.h>
     49   1.27   mycroft #include <fcntl.h>
     50   1.82  christos #include <limits.h>
     51    1.1       cgd #include <stdlib.h>
     52   1.81  christos #include <string.h>
     53   1.82  christos #include <unistd.h>
     54   1.81  christos 
     55    1.1       cgd #include "el.h"
     56   1.94  christos #include "fcns.h"
     57   1.95  christos #include "read.h"
     58   1.95  christos 
     59   1.97  christos #define	EL_MAXMACRO	10
     60   1.97  christos 
     61   1.97  christos struct macros {
     62   1.97  christos 	wchar_t	**macro;
     63   1.97  christos 	int	  level;
     64   1.97  christos 	int	  offset;
     65   1.97  christos };
     66   1.97  christos 
     67   1.95  christos struct el_read_t {
     68   1.97  christos 	struct macros	 macros;
     69   1.95  christos 	el_rfunc_t	 read_char;	/* Function to read a character. */
     70   1.98  christos 	int		 read_errno;
     71   1.95  christos };
     72    1.1       cgd 
     73   1.91  christos static int	read__fixio(int, int);
     74   1.91  christos static int	read_char(EditLine *, wchar_t *);
     75   1.91  christos static int	read_getcmd(EditLine *, el_action_t *, wchar_t *);
     76   1.97  christos static void	read_clearmacros(struct macros *);
     77   1.97  christos static void	read_pop(struct macros *);
     78  1.101  christos static const wchar_t *noedit_wgets(EditLine *, int *);
     79   1.20  christos 
     80   1.20  christos /* read_init():
     81   1.20  christos  *	Initialize the read stuff
     82   1.20  christos  */
     83   1.96  christos libedit_private int
     84   1.20  christos read_init(EditLine *el)
     85   1.20  christos {
     86   1.97  christos 	struct macros *ma;
     87   1.97  christos 
     88   1.95  christos 	if ((el->el_read = el_malloc(sizeof(*el->el_read))) == NULL)
     89   1.95  christos 		return -1;
     90   1.97  christos 
     91   1.97  christos 	ma = &el->el_read->macros;
     92   1.97  christos 	if ((ma->macro = el_malloc(EL_MAXMACRO *
     93   1.97  christos 	    sizeof(*ma->macro))) == NULL) {
     94   1.97  christos 		free(el->el_read);
     95   1.97  christos 		return -1;
     96   1.97  christos 	}
     97   1.97  christos 	ma->level = -1;
     98   1.97  christos 	ma->offset = 0;
     99   1.97  christos 
    100   1.20  christos 	/* builtin read_char */
    101   1.95  christos 	el->el_read->read_char = read_char;
    102   1.20  christos 	return 0;
    103   1.20  christos }
    104   1.20  christos 
    105   1.97  christos /* el_read_end():
    106   1.97  christos  *	Free the data structures used by the read stuff.
    107   1.97  christos  */
    108   1.97  christos libedit_private void
    109   1.97  christos read_end(struct el_read_t *el_read)
    110   1.97  christos {
    111   1.97  christos 	read_clearmacros(&el_read->macros);
    112   1.97  christos 	el_free(el_read->macros.macro);
    113   1.97  christos 	el_read->macros.macro = NULL;
    114   1.97  christos }
    115   1.20  christos 
    116   1.20  christos /* el_read_setfn():
    117   1.20  christos  *	Set the read char function to the one provided.
    118   1.20  christos  *	If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
    119   1.20  christos  */
    120   1.96  christos libedit_private int
    121   1.95  christos el_read_setfn(struct el_read_t *el_read, el_rfunc_t rc)
    122   1.20  christos {
    123   1.95  christos 	el_read->read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
    124   1.20  christos 	return 0;
    125   1.20  christos }
    126   1.20  christos 
    127   1.20  christos 
    128   1.20  christos /* el_read_getfn():
    129   1.20  christos  *	return the current read char function, or EL_BUILTIN_GETCFN
    130   1.20  christos  *	if it is the default one
    131   1.20  christos  */
    132   1.96  christos libedit_private el_rfunc_t
    133   1.95  christos el_read_getfn(struct el_read_t *el_read)
    134   1.20  christos {
    135   1.95  christos        return el_read->read_char == read_char ?
    136   1.95  christos 	    EL_BUILTIN_GETCFN : el_read->read_char;
    137   1.20  christos }
    138   1.20  christos 
    139    1.1       cgd 
    140    1.1       cgd /* read__fixio():
    141    1.1       cgd  *	Try to recover from a read error
    142    1.1       cgd  */
    143   1.10  christos /* ARGSUSED */
    144   1.91  christos static int
    145   1.25  christos read__fixio(int fd __attribute__((__unused__)), int e)
    146    1.1       cgd {
    147   1.17     lukem 
    148   1.17     lukem 	switch (e) {
    149   1.17     lukem 	case -1:		/* Make sure that the code is reachable */
    150    1.1       cgd 
    151    1.1       cgd #ifdef EWOULDBLOCK
    152   1.17     lukem 	case EWOULDBLOCK:
    153   1.17     lukem #ifndef TRY_AGAIN
    154   1.66  christos #define TRY_AGAIN
    155   1.17     lukem #endif
    156    1.1       cgd #endif /* EWOULDBLOCK */
    157    1.1       cgd 
    158    1.1       cgd #if defined(POSIX) && defined(EAGAIN)
    159   1.17     lukem #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
    160   1.17     lukem 	case EAGAIN:
    161   1.17     lukem #ifndef TRY_AGAIN
    162   1.66  christos #define TRY_AGAIN
    163   1.17     lukem #endif
    164   1.17     lukem #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
    165    1.1       cgd #endif /* POSIX && EAGAIN */
    166    1.1       cgd 
    167   1.17     lukem 		e = 0;
    168    1.1       cgd #ifdef TRY_AGAIN
    169   1.17     lukem #if defined(F_SETFL) && defined(O_NDELAY)
    170   1.17     lukem 		if ((e = fcntl(fd, F_GETFL, 0)) == -1)
    171   1.65  christos 			return -1;
    172    1.5  christos 
    173   1.17     lukem 		if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
    174   1.65  christos 			return -1;
    175    1.5  christos 		else
    176   1.17     lukem 			e = 1;
    177   1.17     lukem #endif /* F_SETFL && O_NDELAY */
    178   1.17     lukem 
    179   1.17     lukem #ifdef FIONBIO
    180   1.17     lukem 		{
    181   1.17     lukem 			int zero = 0;
    182   1.17     lukem 
    183   1.64  christos 			if (ioctl(fd, FIONBIO, &zero) == -1)
    184   1.65  christos 				return -1;
    185   1.17     lukem 			else
    186   1.17     lukem 				e = 1;
    187   1.17     lukem 		}
    188   1.17     lukem #endif /* FIONBIO */
    189    1.1       cgd 
    190    1.1       cgd #endif /* TRY_AGAIN */
    191   1.65  christos 		return e ? 0 : -1;
    192    1.1       cgd 
    193   1.17     lukem 	case EINTR:
    194   1.65  christos 		return 0;
    195    1.1       cgd 
    196   1.17     lukem 	default:
    197   1.65  christos 		return -1;
    198   1.17     lukem 	}
    199    1.1       cgd }
    200    1.1       cgd 
    201    1.1       cgd 
    202    1.1       cgd /* el_push():
    203    1.1       cgd  *	Push a macro
    204    1.1       cgd  */
    205   1.91  christos void
    206   1.90  christos el_wpush(EditLine *el, const wchar_t *str)
    207    1.1       cgd {
    208   1.97  christos 	struct macros *ma = &el->el_read->macros;
    209    1.1       cgd 
    210   1.17     lukem 	if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
    211   1.17     lukem 		ma->level++;
    212   1.89  christos 		if ((ma->macro[ma->level] = wcsdup(str)) != NULL)
    213   1.30  christos 			return;
    214   1.30  christos 		ma->level--;
    215   1.17     lukem 	}
    216   1.62  christos 	terminal_beep(el);
    217   1.62  christos 	terminal__flush(el);
    218    1.1       cgd }
    219    1.1       cgd 
    220    1.1       cgd 
    221    1.1       cgd /* read_getcmd():
    222   1.92  christos  *	Get next command from the input stream,
    223   1.92  christos  *	return 0 on success or -1 on EOF or error.
    224   1.53  christos  *	Character values > 255 are not looked up in the map, but inserted.
    225    1.1       cgd  */
    226   1.91  christos static int
    227   1.90  christos read_getcmd(EditLine *el, el_action_t *cmdnum, wchar_t *ch)
    228    1.1       cgd {
    229   1.90  christos 	static const wchar_t meta = (wchar_t)0x80;
    230   1.23  christos 	el_action_t cmd;
    231   1.17     lukem 	int num;
    232    1.1       cgd 
    233   1.23  christos 	do {
    234   1.98  christos 		if ((num = el_wgetc(el, ch)) != 1)
    235   1.92  christos 			return -1;
    236    1.1       cgd 
    237    1.1       cgd #ifdef	KANJI
    238   1.75  christos 		if ((*ch & meta)) {
    239   1.17     lukem 			el->el_state.metanext = 0;
    240   1.17     lukem 			cmd = CcViMap[' '];
    241   1.17     lukem 			break;
    242   1.17     lukem 		} else
    243    1.1       cgd #endif /* KANJI */
    244    1.1       cgd 
    245   1.17     lukem 		if (el->el_state.metanext) {
    246   1.17     lukem 			el->el_state.metanext = 0;
    247   1.75  christos 			*ch |= meta;
    248   1.17     lukem 		}
    249   1.66  christos 		if (*ch >= N_KEYS)
    250   1.66  christos 			cmd = ED_INSERT;
    251   1.53  christos 		else
    252   1.66  christos 			cmd = el->el_map.current[(unsigned char) *ch];
    253   1.17     lukem 		if (cmd == ED_SEQUENCE_LEAD_IN) {
    254   1.63  christos 			keymacro_value_t val;
    255   1.63  christos 			switch (keymacro_get(el, ch, &val)) {
    256   1.17     lukem 			case XK_CMD:
    257   1.17     lukem 				cmd = val.cmd;
    258   1.17     lukem 				break;
    259   1.17     lukem 			case XK_STR:
    260   1.89  christos 				el_wpush(el, val.str);
    261   1.17     lukem 				break;
    262   1.98  christos 			case XK_NOD:
    263   1.98  christos 				return -1;
    264   1.17     lukem 			default:
    265   1.18  christos 				EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
    266   1.17     lukem 				break;
    267   1.17     lukem 			}
    268   1.17     lukem 		}
    269   1.23  christos 	} while (cmd == ED_SEQUENCE_LEAD_IN);
    270   1.17     lukem 	*cmdnum = cmd;
    271   1.92  christos 	return 0;
    272    1.1       cgd }
    273    1.1       cgd 
    274    1.6  christos /* read_char():
    275    1.6  christos  *	Read a character from the tty.
    276    1.6  christos  */
    277   1.91  christos static int
    278   1.84  christos read_char(EditLine *el, wchar_t *cp)
    279    1.6  christos {
    280   1.45  christos 	ssize_t num_read;
    281   1.17     lukem 	int tried = 0;
    282   1.66  christos 	char cbuf[MB_LEN_MAX];
    283   1.66  christos 	size_t cbp = 0;
    284   1.72  christos 	int save_errno = errno;
    285    1.6  christos 
    286   1.46  christos  again:
    287   1.46  christos 	el->el_signal->sig_no = 0;
    288   1.66  christos 	while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
    289   1.68  christos 		int e = errno;
    290   1.56  christos 		switch (el->el_signal->sig_no) {
    291   1.56  christos 		case SIGCONT:
    292   1.89  christos 			el_wset(el, EL_REFRESH);
    293   1.57  christos 			/*FALLTHROUGH*/
    294   1.56  christos 		case SIGWINCH:
    295   1.46  christos 			sig_set(el);
    296   1.46  christos 			goto again;
    297   1.56  christos 		default:
    298   1.56  christos 			break;
    299   1.46  christos 		}
    300   1.72  christos 		if (!tried && read__fixio(el->el_infd, e) == 0) {
    301   1.72  christos 			errno = save_errno;
    302   1.17     lukem 			tried = 1;
    303   1.72  christos 		} else {
    304   1.68  christos 			errno = e;
    305   1.84  christos 			*cp = L'\0';
    306   1.65  christos 			return -1;
    307   1.17     lukem 		}
    308   1.46  christos 	}
    309   1.53  christos 
    310   1.70  christos 	/* Test for EOF */
    311   1.70  christos 	if (num_read == 0) {
    312   1.84  christos 		*cp = L'\0';
    313   1.70  christos 		return 0;
    314   1.70  christos 	}
    315   1.70  christos 
    316   1.77  christos 	for (;;) {
    317   1.88  christos 		mbstate_t mbs;
    318   1.77  christos 
    319   1.53  christos 		++cbp;
    320   1.88  christos 		/* This only works because UTF8 is stateless. */
    321   1.88  christos 		memset(&mbs, 0, sizeof(mbs));
    322   1.88  christos 		switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
    323   1.72  christos 		case (size_t)-1:
    324   1.72  christos 			if (cbp > 1) {
    325   1.72  christos 				/*
    326   1.72  christos 				 * Invalid sequence, discard all bytes
    327   1.72  christos 				 * except the last one.
    328   1.72  christos 				 */
    329   1.72  christos 				cbuf[0] = cbuf[cbp - 1];
    330   1.72  christos 				cbp = 0;
    331   1.77  christos 				break;
    332   1.72  christos 			} else {
    333   1.72  christos 				/* Invalid byte, discard it. */
    334   1.72  christos 				cbp = 0;
    335   1.72  christos 				goto again;
    336   1.72  christos 			}
    337   1.72  christos 		case (size_t)-2:
    338   1.75  christos 			/*
    339   1.75  christos 			 * We don't support other multibyte charsets.
    340   1.75  christos 			 * The second condition shouldn't happen
    341   1.75  christos 			 * and is here merely for additional safety.
    342   1.75  christos 			 */
    343   1.75  christos 			if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
    344   1.75  christos 			    cbp >= MB_LEN_MAX) {
    345   1.68  christos 				errno = EILSEQ;
    346   1.84  christos 				*cp = L'\0';
    347   1.65  christos 				return -1;
    348   1.53  christos 			}
    349   1.72  christos 			/* Incomplete sequence, read another byte. */
    350   1.53  christos 			goto again;
    351   1.72  christos 		default:
    352   1.72  christos 			/* Valid character, process it. */
    353   1.77  christos 			return 1;
    354   1.53  christos 		}
    355   1.77  christos 	}
    356    1.6  christos }
    357    1.6  christos 
    358   1.40  christos /* read_pop():
    359   1.40  christos  *	Pop a macro from the stack
    360   1.40  christos  */
    361   1.91  christos static void
    362   1.97  christos read_pop(struct macros *ma)
    363   1.40  christos {
    364   1.40  christos 	int i;
    365   1.40  christos 
    366   1.40  christos 	el_free(ma->macro[0]);
    367   1.50  christos 	for (i = 0; i < ma->level; i++)
    368   1.50  christos 		ma->macro[i] = ma->macro[i + 1];
    369   1.51  christos 	ma->level--;
    370   1.40  christos 	ma->offset = 0;
    371   1.40  christos }
    372    1.1       cgd 
    373   1.97  christos static void
    374   1.97  christos read_clearmacros(struct macros *ma)
    375   1.97  christos {
    376   1.97  christos 	while (ma->level >= 0)
    377   1.97  christos 		el_free(ma->macro[ma->level--]);
    378   1.97  christos 	ma->offset = 0;
    379   1.97  christos }
    380   1.97  christos 
    381   1.83  christos /* el_wgetc():
    382   1.83  christos  *	Read a wide character
    383    1.1       cgd  */
    384   1.91  christos int
    385   1.83  christos el_wgetc(EditLine *el, wchar_t *cp)
    386    1.1       cgd {
    387   1.97  christos 	struct macros *ma = &el->el_read->macros;
    388   1.17     lukem 	int num_read;
    389    1.1       cgd 
    390   1.62  christos 	terminal__flush(el);
    391   1.17     lukem 	for (;;) {
    392   1.17     lukem 		if (ma->level < 0)
    393   1.17     lukem 			break;
    394   1.12    simonb 
    395   1.40  christos 		if (ma->macro[0][ma->offset] == '\0') {
    396   1.40  christos 			read_pop(ma);
    397   1.17     lukem 			continue;
    398   1.17     lukem 		}
    399   1.40  christos 
    400   1.53  christos 		*cp = ma->macro[0][ma->offset++];
    401   1.40  christos 
    402   1.40  christos 		if (ma->macro[0][ma->offset] == '\0') {
    403   1.30  christos 			/* Needed for QuoteMode On */
    404   1.40  christos 			read_pop(ma);
    405   1.17     lukem 		}
    406   1.40  christos 
    407   1.65  christos 		return 1;
    408    1.1       cgd 	}
    409    1.1       cgd 
    410   1.17     lukem 	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
    411   1.65  christos 		return 0;
    412    1.1       cgd 
    413   1.95  christos 	num_read = (*el->el_read->read_char)(el, cp);
    414   1.98  christos 
    415   1.98  christos 	/*
    416   1.98  christos 	 * Remember the original reason of a read failure
    417   1.98  christos 	 * such that el_wgets() can restore it after doing
    418   1.98  christos 	 * various cleanup operation that might change errno.
    419   1.98  christos 	 */
    420   1.68  christos 	if (num_read < 0)
    421   1.98  christos 		el->el_read->read_errno = errno;
    422   1.98  christos 
    423   1.65  christos 	return num_read;
    424    1.1       cgd }
    425    1.1       cgd 
    426   1.96  christos libedit_private void
    427   1.33  christos read_prepare(EditLine *el)
    428   1.28  christos {
    429   1.28  christos 	if (el->el_flags & HANDLE_SIGNALS)
    430   1.28  christos 		sig_set(el);
    431   1.28  christos 	if (el->el_flags & NO_TTY)
    432   1.28  christos 		return;
    433   1.28  christos 	if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
    434   1.28  christos 		tty_rawmode(el);
    435   1.28  christos 
    436   1.28  christos 	/* This is relatively cheap, and things go terribly wrong if
    437   1.28  christos 	   we have the wrong size. */
    438   1.28  christos 	el_resize(el);
    439   1.28  christos 	re_clear_display(el);	/* reset the display stuff */
    440   1.97  christos 	ch_reset(el);
    441   1.28  christos 	re_refresh(el);		/* print the prompt */
    442   1.35  christos 
    443   1.35  christos 	if (el->el_flags & UNBUFFERED)
    444   1.62  christos 		terminal__flush(el);
    445   1.28  christos }
    446   1.28  christos 
    447   1.96  christos libedit_private void
    448   1.28  christos read_finish(EditLine *el)
    449   1.28  christos {
    450   1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0)
    451   1.28  christos 		(void) tty_cookedmode(el);
    452   1.28  christos 	if (el->el_flags & HANDLE_SIGNALS)
    453   1.28  christos 		sig_clr(el);
    454   1.28  christos }
    455    1.1       cgd 
    456  1.101  christos static const wchar_t *
    457  1.101  christos noedit_wgets(EditLine *el, int *nread)
    458  1.101  christos {
    459  1.101  christos 	el_line_t	*lp = &el->el_line;
    460  1.101  christos 	int		 num;
    461  1.101  christos 
    462  1.101  christos 	while ((num = (*el->el_read->read_char)(el, lp->lastchar)) == 1) {
    463  1.101  christos 		if (lp->lastchar + 1 >= lp->limit &&
    464  1.101  christos 		    !ch_enlargebufs(el, (size_t)2))
    465  1.101  christos 			break;
    466  1.101  christos 		lp->lastchar++;
    467  1.101  christos 		if (el->el_flags & UNBUFFERED ||
    468  1.101  christos 		    lp->lastchar[-1] == '\r' ||
    469  1.101  christos 		    lp->lastchar[-1] == '\n')
    470  1.101  christos 			break;
    471  1.101  christos 	}
    472  1.101  christos 	if (num == -1 && errno == EINTR)
    473  1.101  christos 		lp->lastchar = lp->buffer;
    474  1.101  christos 	lp->cursor = lp->lastchar;
    475  1.101  christos 	*lp->lastchar = '\0';
    476  1.101  christos 	*nread = (int)(lp->lastchar - lp->buffer);
    477  1.101  christos 	return *nread ? lp->buffer : NULL;
    478  1.101  christos }
    479  1.101  christos 
    480   1.91  christos const wchar_t *
    481   1.89  christos el_wgets(EditLine *el, int *nread)
    482    1.1       cgd {
    483   1.17     lukem 	int retval;
    484   1.17     lukem 	el_action_t cmdnum = 0;
    485   1.17     lukem 	int num;		/* how many chars we have read at NL */
    486  1.101  christos 	wchar_t ch;
    487   1.49  christos 	int nrb;
    488    1.1       cgd 
    489   1.49  christos 	if (nread == NULL)
    490   1.49  christos 		nread = &nrb;
    491   1.52  christos 	*nread = 0;
    492   1.98  christos 	el->el_read->read_errno = 0;
    493   1.49  christos 
    494   1.17     lukem 	if (el->el_flags & NO_TTY) {
    495  1.101  christos 		el->el_line.lastchar = el->el_line.buffer;
    496  1.101  christos 		return noedit_wgets(el, nread);
    497    1.7  christos 	}
    498   1.22  christos 
    499    1.1       cgd #ifdef FIONREAD
    500   1.97  christos 	if (el->el_tty.t_mode == EX_IO && el->el_read->macros.level < 0) {
    501   1.93  christos 		int chrs = 0;
    502    1.1       cgd 
    503   1.64  christos 		(void) ioctl(el->el_infd, FIONREAD, &chrs);
    504   1.17     lukem 		if (chrs == 0) {
    505   1.17     lukem 			if (tty_rawmode(el) < 0) {
    506   1.49  christos 				errno = 0;
    507   1.49  christos 				*nread = 0;
    508   1.65  christos 				return NULL;
    509   1.17     lukem 			}
    510   1.17     lukem 		}
    511    1.1       cgd 	}
    512    1.1       cgd #endif /* FIONREAD */
    513    1.1       cgd 
    514   1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0)
    515   1.28  christos 		read_prepare(el);
    516   1.13  sommerfe 
    517   1.17     lukem 	if (el->el_flags & EDIT_DISABLED) {
    518   1.34  christos 		if ((el->el_flags & UNBUFFERED) == 0)
    519  1.101  christos 			el->el_line.lastchar = el->el_line.buffer;
    520   1.62  christos 		terminal__flush(el);
    521  1.101  christos 		return noedit_wgets(el, nread);
    522   1.13  sommerfe 	}
    523   1.22  christos 
    524   1.92  christos 	for (num = -1; num == -1;) {  /* while still editing this line */
    525   1.17     lukem 		/* if EOF or error */
    526   1.99  christos 		if (read_getcmd(el, &cmdnum, &ch) == -1)
    527   1.17     lukem 			break;
    528   1.99  christos 		if ((size_t)cmdnum >= el->el_map.nfunc) /* BUG CHECK command */
    529   1.17     lukem 			continue;	/* try again */
    530   1.17     lukem 		/* now do the real command */
    531   1.23  christos 		/* vi redo needs these way down the levels... */
    532   1.23  christos 		el->el_state.thiscmd = cmdnum;
    533   1.23  christos 		el->el_state.thisch = ch;
    534   1.23  christos 		if (el->el_map.type == MAP_VI &&
    535   1.23  christos 		    el->el_map.current == el->el_map.key &&
    536   1.23  christos 		    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
    537   1.23  christos 			if (cmdnum == VI_DELETE_PREV_CHAR &&
    538   1.23  christos 			    el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
    539   1.88  christos 			    && iswprint(el->el_chared.c_redo.pos[-1]))
    540   1.23  christos 				el->el_chared.c_redo.pos--;
    541   1.23  christos 			else
    542   1.23  christos 				*el->el_chared.c_redo.pos++ = ch;
    543   1.23  christos 		}
    544   1.17     lukem 		retval = (*el->el_map.func[cmdnum]) (el, ch);
    545   1.17     lukem 
    546   1.17     lukem 		/* save the last command here */
    547   1.17     lukem 		el->el_state.lastcmd = cmdnum;
    548    1.1       cgd 
    549   1.17     lukem 		/* use any return value */
    550   1.17     lukem 		switch (retval) {
    551   1.17     lukem 		case CC_CURSOR:
    552   1.17     lukem 			re_refresh_cursor(el);
    553   1.17     lukem 			break;
    554   1.17     lukem 
    555   1.17     lukem 		case CC_REDISPLAY:
    556   1.17     lukem 			re_clear_lines(el);
    557   1.17     lukem 			re_clear_display(el);
    558   1.17     lukem 			/* FALLTHROUGH */
    559   1.17     lukem 
    560   1.17     lukem 		case CC_REFRESH:
    561   1.17     lukem 			re_refresh(el);
    562   1.17     lukem 			break;
    563   1.17     lukem 
    564   1.17     lukem 		case CC_REFRESH_BEEP:
    565   1.17     lukem 			re_refresh(el);
    566   1.62  christos 			terminal_beep(el);
    567   1.17     lukem 			break;
    568   1.17     lukem 
    569   1.17     lukem 		case CC_NORM:	/* normal char */
    570   1.17     lukem 			break;
    571    1.1       cgd 
    572   1.17     lukem 		case CC_ARGHACK:	/* Suggested by Rich Salz */
    573   1.17     lukem 			/* <rsalz (at) pineapple.bbn.com> */
    574   1.23  christos 			continue;	/* keep going... */
    575    1.1       cgd 
    576   1.17     lukem 		case CC_EOF:	/* end of file typed */
    577   1.29  christos 			if ((el->el_flags & UNBUFFERED) == 0)
    578   1.29  christos 				num = 0;
    579   1.29  christos 			else if (num == -1) {
    580   1.31  christos 				*el->el_line.lastchar++ = CONTROL('d');
    581   1.29  christos 				el->el_line.cursor = el->el_line.lastchar;
    582   1.29  christos 				num = 1;
    583   1.29  christos 			}
    584   1.17     lukem 			break;
    585   1.17     lukem 
    586   1.17     lukem 		case CC_NEWLINE:	/* normal end of line */
    587   1.45  christos 			num = (int)(el->el_line.lastchar - el->el_line.buffer);
    588   1.17     lukem 			break;
    589   1.17     lukem 
    590   1.17     lukem 		case CC_FATAL:	/* fatal error, reset to known state */
    591   1.17     lukem 			/* put (real) cursor in a known place */
    592   1.17     lukem 			re_clear_display(el);	/* reset the display stuff */
    593   1.97  christos 			ch_reset(el);	/* reset the input pointers */
    594   1.97  christos 			read_clearmacros(&el->el_read->macros);
    595   1.66  christos 			re_refresh(el); /* print the prompt again */
    596   1.17     lukem 			break;
    597    1.1       cgd 
    598   1.17     lukem 		case CC_ERROR:
    599   1.17     lukem 		default:	/* functions we don't know about */
    600   1.62  christos 			terminal_beep(el);
    601   1.62  christos 			terminal__flush(el);
    602   1.17     lukem 			break;
    603   1.17     lukem 		}
    604   1.23  christos 		el->el_state.argument = 1;
    605   1.23  christos 		el->el_state.doingarg = 0;
    606   1.23  christos 		el->el_chared.c_vcmd.action = NOP;
    607   1.28  christos 		if (el->el_flags & UNBUFFERED)
    608   1.28  christos 			break;
    609    1.1       cgd 	}
    610    1.1       cgd 
    611   1.62  christos 	terminal__flush(el);		/* flush any buffered output */
    612   1.22  christos 	/* make sure the tty is set up correctly */
    613   1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0) {
    614   1.28  christos 		read_finish(el);
    615   1.49  christos 		*nread = num != -1 ? num : 0;
    616  1.101  christos 	} else
    617   1.49  christos 		*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
    618  1.101  christos 
    619   1.49  christos 	if (*nread == 0) {
    620   1.49  christos 		if (num == -1) {
    621   1.49  christos 			*nread = -1;
    622   1.98  christos 			if (el->el_read->read_errno)
    623   1.98  christos 				errno = el->el_read->read_errno;
    624   1.49  christos 		}
    625   1.49  christos 		return NULL;
    626   1.49  christos 	} else
    627   1.49  christos 		return el->el_line.buffer;
    628    1.1       cgd }
    629