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
36 * @(#)read.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libedit/read.c,v 1.4.6.3 2000/10/31 23:51:10 brian Exp $
38 * $DragonFly: src/lib/libedit/read.c,v 1.3 2003/11/12 20:21:29 eirikn Exp $
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_getcmd (EditLine *, el_action_t *, char *);
64 if (el->el_line.cursor > el->el_line.lastchar)
65 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
66 if (el->el_line.cursor < el->el_line.buffer)
67 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
68 if (el->el_line.cursor > el->el_line.limit)
69 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
70 if (el->el_line.lastchar > el->el_line.limit)
71 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
72 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
73 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
75 #endif /* DEBUG_EDIT */
78 * Try to recover from a read error
85 case -1: /* Make sure that the code is reachable */
92 #endif /* EWOULDBLOCK */
94 #if defined(POSIX) && defined(EAGAIN)
95 # if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
100 # endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
101 #endif /* POSIX && EAGAIN */
105 # if defined(F_SETFL) && defined(O_NDELAY)
106 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
109 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
113 # endif /* F_SETFL && O_NDELAY */
116 if (ioctl(fd, FIONBIO, (ioctl_t) &e) == -1)
120 # endif /* FIONBIO */
122 #endif /* TRY_AGAIN */
135 * Try to read the stuff in the input queue;
143 if (el->el_chared.c_macro.nline) {
144 el_free((ptr_t) el->el_chared.c_macro.nline);
145 el->el_chared.c_macro.nline = NULL;
148 if (el->el_tty.t_mode == ED_IO)
152 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
156 chrs = read(el->el_infd, buf, (size_t) MIN(chrs, EL_BUFSIZ - 1));
159 el->el_chared.c_macro.nline = strdup(buf);
160 el_push(el->el_chared.c_macro.nline);
163 #endif /* FIONREAD */
177 c_macro_t *ma = &el->el_chared.c_macro;
179 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
181 ma->macro[ma->level] = (char *) str;
191 * Return next command from the input stream.
194 read_getcmd(el, cmdnum, ch)
199 el_action_t cmd = ED_UNASSIGNED;
202 while (cmd == ED_UNASSIGNED || cmd == ED_SEQUENCE_LEAD_IN) {
203 if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
208 el->el_state.metanext = 0;
215 if (el->el_state.metanext) {
216 el->el_state.metanext = 0;
219 cmd = el->el_map.current[(unsigned char) *ch];
220 if (cmd == ED_SEQUENCE_LEAD_IN) {
222 switch (key_get(el, ch, &val)) {
227 el_push(el, val.str);
231 /* XXX: In the future to run a user function */
240 if (el->el_map.alt == NULL)
241 el->el_map.current = el->el_map.key;
260 c_macro_t *ma = &el->el_chared.c_macro;
265 if (!read_preread(el))
271 if (*ma->macro[ma->level] == 0) {
275 *cp = *ma->macro[ma->level]++ & 0377;
276 if (*ma->macro[ma->level] == 0) { /* Needed for QuoteMode On */
283 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
284 #endif /* DEBUG_READ */
285 if (tty_rawmode(el) < 0) /* make sure the tty is set up correctly */
289 (void) fprintf(el->el_errfile, "Reading a character\n");
290 #endif /* DEBUG_READ */
291 while ((num_read = read(el->el_infd, (char *) &tcp, 1)) == -1)
292 if (!tried && read__fixio(el->el_infd, errno) == 0)
299 (void) fprintf(el->el_errfile, "Got it %c\n", tcp);
300 #endif /* DEBUG_READ */
313 el_action_t cmdnum = 0;
314 int num; /* how many chars we have read at NL */
317 if (el->el_flags & HANDLE_SIGNALS)
320 re_clear_display(el); /* reset the display stuff */
324 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
327 (void) ioctl(el->el_infd, FIONREAD, (ioctl_t) &chrs);
329 if (tty_rawmode(el) < 0) {
336 #endif /* FIONREAD */
338 re_refresh(el); /* print the prompt */
340 for (num = OKCMD; num == OKCMD;) { /* while still editing this line */
343 #endif /* DEBUG_EDIT */
344 /* if EOF or error */
345 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
347 (void) fprintf(el->el_errfile, "Returning from el_gets %d\n", num);
348 #endif /* DEBUG_READ */
352 if (cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
354 (void) fprintf(el->el_errfile,
355 "ERROR: illegal command from key 0%o\r\n", ch);
356 #endif /* DEBUG_EDIT */
357 continue; /* try again */
360 /* now do the real command */
364 for (b = el->el_map.help; b->name; b++)
365 if (b->func == cmdnum)
368 (void) fprintf(el->el_errfile, "Executing %s\n", b->name);
370 (void) fprintf(el->el_errfile, "Error command = %d\n", cmdnum);
372 #endif /* DEBUG_READ */
373 retval = (*el->el_map.func[cmdnum])(el, ch);
375 /* save the last command here */
376 el->el_state.lastcmd = cmdnum;
378 /* use any return value */
381 el->el_state.argument = 1;
382 el->el_state.doingarg = 0;
383 re_refresh_cursor(el);
388 re_clear_display(el);
392 el->el_state.argument = 1;
393 el->el_state.doingarg = 0;
397 case CC_NORM: /* normal char */
398 el->el_state.argument = 1;
399 el->el_state.doingarg = 0;
402 case CC_ARGHACK: /* Suggested by Rich Salz */
403 /* <rsalz@pineapple.bbn.com> */
404 break; /* keep going... */
406 case CC_EOF: /* end of file typed */
410 case CC_NEWLINE: /* normal end of line */
411 num = el->el_line.lastchar - el->el_line.buffer;
414 case CC_FATAL: /* fatal error, reset to known state */
416 (void) fprintf(el->el_errfile, "*** editor fatal ERROR ***\r\n\n");
417 #endif /* DEBUG_READ */
418 /* put (real) cursor in a known place */
419 re_clear_display(el); /* reset the display stuff */
420 ch_reset(el); /* reset the input pointers */
421 re_refresh(el); /* print the prompt again */
422 el->el_state.argument = 1;
423 el->el_state.doingarg = 0;
427 default: /* functions we don't know about */
429 (void) fprintf(el->el_errfile, "*** editor ERROR ***\r\n\n");
430 #endif /* DEBUG_READ */
431 el->el_state.argument = 1;
432 el->el_state.doingarg = 0;
439 term__flush(); /* flush any buffered output */
440 (void) tty_cookedmode(el); /* make sure the tty is set up correctly */
441 if (el->el_flags & HANDLE_SIGNALS)
445 return num ? el->el_line.buffer : NULL;