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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if !defined(lint) && !defined(SCCSID)
39 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
41 static const char rcsid[] =
42 "$FreeBSD: src/lib/libedit/read.c,v 1.4.6.3 2000/10/31 23:51:10 brian Exp $";
43 #endif /* not lint && not SCCSID */
45 * read.c: Clean this junk up! This is horrible code.
46 * Terminal read functions
57 private int read__fixio __P((int, int));
58 private int read_preread __P((EditLine *));
59 private int read_getcmd __P((EditLine *, el_action_t *, char *));
67 if (el->el_line.cursor > el->el_line.lastchar)
68 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
69 if (el->el_line.cursor < el->el_line.buffer)
70 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
71 if (el->el_line.cursor > el->el_line.limit)
72 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
73 if (el->el_line.lastchar > el->el_line.limit)
74 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
75 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
76 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
78 #endif /* DEBUG_EDIT */
81 * Try to recover from a read error
88 case -1: /* Make sure that the code is reachable */
95 #endif /* EWOULDBLOCK */
97 #if defined(POSIX) && defined(EAGAIN)
98 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
103 # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
104 #endif /* POSIX && EAGAIN */
108 # if defined(F_SETFL) && defined(O_NDELAY)
109 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
112 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
116 # endif /* F_SETFL && O_NDELAY */
119 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
123 # endif /* FIONBIO */
125 #endif /* TRY_AGAIN */
138 * Try to read the stuff in the input queue;
146 if (el->el_chared.c_macro.nline) {
147 el_free((ptr_t) el->el_chared.c_macro.nline);
148 el->el_chared.c_macro.nline = NULL;
151 if (el->el_tty.t_mode == ED_IO)
155 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
159 chrs = read(el->el_infd, buf, (size_t) MIN(chrs, EL_BUFSIZ - 1));
162 el->el_chared.c_macro.nline = strdup(buf);
163 el_push(el->el_chared.c_macro.nline);
166 #endif /* FIONREAD */
180 c_macro_t *ma = &el->el_chared.c_macro;
182 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
184 ma->macro[ma->level] = (char *) str;
194 * Return next command from the input stream.
197 read_getcmd(el, cmdnum, ch)
202 el_action_t cmd = ED_UNASSIGNED;
205 while (cmd == ED_UNASSIGNED || cmd == ED_SEQUENCE_LEAD_IN) {
206 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
211 el->el_state.metanext = 0;
218 if (el->el_state.metanext) {
219 el->el_state.metanext = 0;
222 cmd = el->el_map.current[(unsigned char) *ch];
223 if (cmd == ED_SEQUENCE_LEAD_IN) {
225 switch (key_get(el, ch, &val)) {
230 el_push(el, val.str);
234 /* XXX: In the future to run a user function */
243 if (el->el_map.alt == NULL)
244 el->el_map.current = el->el_map.key;
263 c_macro_t *ma = &el->el_chared.c_macro;
268 if (!read_preread(el))
274 if (*ma->macro[ma->level] == 0) {
278 *cp = *ma->macro[ma->level]++ & 0377;
279 if (*ma->macro[ma->level] == 0) { /* Needed for QuoteMode On */
286 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
287 #endif /* DEBUG_READ */
288 if (tty_rawmode(el) < 0) /* make sure the tty is set up correctly */
292 (void) fprintf(el->el_errfile, "Reading a character\n");
293 #endif /* DEBUG_READ */
294 while ((num_read = read(el->el_infd, (char *) &tcp, 1)) == -1)
295 if (!tried && read__fixio(el->el_infd, errno) == 0)
302 (void) fprintf(el->el_errfile, "Got it %c\n", tcp);
303 #endif /* DEBUG_READ */
316 el_action_t cmdnum = 0;
317 int num; /* how many chars we have read at NL */
320 if (el->el_flags & HANDLE_SIGNALS)
323 re_clear_display(el); /* reset the display stuff */
327 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
330 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
332 if (tty_rawmode(el) < 0) {
339 #endif /* FIONREAD */
341 re_refresh(el); /* print the prompt */
343 for (num = OKCMD; num == OKCMD;) { /* while still editing this line */
346 #endif /* DEBUG_EDIT */
347 /* if EOF or error */
348 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
350 (void) fprintf(el->el_errfile, "Returning from el_gets %d\n", num);
351 #endif /* DEBUG_READ */
355 if (cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
357 (void) fprintf(el->el_errfile,
358 "ERROR: illegal command from key 0%o\r\n", ch);
359 #endif /* DEBUG_EDIT */
360 continue; /* try again */
363 /* now do the real command */
367 for (b = el->el_map.help; b->name; b++)
368 if (b->func == cmdnum)
371 (void) fprintf(el->el_errfile, "Executing %s\n", b->name);
373 (void) fprintf(el->el_errfile, "Error command = %d\n", cmdnum);
375 #endif /* DEBUG_READ */
376 retval = (*el->el_map.func[cmdnum])(el, ch);
378 /* save the last command here */
379 el->el_state.lastcmd = cmdnum;
381 /* use any return value */
384 el->el_state.argument = 1;
385 el->el_state.doingarg = 0;
386 re_refresh_cursor(el);
391 re_clear_display(el);
395 el->el_state.argument = 1;
396 el->el_state.doingarg = 0;
400 case CC_NORM: /* normal char */
401 el->el_state.argument = 1;
402 el->el_state.doingarg = 0;
405 case CC_ARGHACK: /* Suggested by Rich Salz */
406 /* <rsalz@pineapple.bbn.com> */
407 break; /* keep going... */
409 case CC_EOF: /* end of file typed */
413 case CC_NEWLINE: /* normal end of line */
414 num = el->el_line.lastchar - el->el_line.buffer;
417 case CC_FATAL: /* fatal error, reset to known state */
419 (void) fprintf(el->el_errfile, "*** editor fatal ERROR ***\r\n\n");
420 #endif /* DEBUG_READ */
421 /* put (real) cursor in a known place */
422 re_clear_display(el); /* reset the display stuff */
423 ch_reset(el); /* reset the input pointers */
424 re_refresh(el); /* print the prompt again */
425 el->el_state.argument = 1;
426 el->el_state.doingarg = 0;
430 default: /* functions we don't know about */
432 (void) fprintf(el->el_errfile, "*** editor ERROR ***\r\n\n");
433 #endif /* DEBUG_READ */
434 el->el_state.argument = 1;
435 el->el_state.doingarg = 0;
442 term__flush(); /* flush any buffered output */
443 (void) tty_cookedmode(el); /* make sure the tty is set up correctly */
444 if (el->el_flags & HANDLE_SIGNALS)
448 return num ? el->el_line.buffer : NULL;