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