1 /* $NetBSD: read.c,v 1.21 2002/03/18 16:00:57 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if !defined(lint) && !defined(SCCSID)
42 static char sccsid
[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
44 __RCSID("$NetBSD: read.c,v 1.21 2002/03/18 16:00:57 christos Exp $");
46 #endif /* not lint && not SCCSID */
49 * read.c: Clean this junk up! This is horrible code.
50 * Terminal read functions
59 private int read__fixio(int, int);
60 private int read_preread(EditLine
*);
61 private int read_char(EditLine
*, char *);
62 private int read_getcmd(EditLine
*, el_action_t
*, char *);
65 * Initialize the read stuff
68 read_init(EditLine
*el
)
70 /* builtin read_char */
71 el
->el_read
.read_char
= read_char
;
77 * Set the read char function to the one provided.
78 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
81 el_read_setfn(EditLine
*el
, el_rfunc_t rc
)
83 el
->el_read
.read_char
= (rc
== EL_BUILTIN_GETCFN
) ? read_char
: rc
;
89 * return the current read char function, or EL_BUILTIN_GETCFN
90 * if it is the default one
93 el_read_getfn(EditLine
*el
)
95 return (el
->el_read
.read_char
== read_char
) ?
96 EL_BUILTIN_GETCFN
: el
->el_read
.read_char
;
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
, 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_chared
.c_macro
.nline
) {
190 el_free((ptr_t
) el
->el_chared
.c_macro
.nline
);
191 el
->el_chared
.c_macro
.nline
= NULL
;
193 if (el
->el_tty
.t_mode
== ED_IO
)
197 (void) ioctl(el
->el_infd
, FIONREAD
, (ioctl_t
) & chrs
);
201 chrs
= read(el
->el_infd
, buf
,
202 (size_t) MIN(chrs
, EL_BUFSIZ
- 1));
205 el
->el_chared
.c_macro
.nline
= strdup(buf
);
206 el_push(el
, el
->el_chared
.c_macro
.nline
);
209 #endif /* FIONREAD */
219 el_push(EditLine
*el
, char *str
)
221 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
223 if (str
!= NULL
&& ma
->level
+ 1 < EL_MAXMACRO
) {
225 ma
->macro
[ma
->level
] = str
;
234 * Return next command from the input stream.
237 read_getcmd(EditLine
*el
, el_action_t
*cmdnum
, char *ch
)
239 el_action_t cmd
= ED_UNASSIGNED
;
242 while (cmd
== ED_UNASSIGNED
|| cmd
== ED_SEQUENCE_LEAD_IN
) {
243 if ((num
= el_getc(el
, ch
)) != 1) /* if EOF or error */
248 el
->el_state
.metanext
= 0;
254 if (el
->el_state
.metanext
) {
255 el
->el_state
.metanext
= 0;
258 cmd
= el
->el_map
.current
[(unsigned char) *ch
];
259 if (cmd
== ED_SEQUENCE_LEAD_IN
) {
261 switch (key_get(el
, ch
, &val
)) {
266 el_push(el
, val
.str
);
270 /* XXX: In the future to run a user function */
275 EL_ABORT((el
->el_errfile
, "Bad XK_ type \n"));
279 if (el
->el_map
.alt
== NULL
)
280 el
->el_map
.current
= el
->el_map
.key
;
288 * Read a character from the tty.
291 read_char(EditLine
*el
, char *cp
)
296 while ((num_read
= read(el
->el_infd
, cp
, 1)) == -1)
297 if (!tried
&& read__fixio(el
->el_infd
, errno
) == 0)
312 el_getc(EditLine
*el
, char *cp
)
315 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
320 if (!read_preread(el
))
326 if (*ma
->macro
[ma
->level
] == 0) {
330 *cp
= *ma
->macro
[ma
->level
]++ & 0377;
331 if (*ma
->macro
[ma
->level
] == 0) { /* Needed for QuoteMode
339 (void) fprintf(el
->el_errfile
, "Turning raw mode on\n");
340 #endif /* DEBUG_READ */
341 if (tty_rawmode(el
) < 0)/* make sure the tty is set up correctly */
345 (void) fprintf(el
->el_errfile
, "Reading a character\n");
346 #endif /* DEBUG_READ */
347 num_read
= (*el
->el_read
.read_char
)(el
, cp
);
349 (void) fprintf(el
->el_errfile
, "Got it %c\n", *cp
);
350 #endif /* DEBUG_READ */
356 el_gets(EditLine
*el
, int *nread
)
359 el_action_t cmdnum
= 0;
360 int num
; /* how many chars we have read at NL */
363 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
364 #endif /* FIONREAD */
366 if (el
->el_flags
& HANDLE_SIGNALS
)
369 if (el
->el_flags
& NO_TTY
) {
370 char *cp
= el
->el_line
.buffer
;
373 while ((*el
->el_read
.read_char
)(el
, cp
) == 1) {
374 /* make sure there is space for next character */
375 if (cp
+ 1 >= el
->el_line
.limit
) {
376 idx
= (cp
- el
->el_line
.buffer
);
377 if (!ch_enlargebufs(el
, 2))
379 cp
= &el
->el_line
.buffer
[idx
];
382 if (cp
[-1] == '\r' || cp
[-1] == '\n')
386 el
->el_line
.cursor
= el
->el_line
.lastchar
= cp
;
389 *nread
= el
->el_line
.cursor
- el
->el_line
.buffer
;
390 return (el
->el_line
.buffer
);
392 re_clear_display(el
); /* reset the display stuff */
396 if (el
->el_tty
.t_mode
== EX_IO
&& ma
->level
< 0) {
399 (void) ioctl(el
->el_infd
, FIONREAD
, (ioctl_t
) & chrs
);
401 if (tty_rawmode(el
) < 0) {
408 #endif /* FIONREAD */
410 re_refresh(el
); /* print the prompt */
412 if (el
->el_flags
& EDIT_DISABLED
) {
413 char *cp
= el
->el_line
.buffer
;
418 while ((*el
->el_read
.read_char
)(el
, cp
) == 1) {
419 /* make sure there is space next character */
420 if (cp
+ 1 >= el
->el_line
.limit
) {
421 idx
= (cp
- el
->el_line
.buffer
);
422 if (!ch_enlargebufs(el
, 2))
424 cp
= &el
->el_line
.buffer
[idx
];
427 if (cp
[-1] == '\r' || cp
[-1] == '\n')
431 el
->el_line
.cursor
= el
->el_line
.lastchar
= cp
;
434 *nread
= el
->el_line
.cursor
- el
->el_line
.buffer
;
435 return (el
->el_line
.buffer
);
437 for (num
= OKCMD
; num
== OKCMD
;) { /* while still editing this
441 #endif /* DEBUG_EDIT */
442 /* if EOF or error */
443 if ((num
= read_getcmd(el
, &cmdnum
, &ch
)) != OKCMD
) {
445 (void) fprintf(el
->el_errfile
,
446 "Returning from el_gets %d\n", num
);
447 #endif /* DEBUG_READ */
450 if ((int) cmdnum
>= el
->el_map
.nfunc
) { /* BUG CHECK command */
452 (void) fprintf(el
->el_errfile
,
453 "ERROR: illegal command from key 0%o\r\n", ch
);
454 #endif /* DEBUG_EDIT */
455 continue; /* try again */
457 /* now do the real command */
461 for (b
= el
->el_map
.help
; b
->name
; b
++)
462 if (b
->func
== cmdnum
)
465 (void) fprintf(el
->el_errfile
,
466 "Executing %s\n", b
->name
);
468 (void) fprintf(el
->el_errfile
,
469 "Error command = %d\n", cmdnum
);
471 #endif /* DEBUG_READ */
472 retval
= (*el
->el_map
.func
[cmdnum
]) (el
, ch
);
474 /* save the last command here */
475 el
->el_state
.lastcmd
= cmdnum
;
477 /* use any return value */
480 el
->el_state
.argument
= 1;
481 el
->el_state
.doingarg
= 0;
482 re_refresh_cursor(el
);
487 re_clear_display(el
);
491 el
->el_state
.argument
= 1;
492 el
->el_state
.doingarg
= 0;
496 case CC_REFRESH_BEEP
:
497 el
->el_state
.argument
= 1;
498 el
->el_state
.doingarg
= 0;
503 case CC_NORM
: /* normal char */
504 el
->el_state
.argument
= 1;
505 el
->el_state
.doingarg
= 0;
508 case CC_ARGHACK
: /* Suggested by Rich Salz */
509 /* <rsalz@pineapple.bbn.com> */
510 break; /* keep going... */
512 case CC_EOF
: /* end of file typed */
516 case CC_NEWLINE
: /* normal end of line */
517 num
= el
->el_line
.lastchar
- el
->el_line
.buffer
;
520 case CC_FATAL
: /* fatal error, reset to known state */
522 (void) fprintf(el
->el_errfile
,
523 "*** editor fatal ERROR ***\r\n\n");
524 #endif /* DEBUG_READ */
525 /* put (real) cursor in a known place */
526 re_clear_display(el
); /* reset the display stuff */
527 ch_reset(el
); /* reset the input pointers */
528 re_refresh(el
); /* print the prompt again */
529 el
->el_state
.argument
= 1;
530 el
->el_state
.doingarg
= 0;
534 default: /* functions we don't know about */
536 (void) fprintf(el
->el_errfile
,
537 "*** editor ERROR ***\r\n\n");
538 #endif /* DEBUG_READ */
539 el
->el_state
.argument
= 1;
540 el
->el_state
.doingarg
= 0;
547 /* make sure the tty is set up correctly */
548 (void) tty_cookedmode(el
);
549 term__flush(); /* flush any buffered output */
550 if (el
->el_flags
& HANDLE_SIGNALS
)
554 return (num
? el
->el_line
.buffer
: NULL
);