Home | History | Annotate | Line # | Download | only in libedit
read.c revision 1.84
      1 /*	$NetBSD: read.c,v 1.84 2016/02/24 17:13:22 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.84 2016/02/24 17:13:22 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 *, wchar_t *);
     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, wchar_t *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 = L'\0';
    335 			return -1;
    336 		}
    337 	}
    338 
    339 	/* Test for EOF */
    340 	if (num_read == 0) {
    341 		*cp = L'\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 = L'\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 
    411 	terminal__flush(el);
    412 	for (;;) {
    413 		if (ma->level < 0) {
    414 			if (!read_preread(el))
    415 				break;
    416 		}
    417 
    418 		if (ma->level < 0)
    419 			break;
    420 
    421 		if (ma->macro[0][ma->offset] == '\0') {
    422 			read_pop(ma);
    423 			continue;
    424 		}
    425 
    426 		*cp = ma->macro[0][ma->offset++];
    427 
    428 		if (ma->macro[0][ma->offset] == '\0') {
    429 			/* Needed for QuoteMode On */
    430 			read_pop(ma);
    431 		}
    432 
    433 		return 1;
    434 	}
    435 
    436 #ifdef DEBUG_READ
    437 	(void) fprintf(el->el_errfile, "Turning raw mode on\n");
    438 #endif /* DEBUG_READ */
    439 	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
    440 		return 0;
    441 
    442 #ifdef DEBUG_READ
    443 	(void) fprintf(el->el_errfile, "Reading a character\n");
    444 #endif /* DEBUG_READ */
    445 	num_read = (*el->el_read.read_char)(el, cp);
    446 	if (num_read < 0)
    447 		el->el_errno = errno;
    448 #ifdef DEBUG_READ
    449 	(void) fprintf(el->el_errfile, "Got it %lc\n", *cp);
    450 #endif /* DEBUG_READ */
    451 	return num_read;
    452 }
    453 
    454 protected void
    455 read_prepare(EditLine *el)
    456 {
    457 	if (el->el_flags & HANDLE_SIGNALS)
    458 		sig_set(el);
    459 	if (el->el_flags & NO_TTY)
    460 		return;
    461 	if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
    462 		tty_rawmode(el);
    463 
    464 	/* This is relatively cheap, and things go terribly wrong if
    465 	   we have the wrong size. */
    466 	el_resize(el);
    467 	re_clear_display(el);	/* reset the display stuff */
    468 	ch_reset(el, 0);
    469 	re_refresh(el);		/* print the prompt */
    470 
    471 	if (el->el_flags & UNBUFFERED)
    472 		terminal__flush(el);
    473 }
    474 
    475 protected void
    476 read_finish(EditLine *el)
    477 {
    478 	if ((el->el_flags & UNBUFFERED) == 0)
    479 		(void) tty_cookedmode(el);
    480 	if (el->el_flags & HANDLE_SIGNALS)
    481 		sig_clr(el);
    482 }
    483 
    484 public const Char *
    485 FUN(el,gets)(EditLine *el, int *nread)
    486 {
    487 	int retval;
    488 	el_action_t cmdnum = 0;
    489 	int num;		/* how many chars we have read at NL */
    490 	wchar_t wc;
    491 	Char ch, *cp;
    492 	int crlf = 0;
    493 	int nrb;
    494 #ifdef FIONREAD
    495 	c_macro_t *ma = &el->el_chared.c_macro;
    496 #endif /* FIONREAD */
    497 
    498 	if (nread == NULL)
    499 		nread = &nrb;
    500 	*nread = 0;
    501 
    502 	if (el->el_flags & NO_TTY) {
    503 		size_t idx;
    504 
    505 		cp = el->el_line.buffer;
    506 		while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
    507 			*cp = (Char)wc;
    508 			/* make sure there is space for next character */
    509 			if (cp + 1 >= el->el_line.limit) {
    510 				idx = (size_t)(cp - el->el_line.buffer);
    511 				if (!ch_enlargebufs(el, (size_t)2))
    512 					break;
    513 				cp = &el->el_line.buffer[idx];
    514 			}
    515 			cp++;
    516 			if (el->el_flags & UNBUFFERED)
    517 				break;
    518 			if (cp[-1] == '\r' || cp[-1] == '\n')
    519 				break;
    520 		}
    521 		if (num == -1) {
    522 			if (errno == EINTR)
    523 				cp = el->el_line.buffer;
    524 			el->el_errno = errno;
    525 		}
    526 
    527 		goto noedit;
    528 	}
    529 
    530 
    531 #ifdef FIONREAD
    532 	if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
    533 		long chrs = 0;
    534 
    535 		(void) ioctl(el->el_infd, FIONREAD, &chrs);
    536 		if (chrs == 0) {
    537 			if (tty_rawmode(el) < 0) {
    538 				errno = 0;
    539 				*nread = 0;
    540 				return NULL;
    541 			}
    542 		}
    543 	}
    544 #endif /* FIONREAD */
    545 
    546 	if ((el->el_flags & UNBUFFERED) == 0)
    547 		read_prepare(el);
    548 
    549 	if (el->el_flags & EDIT_DISABLED) {
    550 		size_t idx;
    551 
    552 		if ((el->el_flags & UNBUFFERED) == 0)
    553 			cp = el->el_line.buffer;
    554 		else
    555 			cp = el->el_line.lastchar;
    556 
    557 		terminal__flush(el);
    558 
    559 		while ((num = (*el->el_read.read_char)(el, &wc)) == 1) {
    560 			*cp = (Char)wc;
    561 			/* make sure there is space next character */
    562 			if (cp + 1 >= el->el_line.limit) {
    563 				idx = (size_t)(cp - el->el_line.buffer);
    564 				if (!ch_enlargebufs(el, (size_t)2))
    565 					break;
    566 				cp = &el->el_line.buffer[idx];
    567 			}
    568 			cp++;
    569 			crlf = cp[-1] == '\r' || cp[-1] == '\n';
    570 			if (el->el_flags & UNBUFFERED)
    571 				break;
    572 			if (crlf)
    573 				break;
    574 		}
    575 
    576 		if (num == -1) {
    577 			if (errno == EINTR)
    578 				cp = el->el_line.buffer;
    579 			el->el_errno = errno;
    580 		}
    581 
    582 		goto noedit;
    583 	}
    584 
    585 	for (num = OKCMD; num == OKCMD;) {	/* while still editing this
    586 						 * line */
    587 #ifdef DEBUG_EDIT
    588 		read_debug(el);
    589 #endif /* DEBUG_EDIT */
    590 		/* if EOF or error */
    591 		if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
    592 			num = -1;
    593 #ifdef DEBUG_READ
    594 			(void) fprintf(el->el_errfile,
    595 			    "Returning from el_gets %d\n", num);
    596 #endif /* DEBUG_READ */
    597 			break;
    598 		}
    599 		if (el->el_errno == EINTR) {
    600 			el->el_line.buffer[0] = '\0';
    601 			el->el_line.lastchar =
    602 			    el->el_line.cursor = el->el_line.buffer;
    603 			break;
    604 		}
    605 		if ((size_t)cmdnum >= el->el_map.nfunc) {	/* BUG CHECK command */
    606 #ifdef DEBUG_EDIT
    607 			(void) fprintf(el->el_errfile,
    608 			    "ERROR: illegal command from key 0%o\r\n", ch);
    609 #endif /* DEBUG_EDIT */
    610 			continue;	/* try again */
    611 		}
    612 		/* now do the real command */
    613 #ifdef DEBUG_READ
    614 		{
    615 			el_bindings_t *b;
    616 			for (b = el->el_map.help; b->name; b++)
    617 				if (b->func == cmdnum)
    618 					break;
    619 			if (b->name)
    620 				(void) fprintf(el->el_errfile,
    621 				    "Executing %s\n", b->name);
    622 			else
    623 				(void) fprintf(el->el_errfile,
    624 				    "Error command = %d\n", cmdnum);
    625 		}
    626 #endif /* DEBUG_READ */
    627 		/* vi redo needs these way down the levels... */
    628 		el->el_state.thiscmd = cmdnum;
    629 		el->el_state.thisch = ch;
    630 		if (el->el_map.type == MAP_VI &&
    631 		    el->el_map.current == el->el_map.key &&
    632 		    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
    633 			if (cmdnum == VI_DELETE_PREV_CHAR &&
    634 			    el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
    635 			    && Isprint(el->el_chared.c_redo.pos[-1]))
    636 				el->el_chared.c_redo.pos--;
    637 			else
    638 				*el->el_chared.c_redo.pos++ = ch;
    639 		}
    640 		retval = (*el->el_map.func[cmdnum]) (el, ch);
    641 #ifdef DEBUG_READ
    642 		(void) fprintf(el->el_errfile,
    643 			"Returned state %d\n", retval );
    644 #endif /* DEBUG_READ */
    645 
    646 		/* save the last command here */
    647 		el->el_state.lastcmd = cmdnum;
    648 
    649 		/* use any return value */
    650 		switch (retval) {
    651 		case CC_CURSOR:
    652 			re_refresh_cursor(el);
    653 			break;
    654 
    655 		case CC_REDISPLAY:
    656 			re_clear_lines(el);
    657 			re_clear_display(el);
    658 			/* FALLTHROUGH */
    659 
    660 		case CC_REFRESH:
    661 			re_refresh(el);
    662 			break;
    663 
    664 		case CC_REFRESH_BEEP:
    665 			re_refresh(el);
    666 			terminal_beep(el);
    667 			break;
    668 
    669 		case CC_NORM:	/* normal char */
    670 			break;
    671 
    672 		case CC_ARGHACK:	/* Suggested by Rich Salz */
    673 			/* <rsalz (at) pineapple.bbn.com> */
    674 			continue;	/* keep going... */
    675 
    676 		case CC_EOF:	/* end of file typed */
    677 			if ((el->el_flags & UNBUFFERED) == 0)
    678 				num = 0;
    679 			else if (num == -1) {
    680 				*el->el_line.lastchar++ = CONTROL('d');
    681 				el->el_line.cursor = el->el_line.lastchar;
    682 				num = 1;
    683 			}
    684 			break;
    685 
    686 		case CC_NEWLINE:	/* normal end of line */
    687 			num = (int)(el->el_line.lastchar - el->el_line.buffer);
    688 			break;
    689 
    690 		case CC_FATAL:	/* fatal error, reset to known state */
    691 #ifdef DEBUG_READ
    692 			(void) fprintf(el->el_errfile,
    693 			    "*** editor fatal ERROR ***\r\n\n");
    694 #endif /* DEBUG_READ */
    695 			/* put (real) cursor in a known place */
    696 			re_clear_display(el);	/* reset the display stuff */
    697 			ch_reset(el, 1);	/* reset the input pointers */
    698 			re_refresh(el); /* print the prompt again */
    699 			break;
    700 
    701 		case CC_ERROR:
    702 		default:	/* functions we don't know about */
    703 #ifdef DEBUG_READ
    704 			(void) fprintf(el->el_errfile,
    705 			    "*** editor ERROR ***\r\n\n");
    706 #endif /* DEBUG_READ */
    707 			terminal_beep(el);
    708 			terminal__flush(el);
    709 			break;
    710 		}
    711 		el->el_state.argument = 1;
    712 		el->el_state.doingarg = 0;
    713 		el->el_chared.c_vcmd.action = NOP;
    714 		if (el->el_flags & UNBUFFERED)
    715 			break;
    716 	}
    717 
    718 	terminal__flush(el);		/* flush any buffered output */
    719 	/* make sure the tty is set up correctly */
    720 	if ((el->el_flags & UNBUFFERED) == 0) {
    721 		read_finish(el);
    722 		*nread = num != -1 ? num : 0;
    723 	} else {
    724 		*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
    725 	}
    726 	goto done;
    727 noedit:
    728 	el->el_line.cursor = el->el_line.lastchar = cp;
    729 	*cp = '\0';
    730 	*nread = (int)(el->el_line.cursor - el->el_line.buffer);
    731 done:
    732 	if (*nread == 0) {
    733 		if (num == -1) {
    734 			*nread = -1;
    735 			errno = el->el_errno;
    736 		}
    737 		return NULL;
    738 	} else
    739 		return el->el_line.buffer;
    740 }
    741