Home | History | Annotate | Line # | Download | only in libedit
read.c revision 1.19
      1  1.19  jdolecek /*	$NetBSD: read.c,v 1.19 2001/01/10 07:45:41 jdolecek 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.1       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1       cgd  *    must display the following acknowledgement:
     20   1.1       cgd  *	This product includes software developed by the University of
     21   1.1       cgd  *	California, Berkeley and its contributors.
     22   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1       cgd  *    may be used to endorse or promote products derived from this software
     24   1.1       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1       cgd  * SUCH DAMAGE.
     37   1.1       cgd  */
     38   1.1       cgd 
     39   1.5  christos #include <sys/cdefs.h>
     40   1.1       cgd #if !defined(lint) && !defined(SCCSID)
     41   1.2     lukem #if 0
     42   1.1       cgd static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/4/93";
     43   1.2     lukem #else
     44  1.19  jdolecek __RCSID("$NetBSD: read.c,v 1.19 2001/01/10 07:45:41 jdolecek Exp $");
     45   1.2     lukem #endif
     46   1.2     lukem #endif /* not lint && not SCCSID */
     47   1.1       cgd 
     48   1.1       cgd /*
     49   1.1       cgd  * read.c: Clean this junk up! This is horrible code.
     50   1.1       cgd  *	   Terminal read functions
     51   1.1       cgd  */
     52   1.1       cgd #include "sys.h"
     53  1.11    kleink #include <errno.h>
     54   1.1       cgd #include <unistd.h>
     55   1.1       cgd #include <stdlib.h>
     56   1.1       cgd #include "el.h"
     57   1.1       cgd 
     58  1.17     lukem #define	OKCMD	-1
     59   1.1       cgd 
     60  1.17     lukem private int	read__fixio(int, int);
     61  1.17     lukem private int	read_preread(EditLine *);
     62  1.17     lukem private int	read_getcmd(EditLine *, el_action_t *, char *);
     63  1.17     lukem private int	read_char(EditLine *, char *);
     64   1.1       cgd 
     65   1.1       cgd #ifdef DEBUG_EDIT
     66   1.1       cgd private void
     67  1.17     lukem read_debug(EditLine *el)
     68   1.1       cgd {
     69   1.1       cgd 
     70  1.17     lukem 	if (el->el_line.cursor > el->el_line.lastchar)
     71  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
     72  1.17     lukem 	if (el->el_line.cursor < el->el_line.buffer)
     73  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
     74  1.17     lukem 	if (el->el_line.cursor > el->el_line.limit)
     75  1.17     lukem 		(void) fprintf(el->el_errfile, "cursor > limit\r\n");
     76  1.17     lukem 	if (el->el_line.lastchar > el->el_line.limit)
     77  1.17     lukem 		(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
     78  1.17     lukem 	if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
     79  1.17     lukem 		(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
     80   1.1       cgd }
     81   1.1       cgd #endif /* DEBUG_EDIT */
     82   1.1       cgd 
     83  1.17     lukem 
     84   1.1       cgd /* read__fixio():
     85   1.1       cgd  *	Try to recover from a read error
     86   1.1       cgd  */
     87  1.10  christos /* ARGSUSED */
     88   1.1       cgd private int
     89  1.17     lukem read__fixio(int fd, int e)
     90   1.1       cgd {
     91  1.17     lukem 
     92  1.17     lukem 	switch (e) {
     93  1.17     lukem 	case -1:		/* Make sure that the code is reachable */
     94   1.1       cgd 
     95   1.1       cgd #ifdef EWOULDBLOCK
     96  1.17     lukem 	case EWOULDBLOCK:
     97  1.17     lukem #ifndef TRY_AGAIN
     98  1.17     lukem #define	TRY_AGAIN
     99  1.17     lukem #endif
    100   1.1       cgd #endif /* EWOULDBLOCK */
    101   1.1       cgd 
    102   1.1       cgd #if defined(POSIX) && defined(EAGAIN)
    103  1.17     lukem #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
    104  1.17     lukem 	case EAGAIN:
    105  1.17     lukem #ifndef TRY_AGAIN
    106  1.17     lukem #define	TRY_AGAIN
    107  1.17     lukem #endif
    108  1.17     lukem #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
    109   1.1       cgd #endif /* POSIX && EAGAIN */
    110   1.1       cgd 
    111  1.17     lukem 		e = 0;
    112   1.1       cgd #ifdef TRY_AGAIN
    113  1.17     lukem #if defined(F_SETFL) && defined(O_NDELAY)
    114  1.17     lukem 		if ((e = fcntl(fd, F_GETFL, 0)) == -1)
    115  1.17     lukem 			return (-1);
    116   1.5  christos 
    117  1.17     lukem 		if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
    118  1.17     lukem 			return (-1);
    119   1.5  christos 		else
    120  1.17     lukem 			e = 1;
    121  1.17     lukem #endif /* F_SETFL && O_NDELAY */
    122  1.17     lukem 
    123  1.17     lukem #ifdef FIONBIO
    124  1.17     lukem 		{
    125  1.17     lukem 			int zero = 0;
    126  1.17     lukem 
    127  1.17     lukem 			if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
    128  1.17     lukem 				return (-1);
    129  1.17     lukem 			else
    130  1.17     lukem 				e = 1;
    131  1.17     lukem 		}
    132  1.17     lukem #endif /* FIONBIO */
    133   1.1       cgd 
    134   1.1       cgd #endif /* TRY_AGAIN */
    135  1.17     lukem 		return (e ? 0 : -1);
    136   1.1       cgd 
    137  1.17     lukem 	case EINTR:
    138  1.17     lukem 		return (0);
    139   1.1       cgd 
    140  1.17     lukem 	default:
    141  1.17     lukem 		return (-1);
    142  1.17     lukem 	}
    143   1.1       cgd }
    144   1.1       cgd 
    145   1.1       cgd 
    146   1.1       cgd /* read_preread():
    147   1.1       cgd  *	Try to read the stuff in the input queue;
    148   1.1       cgd  */
    149   1.1       cgd private int
    150  1.17     lukem read_preread(EditLine *el)
    151   1.1       cgd {
    152  1.17     lukem 	int chrs = 0;
    153   1.1       cgd 
    154  1.17     lukem 	if (el->el_chared.c_macro.nline) {
    155  1.17     lukem 		el_free((ptr_t) el->el_chared.c_macro.nline);
    156  1.17     lukem 		el->el_chared.c_macro.nline = NULL;
    157  1.17     lukem 	}
    158  1.17     lukem 	if (el->el_tty.t_mode == ED_IO)
    159  1.17     lukem 		return (0);
    160   1.1       cgd 
    161   1.1       cgd #ifdef FIONREAD
    162  1.17     lukem 	(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
    163  1.17     lukem 	if (chrs > 0) {
    164  1.17     lukem 		char buf[EL_BUFSIZ];
    165   1.1       cgd 
    166  1.17     lukem 		chrs = read(el->el_infd, buf,
    167  1.17     lukem 		    (size_t) MIN(chrs, EL_BUFSIZ - 1));
    168  1.17     lukem 		if (chrs > 0) {
    169  1.17     lukem 			buf[chrs] = '\0';
    170  1.17     lukem 			el->el_chared.c_macro.nline = strdup(buf);
    171  1.17     lukem 			el_push(el, el->el_chared.c_macro.nline);
    172  1.17     lukem 		}
    173   1.1       cgd 	}
    174  1.17     lukem #endif /* FIONREAD */
    175   1.1       cgd 
    176  1.17     lukem 	return (chrs > 0);
    177   1.1       cgd }
    178   1.1       cgd 
    179   1.1       cgd 
    180   1.1       cgd /* el_push():
    181   1.1       cgd  *	Push a macro
    182   1.1       cgd  */
    183   1.1       cgd public void
    184  1.17     lukem el_push(EditLine *el, const char *str)
    185   1.1       cgd {
    186  1.17     lukem 	c_macro_t *ma = &el->el_chared.c_macro;
    187   1.1       cgd 
    188  1.17     lukem 	if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
    189  1.17     lukem 		ma->level++;
    190  1.17     lukem 		/* LINTED const cast */
    191  1.17     lukem 		ma->macro[ma->level] = (char *) str;
    192  1.17     lukem 	} else {
    193  1.17     lukem 		term_beep(el);
    194  1.17     lukem 		term__flush();
    195  1.17     lukem 	}
    196   1.1       cgd }
    197   1.1       cgd 
    198   1.1       cgd 
    199   1.1       cgd /* read_getcmd():
    200   1.1       cgd  *	Return next command from the input stream.
    201   1.1       cgd  */
    202   1.1       cgd private int
    203  1.17     lukem read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
    204   1.1       cgd {
    205  1.17     lukem 	el_action_t cmd = ED_UNASSIGNED;
    206  1.17     lukem 	int num;
    207   1.1       cgd 
    208  1.17     lukem 	while (cmd == ED_UNASSIGNED || cmd == ED_SEQUENCE_LEAD_IN) {
    209  1.17     lukem 		if ((num = el_getc(el, ch)) != 1)	/* if EOF or error */
    210  1.17     lukem 			return (num);
    211   1.1       cgd 
    212   1.1       cgd #ifdef	KANJI
    213  1.17     lukem 		if ((*ch & 0200)) {
    214  1.17     lukem 			el->el_state.metanext = 0;
    215  1.17     lukem 			cmd = CcViMap[' '];
    216  1.17     lukem 			break;
    217  1.17     lukem 		} else
    218   1.1       cgd #endif /* KANJI */
    219   1.1       cgd 
    220  1.17     lukem 		if (el->el_state.metanext) {
    221  1.17     lukem 			el->el_state.metanext = 0;
    222  1.17     lukem 			*ch |= 0200;
    223  1.17     lukem 		}
    224  1.17     lukem 		cmd = el->el_map.current[(unsigned char) *ch];
    225  1.17     lukem 		if (cmd == ED_SEQUENCE_LEAD_IN) {
    226  1.17     lukem 			key_value_t val;
    227  1.17     lukem 			switch (key_get(el, ch, &val)) {
    228  1.17     lukem 			case XK_CMD:
    229  1.17     lukem 				cmd = val.cmd;
    230  1.17     lukem 				break;
    231  1.17     lukem 			case XK_STR:
    232  1.17     lukem 				el_push(el, val.str);
    233  1.17     lukem 				break;
    234   1.1       cgd #ifdef notyet
    235  1.17     lukem 			case XK_EXE:
    236  1.17     lukem 				/* XXX: In the future to run a user function */
    237  1.17     lukem 				RunCommand(val.str);
    238  1.17     lukem 				break;
    239   1.1       cgd #endif
    240  1.17     lukem 			default:
    241  1.18  christos 				EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
    242  1.17     lukem 				break;
    243  1.17     lukem 			}
    244  1.17     lukem 		}
    245  1.17     lukem 		if (el->el_map.alt == NULL)
    246  1.17     lukem 			el->el_map.current = el->el_map.key;
    247   1.1       cgd 	}
    248  1.17     lukem 	*cmdnum = cmd;
    249  1.17     lukem 	return (OKCMD);
    250   1.1       cgd }
    251   1.1       cgd 
    252  1.17     lukem 
    253   1.6  christos /* read_char():
    254   1.6  christos  *	Read a character from the tty.
    255   1.6  christos  */
    256   1.6  christos private int
    257  1.17     lukem read_char(EditLine *el, char *cp)
    258   1.6  christos {
    259  1.17     lukem 	int num_read;
    260  1.17     lukem 	int tried = 0;
    261   1.6  christos 
    262  1.17     lukem 	while ((num_read = read(el->el_infd, cp, 1)) == -1)
    263  1.17     lukem 		if (!tried && read__fixio(el->el_infd, errno) == 0)
    264  1.17     lukem 			tried = 1;
    265  1.17     lukem 		else {
    266  1.17     lukem 			*cp = '\0';
    267  1.17     lukem 			return (-1);
    268  1.17     lukem 		}
    269   1.6  christos 
    270  1.17     lukem 	return (num_read);
    271   1.6  christos }
    272   1.6  christos 
    273   1.1       cgd 
    274   1.1       cgd /* el_getc():
    275   1.1       cgd  *	Read a character
    276   1.1       cgd  */
    277   1.1       cgd public int
    278  1.17     lukem el_getc(EditLine *el, char *cp)
    279   1.1       cgd {
    280  1.17     lukem 	int num_read;
    281  1.17     lukem 	c_macro_t *ma = &el->el_chared.c_macro;
    282   1.1       cgd 
    283  1.17     lukem 	term__flush();
    284  1.17     lukem 	for (;;) {
    285  1.17     lukem 		if (ma->level < 0) {
    286  1.17     lukem 			if (!read_preread(el))
    287  1.17     lukem 				break;
    288  1.17     lukem 		}
    289  1.17     lukem 		if (ma->level < 0)
    290  1.17     lukem 			break;
    291  1.12    simonb 
    292  1.17     lukem 		if (*ma->macro[ma->level] == 0) {
    293  1.17     lukem 			ma->level--;
    294  1.17     lukem 			continue;
    295  1.17     lukem 		}
    296  1.17     lukem 		*cp = *ma->macro[ma->level]++ & 0377;
    297  1.17     lukem 		if (*ma->macro[ma->level] == 0) {	/* Needed for QuoteMode
    298  1.17     lukem 							 * On */
    299  1.17     lukem 			ma->level--;
    300  1.17     lukem 		}
    301  1.17     lukem 		return (1);
    302   1.1       cgd 	}
    303   1.1       cgd 
    304   1.1       cgd #ifdef DEBUG_READ
    305  1.17     lukem 	(void) fprintf(el->el_errfile, "Turning raw mode on\n");
    306   1.1       cgd #endif /* DEBUG_READ */
    307  1.17     lukem 	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
    308  1.17     lukem 		return (0);
    309   1.1       cgd 
    310   1.1       cgd #ifdef DEBUG_READ
    311  1.17     lukem 	(void) fprintf(el->el_errfile, "Reading a character\n");
    312   1.1       cgd #endif /* DEBUG_READ */
    313  1.17     lukem 	num_read = read_char(el, cp);
    314   1.1       cgd #ifdef DEBUG_READ
    315  1.17     lukem 	(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
    316   1.1       cgd #endif /* DEBUG_READ */
    317  1.17     lukem 	return (num_read);
    318   1.1       cgd }
    319   1.1       cgd 
    320   1.1       cgd 
    321   1.1       cgd public const char *
    322  1.17     lukem el_gets(EditLine *el, int *nread)
    323   1.1       cgd {
    324  1.17     lukem 	int retval;
    325  1.17     lukem 	el_action_t cmdnum = 0;
    326  1.17     lukem 	int num;		/* how many chars we have read at NL */
    327  1.17     lukem 	char ch;
    328   1.8     lukem #ifdef FIONREAD
    329  1.17     lukem 	c_macro_t *ma = &el->el_chared.c_macro;
    330   1.8     lukem #endif /* FIONREAD */
    331   1.1       cgd 
    332  1.17     lukem 	if (el->el_flags & HANDLE_SIGNALS)
    333  1.17     lukem 		sig_set(el);
    334   1.6  christos 
    335  1.17     lukem 	if (el->el_flags & NO_TTY) {
    336  1.17     lukem 		char *cp = el->el_line.buffer;
    337  1.19  jdolecek 		size_t idx;
    338   1.6  christos 
    339  1.17     lukem 		while (read_char(el, cp) == 1) {
    340  1.19  jdolecek 			/* make sure there is space for next character */
    341  1.19  jdolecek 			if (cp + 1 >= el->el_line.limit) {
    342  1.19  jdolecek 				idx = (cp - el->el_line.buffer);
    343  1.19  jdolecek 				if (!ch_enlargebufs(el, 2))
    344  1.19  jdolecek 					break;
    345  1.19  jdolecek 				cp = &el->el_line.buffer[idx];
    346  1.19  jdolecek 			}
    347  1.17     lukem 			cp++;
    348  1.17     lukem 			if (cp[-1] == '\r' || cp[-1] == '\n')
    349  1.17     lukem 				break;
    350   1.6  christos 		}
    351  1.19  jdolecek 
    352  1.17     lukem 		el->el_line.cursor = el->el_line.lastchar = cp;
    353  1.17     lukem 		*cp = '\0';
    354  1.17     lukem 		if (nread)
    355  1.17     lukem 			*nread = el->el_line.cursor - el->el_line.buffer;
    356  1.17     lukem 		return (el->el_line.buffer);
    357   1.7  christos 	}
    358  1.17     lukem 	re_clear_display(el);	/* reset the display stuff */
    359  1.17     lukem 	ch_reset(el);
    360   1.1       cgd 
    361   1.1       cgd #ifdef FIONREAD
    362  1.17     lukem 	if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
    363  1.17     lukem 		long chrs = 0;
    364   1.1       cgd 
    365  1.17     lukem 		(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
    366  1.17     lukem 		if (chrs == 0) {
    367  1.17     lukem 			if (tty_rawmode(el) < 0) {
    368  1.17     lukem 				if (nread)
    369  1.17     lukem 					*nread = 0;
    370  1.17     lukem 				return (NULL);
    371  1.17     lukem 			}
    372  1.17     lukem 		}
    373   1.1       cgd 	}
    374   1.1       cgd #endif /* FIONREAD */
    375   1.1       cgd 
    376  1.17     lukem 	re_refresh(el);		/* print the prompt */
    377  1.13  sommerfe 
    378  1.17     lukem 	if (el->el_flags & EDIT_DISABLED) {
    379  1.17     lukem 		char *cp = el->el_line.buffer;
    380  1.19  jdolecek 		size_t idx;
    381  1.13  sommerfe 
    382  1.17     lukem 		term__flush();
    383  1.17     lukem 
    384  1.17     lukem 		while (read_char(el, cp) == 1) {
    385  1.19  jdolecek 			/* make sure there is space next character */
    386  1.19  jdolecek 			if (cp + 1 >= el->el_line.limit) {
    387  1.19  jdolecek 				idx = (cp - el->el_line.buffer);
    388  1.19  jdolecek 				if (!ch_enlargebufs(el, 2))
    389  1.19  jdolecek 					break;
    390  1.19  jdolecek 				cp = &el->el_line.buffer[idx];
    391  1.19  jdolecek 			}
    392  1.17     lukem 			cp++;
    393  1.17     lukem 			if (cp[-1] == '\r' || cp[-1] == '\n')
    394  1.17     lukem 				break;
    395  1.19  jdolecek 		}
    396  1.13  sommerfe 
    397  1.17     lukem 		el->el_line.cursor = el->el_line.lastchar = cp;
    398  1.17     lukem 		*cp = '\0';
    399  1.17     lukem 		if (nread)
    400  1.17     lukem 			*nread = el->el_line.cursor - el->el_line.buffer;
    401  1.17     lukem 		return (el->el_line.buffer);
    402  1.13  sommerfe 	}
    403  1.17     lukem 	for (num = OKCMD; num == OKCMD;) {	/* while still editing this
    404  1.17     lukem 						 * line */
    405   1.1       cgd #ifdef DEBUG_EDIT
    406  1.17     lukem 		read_debug(el);
    407   1.1       cgd #endif /* DEBUG_EDIT */
    408  1.17     lukem 		/* if EOF or error */
    409  1.17     lukem 		if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
    410   1.1       cgd #ifdef DEBUG_READ
    411  1.17     lukem 			(void) fprintf(el->el_errfile,
    412  1.17     lukem 			    "Returning from el_gets %d\n", num);
    413   1.1       cgd #endif /* DEBUG_READ */
    414  1.17     lukem 			break;
    415  1.17     lukem 		}
    416  1.17     lukem 		if ((int) cmdnum >= el->el_map.nfunc) {	/* BUG CHECK command */
    417   1.1       cgd #ifdef DEBUG_EDIT
    418  1.17     lukem 			(void) fprintf(el->el_errfile,
    419  1.17     lukem 			    "ERROR: illegal command from key 0%o\r\n", ch);
    420   1.1       cgd #endif /* DEBUG_EDIT */
    421  1.17     lukem 			continue;	/* try again */
    422  1.17     lukem 		}
    423  1.17     lukem 		/* now do the real command */
    424   1.1       cgd #ifdef DEBUG_READ
    425  1.17     lukem 		{
    426  1.17     lukem 			el_bindings_t *b;
    427  1.17     lukem 			for (b = el->el_map.help; b->name; b++)
    428  1.17     lukem 				if (b->func == cmdnum)
    429  1.17     lukem 					break;
    430  1.17     lukem 			if (b->name)
    431  1.17     lukem 				(void) fprintf(el->el_errfile,
    432  1.17     lukem 				    "Executing %s\n", b->name);
    433  1.17     lukem 			else
    434  1.17     lukem 				(void) fprintf(el->el_errfile,
    435  1.17     lukem 				    "Error command = %d\n", cmdnum);
    436  1.17     lukem 		}
    437   1.1       cgd #endif /* DEBUG_READ */
    438  1.17     lukem 		retval = (*el->el_map.func[cmdnum]) (el, ch);
    439  1.17     lukem 
    440  1.17     lukem 		/* save the last command here */
    441  1.17     lukem 		el->el_state.lastcmd = cmdnum;
    442   1.1       cgd 
    443  1.17     lukem 		/* use any return value */
    444  1.17     lukem 		switch (retval) {
    445  1.17     lukem 		case CC_CURSOR:
    446  1.17     lukem 			el->el_state.argument = 1;
    447  1.17     lukem 			el->el_state.doingarg = 0;
    448  1.17     lukem 			re_refresh_cursor(el);
    449  1.17     lukem 			break;
    450  1.17     lukem 
    451  1.17     lukem 		case CC_REDISPLAY:
    452  1.17     lukem 			re_clear_lines(el);
    453  1.17     lukem 			re_clear_display(el);
    454  1.17     lukem 			/* FALLTHROUGH */
    455  1.17     lukem 
    456  1.17     lukem 		case CC_REFRESH:
    457  1.17     lukem 			el->el_state.argument = 1;
    458  1.17     lukem 			el->el_state.doingarg = 0;
    459  1.17     lukem 			re_refresh(el);
    460  1.17     lukem 			break;
    461  1.17     lukem 
    462  1.17     lukem 		case CC_REFRESH_BEEP:
    463  1.17     lukem 			el->el_state.argument = 1;
    464  1.17     lukem 			el->el_state.doingarg = 0;
    465  1.17     lukem 			re_refresh(el);
    466  1.17     lukem 			term_beep(el);
    467  1.17     lukem 			break;
    468  1.17     lukem 
    469  1.17     lukem 		case CC_NORM:	/* normal char */
    470  1.17     lukem 			el->el_state.argument = 1;
    471  1.17     lukem 			el->el_state.doingarg = 0;
    472  1.17     lukem 			break;
    473   1.1       cgd 
    474  1.17     lukem 		case CC_ARGHACK:	/* Suggested by Rich Salz */
    475  1.17     lukem 			/* <rsalz (at) pineapple.bbn.com> */
    476  1.17     lukem 			break;	/* keep going... */
    477   1.1       cgd 
    478  1.17     lukem 		case CC_EOF:	/* end of file typed */
    479  1.17     lukem 			num = 0;
    480  1.17     lukem 			break;
    481  1.17     lukem 
    482  1.17     lukem 		case CC_NEWLINE:	/* normal end of line */
    483  1.17     lukem 			num = el->el_line.lastchar - el->el_line.buffer;
    484  1.17     lukem 			break;
    485  1.17     lukem 
    486  1.17     lukem 		case CC_FATAL:	/* fatal error, reset to known state */
    487   1.1       cgd #ifdef DEBUG_READ
    488  1.17     lukem 			(void) fprintf(el->el_errfile,
    489  1.17     lukem 			    "*** editor fatal ERROR ***\r\n\n");
    490   1.1       cgd #endif /* DEBUG_READ */
    491  1.17     lukem 			/* put (real) cursor in a known place */
    492  1.17     lukem 			re_clear_display(el);	/* reset the display stuff */
    493  1.17     lukem 			ch_reset(el);	/* reset the input pointers */
    494  1.17     lukem 			re_refresh(el);	/* print the prompt again */
    495  1.17     lukem 			el->el_state.argument = 1;
    496  1.17     lukem 			el->el_state.doingarg = 0;
    497  1.17     lukem 			break;
    498   1.1       cgd 
    499  1.17     lukem 		case CC_ERROR:
    500  1.17     lukem 		default:	/* functions we don't know about */
    501   1.1       cgd #ifdef DEBUG_READ
    502  1.17     lukem 			(void) fprintf(el->el_errfile,
    503  1.17     lukem 			    "*** editor ERROR ***\r\n\n");
    504   1.1       cgd #endif /* DEBUG_READ */
    505  1.17     lukem 			el->el_state.argument = 1;
    506  1.17     lukem 			el->el_state.doingarg = 0;
    507  1.17     lukem 			term_beep(el);
    508  1.17     lukem 			term__flush();
    509  1.17     lukem 			break;
    510  1.17     lukem 		}
    511   1.1       cgd 	}
    512   1.1       cgd 
    513  1.17     lukem 				/* make sure the tty is set up correctly */
    514  1.17     lukem 	(void) tty_cookedmode(el);
    515  1.17     lukem 	term__flush();		/* flush any buffered output */
    516  1.17     lukem 	if (el->el_flags & HANDLE_SIGNALS)
    517  1.17     lukem 		sig_clr(el);
    518  1.17     lukem 	if (nread)
    519  1.17     lukem 		*nread = num;
    520  1.17     lukem 	return (num ? el->el_line.buffer : NULL);
    521   1.1       cgd }
    522