1
/* $NetBSD: read.c,v 1.39 2005/08/02 12:11:14 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. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#if !defined(lint) && !defined(SCCSID)
38
static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
40
__RCSID("$NetBSD: read.c,v 1.39 2005/08/02 12:11:14 christos Exp $");
42
#endif /* not lint && not SCCSID */
45
* read.c: Clean this junk up! This is horrible code.
46
* Terminal read functions
56
private int read__fixio(int, int);
57
private int read_preread(EditLine *);
58
private int read_char(EditLine *, char *);
59
private int read_getcmd(EditLine *, el_action_t *, char *);
62
* Initialize the read stuff
65
read_init(EditLine *el)
67
/* builtin read_char */
68
el->el_read.read_char = read_char;
74
* Set the read char function to the one provided.
75
* If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
78
el_read_setfn(EditLine *el, el_rfunc_t rc)
80
el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
86
* return the current read char function, or EL_BUILTIN_GETCFN
87
* if it is the default one
90
el_read_getfn(EditLine *el)
92
return (el->el_read.read_char == read_char) ?
93
EL_BUILTIN_GETCFN : el->el_read.read_char;
98
#define MIN(A,B) ((A) < (B) ? (A) : (B))
103
read_debug(EditLine *el)
106
if (el->el_line.cursor > el->el_line.lastchar)
107
(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
108
if (el->el_line.cursor < el->el_line.buffer)
109
(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
110
if (el->el_line.cursor > el->el_line.limit)
111
(void) fprintf(el->el_errfile, "cursor > limit\r\n");
112
if (el->el_line.lastchar > el->el_line.limit)
113
(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
114
if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
115
(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
117
#endif /* DEBUG_EDIT */
121
* Try to recover from a read error
125
read__fixio(int fd __attribute__((__unused__)), int e)
129
case -1: /* Make sure that the code is reachable */
136
#endif /* EWOULDBLOCK */
138
#if defined(POSIX) && defined(EAGAIN)
139
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
144
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
145
#endif /* POSIX && EAGAIN */
149
#if defined(F_SETFL) && defined(O_NDELAY)
150
if ((e = fcntl(fd, F_GETFL, 0)) == -1)
153
if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
157
#endif /* F_SETFL && O_NDELAY */
163
if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
170
#endif /* TRY_AGAIN */
183
* Try to read the stuff in the input queue;
186
read_preread(EditLine *el)
190
if (el->el_tty.t_mode == ED_IO)
194
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
198
chrs = read(el->el_infd, buf,
199
(size_t) MIN(chrs, EL_BUFSIZ - 1));
205
#endif /* FIONREAD */
215
el_push(EditLine *el, char *str)
217
c_macro_t *ma = &el->el_chared.c_macro;
219
if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
221
if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
225
#if 0 /* TCM - why on earth would you beep here? */
233
* Return next command from the input stream.
236
read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
242
if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
247
el->el_state.metanext = 0;
253
if (el->el_state.metanext) {
254
el->el_state.metanext = 0;
257
cmd = el->el_map.current[(unsigned char) *ch];
258
if (cmd == ED_SEQUENCE_LEAD_IN) {
260
switch (key_get(el, ch, &val)) {
265
el_push(el, val.str);
269
/* XXX: In the future to run a user function */
274
EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
278
if (el->el_map.alt == NULL)
279
el->el_map.current = el->el_map.key;
280
} while (cmd == ED_SEQUENCE_LEAD_IN);
287
* Read a character from the tty.
290
read_char(EditLine *el, char *cp)
295
while ((num_read = read(el->el_infd, cp, 1)) == -1)
296
if (!tried && read__fixio(el->el_infd, errno) == 0)
311
el_getc(EditLine *el, char *cp)
314
c_macro_t *ma = &el->el_chared.c_macro;
319
if (!read_preread(el))
325
if (ma->macro[ma->level][ma->offset] == '\0') {
326
el_free(ma->macro[ma->level--]);
330
*cp = ma->macro[ma->level][ma->offset++] & 0377;
331
if (ma->macro[ma->level][ma->offset] == '\0') {
332
/* Needed for QuoteMode On */
333
el_free(ma->macro[ma->level--]);
340
(void) fprintf(el->el_errfile, "Turning raw mode on\n");
341
#endif /* DEBUG_READ */
342
if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
346
(void) fprintf(el->el_errfile, "Reading a character\n");
347
#endif /* DEBUG_READ */
348
num_read = (*el->el_read.read_char)(el, cp);
350
(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
351
#endif /* DEBUG_READ */
356
read_prepare(EditLine *el)
358
if (el->el_flags & HANDLE_SIGNALS)
360
if (el->el_flags & NO_TTY)
362
if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
365
/* This is relatively cheap, and things go terribly wrong if
366
we have the wrong size. */
368
re_clear_display(el); /* reset the display stuff */
370
re_refresh(el); /* print the prompt */
372
if (el->el_flags & UNBUFFERED)
377
read_finish(EditLine *el)
379
if ((el->el_flags & UNBUFFERED) == 0)
380
(void) tty_cookedmode(el);
381
if (el->el_flags & HANDLE_SIGNALS)
386
el_gets(EditLine *el, int *nread)
389
el_action_t cmdnum = 0;
390
int num; /* how many chars we have read at NL */
394
c_macro_t *ma = &el->el_chared.c_macro;
395
#endif /* FIONREAD */
397
if (el->el_flags & NO_TTY) {
398
char *cp = el->el_line.buffer;
401
while ((*el->el_read.read_char)(el, cp) == 1) {
402
/* make sure there is space for next character */
403
if (cp + 1 >= el->el_line.limit) {
404
idx = (cp - el->el_line.buffer);
405
if (!ch_enlargebufs(el, 2))
407
cp = &el->el_line.buffer[idx];
410
if (el->el_flags & UNBUFFERED)
412
if (cp[-1] == '\r' || cp[-1] == '\n')
416
el->el_line.cursor = el->el_line.lastchar = cp;
419
*nread = el->el_line.cursor - el->el_line.buffer;
420
return (el->el_line.buffer);
425
if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
428
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
430
if (tty_rawmode(el) < 0) {
437
#endif /* FIONREAD */
439
if ((el->el_flags & UNBUFFERED) == 0)
442
if (el->el_flags & EDIT_DISABLED) {
445
if ((el->el_flags & UNBUFFERED) == 0)
446
cp = el->el_line.buffer;
448
cp = el->el_line.lastchar;
452
while ((*el->el_read.read_char)(el, cp) == 1) {
453
/* make sure there is space next character */
454
if (cp + 1 >= el->el_line.limit) {
455
idx = (cp - el->el_line.buffer);
456
if (!ch_enlargebufs(el, 2))
458
cp = &el->el_line.buffer[idx];
460
if (*cp == 4) /* ought to be stty eof */
463
crlf = cp[-1] == '\r' || cp[-1] == '\n';
464
if (el->el_flags & UNBUFFERED)
470
el->el_line.cursor = el->el_line.lastchar = cp;
473
*nread = el->el_line.cursor - el->el_line.buffer;
474
return (el->el_line.buffer);
477
for (num = OKCMD; num == OKCMD;) { /* while still editing this
481
#endif /* DEBUG_EDIT */
482
/* if EOF or error */
483
if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
485
(void) fprintf(el->el_errfile,
486
"Returning from el_gets %d\n", num);
487
#endif /* DEBUG_READ */
490
if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
492
(void) fprintf(el->el_errfile,
493
"ERROR: illegal command from key 0%o\r\n", ch);
494
#endif /* DEBUG_EDIT */
495
continue; /* try again */
497
/* now do the real command */
501
for (b = el->el_map.help; b->name; b++)
502
if (b->func == cmdnum)
505
(void) fprintf(el->el_errfile,
506
"Executing %s\n", b->name);
508
(void) fprintf(el->el_errfile,
509
"Error command = %d\n", cmdnum);
511
#endif /* DEBUG_READ */
512
/* vi redo needs these way down the levels... */
513
el->el_state.thiscmd = cmdnum;
514
el->el_state.thisch = ch;
515
if (el->el_map.type == MAP_VI &&
516
el->el_map.current == el->el_map.key &&
517
el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
518
if (cmdnum == VI_DELETE_PREV_CHAR &&
519
el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
520
&& isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
521
el->el_chared.c_redo.pos--;
523
*el->el_chared.c_redo.pos++ = ch;
525
retval = (*el->el_map.func[cmdnum]) (el, ch);
527
(void) fprintf(el->el_errfile,
528
"Returned state %d\n", retval );
529
#endif /* DEBUG_READ */
531
/* save the last command here */
532
el->el_state.lastcmd = cmdnum;
534
/* use any return value */
537
re_refresh_cursor(el);
542
re_clear_display(el);
549
case CC_REFRESH_BEEP:
554
case CC_NORM: /* normal char */
557
case CC_ARGHACK: /* Suggested by Rich Salz */
558
/* <rsalz@pineapple.bbn.com> */
559
continue; /* keep going... */
561
case CC_EOF: /* end of file typed */
562
if ((el->el_flags & UNBUFFERED) == 0)
564
else if (num == -1) {
565
*el->el_line.lastchar++ = CONTROL('d');
566
el->el_line.cursor = el->el_line.lastchar;
571
case CC_NEWLINE: /* normal end of line */
572
num = el->el_line.lastchar - el->el_line.buffer;
575
case CC_FATAL: /* fatal error, reset to known state */
577
(void) fprintf(el->el_errfile,
578
"*** editor fatal ERROR ***\r\n\n");
579
#endif /* DEBUG_READ */
580
/* put (real) cursor in a known place */
581
re_clear_display(el); /* reset the display stuff */
582
ch_reset(el, 1); /* reset the input pointers */
583
re_refresh(el); /* print the prompt again */
587
default: /* functions we don't know about */
589
(void) fprintf(el->el_errfile,
590
"*** editor ERROR ***\r\n\n");
591
#endif /* DEBUG_READ */
596
el->el_state.argument = 1;
597
el->el_state.doingarg = 0;
598
el->el_chared.c_vcmd.action = NOP;
599
if (el->el_flags & UNBUFFERED)
603
term__flush(); /* flush any buffered output */
604
/* make sure the tty is set up correctly */
605
if ((el->el_flags & UNBUFFERED) == 0) {
611
*nread = el->el_line.lastchar - el->el_line.buffer;
613
return (num ? el->el_line.buffer : NULL);