1 /* $OpenBSD: read.c,v 1.44 2016/05/25 09:36:21 schwarze Exp $ */
2 /* $NetBSD: read.c,v 1.100 2016/05/24 19:31:27 christos Exp $ */
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * read.c: Clean this junk up! This is horrible code.
40 * Terminal read functions
54 #define EL_MAXMACRO 10
64 el_rfunc_t read_char
; /* Function to read a character. */
68 static int read__fixio(int, int);
69 static int read_char(EditLine
*, wchar_t *);
70 static int read_getcmd(EditLine
*, el_action_t
*, wchar_t *);
71 static void read_clearmacros(struct macros
*);
72 static void read_pop(struct macros
*);
75 * Initialize the read stuff
78 read_init(EditLine
*el
)
82 if ((el
->el_read
= malloc(sizeof(*el
->el_read
))) == NULL
)
85 ma
= &el
->el_read
->macros
;
86 if ((ma
->macro
= reallocarray(NULL
, EL_MAXMACRO
,
87 sizeof(*ma
->macro
))) == NULL
) {
94 /* builtin read_char */
95 el
->el_read
->read_char
= read_char
;
100 * Free the data structures used by the read stuff.
103 read_end(struct el_read_t
*el_read
)
105 read_clearmacros(&el_read
->macros
);
106 free(el_read
->macros
.macro
);
107 el_read
->macros
.macro
= NULL
;
111 * Set the read char function to the one provided.
112 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
115 el_read_setfn(struct el_read_t
*el_read
, el_rfunc_t rc
)
117 el_read
->read_char
= (rc
== EL_BUILTIN_GETCFN
) ? read_char
: rc
;
123 * return the current read char function, or EL_BUILTIN_GETCFN
124 * if it is the default one
127 el_read_getfn(struct el_read_t
*el_read
)
129 return el_read
->read_char
== read_char
?
130 EL_BUILTIN_GETCFN
: el_read
->read_char
;
135 * Try to recover from a read error
139 read__fixio(int fd
__attribute__((__unused__
)), int e
)
143 case -1: /* Make sure that the code is reachable */
150 #endif /* EWOULDBLOCK */
152 #if defined(POSIX) && defined(EAGAIN)
153 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
158 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
159 #endif /* POSIX && EAGAIN */
163 #if defined(F_SETFL) && defined(O_NDELAY)
164 if ((e
= fcntl(fd
, F_GETFL
)) == -1)
167 if (fcntl(fd
, F_SETFL
, e
& ~O_NDELAY
) == -1)
171 #endif /* F_SETFL && O_NDELAY */
177 if (ioctl(fd
, FIONBIO
, &zero
) == -1)
184 #endif /* TRY_AGAIN */
200 el_wpush(EditLine
*el
, const wchar_t *str
)
202 struct macros
*ma
= &el
->el_read
->macros
;
204 if (str
!= NULL
&& ma
->level
+ 1 < EL_MAXMACRO
) {
206 if ((ma
->macro
[ma
->level
] = wcsdup(str
)) != NULL
)
216 * Get next command from the input stream,
217 * return 0 on success or -1 on EOF or error.
218 * Character values > 255 are not looked up in the map, but inserted.
221 read_getcmd(EditLine
*el
, el_action_t
*cmdnum
, wchar_t *ch
)
223 static const wchar_t meta
= (wchar_t)0x80;
228 if ((num
= el_wgetc(el
, ch
)) != 1)
233 el
->el_state
.metanext
= 0;
239 if (el
->el_state
.metanext
) {
240 el
->el_state
.metanext
= 0;
246 cmd
= el
->el_map
.current
[(unsigned char) *ch
];
247 if (cmd
== ED_SEQUENCE_LEAD_IN
) {
248 keymacro_value_t val
;
249 switch (keymacro_get(el
, ch
, &val
)) {
254 el_wpush(el
, val
.str
);
259 EL_ABORT((el
->el_errfile
, "Bad XK_ type \n"));
263 } while (cmd
== ED_SEQUENCE_LEAD_IN
);
269 * Read a character from the tty.
272 read_char(EditLine
*el
, wchar_t *cp
)
276 char cbuf
[MB_LEN_MAX
];
278 int save_errno
= errno
;
281 el
->el_signal
->sig_no
= 0;
282 while ((num_read
= read(el
->el_infd
, cbuf
+ cbp
, 1)) == -1) {
284 switch (el
->el_signal
->sig_no
) {
286 el_set(el
, EL_REFRESH
);
294 if (!tried
&& read__fixio(el
->el_infd
, e
) == 0) {
314 /* This only works because UTF8 is stateless. */
315 memset(&mbs
, 0, sizeof(mbs
));
316 switch (mbrtowc(cp
, cbuf
, cbp
, &mbs
)) {
320 * Invalid sequence, discard all bytes
321 * except the last one.
323 cbuf
[0] = cbuf
[cbp
- 1];
327 /* Invalid byte, discard it. */
333 * We don't support other multibyte charsets.
334 * The second condition shouldn't happen
335 * and is here merely for additional safety.
337 if ((el
->el_flags
& CHARSET_IS_UTF8
) == 0 ||
343 /* Incomplete sequence, read another byte. */
346 /* Valid character, process it. */
353 * Pop a macro from the stack
356 read_pop(struct macros
*ma
)
361 for (i
= 0; i
< ma
->level
; i
++)
362 ma
->macro
[i
] = ma
->macro
[i
+ 1];
368 read_clearmacros(struct macros
*ma
)
370 while (ma
->level
>= 0)
371 free(ma
->macro
[ma
->level
--]);
376 * Read a wide character
379 el_wgetc(EditLine
*el
, wchar_t *cp
)
381 struct macros
*ma
= &el
->el_read
->macros
;
389 if (ma
->macro
[0][ma
->offset
] == '\0') {
394 *cp
= ma
->macro
[0][ma
->offset
++];
396 if (ma
->macro
[0][ma
->offset
] == '\0') {
397 /* Needed for QuoteMode On */
404 if (tty_rawmode(el
) < 0)/* make sure the tty is set up correctly */
407 num_read
= (*el
->el_read
->read_char
)(el
, cp
);
410 * Remember the original reason of a read failure
411 * such that el_wgets() can restore it after doing
412 * various cleanup operation that might change errno.
415 el
->el_read
->read_errno
= errno
;
421 read_prepare(EditLine
*el
)
423 if (el
->el_flags
& HANDLE_SIGNALS
)
425 if (el
->el_flags
& NO_TTY
)
427 if ((el
->el_flags
& (UNBUFFERED
|EDIT_DISABLED
)) == UNBUFFERED
)
430 /* This is relatively cheap, and things go terribly wrong if
431 we have the wrong size. */
433 re_clear_display(el
); /* reset the display stuff */
435 re_refresh(el
); /* print the prompt */
437 if (el
->el_flags
& UNBUFFERED
)
442 read_finish(EditLine
*el
)
444 if ((el
->el_flags
& UNBUFFERED
) == 0)
445 (void) tty_cookedmode(el
);
446 if (el
->el_flags
& HANDLE_SIGNALS
)
451 el_wgets(EditLine
*el
, int *nread
)
454 el_action_t cmdnum
= 0;
455 int num
; /* how many chars we have read at NL */
464 el
->el_read
->read_errno
= 0;
466 if (el
->el_flags
& NO_TTY
) {
469 cp
= el
->el_line
.buffer
;
470 while ((num
= (*el
->el_read
->read_char
)(el
, &wc
)) == 1) {
472 /* make sure there is space for next character */
473 if (cp
+ 1 >= el
->el_line
.limit
) {
474 idx
= (cp
- el
->el_line
.buffer
);
475 if (!ch_enlargebufs(el
, 2))
477 cp
= &el
->el_line
.buffer
[idx
];
480 if (el
->el_flags
& UNBUFFERED
)
482 if (cp
[-1] == '\r' || cp
[-1] == '\n')
485 if (num
== -1 && errno
== EINTR
)
486 cp
= el
->el_line
.buffer
;
492 if (el
->el_tty
.t_mode
== EX_IO
&& el
->el_read
->macros
.level
< 0) {
495 (void) ioctl(el
->el_infd
, FIONREAD
, &chrs
);
497 if (tty_rawmode(el
) < 0) {
504 #endif /* FIONREAD */
506 if ((el
->el_flags
& UNBUFFERED
) == 0)
509 if (el
->el_flags
& EDIT_DISABLED
) {
512 if ((el
->el_flags
& UNBUFFERED
) == 0)
513 cp
= el
->el_line
.buffer
;
515 cp
= el
->el_line
.lastchar
;
519 while ((num
= (*el
->el_read
->read_char
)(el
, &wc
)) == 1) {
521 /* make sure there is space next character */
522 if (cp
+ 1 >= el
->el_line
.limit
) {
523 idx
= (cp
- el
->el_line
.buffer
);
524 if (!ch_enlargebufs(el
, 2))
526 cp
= &el
->el_line
.buffer
[idx
];
529 crlf
= cp
[-1] == '\r' || cp
[-1] == '\n';
530 if (el
->el_flags
& UNBUFFERED
)
535 if (num
== -1 && errno
== EINTR
)
536 cp
= el
->el_line
.buffer
;
540 for (num
= -1; num
== -1;) { /* while still editing this line */
541 /* if EOF or error */
542 if (read_getcmd(el
, &cmdnum
, &ch
) == -1)
544 if ((int)cmdnum
>= el
->el_map
.nfunc
) /* BUG CHECK command */
545 continue; /* try again */
546 /* now do the real command */
547 /* vi redo needs these way down the levels... */
548 el
->el_state
.thiscmd
= cmdnum
;
549 el
->el_state
.thisch
= ch
;
550 if (el
->el_map
.type
== MAP_VI
&&
551 el
->el_map
.current
== el
->el_map
.key
&&
552 el
->el_chared
.c_redo
.pos
< el
->el_chared
.c_redo
.lim
) {
553 if (cmdnum
== VI_DELETE_PREV_CHAR
&&
554 el
->el_chared
.c_redo
.pos
!= el
->el_chared
.c_redo
.buf
555 && iswprint(el
->el_chared
.c_redo
.pos
[-1]))
556 el
->el_chared
.c_redo
.pos
--;
558 *el
->el_chared
.c_redo
.pos
++ = ch
;
560 retval
= (*el
->el_map
.func
[cmdnum
]) (el
, ch
);
562 /* save the last command here */
563 el
->el_state
.lastcmd
= cmdnum
;
565 /* use any return value */
568 re_refresh_cursor(el
);
573 re_clear_display(el
);
580 case CC_REFRESH_BEEP
:
585 case CC_NORM
: /* normal char */
588 case CC_ARGHACK
: /* Suggested by Rich Salz */
589 /* <rsalz@pineapple.bbn.com> */
590 continue; /* keep going... */
592 case CC_EOF
: /* end of file typed */
593 if ((el
->el_flags
& UNBUFFERED
) == 0)
595 else if (num
== -1) {
596 *el
->el_line
.lastchar
++ = CONTROL('d');
597 el
->el_line
.cursor
= el
->el_line
.lastchar
;
602 case CC_NEWLINE
: /* normal end of line */
603 num
= (int)(el
->el_line
.lastchar
- el
->el_line
.buffer
);
606 case CC_FATAL
: /* fatal error, reset to known state */
607 /* put (real) cursor in a known place */
608 re_clear_display(el
); /* reset the display stuff */
609 ch_reset(el
); /* reset the input pointers */
610 read_clearmacros(&el
->el_read
->macros
);
611 re_refresh(el
); /* print the prompt again */
615 default: /* functions we don't know about */
620 el
->el_state
.argument
= 1;
621 el
->el_state
.doingarg
= 0;
622 el
->el_chared
.c_vcmd
.action
= NOP
;
623 if (el
->el_flags
& UNBUFFERED
)
627 terminal__flush(el
); /* flush any buffered output */
628 /* make sure the tty is set up correctly */
629 if ((el
->el_flags
& UNBUFFERED
) == 0) {
631 *nread
= num
!= -1 ? num
: 0;
633 *nread
= (int)(el
->el_line
.lastchar
- el
->el_line
.buffer
);
637 el
->el_line
.cursor
= el
->el_line
.lastchar
= cp
;
639 *nread
= (int)(el
->el_line
.cursor
- el
->el_line
.buffer
);
644 if (el
->el_read
->read_errno
)
645 errno
= el
->el_read
->read_errno
;
649 return el
->el_line
.buffer
;