1 /* $NetBSD: read.c,v 1.70 2013/05/27 23:55:55 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.70 2013/05/27 23:55:55 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * read.c: Clean this junk up! This is horrible code.
46 * Terminal read functions
55 #define OKCMD -1 /* must be -1! */
57 private int read__fixio(int, int);
58 private int read_preread(EditLine
*);
59 private int read_char(EditLine
*, Char
*);
60 private int read_getcmd(EditLine
*, el_action_t
*, Char
*);
61 private void read_pop(c_macro_t
*);
64 * Initialize the read stuff
67 read_init(EditLine
*el
)
69 /* builtin read_char */
70 el
->el_read
.read_char
= read_char
;
76 * Set the read char function to the one provided.
77 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
80 el_read_setfn(EditLine
*el
, el_rfunc_t rc
)
82 el
->el_read
.read_char
= (rc
== EL_BUILTIN_GETCFN
) ? read_char
: rc
;
88 * return the current read char function, or EL_BUILTIN_GETCFN
89 * if it is the default one
92 el_read_getfn(EditLine
*el
)
94 return el
->el_read
.read_char
== read_char
?
95 EL_BUILTIN_GETCFN
: el
->el_read
.read_char
;
100 #define MIN(A,B) ((A) < (B) ? (A) : (B))
105 read_debug(EditLine
*el
)
108 if (el
->el_line
.cursor
> el
->el_line
.lastchar
)
109 (void) fprintf(el
->el_errfile
, "cursor > lastchar\r\n");
110 if (el
->el_line
.cursor
< el
->el_line
.buffer
)
111 (void) fprintf(el
->el_errfile
, "cursor < buffer\r\n");
112 if (el
->el_line
.cursor
> el
->el_line
.limit
)
113 (void) fprintf(el
->el_errfile
, "cursor > limit\r\n");
114 if (el
->el_line
.lastchar
> el
->el_line
.limit
)
115 (void) fprintf(el
->el_errfile
, "lastchar > limit\r\n");
116 if (el
->el_line
.limit
!= &el
->el_line
.buffer
[EL_BUFSIZ
- 2])
117 (void) fprintf(el
->el_errfile
, "limit != &buffer[EL_BUFSIZ-2]\r\n");
119 #endif /* DEBUG_EDIT */
123 * Try to recover from a read error
127 read__fixio(int fd
__attribute__((__unused__
)), int e
)
131 case -1: /* Make sure that the code is reachable */
138 #endif /* EWOULDBLOCK */
140 #if defined(POSIX) && defined(EAGAIN)
141 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
146 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
147 #endif /* POSIX && EAGAIN */
151 #if defined(F_SETFL) && defined(O_NDELAY)
152 if ((e
= fcntl(fd
, F_GETFL
, 0)) == -1)
155 if (fcntl(fd
, F_SETFL
, e
& ~O_NDELAY
) == -1)
159 #endif /* F_SETFL && O_NDELAY */
165 if (ioctl(fd
, FIONBIO
, &zero
) == -1)
172 #endif /* TRY_AGAIN */
185 * Try to read the stuff in the input queue;
188 read_preread(EditLine
*el
)
192 if (el
->el_tty
.t_mode
== ED_IO
)
196 /* FIONREAD attempts to buffer up multiple bytes, and to make that work
197 * properly with partial wide/UTF-8 characters would need some careful work. */
199 (void) ioctl(el
->el_infd
, FIONREAD
, &chrs
);
203 chrs
= read(el
->el_infd
, buf
,
204 (size_t) MIN(chrs
, EL_BUFSIZ
- 1));
210 #endif /* FIONREAD */
220 FUN(el
,push
)(EditLine
*el
, const Char
*str
)
222 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
224 if (str
!= NULL
&& ma
->level
+ 1 < EL_MAXMACRO
) {
226 if ((ma
->macro
[ma
->level
] = Strdup(str
)) != NULL
)
236 * Get next command from the input stream, return OKCMD on success.
237 * Character values > 255 are not looked up in the map, but inserted.
240 read_getcmd(EditLine
*el
, el_action_t
*cmdnum
, Char
*ch
)
247 if ((num
= FUN(el
,getc
)(el
, ch
)) != 1) {/* if EOF or error */
248 el
->el_errno
= num
== 0 ? 0 : errno
;
249 return 0; /* not OKCMD */
254 el
->el_state
.metanext
= 0;
260 if (el
->el_state
.metanext
) {
261 el
->el_state
.metanext
= 0;
269 cmd
= el
->el_map
.current
[(unsigned char) *ch
];
270 if (cmd
== ED_SEQUENCE_LEAD_IN
) {
271 keymacro_value_t val
;
272 switch (keymacro_get(el
, ch
, &val
)) {
277 FUN(el
,push
)(el
, val
.str
);
281 /* XXX: In the future to run a user function */
286 EL_ABORT((el
->el_errfile
, "Bad XK_ type \n"));
290 if (el
->el_map
.alt
== NULL
)
291 el
->el_map
.current
= el
->el_map
.key
;
292 } while (cmd
== ED_SEQUENCE_LEAD_IN
);
299 * Test whether a byte is a leading byte of a UTF-8 sequence.
304 return c
< 0x80 || /* single byte char */
305 (c
>= 0xc2 && c
<= 0xf4); /* start of multibyte sequence */
310 * Read a character from the tty.
313 read_char(EditLine
*el
, Char
*cp
)
317 char cbuf
[MB_LEN_MAX
];
322 el
->el_signal
->sig_no
= 0;
323 while ((num_read
= read(el
->el_infd
, cbuf
+ cbp
, (size_t)1)) == -1) {
325 switch (el
->el_signal
->sig_no
) {
327 FUN(el
,set
)(el
, EL_REFRESH
);
335 if (!tried
&& read__fixio(el
->el_infd
, e
) == 0)
352 if (el
->el_flags
& CHARSET_IS_UTF8
) {
353 if (!utf8_islead((unsigned char)cbuf
[0]))
354 goto again
; /* discard the byte we read and try again */
356 if ((bytes
= ct_mbtowc(cp
, cbuf
, cbp
)) == -1) {
358 if (cbp
>= MB_LEN_MAX
) { /* "shouldn't happen" */
365 } else if (isascii((unsigned char)cbuf
[0]) ||
366 /* we don't support other multibyte charsets */
368 /* Try non-ASCII characters in a 8-bit character set */
369 (bytes
= ct_mbtowc(cp
, cbuf
, cbp
)) != 1)
371 *cp
= (unsigned char)cbuf
[0];
373 if ((el
->el_flags
& IGNORE_EXTCHARS
) && bytes
> 1) {
374 cbp
= 0; /* skip this character */
378 return (int)num_read
;
382 * Pop a macro from the stack
385 read_pop(c_macro_t
*ma
)
389 el_free(ma
->macro
[0]);
390 for (i
= 0; i
< ma
->level
; i
++)
391 ma
->macro
[i
] = ma
->macro
[i
+ 1];
400 FUN(el
,getc
)(EditLine
*el
, Char
*cp
)
403 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
408 if (!read_preread(el
))
415 if (ma
->macro
[0][ma
->offset
] == '\0') {
420 *cp
= ma
->macro
[0][ma
->offset
++];
422 if (ma
->macro
[0][ma
->offset
] == '\0') {
423 /* Needed for QuoteMode On */
431 (void) fprintf(el
->el_errfile
, "Turning raw mode on\n");
432 #endif /* DEBUG_READ */
433 if (tty_rawmode(el
) < 0)/* make sure the tty is set up correctly */
437 (void) fprintf(el
->el_errfile
, "Reading a character\n");
438 #endif /* DEBUG_READ */
439 num_read
= (*el
->el_read
.read_char
)(el
, cp
);
441 el
->el_errno
= errno
;
443 if (el
->el_flags
& NARROW_READ
)
444 *cp
= *(char *)(void *)cp
;
447 (void) fprintf(el
->el_errfile
, "Got it %c\n", *cp
);
448 #endif /* DEBUG_READ */
453 read_prepare(EditLine
*el
)
455 if (el
->el_flags
& HANDLE_SIGNALS
)
457 if (el
->el_flags
& NO_TTY
)
459 if ((el
->el_flags
& (UNBUFFERED
|EDIT_DISABLED
)) == UNBUFFERED
)
462 /* This is relatively cheap, and things go terribly wrong if
463 we have the wrong size. */
465 re_clear_display(el
); /* reset the display stuff */
467 re_refresh(el
); /* print the prompt */
469 if (el
->el_flags
& UNBUFFERED
)
474 read_finish(EditLine
*el
)
476 if ((el
->el_flags
& UNBUFFERED
) == 0)
477 (void) tty_cookedmode(el
);
478 if (el
->el_flags
& HANDLE_SIGNALS
)
483 FUN(el
,gets
)(EditLine
*el
, int *nread
)
486 el_action_t cmdnum
= 0;
487 int num
; /* how many chars we have read at NL */
492 c_macro_t
*ma
= &el
->el_chared
.c_macro
;
493 #endif /* FIONREAD */
499 if (el
->el_flags
& NO_TTY
) {
502 cp
= el
->el_line
.buffer
;
503 while ((num
= (*el
->el_read
.read_char
)(el
, cp
)) == 1) {
504 /* make sure there is space for next character */
505 if (cp
+ 1 >= el
->el_line
.limit
) {
506 idx
= (size_t)(cp
- el
->el_line
.buffer
);
507 if (!ch_enlargebufs(el
, (size_t)2))
509 cp
= &el
->el_line
.buffer
[idx
];
512 if (el
->el_flags
& UNBUFFERED
)
514 if (cp
[-1] == '\r' || cp
[-1] == '\n')
519 cp
= el
->el_line
.buffer
;
520 el
->el_errno
= errno
;
528 if (el
->el_tty
.t_mode
== EX_IO
&& ma
->level
< 0) {
531 (void) ioctl(el
->el_infd
, FIONREAD
, &chrs
);
533 if (tty_rawmode(el
) < 0) {
540 #endif /* FIONREAD */
542 if ((el
->el_flags
& UNBUFFERED
) == 0)
545 if (el
->el_flags
& EDIT_DISABLED
) {
548 if ((el
->el_flags
& UNBUFFERED
) == 0)
549 cp
= el
->el_line
.buffer
;
551 cp
= el
->el_line
.lastchar
;
555 while ((num
= (*el
->el_read
.read_char
)(el
, cp
)) == 1) {
556 /* make sure there is space next character */
557 if (cp
+ 1 >= el
->el_line
.limit
) {
558 idx
= (size_t)(cp
- el
->el_line
.buffer
);
559 if (!ch_enlargebufs(el
, (size_t)2))
561 cp
= &el
->el_line
.buffer
[idx
];
564 crlf
= cp
[-1] == '\r' || cp
[-1] == '\n';
565 if (el
->el_flags
& UNBUFFERED
)
573 cp
= el
->el_line
.buffer
;
574 el
->el_errno
= errno
;
580 for (num
= OKCMD
; num
== OKCMD
;) { /* while still editing this
584 #endif /* DEBUG_EDIT */
585 /* if EOF or error */
586 if ((num
= read_getcmd(el
, &cmdnum
, &ch
)) != OKCMD
) {
589 (void) fprintf(el
->el_errfile
,
590 "Returning from el_gets %d\n", num
);
591 #endif /* DEBUG_READ */
594 if (el
->el_errno
== EINTR
) {
595 el
->el_line
.buffer
[0] = '\0';
596 el
->el_line
.lastchar
=
597 el
->el_line
.cursor
= el
->el_line
.buffer
;
600 if ((unsigned int)cmdnum
>= (unsigned int)el
->el_map
.nfunc
) { /* BUG CHECK command */
602 (void) fprintf(el
->el_errfile
,
603 "ERROR: illegal command from key 0%o\r\n", ch
);
604 #endif /* DEBUG_EDIT */
605 continue; /* try again */
607 /* now do the real command */
611 for (b
= el
->el_map
.help
; b
->name
; b
++)
612 if (b
->func
== cmdnum
)
615 (void) fprintf(el
->el_errfile
,
616 "Executing %s\n", b
->name
);
618 (void) fprintf(el
->el_errfile
,
619 "Error command = %d\n", cmdnum
);
621 #endif /* DEBUG_READ */
622 /* vi redo needs these way down the levels... */
623 el
->el_state
.thiscmd
= cmdnum
;
624 el
->el_state
.thisch
= ch
;
625 if (el
->el_map
.type
== MAP_VI
&&
626 el
->el_map
.current
== el
->el_map
.key
&&
627 el
->el_chared
.c_redo
.pos
< el
->el_chared
.c_redo
.lim
) {
628 if (cmdnum
== VI_DELETE_PREV_CHAR
&&
629 el
->el_chared
.c_redo
.pos
!= el
->el_chared
.c_redo
.buf
630 && Isprint(el
->el_chared
.c_redo
.pos
[-1]))
631 el
->el_chared
.c_redo
.pos
--;
633 *el
->el_chared
.c_redo
.pos
++ = ch
;
635 retval
= (*el
->el_map
.func
[cmdnum
]) (el
, ch
);
637 (void) fprintf(el
->el_errfile
,
638 "Returned state %d\n", retval
);
639 #endif /* DEBUG_READ */
641 /* save the last command here */
642 el
->el_state
.lastcmd
= cmdnum
;
644 /* use any return value */
647 re_refresh_cursor(el
);
652 re_clear_display(el
);
659 case CC_REFRESH_BEEP
:
664 case CC_NORM
: /* normal char */
667 case CC_ARGHACK
: /* Suggested by Rich Salz */
668 /* <rsalz@pineapple.bbn.com> */
669 continue; /* keep going... */
671 case CC_EOF
: /* end of file typed */
672 if ((el
->el_flags
& UNBUFFERED
) == 0)
674 else if (num
== -1) {
675 *el
->el_line
.lastchar
++ = CONTROL('d');
676 el
->el_line
.cursor
= el
->el_line
.lastchar
;
681 case CC_NEWLINE
: /* normal end of line */
682 num
= (int)(el
->el_line
.lastchar
- el
->el_line
.buffer
);
685 case CC_FATAL
: /* fatal error, reset to known state */
687 (void) fprintf(el
->el_errfile
,
688 "*** editor fatal ERROR ***\r\n\n");
689 #endif /* DEBUG_READ */
690 /* put (real) cursor in a known place */
691 re_clear_display(el
); /* reset the display stuff */
692 ch_reset(el
, 1); /* reset the input pointers */
693 re_refresh(el
); /* print the prompt again */
697 default: /* functions we don't know about */
699 (void) fprintf(el
->el_errfile
,
700 "*** editor ERROR ***\r\n\n");
701 #endif /* DEBUG_READ */
706 el
->el_state
.argument
= 1;
707 el
->el_state
.doingarg
= 0;
708 el
->el_chared
.c_vcmd
.action
= NOP
;
709 if (el
->el_flags
& UNBUFFERED
)
713 terminal__flush(el
); /* flush any buffered output */
714 /* make sure the tty is set up correctly */
715 if ((el
->el_flags
& UNBUFFERED
) == 0) {
717 *nread
= num
!= -1 ? num
: 0;
719 *nread
= (int)(el
->el_line
.lastchar
- el
->el_line
.buffer
);
723 el
->el_line
.cursor
= el
->el_line
.lastchar
= cp
;
725 *nread
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
730 errno
= el
->el_errno
;
734 return el
->el_line
.buffer
;