read.c 14.1 KB
Newer Older
1
/*	$NetBSD: read.c,v 1.43 2009/02/05 19:15:44 christos Exp $	*/
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Christos Zoulas of Cornell University.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
unknown's avatar
unknown committed
18
 * 3. Neither the name of the University nor the names of its contributors
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

35 36 37 38 39 40 41
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
#if 0
static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/4/93";
#else
#endif
#endif /* not lint && not SCCSID */
42 43 44 45 46 47

/*
 * read.c: Clean this junk up! This is horrible code.
 *	   Terminal read functions
 */
#include <errno.h>
unknown's avatar
unknown committed
48
#include <fcntl.h>
49 50 51 52 53 54 55 56 57
#include <unistd.h>
#include <stdlib.h>
#include "el.h"

#define	OKCMD	-1

private int	read__fixio(int, int);
private int	read_preread(EditLine *);
private int	read_char(EditLine *, char *);
unknown's avatar
unknown committed
58
private int	read_getcmd(EditLine *, el_action_t *, char *);
59
private void	read_pop(c_macro_t *);
unknown's avatar
unknown committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

/* read_init():
 *	Initialize the read stuff
 */
protected int
read_init(EditLine *el)
{
	/* builtin read_char */
	el->el_read.read_char = read_char;
	return 0;
}


/* el_read_setfn():
 *	Set the read char function to the one provided.
 *	If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
 */
protected int
el_read_setfn(EditLine *el, el_rfunc_t rc)
{
	el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
	return 0;
}


/* el_read_getfn():
 *	return the current read char function, or EL_BUILTIN_GETCFN
 *	if it is the default one
 */
protected el_rfunc_t
el_read_getfn(EditLine *el)
{
       return (el->el_read.read_char == read_char) ?
	    EL_BUILTIN_GETCFN : el->el_read.read_char;
}
95

unknown's avatar
unknown committed
96

