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 * @(#)read.c 8.1 (Berkeley) 6/4/93
33 * $NetBSD: read.c,v 1.36 2005/06/01 11:37:52 lukem Exp $
34 * $DragonFly: src/lib/libedit/read.c,v 1.4 2005/11/13 11:58:30 corecode Exp $
40 * read.c: Clean this junk up! This is horrible code.
41 * Terminal read functions
51 private int read__fixio(int, int);
52 private int read_preread(EditLine *);
53 private int read_char(EditLine *, char *);
54 private int read_getcmd(EditLine *, el_action_t *, char *);
57 * Initialize the read stuff
60 read_init(EditLine *el)
62 /* builtin read_char */
63 el->el_read.read_char = read_char;
69 * Set the read char function to the one provided.
70 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
73 el_read_setfn(EditLine *el, el_rfunc_t rc)
75 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
81 * return the current read char function, or EL_BUILTIN_GETCFN
82 * if it is the default one
85 el_read_getfn(EditLine *el)
87 return (el->el_read.read_char == read_char) ?
88 EL_BUILTIN_GETCFN : el->el_read.read_char;
93 #define MIN(A,B) ((A) < (B) ? (A) : (B))
98 read_debug(EditLine *el)
101 if (el->el_line.cursor > el->el_line.lastchar)
102 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
103 if (el->el_line.cursor < el->el_line.buffer)
104 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
105 if (el->el_line.cursor > el->el_line.limit)
106 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
107 if (el->el_line.lastchar > el->el_line.limit)
108 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
109 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
110 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
112 #endif /* DEBUG_EDIT */
116 * Try to recover from a read error
120 read__fixio(int fd __attribute__((__unused__)), int e)
124 case -1: /* Make sure that the code is reachable */
131 #endif /* EWOULDBLOCK */
133 #if defined(POSIX) && defined(EAGAIN)
134 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
139 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
140 #endif /* POSIX && EAGAIN */
144 #if defined(F_SETFL) && defined(O_NDELAY)
145 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
148 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
152 #endif /* F_SETFL && O_NDELAY */
158 if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
165 #endif /* TRY_AGAIN */
178 * Try to read the stuff in the input queue;
181 read_preread(EditLine *el)
185 if (el->el_tty.t_mode == ED_IO)
189 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
193 chrs = read(el->el_infd, buf,
194 (size_t) MIN(chrs, EL_BUFSIZ - 1));
200 #endif /* FIONREAD */
210 el_push(EditLine *el, char *str)
212 c_macro_t *ma = &el->el_chared.c_macro;
214 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
216 if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
226 * Return next command from the input stream.
229 read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
235 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
240 el->el_state.metanext = 0;
246 if (el->el_state.metanext) {
247 el->el_state.metanext = 0;
250 cmd = el->el_map.current[(unsigned char) *ch];
251 if (cmd == ED_SEQUENCE_LEAD_IN) {
253 switch (key_get(el, ch, &val)) {
258 el_push(el, val.str);
262 /* XXX: In the future to run a user function */
267 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
271 if (el->el_map.alt == NULL)
272 el->el_map.current = el->el_map.key;
273 } while (cmd == ED_SEQUENCE_LEAD_IN);
280 * Read a character from the tty.
283 read_char(EditLine *el, char *cp)
288 while ((num_read = read(el->el_infd, cp, 1)) == -1)
289 if (!tried && read__fixio(el->el_infd, errno) == 0)
304 el_getc(EditLine *el, char *cp)
307 c_macro_t *ma = &el->el_chared.c_macro;
312 if (!read_preread(el))
318 if (ma->macro[ma->level][ma->offset] == '\0') {
319 el_free(ma->macro[ma->level--]);
323 *cp = ma->macro[ma->level][ma->offset++] & 0377;
324 if (ma->macro[ma->level][ma->offset] == '\0') {
325 /* Needed for QuoteMode On */
326 el_free(ma->macro[ma->level--]);
333 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
334 #endif /* DEBUG_READ */
335 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
339 (void) fprintf(el->el_errfile, "Reading a character\n");
340 #endif /* DEBUG_READ */
341 num_read = (*el->el_read.read_char)(el, cp);
343 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
344 #endif /* DEBUG_READ */
349 read_prepare(EditLine *el)
351 if (el->el_flags & HANDLE_SIGNALS)
353 if (el->el_flags & NO_TTY)
355 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
358 /* This is relatively cheap, and things go terribly wrong if
359 we have the wrong size. */
361 re_clear_display(el); /* reset the display stuff */
363 re_refresh(el); /* print the prompt */
365 if (el->el_flags & UNBUFFERED)
370 read_finish(EditLine *el)
372 if ((el->el_flags & UNBUFFERED) == 0)
373 (void) tty_cookedmode(el);
374 if (el->el_flags & HANDLE_SIGNALS)
379 el_gets(EditLine *el, int *nread)
382 el_action_t cmdnum = 0;
383 int num; /* how many chars we have read at NL */
387 c_macro_t *ma = &el->el_chared.c_macro;
388 #endif /* FIONREAD */
390 if (el->el_flags & NO_TTY) {
391 char *cp = el->el_line.buffer;
394 while ((*el->el_read.read_char)(el, cp) == 1) {
395 /* make sure there is space for next character */
396 if (cp + 1 >= el->el_line.limit) {
397 idx = (cp - el->el_line.buffer);
398 if (!ch_enlargebufs(el, 2))
400 cp = &el->el_line.buffer[idx];
403 if (el->el_flags & UNBUFFERED)
405 if (cp[-1] == '\r' || cp[-1] == '\n')
409 el->el_line.cursor = el->el_line.lastchar = cp;
412 *nread = el->el_line.cursor - el->el_line.buffer;
413 return (el->el_line.buffer);
418 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
421 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
423 if (tty_rawmode(el) < 0) {
430 #endif /* FIONREAD */
432 if ((el->el_flags & UNBUFFERED) == 0)
435 if (el->el_flags & EDIT_DISABLED) {
438 if ((el->el_flags & UNBUFFERED) == 0)
439 cp = el->el_line.buffer;
441 cp = el->el_line.lastchar;
445 while ((*el->el_read.read_char)(el, cp) == 1) {
446 /* make sure there is space next character */
447 if (cp + 1 >= el->el_line.limit) {
448 idx = (cp - el->el_line.buffer);
449 if (!ch_enlargebufs(el, 2))
451 cp = &el->el_line.buffer[idx];
453 if (*cp == 4) /* ought to be stty eof */
456 crlf = cp[-1] == '\r' || cp[-1] == '\n';
457 if (el->el_flags & UNBUFFERED)
463 el->el_line.cursor = el->el_line.lastchar = cp;
466 *nread = el->el_line.cursor - el->el_line.buffer;
467 return (el->el_line.buffer);
470 for (num = OKCMD; num == OKCMD;) { /* while still editing this
474 #endif /* DEBUG_EDIT */
475 /* if EOF or error */
476 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
478 (void) fprintf(el->el_errfile,
479 "Returning from el_gets %d\n", num);
480 #endif /* DEBUG_READ */
483 if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
485 (void) fprintf(el->el_errfile,
486 "ERROR: illegal command from key 0%o\r\n", ch);
487 #endif /* DEBUG_EDIT */
488 continue; /* try again */
490 /* now do the real command */
494 for (b = el->el_map.help; b->name; b++)
495 if (b->func == cmdnum)
498 (void) fprintf(el->el_errfile,
499 "Executing %s\n", b->name);
501 (void) fprintf(el->el_errfile,
502 "Error command = %d\n", cmdnum);
504 #endif /* DEBUG_READ */
505 /* vi redo needs these way down the levels... */
506 el->el_state.thiscmd = cmdnum;
507 el->el_state.thisch = ch;
508 if (el->el_map.type == MAP_VI &&
509 el->el_map.current == el->el_map.key &&
510 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
511 if (cmdnum == VI_DELETE_PREV_CHAR &&
512 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
513 && isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
514 el->el_chared.c_redo.pos--;
516 *el->el_chared.c_redo.pos++ = ch;
518 retval = (*el->el_map.func[cmdnum]) (el, ch);
520 (void) fprintf(el->el_errfile,
521 "Returned state %d\n", retval );
522 #endif /* DEBUG_READ */
524 /* save the last command here */
525 el->el_state.lastcmd = cmdnum;
527 /* use any return value */
530 re_refresh_cursor(el);
535 re_clear_display(el);
542 case CC_REFRESH_BEEP:
547 case CC_NORM: /* normal char */
550 case CC_ARGHACK: /* Suggested by Rich Salz */
551 /* <rsalz@pineapple.bbn.com> */
552 continue; /* keep going... */
554 case CC_EOF: /* end of file typed */
555 if ((el->el_flags & UNBUFFERED) == 0)
557 else if (num == -1) {
558 *el->el_line.lastchar++ = CONTROL('d');
559 el->el_line.cursor = el->el_line.lastchar;
564 case CC_NEWLINE: /* normal end of line */
565 num = el->el_line.lastchar - el->el_line.buffer;
568 case CC_FATAL: /* fatal error, reset to known state */
570 (void) fprintf(el->el_errfile,
571 "*** editor fatal ERROR ***\r\n\n");
572 #endif /* DEBUG_READ */
573 /* put (real) cursor in a known place */
574 re_clear_display(el); /* reset the display stuff */
575 ch_reset(el); /* reset the input pointers */
576 re_refresh(el); /* print the prompt again */
580 default: /* functions we don't know about */
582 (void) fprintf(el->el_errfile,
583 "*** editor ERROR ***\r\n\n");
584 #endif /* DEBUG_READ */
589 el->el_state.argument = 1;
590 el->el_state.doingarg = 0;
591 el->el_chared.c_vcmd.action = NOP;
592 if (el->el_flags & UNBUFFERED)
596 term__flush(); /* flush any buffered output */
597 /* make sure the tty is set up correctly */
598 if ((el->el_flags & UNBUFFERED) == 0) {
604 *nread = el->el_line.lastchar - el->el_line.buffer;
606 return (num ? el->el_line.buffer : NULL);