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