1
/* $NetBSD: read.c,v 1.35 2005/03/09 23:55:02 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
38
* read.c: Clean this junk up! This is horrible code.
39
* Terminal read functions
49
private int read__fixio(int, int);
50
private int read_preread(EditLine *);
51
private int read_char(EditLine *, char *);
52
private int read_getcmd(EditLine *, el_action_t *, char *);
55
* Initialize the read stuff
58
read_init(EditLine *el)
60
/* builtin read_char */
61
el->el_read.read_char = read_char;
67
* Set the read char function to the one provided.
68
* If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
71
el_read_setfn(EditLine *el, el_rfunc_t rc)
73
el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
79
* return the current read char function, or EL_BUILTIN_GETCFN
80
* if it is the default one
83
el_read_getfn(EditLine *el)
85
return (el->el_read.read_char == read_char) ?
86
EL_BUILTIN_GETCFN : el->el_read.read_char;
91
#define MIN(A,B) ((A) < (B) ? (A) : (B))
96
read_debug(EditLine *el)
99
if (el->el_line.cursor > el->el_line.lastchar)
100
(void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
101
if (el->el_line.cursor < el->el_line.buffer)
102
(void) fprintf(el->el_errfile, "cursor < buffer\r\n");
103
if (el->el_line.cursor > el->el_line.limit)
104
(void) fprintf(el->el_errfile, "cursor > limit\r\n");
105
if (el->el_line.lastchar > el->el_line.limit)
106
(void) fprintf(el->el_errfile, "lastchar > limit\r\n");
107
if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
108
(void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
110
#endif /* DEBUG_EDIT */
114
* Try to recover from a read error
118
read__fixio(int fd __attribute__((__unused__)), int e)
122
case -1: /* Make sure that the code is reachable */
129
#endif /* EWOULDBLOCK */
131
#if defined(POSIX) && defined(EAGAIN)
132
#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
137
#endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
138
#endif /* POSIX && EAGAIN */
142
#if defined(F_SETFL) && defined(O_NDELAY)
143
if ((e = fcntl(fd, F_GETFL, 0)) == -1)
146
if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
150
#endif /* F_SETFL && O_NDELAY */
156
if (ioctl(fd, FIONBIO, (ioctl_t) & zero) == -1)
163
#endif /* TRY_AGAIN */
176
* Try to read the stuff in the input queue;
179
read_preread(EditLine *el)
183
if (el->el_tty.t_mode == ED_IO)
187
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
191
chrs = read(el->el_infd, buf,
192
(size_t) MIN(chrs, EL_BUFSIZ - 1));
198
#endif /* FIONREAD */
208
el_push(EditLine *el, char *str)
210
c_macro_t *ma = &el->el_chared.c_macro;
212
if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
214
if ((ma->macro[ma->level] = el_strdup(str)) != NULL)
224
* Return next command from the input stream.
227
read_getcmd(EditLine *el, el_action_t *cmdnum, char *ch)
233
if ((num = el_getc(el, ch)) != 1) /* if EOF or error */
238
el->el_state.metanext = 0;
244
if (el->el_state.metanext) {
245
el->el_state.metanext = 0;
248
cmd = el->el_map.current[(unsigned char) *ch];
249
if (cmd == ED_SEQUENCE_LEAD_IN) {
251
switch (key_get(el, ch, &val)) {
256
el_push(el, val.str);
260
/* XXX: In the future to run a user function */
265
EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
269
if (el->el_map.alt == NULL)
270
el->el_map.current = el->el_map.key;
271
} while (cmd == ED_SEQUENCE_LEAD_IN);
278
* Read a character from the tty.
281
read_char(EditLine *el, char *cp)
286
while ((num_read = read(el->el_infd, cp, 1)) == -1)
287
if (!tried && read__fixio(el->el_infd, errno) == 0)
302
el_getc(EditLine *el, char *cp)
305
c_macro_t *ma = &el->el_chared.c_macro;
310
if (!read_preread(el))
316
if (ma->macro[ma->level][ma->offset] == '\0') {
317
el_free(ma->macro[ma->level--]);
321
*cp = ma->macro[ma->level][ma->offset++] & 0377;
322
if (ma->macro[ma->level][ma->offset] == '\0') {
323
/* Needed for QuoteMode On */
324
el_free(ma->macro[ma->level--]);
331
(void) fprintf(el->el_errfile, "Turning raw mode on\n");
332
#endif /* DEBUG_READ */
333
if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
337
(void) fprintf(el->el_errfile, "Reading a character\n");
338
#endif /* DEBUG_READ */
339
num_read = (*el->el_read.read_char)(el, cp);
341
(void) fprintf(el->el_errfile, "Got it %c\n", *cp);
342
#endif /* DEBUG_READ */
347
read_prepare(EditLine *el)
349
if (el->el_flags & HANDLE_SIGNALS)
351
if (el->el_flags & NO_TTY)
353
if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
356
/* This is relatively cheap, and things go terribly wrong if
357
we have the wrong size. */
359
re_clear_display(el); /* reset the display stuff */
361
re_refresh(el); /* print the prompt */
363
if (el->el_flags & UNBUFFERED)
368
read_finish(EditLine *el)
370
if ((el->el_flags & UNBUFFERED) == 0)
371
(void) tty_cookedmode(el);
372
if (el->el_flags & HANDLE_SIGNALS)
377
el_gets(EditLine *el, int *nread)
380
el_action_t cmdnum = 0;
381
int num; /* how many chars we have read at NL */
385
c_macro_t *ma = &el->el_chared.c_macro;
386
#endif /* FIONREAD */
388
if (el->el_flags & NO_TTY) {
389
char *cp = el->el_line.buffer;
392
while ((*el->el_read.read_char)(el, cp) == 1) {
393
/* make sure there is space for next character */
394
if (cp + 1 >= el->el_line.limit) {
395
idx = (cp - el->el_line.buffer);
396
if (!ch_enlargebufs(el, 2))
398
cp = &el->el_line.buffer[idx];
401
if (el->el_flags & UNBUFFERED)
403
if (cp[-1] == '\r' || cp[-1] == '\n')
407
el->el_line.cursor = el->el_line.lastchar = cp;
410
*nread = el->el_line.cursor - el->el_line.buffer;
411
return (el->el_line.buffer);
416
if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
419
(void) ioctl(el->el_infd, FIONREAD, (ioctl_t) & chrs);
421
if (tty_rawmode(el) < 0) {
428
#endif /* FIONREAD */
430
if ((el->el_flags & UNBUFFERED) == 0)
433
if (el->el_flags & EDIT_DISABLED) {
436
if ((el->el_flags & UNBUFFERED) == 0)
437
cp = el->el_line.buffer;
439
cp = el->el_line.lastchar;
443
while ((*el->el_read.read_char)(el, cp) == 1) {
444
/* make sure there is space next character */
445
if (cp + 1 >= el->el_line.limit) {
446
idx = (cp - el->el_line.buffer);
447
if (!ch_enlargebufs(el, 2))
449
cp = &el->el_line.buffer[idx];
451
if (*cp == 4) /* ought to be stty eof */
454
crlf = cp[-1] == '\r' || cp[-1] == '\n';
455
if (el->el_flags & UNBUFFERED)
461
el->el_line.cursor = el->el_line.lastchar = cp;
464
*nread = el->el_line.cursor - el->el_line.buffer;
465
return (el->el_line.buffer);
468
for (num = OKCMD; num == OKCMD;) { /* while still editing this
472
#endif /* DEBUG_EDIT */
473
/* if EOF or error */
474
if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
476
(void) fprintf(el->el_errfile,
477
"Returning from el_gets %d\n", num);
478
#endif /* DEBUG_READ */
481
if ((unsigned int)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
483
(void) fprintf(el->el_errfile,
484
"ERROR: illegal command from key 0%o\r\n", ch);
485
#endif /* DEBUG_EDIT */
486
continue; /* try again */
488
/* now do the real command */
492
for (b = el->el_map.help; b->name; b++)
493
if (b->func == cmdnum)
496
(void) fprintf(el->el_errfile,
497
"Executing %s\n", b->name);
499
(void) fprintf(el->el_errfile,
500
"Error command = %d\n", cmdnum);
502
#endif /* DEBUG_READ */
503
/* vi redo needs these way down the levels... */
504
el->el_state.thiscmd = cmdnum;
505
el->el_state.thisch = ch;
506
if (el->el_map.type == MAP_VI &&
507
el->el_map.current == el->el_map.key &&
508
el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
509
if (cmdnum == VI_DELETE_PREV_CHAR &&
510
el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
511
&& isprint((unsigned char)el->el_chared.c_redo.pos[-1]))
512
el->el_chared.c_redo.pos--;
514
*el->el_chared.c_redo.pos++ = ch;
516
retval = (*el->el_map.func[cmdnum]) (el, ch);
518
(void) fprintf(el->el_errfile,
519
"Returned state %d\n", retval );
520
#endif /* DEBUG_READ */
522
/* save the last command here */
523
el->el_state.lastcmd = cmdnum;
525
/* use any return value */
528
re_refresh_cursor(el);
533
re_clear_display(el);
540
case CC_REFRESH_BEEP:
545
case CC_NORM: /* normal char */
548
case CC_ARGHACK: /* Suggested by Rich Salz */
549
/* <rsalz@pineapple.bbn.com> */
550
continue; /* keep going... */
552
case CC_EOF: /* end of file typed */
553
if ((el->el_flags & UNBUFFERED) == 0)
555
else if (num == -1) {
556
*el->el_line.lastchar++ = CONTROL('d');
557
el->el_line.cursor = el->el_line.lastchar;
562
case CC_NEWLINE: /* normal end of line */
563
num = el->el_line.lastchar - el->el_line.buffer;
566
case CC_FATAL: /* fatal error, reset to known state */
568
(void) fprintf(el->el_errfile,
569
"*** editor fatal ERROR ***\r\n\n");
570
#endif /* DEBUG_READ */
571
/* put (real) cursor in a known place */
572
re_clear_display(el); /* reset the display stuff */
573
ch_reset(el); /* reset the input pointers */
574
re_refresh(el); /* print the prompt again */
578
default: /* functions we don't know about */
580
(void) fprintf(el->el_errfile,
581
"*** editor ERROR ***\r\n\n");
582
#endif /* DEBUG_READ */
587
el->el_state.argument = 1;
588
el->el_state.doingarg = 0;
589
el->el_chared.c_vcmd.action = NOP;
590
if (el->el_flags & UNBUFFERED)
594
term__flush(); /* flush any buffered output */
595
/* make sure the tty is set up correctly */
596
if ((el->el_flags & UNBUFFERED) == 0) {
602
*nread = el->el_line.lastchar - el->el_line.buffer;
604
return (num ? el->el_line.buffer : NULL);