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