unknown's avatar
unknown committed
97 98 99 100
#ifndef MIN
#define MIN(A,B) ((A) < (B) ? (A) : (B))
#endif

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#ifdef DEBUG_EDIT
private void
read_debug(EditLine *el)
{

	if (el->el_line.cursor > el->el_line.lastchar)
		(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
	if (el->el_line.cursor < el->el_line.buffer)
		(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
	if (el->el_line.cursor > el->el_line.limit)
		(void) fprintf(el->el_errfile, "cursor > limit\r\n");
	if (el->el_line.lastchar > el->el_line.limit)
		(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
	if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
		(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
}
#endif /* DEBUG_EDIT */


/* read__fixio():
 *	Try to recover from a read error
 */
/* ARGSUSED */
private int
unknown's avatar
unknown committed
125
read__fixio(int fd __attribute__((__unused__)), int e)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
{

	switch (e) {
	case -1:		/* Make sure that the code is reachable */

#ifdef EWOULDBLOCK
	case EWOULDBLOCK:
#ifndef TRY_AGAIN
#define	TRY_AGAIN
#endif
#endif /* EWOULDBLOCK */

#if defined(POSIX) && defined(EAGAIN)
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
	case EAGAIN:
#ifndef TRY_AGAIN
#define	TRY_AGAIN
#endif
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
#endif /* POSIX && EAGAIN */

		e = 0;
#ifdef TRY_AGAIN
#if defined(F_SETFL) && defined(O_NDELAY)
		if ((e = fcntl(fd, F_GETFL, 0)) == -1)
			return (-1);

		if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
			return (-1);
		else
			e = 1;
#endif /* F_SETFL && O_NDELAY */

#ifdef FIONBIO
		{
			int zero = 0;

			if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
				return (-1);
			else
				e = 1;
		}
#endif /* FIONBIO */

#endif /* TRY_AGAIN */
		return (e ? 0 : -1);

	case EINTR:
		return (0);

	default:
		return (-1);
	}
}


/* read_preread():
 *	Try to read the stuff in the input queue;
 */
private int
read_preread(EditLine *el)
{
	int chrs = 0;

	if (el->el_tty.t_mode == ED_IO)
		return (0);

#ifdef FIONREAD
	(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
	if (chrs > 0) {
		char buf[EL_BUFSIZ];

		chrs = read(el->el_infd, buf,
		    (size_t) MIN(chrs, EL_BUFSIZ - 1));
		if (chrs > 0) {
			buf[chrs] = '\0';
unknown's avatar
unknown committed
202
			el_push(el, buf);
203 204 205 206 207 208 209 210 211 212 213 214
		}
	}
#endif /* FIONREAD */

	return (chrs > 0);
}


/* el_push():
 *	Push a macro
 */
public void
215
el_push(EditLine *el, const char *str)
216 217 218 219 220
{
	c_macro_t *ma = &el->el_chared.c_macro;

	if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
		ma->level++;
unknown's avatar
unknown committed
221 222 223
		if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
			return;
		ma->level--;
224
	}
unknown's avatar
unknown committed
225
	term_beep(el);
226
	term__flush(el);
227 228 229 230 231 232 233 234 235
}


/* read_getcmd():
 *	Return next command from the input stream.
 */
private int
read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
{
unknown's avatar
unknown committed
236
	el_action_t cmd;
237 238
	int num;

unknown's avatar
unknown committed
239
	do {
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
		if ((num = el_getc(el, ch)) != 1)	/* if EOF or error */
			return (num);

#ifdef	KANJI
		if ((*ch & 0200)) {
			el->el_state.metanext = 0;
			cmd = CcViMap[' '];
			break;
		} else
#endif /* KANJI */

		if (el->el_state.metanext) {
			el->el_state.metanext = 0;
			*ch |= 0200;
		}
		cmd = el->el_map.current[(unsigned char) *ch];
		if (cmd == ED_SEQUENCE_LEAD_IN) {
			key_value_t val;
unknown's avatar
unknown committed
258
			switch (key_get(el, ch, &val)) {
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			case XK_CMD:
				cmd = val.cmd;
				break;
			case XK_STR:
				el_push(el, val.str);
				break;
#ifdef notyet
			case XK_EXE:
				/* XXX: In the future to run a user function */
				RunCommand(val.str);
				break;
#endif
			default:
				EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
				break;
			}
		}
		if (el->el_map.alt == NULL)
			el->el_map.current = el->el_map.key;
unknown's avatar
unknown committed
278
	} while (cmd == ED_SEQUENCE_LEAD_IN);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	*cmdnum = cmd;
	return (OKCMD);
}


/* read_char():
 *	Read a character from the tty.
 */
private int
read_char(EditLine *el, char *cp)
{
	int num_read;
	int tried = 0;

	while ((num_read = read(el->el_infd, cp, 1)) == -1)
		if (!tried && read__fixio(el->el_infd, errno) == 0)
			tried = 1;
		else {
			*cp = '\0';
			return (-1);
		}

	return (num_read);
}

304 305 306 307 308 309 310 311 312 313 314 315 316
/* read_pop():
 *	Pop a macro from the stack
 */
private void
read_pop(c_macro_t *ma)
{
	int i;

	el_free(ma->macro[0]);
	for (i = ma->level--; i > 0; i--)
		ma->macro[i - 1] = ma->macro[i];
	ma->offset = 0;
}
317 318 319 320 321 322 323 324 325 326

/* el_getc():
 *	Read a character
 */
public int
el_getc(EditLine *el, char *cp)
{
	int num_read;
	c_macro_t *ma = &el->el_chared.c_macro;

327
	term__flush(el);
328 329 330 331 332
	for (;;) {
		if (ma->level < 0) {
			if (!read_preread(el))
				break;
		}
333

334 335 336
		if (ma->level < 0)
			break;

337 338
		if (ma->macro[0][ma->offset] == '\0') {
			read_pop(ma);
339 340
			continue;
		}
341 342 343 344

		*cp = ma->macro[0][ma->offset++] & 0377;

		if (ma->macro[0][ma->offset] == '\0') {
unknown's avatar
unknown committed
345
			/* Needed for QuoteMode On */
346
			read_pop(ma);
347
		}
348

349 350 351 352 353 354 355 356 357 358 359 360
		return (1);
	}

#ifdef DEBUG_READ
	(void) fprintf(el->el_errfile, "Turning raw mode on\n");
#endif /* DEBUG_READ */
	if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
		return (0);

#ifdef DEBUG_READ
	(void) fprintf(el->el_errfile, "Reading a character\n");
#endif /* DEBUG_READ */
unknown's avatar
unknown committed
361
	num_read = (*el->el_read.read_char)(el, cp);
362 363 364 365 366 367
#ifdef DEBUG_READ
	(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
#endif /* DEBUG_READ */
	return (num_read);
}

unknown's avatar
unknown committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381
protected void
read_prepare(EditLine *el)
{
	if (el->el_flags & HANDLE_SIGNALS)
		sig_set(el);
	if (el->el_flags & NO_TTY)
		return;
	if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
		tty_rawmode(el);

	/* This is relatively cheap, and things go terribly wrong if
	   we have the wrong size. */
	el_resize(el);
	re_clear_display(el);	/* reset the display stuff */
382
	ch_reset(el, 0);
unknown's avatar
unknown committed
383 384 385
	re_refresh(el);		/* print the prompt */

	if (el->el_flags & UNBUFFERED)
386
		term__flush(el);
unknown's avatar
unknown committed
387 388 389 390 391 392 393 394 395 396
}

protected void
read_finish(EditLine *el)
{
	if ((el->el_flags & UNBUFFERED) == 0)
		(void) tty_cookedmode(el);
	if (el->el_flags & HANDLE_SIGNALS)
		sig_clr(el);
}
397 398 399 400 401 402 403 404

public const char *
el_gets(EditLine *el, int *nread)
{
	int retval;
	el_action_t cmdnum = 0;
	int num;		/* how many chars we have read at NL */
	char ch;
unknown's avatar
unknown committed
405
	int crlf = 0;
406 407 408 409 410 411 412 413
#ifdef FIONREAD
	c_macro_t *ma = &el->el_chared.c_macro;
#endif /* FIONREAD */

	if (el->el_flags & NO_TTY) {
		char *cp = el->el_line.buffer;
		size_t idx;

unknown's avatar
unknown committed
414
		while ((*el->el_read.read_char)(el, cp) == 1) {
415 416 417 418 419 420 421 422
			/* make sure there is space for next character */
			if (cp + 1 >= el->el_line.limit) {
				idx = (cp - el->el_line.buffer);
				if (!ch_enlargebufs(el, 2))
					break;
				cp = &el->el_line.buffer[idx];
			}
			cp++;
unknown's avatar
unknown committed
423 424
			if (el->el_flags & UNBUFFERED)
				break;
425 426 427 428 429 430 431 432 433 434
			if (cp[-1] == '\r' || cp[-1] == '\n')
				break;
		}

		el->el_line.cursor = el->el_line.lastchar = cp;
		*cp = '\0';
		if (nread)
			*nread = el->el_line.cursor - el->el_line.buffer;
		return (el->el_line.buffer);
	}
unknown's avatar
unknown committed
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

#ifdef FIONREAD
	if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
		long chrs = 0;

		(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
		if (chrs == 0) {
			if (tty_rawmode(el) < 0) {
				if (nread)
					*nread = 0;
				return (NULL);
			}
		}
	}
#endif /* FIONREAD */

unknown's avatar
unknown committed
452 453
	if ((el->el_flags & UNBUFFERED) == 0)
		read_prepare(el);
454 455

	if (el->el_flags & EDIT_DISABLED) {
unknown's avatar
unknown committed
456
		char *cp;
457
		size_t idx;
unknown's avatar
unknown committed
458 459 460 461
		if ((el->el_flags & UNBUFFERED) == 0)
			cp = el->el_line.buffer;
		else
			cp = el->el_line.lastchar;
462

463
		term__flush(el);
464

unknown's avatar
unknown committed
465
		while ((*el->el_read.read_char)(el, cp) == 1) {
466 467 468 469 470 471 472
			/* make sure there is space next character */
			if (cp + 1 >= el->el_line.limit) {
				idx = (cp - el->el_line.buffer);
				if (!ch_enlargebufs(el, 2))
					break;
				cp = &el->el_line.buffer[idx];
			}
unknown's avatar
unknown committed
473 474
			if (*cp == 4)	/* ought to be stty eof */
				break;
475
			cp++;
unknown's avatar
unknown committed
476 477 478 479
			crlf = cp[-1] == '\r' || cp[-1] == '\n';
			if (el->el_flags & UNBUFFERED)
				break;
			if (crlf)
480 481 482 483 484 485 486 487 488
				break;
		}

		el->el_line.cursor = el->el_line.lastchar = cp;
		*cp = '\0';
		if (nread)
			*nread = el->el_line.cursor - el->el_line.buffer;
		return (el->el_line.buffer);
	}
unknown's avatar
unknown committed
489

490 491 492 493 494 495 496 497 498 499 500 501 502
	for (num = OKCMD; num == OKCMD;) {	/* while still editing this
						 * line */
#ifdef DEBUG_EDIT
		read_debug(el);
#endif /* DEBUG_EDIT */
		/* if EOF or error */
		if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
#ifdef DEBUG_READ
			(void) fprintf(el->el_errfile,
			    "Returning from el_gets %d\n", num);
#endif /* DEBUG_READ */
			break;
		}
503
		if ((unsigned int)cmdnum >= (unsigned int)el->el_map.nfunc) {	/* BUG CHECK command */
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
#ifdef DEBUG_EDIT
			(void) fprintf(el->el_errfile,
			    "ERROR: illegal command from key 0%o\r\n", ch);
#endif /* DEBUG_EDIT */
			continue;	/* try again */
		}
		/* now do the real command */
#ifdef DEBUG_READ
		{
			el_bindings_t *b;
			for (b = el->el_map.help; b->name; b++)
				if (b->func == cmdnum)
					break;
			if (b->name)
				(void) fprintf(el->el_errfile,
				    "Executing %s\n", b->name);
			else
				(void) fprintf(el->el_errfile,
				    "Error command = %d\n", cmdnum);
		}
#endif /* DEBUG_READ */
unknown's avatar
unknown committed
525 526 527 528 529 530 531 532
		/* vi redo needs these way down the levels... */
		el->el_state.thiscmd = cmdnum;
		el->el_state.thisch = ch;
		if (el->el_map.type == MAP_VI &&
		    el->el_map.current == el->el_map.key &&
		    el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
			if (cmdnum == VI_DELETE_PREV_CHAR &&
			    el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
533
			    && el_isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
unknown's avatar
unknown committed
534 535 536 537
				el->el_chared.c_redo.pos--;
			else
				*el->el_chared.c_redo.pos++ = ch;
		}
538
		retval = (*el->el_map.func[cmdnum]) (el, ch);
unknown's avatar
unknown committed
539 540 541 542
#ifdef DEBUG_READ
		(void) fprintf(el->el_errfile,
			"Returned state %d\n", retval );
#endif /* DEBUG_READ */
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

		/* save the last command here */
		el->el_state.lastcmd = cmdnum;

		/* use any return value */
		switch (retval) {
		case CC_CURSOR:
			re_refresh_cursor(el);
			break;

		case CC_REDISPLAY:
			re_clear_lines(el);
			re_clear_display(el);
			/* FALLTHROUGH */

		case CC_REFRESH:
			re_refresh(el);
			break;

		case CC_REFRESH_BEEP:
			re_refresh(el);
			term_beep(el);
			break;

		case CC_NORM:	/* normal char */
			break;

		case CC_ARGHACK:	/* Suggested by Rich Salz */
			/* <rsalz@pineapple.bbn.com> */
unknown's avatar
unknown committed
572
			continue;	/* keep going... */
573 574

		case CC_EOF:	/* end of file typed */
unknown's avatar
unknown committed
575 576 577 578 579 580 581
			if ((el->el_flags & UNBUFFERED) == 0)
				num = 0;
			else if (num == -1) {
				*el->el_line.lastchar++ = CONTROL('d');
				el->el_line.cursor = el->el_line.lastchar;
				num = 1;
			}
582 583 584 585 586 587 588 589 590 591 592 593 594
			break;

		case CC_NEWLINE:	/* normal end of line */
			num = el->el_line.lastchar - el->el_line.buffer;
			break;

		case CC_FATAL:	/* fatal error, reset to known state */
#ifdef DEBUG_READ
			(void) fprintf(el->el_errfile,
			    "*** editor fatal ERROR ***\r\n\n");
#endif /* DEBUG_READ */
			/* put (real) cursor in a known place */
			re_clear_display(el);	/* reset the display stuff */
595
			ch_reset(el, 1);	/* reset the input pointers */
596 597 598 599 600 601 602 603 604 605
			re_refresh(el);	/* print the prompt again */
			break;

		case CC_ERROR:
		default:	/* functions we don't know about */
#ifdef DEBUG_READ
			(void) fprintf(el->el_errfile,
			    "*** editor ERROR ***\r\n\n");
#endif /* DEBUG_READ */
			term_beep(el);
606
			term__flush(el);
607 608
			break;
		}
unknown's avatar
unknown committed
609 610 611
		el->el_state.argument = 1;
		el->el_state.doingarg = 0;
		el->el_chared.c_vcmd.action = NOP;
unknown's avatar
unknown committed
612 613
		if (el->el_flags & UNBUFFERED)
			break;
614 615
	}

616
	term__flush(el);		/* flush any buffered output */
unknown's avatar
unknown committed
617
	/* make sure the tty is set up correctly */
unknown's avatar
unknown committed
618 619 620 621 622 623 624 625
	if ((el->el_flags & UNBUFFERED) == 0) {
		read_finish(el);
		if (nread)
			*nread = num;
	} else {
		if (nread)
			*nread = el->el_line.lastchar - el->el_line.buffer;
	}
626 627
	return (num ? el->el_line.buffer : NULL);
}