2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: read.c,v 1.40 2007/03/01 21:41:45 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * read.c: Clean this junk up! This is horrible code.
43 * Terminal read functions
54 private int read__fixio(int, int);
55 private int read_preread(EditLine *);
56 private int read_char(EditLine *, char *);
57 private int read_getcmd(EditLine *, el_action_t *, char *);
58 private void read_pop(c_macro_t *);
61 * Initialize the read stuff
64 read_init(EditLine *el)
66 /* builtin read_char */
67 el->el_read.read_char = read_char;
73 * Set the read char function to the one provided.
74 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
77 el_read_setfn(EditLine *el, el_rfunc_t rc)
79 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
85 * return the current read char function, or EL_BUILTIN_GETCFN
86 * if it is the default one
89 el_read_getfn(EditLine *el)
91 return (el->el_read.read_char == read_char) ?
92 EL_BUILTIN_GETCFN : el->el_read.read_char;
97 #define MIN(A,B) ((A) < (B) ? (A) : (B))
102 read_debug(EditLine *el)
105 if (el->el_line.cursor > el->el_line.lastchar)
106 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
107 if (el->el_line.cursor < el->el_line.buffer)
108 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
109 if (el->el_line.cursor > el->el_line.limit)
110 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
111 if (el->el_line.lastchar > el->el_line.limit)
112 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
113 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
114 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
116 #endif /* DEBUG_EDIT */
120 * Try to recover from a read error
124 read__fixio(int fd __unused, int e)
128 case -1: /* Make sure that the code is reachable */
135 #endif /* EWOULDBLOCK */
137 #if defined(POSIX) && defined(EAGAIN)
138 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
143 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
144 #endif /* POSIX && EAGAIN */
148 #if defined(F_SETFL) && defined(O_NDELAY)
149 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
152 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
156 #endif /* F_SETFL && O_NDELAY */
162 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
169 #endif /* TRY_AGAIN */
182 * Try to read the stuff in the input queue;
185 read_preread(EditLine *el)
189 if (el->el_tty.t_mode == ED_IO)
193 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
197 chrs = read(el->el_infd, buf,
198 (size_t) MIN(chrs, EL_BUFSIZ - 1));
204 #endif /* FIONREAD */
214 el_push(EditLine *el, const char *str)
216 c_macro_t *ma = &el->el_chared.c_macro;
218 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
220 if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
230 * Return next command from the input stream.
233 read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
239 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
244 el->el_state.metanext = 0;
250 if (el->el_state.metanext) {
251 el->el_state.metanext = 0;
254 cmd = el->el_map.current[(unsigned char) *ch];
255 if (cmd == ED_SEQUENCE_LEAD_IN) {
257 switch (key_get(el, ch, &val)) {
262 el_push(el, val.str);
266 /* XXX: In the future to run a user function */
271 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
275 if (el->el_map.alt == NULL)
276 el->el_map.current = el->el_map.key;
277 } while (cmd == ED_SEQUENCE_LEAD_IN);
284 * Read a character from the tty.
287 read_char(EditLine *el, char *cp)
292 while ((num_read = read(el->el_infd, cp, 1)) == -1)
293 if (!tried && read__fixio(el->el_infd, errno) == 0)
304 * Pop a macro from the stack
307 read_pop(c_macro_t *ma)
311 el_free(ma->macro[0]);
312 for (i = ma->level--; i > 0; i--)
313 ma->macro[i - 1] = ma->macro[i];
321 el_getc(EditLine *el, char *cp)
324 c_macro_t *ma = &el->el_chared.c_macro;
329 if (!read_preread(el))
336 if (ma->macro[0][ma->offset] == '\0') {
341 *cp = ma->macro[0][ma->offset++] & 0377;
343 if (ma->macro[0][ma->offset] == '\0') {
344 /* Needed for QuoteMode On */
352 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
353 #endif /* DEBUG_READ */
354 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
358 (void) fprintf(el->el_errfile, "Reading a character\n");
359 #endif /* DEBUG_READ */
360 num_read = (*el->el_read.read_char)(el, cp);
362 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
363 #endif /* DEBUG_READ */
368 read_prepare(EditLine *el)
370 if (el->el_flags & HANDLE_SIGNALS)
372 if (el->el_flags & NO_TTY)
374 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
377 /* This is relatively cheap, and things go terribly wrong if
378 we have the wrong size. */
380 re_clear_display(el); /* reset the display stuff */
382 re_refresh(el); /* print the prompt */
384 if (el->el_flags & UNBUFFERED)
389 read_finish(EditLine *el)
391 if ((el->el_flags & UNBUFFERED) == 0)
392 (void) tty_cookedmode(el);
393 if (el->el_flags & HANDLE_SIGNALS)
398 el_gets(EditLine *el, int *nread)
401 el_action_t cmdnum = 0;
402 int num; /* how many chars we have read at NL */
406 c_macro_t *ma = &el->el_chared.c_macro;
407 #endif /* FIONREAD */
409 if (el->el_flags & NO_TTY) {
410 char *cp = el->el_line.buffer;
413 while ((*el->el_read.read_char)(el, cp) == 1) {
414 /* make sure there is space for next character */
415 if (cp + 1 >= el->el_line.limit) {
416 idx = (cp - el->el_line.buffer);
417 if (!ch_enlargebufs(el, 2))
419 cp = &el->el_line.buffer[idx];
422 if (el->el_flags & UNBUFFERED)
424 if (cp[-1] == '\r' || cp[-1] == '\n')
428 el->el_line.cursor = el->el_line.lastchar = cp;
431 *nread = el->el_line.cursor - el->el_line.buffer;
432 return (el->el_line.buffer);
437 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
440 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
442 if (tty_rawmode(el) < 0) {
449 #endif /* FIONREAD */
451 if ((el->el_flags & UNBUFFERED) == 0)
454 if (el->el_flags & EDIT_DISABLED) {
457 if ((el->el_flags & UNBUFFERED) == 0)
458 cp = el->el_line.buffer;
460 cp = el->el_line.lastchar;
464 while ((*el->el_read.read_char)(el, cp) == 1) {
465 /* make sure there is space next character */
466 if (cp + 1 >= el->el_line.limit) {
467 idx = (cp - el->el_line.buffer);
468 if (!ch_enlargebufs(el, 2))
470 cp = &el->el_line.buffer[idx];
472 if (*cp == 4) /* ought to be stty eof */
475 crlf = cp[-1] == '\r' || cp[-1] == '\n';
476 if (el->el_flags & UNBUFFERED)
482 el->el_line.cursor = el->el_line.lastchar = cp;
485 *nread = el->el_line.cursor - el->el_line.buffer;
486 return (el->el_line.buffer);
489 for (num = OKCMD; num == OKCMD;) { /* while still editing this
493 #endif /* DEBUG_EDIT */
494 /* if EOF or error */
495 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
497 (void) fprintf(el->el_errfile,
498 "Returning from el_gets %d\n", num);
499 #endif /* DEBUG_READ */
502 if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
504 (void) fprintf(el->el_errfile,
505 "ERROR: illegal command from key 0%o\r\n", ch);
506 #endif /* DEBUG_EDIT */
507 continue; /* try again */
509 /* now do the real command */
513 for (b = el->el_map.help; b->name; b++)
514 if (b->func == cmdnum)
517 (void) fprintf(el->el_errfile,
518 "Executing %s\n", b->name);
520 (void) fprintf(el->el_errfile,
521 "Error command = %d\n", cmdnum);
523 #endif /* DEBUG_READ */
524 /* vi redo needs these way down the levels... */
525 el->el_state.thiscmd = cmdnum;
526 el->el_state.thisch = ch;
527 if (el->el_map.type == MAP_VI &&
528 el->el_map.current == el->el_map.key &&
529 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
530 if (cmdnum == VI_DELETE_PREV_CHAR &&
531 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
532 && isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
533 el->el_chared.c_redo.pos--;
535 *el->el_chared.c_redo.pos++ = ch;
537 retval = (*el->el_map.func[cmdnum]) (el, ch);
539 (void) fprintf(el->el_errfile,
540 "Returned state %d\n", retval );
541 #endif /* DEBUG_READ */
543 /* save the last command here */
544 el->el_state.lastcmd = cmdnum;
546 /* use any return value */
549 re_refresh_cursor(el);
554 re_clear_display(el);
561 case CC_REFRESH_BEEP:
566 case CC_NORM: /* normal char */
569 case CC_ARGHACK: /* Suggested by Rich Salz */
570 /* <rsalz@pineapple.bbn.com> */
571 continue; /* keep going... */
573 case CC_EOF: /* end of file typed */
574 if ((el->el_flags & UNBUFFERED) == 0)
576 else if (num == -1) {
577 *el->el_line.lastchar++ = CONTROL('d');
578 el->el_line.cursor = el->el_line.lastchar;
583 case CC_NEWLINE: /* normal end of line */
584 num = el->el_line.lastchar - el->el_line.buffer;
587 case CC_FATAL: /* fatal error, reset to known state */
589 (void) fprintf(el->el_errfile,
590 "*** editor fatal ERROR ***\r\n\n");
591 #endif /* DEBUG_READ */
592 /* put (real) cursor in a known place */
593 re_clear_display(el); /* reset the display stuff */
594 ch_reset(el, 1); /* reset the input pointers */
595 re_refresh(el); /* print the prompt again */
599 default: /* functions we don't know about */
601 (void) fprintf(el->el_errfile,
602 "*** editor ERROR ***\r\n\n");
603 #endif /* DEBUG_READ */
608 el->el_state.argument = 1;
609 el->el_state.doingarg = 0;
610 el->el_chared.c_vcmd.action = NOP;
611 if (el->el_flags & UNBUFFERED)
615 term__flush(); /* flush any buffered output */
616 /* make sure the tty is set up correctly */
617 if ((el->el_flags & UNBUFFERED) == 0) {
623 *nread = el->el_line.lastchar - el->el_line.buffer;
625 return (num ? el->el_line.buffer : NULL);