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