Home | History | Annotate | Line # | Download | only in libedit
read.c revision 1.94
      1  1.94  christos /*	$NetBSD: read.c,v 1.94 2016/04/18 17:01:19 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.94  christos __RCSID("$NetBSD: read.c,v 1.94 2016/04/18 17:01:19 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.1       cgd  * read.c: Clean this junk up! This is horrible code.
     46   1.1       cgd  *	   Terminal read functions
     47   1.1       cgd  */
     48  1.81  christos #include <ctype.h>
     49  1.11    kleink #include <errno.h>
     50  1.27   mycroft #include <fcntl.h>
     51  1.82  christos #include <limits.h>
     52   1.1       cgd #include <stdlib.h>
     53  1.81  christos #include <string.h>
     54  1.82  christos #include <unistd.h>
     55  1.81  christos 
     56   1.1       cgd #include "el.h"
     57  1.94  christos #include "fcns.h"
     58   1.1       cgd 
     59  1.91  christos static int	read__fixio(int, int);
     60  1.91  christos static int	read_char(EditLine *, wchar_t *);
     61  1.91  christos static int	read_getcmd(EditLine *, el_action_t *, wchar_t *);
     62  1.91  christos static void	read_pop(c_macro_t *);
     63  1.20  christos 
     64  1.20  christos /* read_init():
     65  1.20  christos  *	Initialize the read stuff
     66  1.20  christos  */
     67  1.20  christos protected int
     68  1.20  christos read_init(EditLine *el)
     69  1.20  christos {
     70  1.20  christos 	/* builtin read_char */
     71  1.20  christos 	el->el_read.read_char = read_char;
     72  1.20  christos 	return 0;
     73  1.20  christos }
     74  1.20  christos 
     75  1.20  christos 
     76  1.20  christos /* el_read_setfn():
     77  1.20  christos  *	Set the read char function to the one provided.
     78  1.20  christos  *	If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
     79  1.20  christos  */
     80  1.20  christos protected int
     81  1.20  christos el_read_setfn(EditLine *el, el_rfunc_t rc)
     82  1.20  christos {
     83  1.20  christos 	el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
     84  1.20  christos 	return 0;
     85  1.20  christos }
     86  1.20  christos 
     87  1.20  christos 
     88  1.20  christos /* el_read_getfn():
     89  1.20  christos  *	return the current read char function, or EL_BUILTIN_GETCFN
     90  1.20  christos  *	if it is the default one
     91  1.20  christos  */
     92  1.20  christos protected el_rfunc_t
     93  1.20  christos el_read_getfn(EditLine *el)
     94  1.20  christos {
     95  1.65  christos        return el->el_read.read_char == read_char ?
     96  1.20  christos 	    EL_BUILTIN_GETCFN : el->el_read.read_char;
     97  1.20  christos }
     98  1.20  christos 
     99   1.1       cgd 
    100   1.1       cgd #ifdef DEBUG_EDIT
    101  1.91  christos static void
    102  1.17     lukem read_debug(EditLine *el)
    103   1.1       cgd {
    104   1.1       cgd 
    105  1.17     lukem 	if (el->el_line.cursor > el->el_line.lastchar)
    106  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
    107  1.17     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    108  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
    109  1.17     lukem 	if (el->el_line.cursor > el->el_line.limit)
    110  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor > limit\r\n");
    111  1.17     lukem 	if (el->el_line.lastchar > el->el_line.limit)
    112  1.17     lukem 		(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
    113  1.17     lukem 	if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
    114  1.17     lukem 		(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
    115   1.1       cgd }
    116   1.1       cgd #endif /* DEBUG_EDIT */
    117   1.1       cgd 
    118  1.17     lukem 
    119   1.1       cgd /* read__fixio():
    120   1.1       cgd  *	Try to recover from a read error
    121   1.1       cgd  */
    122  1.10  christos /* ARGSUSED */
    123  1.91  christos static int
    124  1.25  christos read__fixio(int fd __attribute__((__unused__)), int e)
    125   1.1       cgd {
    126  1.17     lukem 
    127  1.17     lukem 	switch (e) {
    128  1.17     lukem 	case -1:		/* Make sure that the code is reachable */
    129   1.1       cgd 
    130   1.1       cgd #ifdef EWOULDBLOCK
    131  1.17     lukem 	case EWOULDBLOCK:
    132  1.17     lukem #ifndef TRY_AGAIN
    133  1.66  christos #define TRY_AGAIN
    134  1.17     lukem #endif
    135   1.1       cgd #endif /* EWOULDBLOCK */
    136   1.1       cgd 
    137   1.1       cgd #if defined(POSIX) && defined(EAGAIN)
    138  1.17     lukem #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
    139  1.17     lukem 	case EAGAIN:
    140  1.17     lukem #ifndef TRY_AGAIN
    141  1.66  christos #define TRY_AGAIN
    142  1.17     lukem #endif
    143  1.17     lukem #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
    144   1.1       cgd #endif /* POSIX && EAGAIN */
    145   1.1       cgd 
    146  1.17     lukem 		e = 0;
    147   1.1       cgd #ifdef TRY_AGAIN
    148  1.17     lukem #if defined(F_SETFL) && defined(O_NDELAY)
    149  1.17     lukem 		if ((e = fcntl(fd, F_GETFL, 0)) == -1)
    150  1.65  christos 			return -1;
    151   1.5  christos 
    152  1.17     lukem 		if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
    153  1.65  christos 			return -1;
    154   1.5  christos 		else
    155  1.17     lukem 			e = 1;
    156  1.17     lukem #endif /* F_SETFL && O_NDELAY */
    157  1.17     lukem 
    158  1.17     lukem #ifdef FIONBIO
    159  1.17     lukem 		{
    160  1.17     lukem 			int zero = 0;
    161  1.17     lukem 
    162  1.64  christos 			if (ioctl(fd, FIONBIO, &zero) == -1)
    163  1.65  christos 				return -1;
    164  1.17     lukem 			else
    165  1.17     lukem 				e = 1;
    166  1.17     lukem 		}
    167  1.17     lukem #endif /* FIONBIO */
    168   1.1       cgd 
    169   1.1       cgd #endif /* TRY_AGAIN */
    170  1.65  christos 		return e ? 0 : -1;
    171   1.1       cgd 
    172  1.17     lukem 	case EINTR:
    173  1.65  christos 		return 0;
    174   1.1       cgd 
    175  1.17     lukem 	default:
    176  1.65  christos 		return -1;
    177  1.17     lukem 	}
    178   1.1       cgd }
    179   1.1       cgd 
    180   1.1       cgd 
    181   1.1       cgd /* el_push():
    182   1.1       cgd  *	Push a macro
    183   1.1       cgd  */
    184  1.91  christos void
    185  1.90  christos el_wpush(EditLine *el, const wchar_t *str)
    186   1.1       cgd {
    187  1.17     lukem 	c_macro_t *ma = &el->el_chared.c_macro;
    188   1.1       cgd 
    189  1.17     lukem 	if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
    190  1.17     lukem 		ma->level++;
    191  1.89  christos 		if ((ma->macro[ma->level] = wcsdup(str)) != NULL)
    192  1.30  christos 			return;
    193  1.30  christos 		ma->level--;
    194  1.17     lukem 	}
    195  1.62  christos 	terminal_beep(el);
    196  1.62  christos 	terminal__flush(el);
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd 
    200   1.1       cgd /* read_getcmd():
    201  1.92  christos  *	Get next command from the input stream,
    202  1.92  christos  *	return 0 on success or -1 on EOF or error.
    203  1.53  christos  *	Character values > 255 are not looked up in the map, but inserted.
    204   1.1       cgd  */
    205  1.91  christos static int
    206  1.90  christos read_getcmd(EditLine *el, el_action_t *cmdnum, wchar_t *ch)
    207   1.1       cgd {
    208  1.90  christos 	static const wchar_t meta = (wchar_t)0x80;
    209  1.23  christos 	el_action_t cmd;
    210  1.17     lukem 	int num;
    211   1.1       cgd 
    212  1.44  christos 	el->el_errno = 0;
    213  1.23  christos 	do {
    214  1.90  christos 		if ((num = el_wgetc(el, ch)) != 1) {/* if EOF or error */
    215  1.49  christos 			el->el_errno = num == 0 ? 0 : errno;
    216  1.92  christos 			return -1;
    217  1.44  christos 		}
    218   1.1       cgd 
    219   1.1       cgd #ifdef	KANJI
    220  1.75  christos 		if ((*ch & meta)) {
    221  1.17     lukem 			el->el_state.metanext = 0;
    222  1.17     lukem 			cmd = CcViMap[' '];
    223  1.17     lukem 			break;
    224  1.17     lukem 		} else
    225   1.1       cgd #endif /* KANJI */
    226   1.1       cgd 
    227  1.17     lukem 		if (el->el_state.metanext) {
    228  1.17     lukem 			el->el_state.metanext = 0;
    229  1.75  christos 			*ch |= meta;
    230  1.17     lukem 		}
    231  1.66  christos 		if (*ch >= N_KEYS)
    232  1.66  christos 			cmd = ED_INSERT;
    233  1.53  christos 		else
    234  1.66  christos 			cmd = el->el_map.current[(unsigned char) *ch];
    235  1.17     lukem 		if (cmd == ED_SEQUENCE_LEAD_IN) {
    236  1.63  christos 			keymacro_value_t val;
    237  1.63  christos 			switch (keymacro_get(el, ch, &val)) {
    238  1.17     lukem 			case XK_CMD:
    239  1.17     lukem 				cmd = val.cmd;
    240  1.17     lukem 				break;
    241  1.17     lukem 			case XK_STR:
    242  1.89  christos 				el_wpush(el, val.str);
    243  1.17     lukem 				break;
    244  1.17     lukem 			default:
    245  1.18  christos 				EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
    246  1.17     lukem 				break;
    247  1.17     lukem 			}
    248  1.17     lukem 		}
    249  1.17     lukem 		if (el->el_map.alt == NULL)
    250  1.17     lukem 			el->el_map.current = el->el_map.key;
    251  1.23  christos 	} while (cmd == ED_SEQUENCE_LEAD_IN);
    252  1.17     lukem 	*cmdnum = cmd;
    253  1.92  christos 	return 0;
    254   1.1       cgd }
    255   1.1       cgd 
    256   1.6  christos /* read_char():
    257   1.6  christos  *	Read a character from the tty.
    258   1.6  christos  */
    259  1.91  christos static int
    260  1.84  christos read_char(EditLine *el, wchar_t *cp)
    261   1.6  christos {
    262  1.45  christos 	ssize_t num_read;
    263  1.17     lukem 	int tried = 0;
    264  1.66  christos 	char cbuf[MB_LEN_MAX];
    265  1.66  christos 	size_t cbp = 0;
    266  1.72  christos 	int save_errno = errno;
    267   1.6  christos 
    268  1.46  christos  again:
    269  1.46  christos 	el->el_signal->sig_no = 0;
    270  1.66  christos 	while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
    271  1.68  christos 		int e = errno;
    272  1.56  christos 		switch (el->el_signal->sig_no) {
    273  1.56  christos 		case SIGCONT:
    274  1.89  christos 			el_wset(el, EL_REFRESH);
    275  1.57  christos 			/*FALLTHROUGH*/
    276  1.56  christos 		case SIGWINCH:
    277  1.46  christos 			sig_set(el);
    278  1.46  christos 			goto again;
    279  1.56  christos 		default:
    280  1.56  christos 			break;
    281  1.46  christos 		}
    282  1.72  christos 		if (!tried && read__fixio(el->el_infd, e) == 0) {
    283  1.72  christos 			errno = save_errno;
    284  1.17     lukem 			tried = 1;
    285  1.72  christos 		} else {
    286  1.68  christos 			errno = e;
    287  1.84  christos 			*cp = L'\0';
    288  1.65  christos 			return -1;
    289  1.17     lukem 		}
    290  1.46  christos 	}
    291  1.53  christos 
    292  1.70  christos 	/* Test for EOF */
    293  1.70  christos 	if (num_read == 0) {
    294  1.84  christos 		*cp = L'\0';
    295  1.70  christos 		return 0;
    296  1.70  christos 	}
    297  1.70  christos 
    298  1.77  christos 	for (;;) {
    299  1.88  christos 		mbstate_t mbs;
    300  1.77  christos 
    301  1.53  christos 		++cbp;
    302  1.88  christos 		/* This only works because UTF8 is stateless. */
    303  1.88  christos 		memset(&mbs, 0, sizeof(mbs));
    304  1.88  christos 		switch (mbrtowc(cp, cbuf, cbp, &mbs)) {
    305  1.72  christos 		case (size_t)-1:
    306  1.72  christos 			if (cbp > 1) {
    307  1.72  christos 				/*
    308  1.72  christos 				 * Invalid sequence, discard all bytes
    309  1.72  christos 				 * except the last one.
    310  1.72  christos 				 */
    311  1.72  christos 				cbuf[0] = cbuf[cbp - 1];
    312  1.72  christos 				cbp = 0;
    313  1.77  christos 				break;
    314  1.72  christos 			} else {
    315  1.72  christos 				/* Invalid byte, discard it. */
    316  1.72  christos 				cbp = 0;
    317  1.72  christos 				goto again;
    318  1.72  christos 			}
    319  1.72  christos 		case (size_t)-2:
    320  1.75  christos 			/*
    321  1.75  christos 			 * We don't support other multibyte charsets.
    322  1.75  christos 			 * The second condition shouldn't happen
    323  1.75  christos 			 * and is here merely for additional safety.
    324  1.75  christos 			 */
    325  1.75  christos 			if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
    326  1.75  christos 			    cbp >= MB_LEN_MAX) {
    327  1.68  christos 				errno = EILSEQ;
    328  1.84  christos 				*cp = L'\0';
    329  1.65  christos 				return -1;
    330  1.53  christos 			}
    331  1.72  christos 			/* Incomplete sequence, read another byte. */
    332  1.53  christos 			goto again;
    333  1.72  christos 		default:
    334  1.72  christos 			/* Valid character, process it. */
    335  1.77  christos 			return 1;
    336  1.53  christos 		}
    337  1.77  christos 	}
    338   1.6  christos }
    339   1.6  christos 
    340  1.40  christos /* read_pop():
    341  1.40  christos  *	Pop a macro from the stack
    342  1.40  christos  */
    343  1.91  christos static void
    344  1.40  christos read_pop(c_macro_t *ma)
    345  1.40  christos {
    346  1.40  christos 	int i;
    347  1.40  christos 
    348  1.40  christos 	el_free(ma->macro[0]);
    349  1.50  christos 	for (i = 0; i < ma->level; i++)
    350  1.50  christos 		ma->macro[i] = ma->macro[i + 1];
    351  1.51  christos 	ma->level--;
    352  1.40  christos 	ma->offset = 0;
    353  1.40  christos }
    354   1.1       cgd 
    355  1.83  christos /* el_wgetc():
    356  1.83  christos  *	Read a wide character
    357   1.1       cgd  */
    358  1.91  christos int
    359  1.83  christos el_wgetc(EditLine *el, wchar_t *cp)
    360   1.1       cgd {
    361  1.17     lukem 	int num_read;
    362  1.17     lukem 	c_macro_t *ma = &el->el_chared.c_macro;
    363   1.1       cgd 
    364  1.62  christos 	terminal__flush(el);
    365  1.17     lukem 	for (;;) {
    366  1.17     lukem 		if (ma->level < 0)
    367  1.17     lukem 			break;
    368  1.12    simonb 
    369  1.40  christos 		if (ma->macro[0][ma->offset] == '\0') {
    370  1.40  christos 			read_pop(ma);
    371  1.17     lukem 			continue;
    372  1.17     lukem 		}
    373  1.40  christos 
    374  1.53  christos 		*cp = ma->macro[0][ma->offset++];
    375  1.40  christos 
    376  1.40  christos 		if (ma->macro[0][ma->offset] == '\0') {
    377  1.30  christos 			/* Needed for QuoteMode On */
    378  1.40  christos 			read_pop(ma);
    379  1.17     lukem 		}
    380  1.40  christos 
    381  1.65  christos 		return 1;
    382   1.1       cgd 	}
    383   1.1       cgd 
    384   1.1       cgd #ifdef DEBUG_READ
    385  1.17     lukem 	(void) fprintf(el->el_errfile, "Turning raw mode on\n");
    386   1.1       cgd #endif /* DEBUG_READ */
    387  1.17     lukem 	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
    388  1.65  christos 		return 0;
    389   1.1       cgd 
    390   1.1       cgd #ifdef DEBUG_READ
    391  1.17     lukem 	(void) fprintf(el->el_errfile, "Reading a character\n");
    392   1.1       cgd #endif /* DEBUG_READ */
    393  1.84  christos 	num_read = (*el->el_read.read_char)(el, cp);
    394  1.68  christos 	if (num_read < 0)
    395  1.68  christos 		el->el_errno = errno;
    396   1.1       cgd #ifdef DEBUG_READ
    397  1.83  christos 	(void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
    398   1.1       cgd #endif /* DEBUG_READ */
    399  1.65  christos 	return num_read;
    400   1.1       cgd }
    401   1.1       cgd 
    402  1.28  christos protected void
    403  1.33  christos read_prepare(EditLine *el)
    404  1.28  christos {
    405  1.28  christos 	if (el->el_flags & HANDLE_SIGNALS)
    406  1.28  christos 		sig_set(el);
    407  1.28  christos 	if (el->el_flags & NO_TTY)
    408  1.28  christos 		return;
    409  1.28  christos 	if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
    410  1.28  christos 		tty_rawmode(el);
    411  1.28  christos 
    412  1.28  christos 	/* This is relatively cheap, and things go terribly wrong if
    413  1.28  christos 	   we have the wrong size. */
    414  1.28  christos 	el_resize(el);
    415  1.28  christos 	re_clear_display(el);	/* reset the display stuff */
    416  1.37  christos 	ch_reset(el, 0);
    417  1.28  christos 	re_refresh(el);		/* print the prompt */
    418  1.35  christos 
    419  1.35  christos 	if (el->el_flags & UNBUFFERED)
    420  1.62  christos 		terminal__flush(el);
    421  1.28  christos }
    422  1.28  christos 
    423  1.28  christos protected void
    424  1.28  christos read_finish(EditLine *el)
    425  1.28  christos {
    426  1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0)
    427  1.28  christos 		(void) tty_cookedmode(el);
    428  1.28  christos 	if (el->el_flags & HANDLE_SIGNALS)
    429  1.28  christos 		sig_clr(el);
    430  1.28  christos }
    431   1.1       cgd 
    432  1.91  christos const wchar_t *
    433  1.89  christos el_wgets(EditLine *el, int *nread)
    434   1.1       cgd {
    435  1.17     lukem 	int retval;
    436  1.17     lukem 	el_action_t cmdnum = 0;
    437  1.17     lukem 	int num;		/* how many chars we have read at NL */
    438  1.84  christos 	wchar_t wc;
    439  1.90  christos 	wchar_t ch, *cp;
    440  1.28  christos 	int crlf = 0;
    441  1.49  christos 	int nrb;
    442   1.1       cgd 
    443  1.49  christos 	if (nread == NULL)
    444  1.49  christos 		nread = &nrb;
    445  1.52  christos 	*nread = 0;
    446  1.49  christos 
    447  1.17     lukem 	if (el->el_flags & NO_TTY) {
    448  1.19  jdolecek 		size_t idx;
    449   1.6  christos 
    450  1.58  christos 		cp = el->el_line.buffer;
    451  1.84  christos 		while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
    452  1.90  christos 			*cp = wc;
    453  1.19  jdolecek 			/* make sure there is space for next character */
    454  1.19  jdolecek 			if (cp + 1 >= el->el_line.limit) {
    455  1.67  christos 				idx = (size_t)(cp - el->el_line.buffer);
    456  1.66  christos 				if (!ch_enlargebufs(el, (size_t)2))
    457  1.19  jdolecek 					break;
    458  1.19  jdolecek 				cp = &el->el_line.buffer[idx];
    459  1.19  jdolecek 			}
    460  1.17     lukem 			cp++;
    461  1.28  christos 			if (el->el_flags & UNBUFFERED)
    462  1.28  christos 				break;
    463  1.17     lukem 			if (cp[-1] == '\r' || cp[-1] == '\n')
    464  1.17     lukem 				break;
    465   1.6  christos 		}
    466  1.49  christos 		if (num == -1) {
    467  1.49  christos 			if (errno == EINTR)
    468  1.49  christos 				cp = el->el_line.buffer;
    469  1.49  christos 			el->el_errno = errno;
    470  1.49  christos 		}
    471  1.19  jdolecek 
    472  1.58  christos 		goto noedit;
    473   1.7  christos 	}
    474  1.22  christos 
    475   1.1       cgd 
    476   1.1       cgd #ifdef FIONREAD
    477  1.92  christos 	if (el->el_tty.t_mode == EX_IO && el->el_chared.c_macro.level < 0) {
    478  1.93  christos 		int chrs = 0;
    479   1.1       cgd 
    480  1.64  christos 		(void) ioctl(el->el_infd, FIONREAD, &chrs);
    481  1.17     lukem 		if (chrs == 0) {
    482  1.17     lukem 			if (tty_rawmode(el) < 0) {
    483  1.49  christos 				errno = 0;
    484  1.49  christos 				*nread = 0;
    485  1.65  christos 				return NULL;
    486  1.17     lukem 			}
    487  1.17     lukem 		}
    488   1.1       cgd 	}
    489   1.1       cgd #endif /* FIONREAD */
    490   1.1       cgd 
    491  1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0)
    492  1.28  christos 		read_prepare(el);
    493  1.13  sommerfe 
    494  1.17     lukem 	if (el->el_flags & EDIT_DISABLED) {
    495  1.19  jdolecek 		size_t idx;
    496  1.44  christos 
    497  1.34  christos 		if ((el->el_flags & UNBUFFERED) == 0)
    498  1.34  christos 			cp = el->el_line.buffer;
    499  1.34  christos 		else
    500  1.34  christos 			cp = el->el_line.lastchar;
    501  1.13  sommerfe 
    502  1.62  christos 		terminal__flush(el);
    503  1.17     lukem 
    504  1.84  christos 		while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
    505  1.90  christos 			*cp = wc;
    506  1.19  jdolecek 			/* make sure there is space next character */
    507  1.19  jdolecek 			if (cp + 1 >= el->el_line.limit) {
    508  1.67  christos 				idx = (size_t)(cp - el->el_line.buffer);
    509  1.66  christos 				if (!ch_enlargebufs(el, (size_t)2))
    510  1.19  jdolecek 					break;
    511  1.19  jdolecek 				cp = &el->el_line.buffer[idx];
    512  1.19  jdolecek 			}
    513  1.17     lukem 			cp++;
    514  1.28  christos 			crlf = cp[-1] == '\r' || cp[-1] == '\n';
    515  1.28  christos 			if (el->el_flags & UNBUFFERED)
    516  1.28  christos 				break;
    517  1.28  christos 			if (crlf)
    518  1.17     lukem 				break;
    519  1.19  jdolecek 		}
    520  1.49  christos 
    521  1.49  christos 		if (num == -1) {
    522  1.49  christos 			if (errno == EINTR)
    523  1.49  christos 				cp = el->el_line.buffer;
    524  1.49  christos 			el->el_errno = errno;
    525  1.49  christos 		}
    526  1.13  sommerfe 
    527  1.58  christos 		goto noedit;
    528  1.13  sommerfe 	}
    529  1.22  christos 
    530  1.92  christos 	for (num = -1; num == -1;) {  /* while still editing this line */
    531   1.1       cgd #ifdef DEBUG_EDIT
    532  1.17     lukem 		read_debug(el);
    533   1.1       cgd #endif /* DEBUG_EDIT */
    534  1.17     lukem 		/* if EOF or error */
    535  1.92  christos 		if (read_getcmd(el, &cmdnum, &ch) == -1) {
    536   1.1       cgd #ifdef DEBUG_READ
    537  1.17     lukem 			(void) fprintf(el->el_errfile,
    538  1.92  christos 			    "Returning from el_gets\n");
    539   1.1       cgd #endif /* DEBUG_READ */
    540  1.17     lukem 			break;
    541  1.17     lukem 		}
    542  1.44  christos 		if (el->el_errno == EINTR) {
    543  1.44  christos 			el->el_line.buffer[0] = '\0';
    544  1.44  christos 			el->el_line.lastchar =
    545  1.44  christos 			    el->el_line.cursor = el->el_line.buffer;
    546  1.44  christos 			break;
    547  1.44  christos 		}
    548  1.71  christos 		if ((size_t)cmdnum >= el->el_map.nfunc) {	/* BUG CHECK command */
    549   1.1       cgd #ifdef DEBUG_EDIT
    550  1.17     lukem 			(void) fprintf(el->el_errfile,
    551  1.17     lukem 			    "ERROR: illegal command from key 0%o\r\n", ch);
    552   1.1       cgd #endif /* DEBUG_EDIT */
    553  1.17     lukem 			continue;	/* try again */
    554  1.17     lukem 		}
    555  1.17     lukem 		/* now do the real command */
    556   1.1       cgd #ifdef DEBUG_READ
    557  1.17     lukem 		{
    558  1.17     lukem 			el_bindings_t *b;
    559  1.17     lukem 			for (b = el->el_map.help; b->name; b++)
    560  1.17     lukem 				if (b->func == cmdnum)
    561  1.17     lukem 					break;
    562  1.17     lukem 			if (b->name)
    563  1.17     lukem 				(void) fprintf(el->el_errfile,
    564  1.88  christos 				    "Executing %ls\n", b->name);
    565  1.17     lukem 			else
    566  1.17     lukem 				(void) fprintf(el->el_errfile,
    567  1.17     lukem 				    "Error command = %d\n", cmdnum);
    568  1.17     lukem 		}
    569   1.1       cgd #endif /* DEBUG_READ */
    570  1.23  christos 		/* vi redo needs these way down the levels... */
    571  1.23  christos 		el->el_state.thiscmd = cmdnum;
    572  1.23  christos 		el->el_state.thisch = ch;
    573  1.23  christos 		if (el->el_map.type == MAP_VI &&
    574  1.23  christos 		    el->el_map.current == el->el_map.key &&
    575  1.23  christos 		    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
    576  1.23  christos 			if (cmdnum == VI_DELETE_PREV_CHAR &&
    577  1.23  christos 			    el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
    578  1.88  christos 			    && iswprint(el->el_chared.c_redo.pos[-1]))
    579  1.23  christos 				el->el_chared.c_redo.pos--;
    580  1.23  christos 			else
    581  1.23  christos 				*el->el_chared.c_redo.pos++ = ch;
    582  1.23  christos 		}
    583  1.17     lukem 		retval = (*el->el_map.func[cmdnum]) (el, ch);
    584  1.22  christos #ifdef DEBUG_READ
    585  1.22  christos 		(void) fprintf(el->el_errfile,
    586  1.22  christos 			"Returned state %d\n", retval );
    587  1.22  christos #endif /* DEBUG_READ */
    588  1.17     lukem 
    589  1.17     lukem 		/* save the last command here */
    590  1.17     lukem 		el->el_state.lastcmd = cmdnum;
    591   1.1       cgd 
    592  1.17     lukem 		/* use any return value */
    593  1.17     lukem 		switch (retval) {
    594  1.17     lukem 		case CC_CURSOR:
    595  1.17     lukem 			re_refresh_cursor(el);
    596  1.17     lukem 			break;
    597  1.17     lukem 
    598  1.17     lukem 		case CC_REDISPLAY:
    599  1.17     lukem 			re_clear_lines(el);
    600  1.17     lukem 			re_clear_display(el);
    601  1.17     lukem 			/* FALLTHROUGH */
    602  1.17     lukem 
    603  1.17     lukem 		case CC_REFRESH:
    604  1.17     lukem 			re_refresh(el);
    605  1.17     lukem 			break;
    606  1.17     lukem 
    607  1.17     lukem 		case CC_REFRESH_BEEP:
    608  1.17     lukem 			re_refresh(el);
    609  1.62  christos 			terminal_beep(el);
    610  1.17     lukem 			break;
    611  1.17     lukem 
    612  1.17     lukem 		case CC_NORM:	/* normal char */
    613  1.17     lukem 			break;
    614   1.1       cgd 
    615  1.17     lukem 		case CC_ARGHACK:	/* Suggested by Rich Salz */
    616  1.17     lukem 			/* <rsalz (at) pineapple.bbn.com> */
    617  1.23  christos 			continue;	/* keep going... */
    618   1.1       cgd 
    619  1.17     lukem 		case CC_EOF:	/* end of file typed */
    620  1.29  christos 			if ((el->el_flags & UNBUFFERED) == 0)
    621  1.29  christos 				num = 0;
    622  1.29  christos 			else if (num == -1) {
    623  1.31  christos 				*el->el_line.lastchar++ = CONTROL('d');
    624  1.29  christos 				el->el_line.cursor = el->el_line.lastchar;
    625  1.29  christos 				num = 1;
    626  1.29  christos 			}
    627  1.17     lukem 			break;
    628  1.17     lukem 
    629  1.17     lukem 		case CC_NEWLINE:	/* normal end of line */
    630  1.45  christos 			num = (int)(el->el_line.lastchar - el->el_line.buffer);
    631  1.17     lukem 			break;
    632  1.17     lukem 
    633  1.17     lukem 		case CC_FATAL:	/* fatal error, reset to known state */
    634   1.1       cgd #ifdef DEBUG_READ
    635  1.17     lukem 			(void) fprintf(el->el_errfile,
    636  1.17     lukem 			    "*** editor fatal ERROR ***\r\n\n");
    637   1.1       cgd #endif /* DEBUG_READ */
    638  1.17     lukem 			/* put (real) cursor in a known place */
    639  1.17     lukem 			re_clear_display(el);	/* reset the display stuff */
    640  1.39  christos 			ch_reset(el, 1);	/* reset the input pointers */
    641  1.66  christos 			re_refresh(el); /* print the prompt again */
    642  1.17     lukem 			break;
    643   1.1       cgd 
    644  1.17     lukem 		case CC_ERROR:
    645  1.17     lukem 		default:	/* functions we don't know about */
    646   1.1       cgd #ifdef DEBUG_READ
    647  1.17     lukem 			(void) fprintf(el->el_errfile,
    648  1.17     lukem 			    "*** editor ERROR ***\r\n\n");
    649   1.1       cgd #endif /* DEBUG_READ */
    650  1.62  christos 			terminal_beep(el);
    651  1.62  christos 			terminal__flush(el);
    652  1.17     lukem 			break;
    653  1.17     lukem 		}
    654  1.23  christos 		el->el_state.argument = 1;
    655  1.23  christos 		el->el_state.doingarg = 0;
    656  1.23  christos 		el->el_chared.c_vcmd.action = NOP;
    657  1.28  christos 		if (el->el_flags & UNBUFFERED)
    658  1.28  christos 			break;
    659   1.1       cgd 	}
    660   1.1       cgd 
    661  1.62  christos 	terminal__flush(el);		/* flush any buffered output */
    662  1.22  christos 	/* make sure the tty is set up correctly */
    663  1.28  christos 	if ((el->el_flags & UNBUFFERED) == 0) {
    664  1.28  christos 		read_finish(el);
    665  1.49  christos 		*nread = num != -1 ? num : 0;
    666  1.28  christos 	} else {
    667  1.49  christos 		*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
    668  1.28  christos 	}
    669  1.58  christos 	goto done;
    670  1.58  christos noedit:
    671  1.58  christos 	el->el_line.cursor = el->el_line.lastchar = cp;
    672  1.58  christos 	*cp = '\0';
    673  1.58  christos 	*nread = (int)(el->el_line.cursor - el->el_line.buffer);
    674  1.49  christos done:
    675  1.49  christos 	if (*nread == 0) {
    676  1.49  christos 		if (num == -1) {
    677  1.49  christos 			*nread = -1;
    678  1.49  christos 			errno = el->el_errno;
    679  1.49  christos 		}
    680  1.49  christos 		return NULL;
    681  1.49  christos 	} else
    682  1.49  christos 		return el->el_line.buffer;
    683   1.1       cgd }
    684