Home | History | Annotate | Line # | Download | only in libedit
read.c revision 1.77
      1 /*	$NetBSD: read.c,v 1.77 2016/02/14 14:47:48 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.77 2016/02/14 14:47:48 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, &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, &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 	terminal_beep(el);
    231 	terminal__flush(el);
    232 }
    233 
    234 
    235 /* read_getcmd():
    236  *	Get next command from the input stream, return OKCMD on success.
    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 	static const Char meta = (Char)0x80;
    243 	el_action_t cmd;
    244 	int num;
    245 
    246 	el->el_errno = 0;
    247 	do {
    248 		if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
    249 			el->el_errno = num == 0 ? 0 : errno;
    250 			return 0;	/* not OKCMD */
    251 		}
    252 
    253 #ifdef	KANJI
    254 		if ((*ch & meta)) {
    255 			el->el_state.metanext = 0;
    256 			cmd = CcViMap[' '];
    257 			break;
    258 		} else
    259 #endif /* KANJI */
    260 
    261 		if (el->el_state.metanext) {
    262 			el->el_state.metanext = 0;
    263 			*ch |= meta;
    264 		}
    265 #ifdef WIDECHAR
    266 		if (*ch >= N_KEYS)
    267 			cmd = ED_INSERT;
    268 		else
    269 #endif
    270 			cmd = el->el_map.current[(unsigned char) *ch];
    271 		if (cmd == ED_SEQUENCE_LEAD_IN) {
    272 			keymacro_value_t val;
    273 			switch (keymacro_get(el, ch, &val)) {
    274 			case XK_CMD:
    275 				cmd = val.cmd;
    276 				break;
    277 			case XK_STR:
    278 				FUN(el,push)(el, val.str);
    279 				break;
    280 #ifdef notyet
    281 			case XK_EXE:
    282 				/* XXX: In the future to run a user function */
    283 				RunCommand(val.str);
    284 				break;
    285 #endif
    286 			default:
    287 				EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
    288 				break;
    289 			}
    290 		}
    291 		if (el->el_map.alt == NULL)
    292 			el->el_map.current = el->el_map.key;
    293 	} while (cmd == ED_SEQUENCE_LEAD_IN);
    294 	*cmdnum = cmd;
    295 	return OKCMD;
    296 }
    297 
    298 /* read_char():
    299  *	Read a character from the tty.
    300  */
    301 private int
    302 read_char(EditLine *el, Char *cp)
    303 {
    304 	ssize_t num_read;
    305 	int tried = 0;
    306 	char cbuf[MB_LEN_MAX];
    307 	size_t cbp = 0;
    308 	int save_errno = errno;
    309 
    310  again:
    311 	el->el_signal->sig_no = 0;
    312 	while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
    313 		int e = errno;
    314 		switch (el->el_signal->sig_no) {
    315 		case SIGCONT:
    316 			FUN(el,set)(el, EL_REFRESH);
    317 			/*FALLTHROUGH*/
    318 		case SIGWINCH:
    319 			sig_set(el);
    320 			goto again;
    321 		default:
    322 			break;
    323 		}
    324 		if (!tried && read__fixio(el->el_infd, e) == 0) {
    325 			errno = save_errno;
    326 			tried = 1;
    327 		} else {
    328 			errno = e;
    329 			*cp = '\0';
    330 			return -1;
    331 		}
    332 	}
    333 
    334 	/* Test for EOF */
    335 	if (num_read == 0) {
    336 		*cp = '\0';
    337 		return 0;
    338 	}
    339 
    340 	for (;;) {
    341 		mbstate_t mbs;
    342 
    343 		++cbp;
    344 		/* This only works because UTF8 is stateless */
    345 		memset(&mbs, 0, sizeof(mbs));
    346 		switch (ct_mbrtowc(cp, cbuf, cbp, &mbs)) {
    347 		case (size_t)-1:
    348 			if (cbp > 1) {
    349 				/*
    350 				 * Invalid sequence, discard all bytes
    351 				 * except the last one.
    352 				 */
    353 				cbuf[0] = cbuf[cbp - 1];
    354 				cbp = 0;
    355 				break;
    356 			} else {
    357 				/* Invalid byte, discard it. */
    358 				cbp = 0;
    359 				goto again;
    360 			}
    361 		case (size_t)-2:
    362 			/*
    363 			 * We don't support other multibyte charsets.
    364 			 * The second condition shouldn't happen
    365 			 * and is here merely for additional safety.
    366 			 */
    367 			if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
    368 			    cbp >= MB_LEN_MAX) {
    369 				errno = EILSEQ;
    370 				*cp = '\0';
    371 				return -1;
    372 			}
    373 			/* Incomplete sequence, read another byte. */
    374 			goto again;
    375 		default:
    376 			/* Valid character, process it. */
    377 			return 1;
    378 		}
    379 	}
    380 }
    381 
    382 /* read_pop():
    383  *	Pop a macro from the stack
    384  */
    385 private void
    386 read_pop(c_macro_t *ma)
    387 {
    388 	int i;
    389 
    390 	el_free(ma->macro[0]);
    391 	for (i = 0; i < ma->level; i++)
    392 		ma->macro[i] = ma->macro[i + 1];
    393 	ma->level--;
    394 	ma->offset = 0;
    395 }
    396 
    397 /* el_getc():
    398  *	Read a character
    399  */
    400 public int
    401 FUN(el,getc)(EditLine *el, Char *cp)
    402 {
    403 	int num_read;
    404 	c_macro_t *ma = &el->el_chared.c_macro;
    405 
    406 	terminal__flush(el);
    407 	for (;;) {
    408 		if (ma->level < 0) {
    409 			if (!read_preread(el))
    410 				break;
    411 		}
    412 
    413 		if (ma->level < 0)
    414 			break;
    415 
    416 		if (ma->macro[0][ma->offset] == '\0') {
    417 			read_pop(ma);
    418 			continue;
    419 		}
    420 
    421 		*cp = ma->macro[0][ma->offset++];
    422 
    423 		if (ma->macro[0][ma->offset] == '\0') {
    424 			/* Needed for QuoteMode On */
    425 			read_pop(ma);
    426 		}
    427 
    428 		return 1;
    429 	}
    430 
    431 #ifdef DEBUG_READ
    432 	(void) fprintf(el->el_errfile, "Turning raw mode on\n");
    433 #endif /* DEBUG_READ */
    434 	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
    435 		return 0;
    436 
    437 #ifdef DEBUG_READ
    438 	(void) fprintf(el->el_errfile, "Reading a character\n");
    439 #endif /* DEBUG_READ */
    440 	num_read = (*el->el_read.read_char)(el, cp);
    441 	if (num_read < 0)
    442 		el->el_errno = errno;
    443 #ifdef WIDECHAR
    444 	if (el->el_flags & NARROW_READ)
    445 		*cp = *(char *)(void *)cp;
    446 #endif
    447 #ifdef DEBUG_READ
    448 	(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
    449 #endif /* DEBUG_READ */
    450 	return num_read;
    451 }
    452 
    453 protected void
    454 read_prepare(EditLine *el)
    455 {
    456 	if (el->el_flags & HANDLE_SIGNALS)
    457 		sig_set(el);
    458 	if (el->el_flags & NO_TTY)
    459 		return;
    460 	if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
    461 		tty_rawmode(el);
    462 
    463 	/* This is relatively cheap, and things go terribly wrong if
    464 	   we have the wrong size. */
    465 	el_resize(el);
    466 	re_clear_display(el);	/* reset the display stuff */
    467 	ch_reset(el, 0);
    468 	re_refresh(el);		/* print the prompt */
    469 
    470 	if (el->el_flags & UNBUFFERED)
    471 		terminal__flush(el);
    472 }
    473 
    474 protected void
    475 read_finish(EditLine *el)
    476 {
    477 	if ((el->el_flags & UNBUFFERED) == 0)
    478 		(void) tty_cookedmode(el);
    479 	if (el->el_flags & HANDLE_SIGNALS)
    480 		sig_clr(el);
    481 }
    482 
    483 public const Char *
    484 FUN(el,gets)(EditLine *el, int *nread)
    485 {
    486 	int retval;
    487 	el_action_t cmdnum = 0;
    488 	int num;		/* how many chars we have read at NL */
    489 	Char ch, *cp;
    490 	int crlf = 0;
    491 	int nrb;
    492 #ifdef FIONREAD
    493 	c_macro_t *ma = &el->el_chared.c_macro;
    494 #endif /* FIONREAD */
    495 
    496 	if (nread == NULL)
    497 		nread = &nrb;
    498 	*nread = 0;
    499 
    500 	if (el->el_flags & NO_TTY) {
    501 		size_t idx;
    502 
    503 		cp = el->el_line.buffer;
    504 		while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
    505 			/* make sure there is space for next character */
    506 			if (cp + 1 >= el->el_line.limit) {
    507 				idx = (size_t)(cp - el->el_line.buffer);
    508 				if (!ch_enlargebufs(el, (size_t)2))
    509 					break;
    510 				cp = &el->el_line.buffer[idx];
    511 			}
    512 			cp++;
    513 			if (el->el_flags & UNBUFFERED)
    514 				break;
    515 			if (cp[-1] == '\r' || cp[-1] == '\n')
    516 				break;
    517 		}
    518 		if (num == -1) {
    519 			if (errno == EINTR)
    520 				cp = el->el_line.buffer;
    521 			el->el_errno = errno;
    522 		}
    523 
    524 		goto noedit;
    525 	}
    526 
    527 
    528 #ifdef FIONREAD
    529 	if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
    530 		long chrs = 0;
    531 
    532 		(void) ioctl(el->el_infd, FIONREAD, &chrs);
    533 		if (chrs == 0) {
    534 			if (tty_rawmode(el) < 0) {
    535 				errno = 0;
    536 				*nread = 0;
    537 				return NULL;
    538 			}
    539 		}
    540 	}
    541 #endif /* FIONREAD */
    542 
    543 	if ((el->el_flags & UNBUFFERED) == 0)
    544 		read_prepare(el);
    545 
    546 	if (el->el_flags & EDIT_DISABLED) {
    547 		size_t idx;
    548 
    549 		if ((el->el_flags & UNBUFFERED) == 0)
    550 			cp = el->el_line.buffer;
    551 		else
    552 			cp = el->el_line.lastchar;
    553 
    554 		terminal__flush(el);
    555 
    556 		while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
    557 			/* make sure there is space next character */
    558 			if (cp + 1 >= el->el_line.limit) {
    559 				idx = (size_t)(cp - el->el_line.buffer);
    560 				if (!ch_enlargebufs(el, (size_t)2))
    561 					break;
    562 				cp = &el->el_line.buffer[idx];
    563 			}
    564 			cp++;
    565 			crlf = cp[-1] == '\r' || cp[-1] == '\n';
    566 			if (el->el_flags & UNBUFFERED)
    567 				break;
    568 			if (crlf)
    569 				break;
    570 		}
    571 
    572 		if (num == -1) {
    573 			if (errno == EINTR)
    574 				cp = el->el_line.buffer;
    575 			el->el_errno = errno;
    576 		}
    577 
    578 		goto noedit;
    579 	}
    580 
    581 	for (num = OKCMD; num == OKCMD;) {	/* while still editing this
    582 						 * line */
    583 #ifdef DEBUG_EDIT
    584 		read_debug(el);
    585 #endif /* DEBUG_EDIT */
    586 		/* if EOF or error */
    587 		if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
    588 			num = -1;
    589 #ifdef DEBUG_READ
    590 			(void) fprintf(el->el_errfile,
    591 			    "Returning from el_gets %d\n", num);
    592 #endif /* DEBUG_READ */
    593 			break;
    594 		}
    595 		if (el->el_errno == EINTR) {
    596 			el->el_line.buffer[0] = '\0';
    597 			el->el_line.lastchar =
    598 			    el->el_line.cursor = el->el_line.buffer;
    599 			break;
    600 		}
    601 		if ((size_t)cmdnum >= el->el_map.nfunc) {	/* BUG CHECK command */
    602 #ifdef DEBUG_EDIT
    603 			(void) fprintf(el->el_errfile,
    604 			    "ERROR: illegal command from key 0%o\r\n", ch);
    605 #endif /* DEBUG_EDIT */
    606 			continue;	/* try again */
    607 		}
    608 		/* now do the real command */
    609 #ifdef DEBUG_READ
    610 		{
    611 			el_bindings_t *b;
    612 			for (b = el->el_map.help; b->name; b++)
    613 				if (b->func == cmdnum)
    614 					break;
    615 			if (b->name)
    616 				(void) fprintf(el->el_errfile,
    617 				    "Executing %s\n", b->name);
    618 			else
    619 				(void) fprintf(el->el_errfile,
    620 				    "Error command = %d\n", cmdnum);
    621 		}
    622 #endif /* DEBUG_READ */
    623 		/* vi redo needs these way down the levels... */
    624 		el->el_state.thiscmd = cmdnum;
    625 		el->el_state.thisch = ch;
    626 		if (el->el_map.type == MAP_VI &&
    627 		    el->el_map.current == el->el_map.key &&
    628 		    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
    629 			if (cmdnum == VI_DELETE_PREV_CHAR &&
    630 			    el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
    631 			    && Isprint(el->el_chared.c_redo.pos[-1]))
    632 				el->el_chared.c_redo.pos--;
    633 			else
    634 				*el->el_chared.c_redo.pos++ = ch;
    635 		}
    636 		retval = (*el->el_map.func[cmdnum]) (el, ch);
    637 #ifdef DEBUG_READ
    638 		(void) fprintf(el->el_errfile,
    639 			"Returned state %d\n", retval );
    640 #endif /* DEBUG_READ */
    641 
    642 		/* save the last command here */
    643 		el->el_state.lastcmd = cmdnum;
    644 
    645 		/* use any return value */
    646 		switch (retval) {
    647 		case CC_CURSOR:
    648 			re_refresh_cursor(el);
    649 			break;
    650 
    651 		case CC_REDISPLAY:
    652 			re_clear_lines(el);
    653 			re_clear_display(el);
    654 			/* FALLTHROUGH */
    655 
    656 		case CC_REFRESH:
    657 			re_refresh(el);
    658 			break;
    659 
    660 		case CC_REFRESH_BEEP:
    661 			re_refresh(el);
    662 			terminal_beep(el);
    663 			break;
    664 
    665 		case CC_NORM:	/* normal char */
    666 			break;
    667 
    668 		case CC_ARGHACK:	/* Suggested by Rich Salz */
    669 			/* <rsalz (at) pineapple.bbn.com> */
    670 			continue;	/* keep going... */
    671 
    672 		case CC_EOF:	/* end of file typed */
    673 			if ((el->el_flags & UNBUFFERED) == 0)
    674 				num = 0;
    675 			else if (num == -1) {
    676 				*el->el_line.lastchar++ = CONTROL('d');
    677 				el->el_line.cursor = el->el_line.lastchar;
    678 				num = 1;
    679 			}
    680 			break;
    681 
    682 		case CC_NEWLINE:	/* normal end of line */
    683 			num = (int)(el->el_line.lastchar - el->el_line.buffer);
    684 			break;
    685 
    686 		case CC_FATAL:	/* fatal error, reset to known state */
    687 #ifdef DEBUG_READ
    688 			(void) fprintf(el->el_errfile,
    689 			    "*** editor fatal ERROR ***\r\n\n");
    690 #endif /* DEBUG_READ */
    691 			/* put (real) cursor in a known place */
    692 			re_clear_display(el);	/* reset the display stuff */
    693 			ch_reset(el, 1);	/* reset the input pointers */
    694 			re_refresh(el); /* print the prompt again */
    695 			break;
    696 
    697 		case CC_ERROR:
    698 		default:	/* functions we don't know about */
    699 #ifdef DEBUG_READ
    700 			(void) fprintf(el->el_errfile,
    701 			    "*** editor ERROR ***\r\n\n");
    702 #endif /* DEBUG_READ */
    703 			terminal_beep(el);
    704 			terminal__flush(el);
    705 			break;
    706 		}
    707 		el->el_state.argument = 1;
    708 		el->el_state.doingarg = 0;
    709 		el->el_chared.c_vcmd.action = NOP;
    710 		if (el->el_flags & UNBUFFERED)
    711 			break;
    712 	}
    713 
    714 	terminal__flush(el);		/* flush any buffered output */
    715 	/* make sure the tty is set up correctly */
    716 	if ((el->el_flags & UNBUFFERED) == 0) {
    717 		read_finish(el);
    718 		*nread = num != -1 ? num : 0;
    719 	} else {
    720 		*nread = (int)(el->el_line.lastchar - el->el_line.buffer);
    721 	}
    722 	goto done;
    723 noedit:
    724 	el->el_line.cursor = el->el_line.lastchar = cp;
    725 	*cp = '\0';
    726 	*nread = (int)(el->el_line.cursor - el->el_line.buffer);
    727 done:
    728 	if (*nread == 0) {
    729 		if (num == -1) {
    730 			*nread = -1;
    731 			errno = el->el_errno;
    732 		}
    733 		return NULL;
    734 	} else
    735 		return el->el_line.buffer;
    736 }
    737