read.c revision 1.81 1 /* $NetBSD: read.c,v 1.81 2016/02/16 22:53:14 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: read.c,v 1.81 2016/02/16 22:53:14 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
43
44 /*
45 * read.c: Clean this junk up! This is horrible code.
46 * Terminal read functions
47 */
48 #include <ctype.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <limits.h>
55
56 #include "el.h"
57
58 #define OKCMD -1 /* must be -1! */
59
60 private int read__fixio(int, int);
61 private int read_preread(EditLine *);
62 private int read_char(EditLine *, Char *);
63 private int read_getcmd(EditLine *, el_action_t *, Char *);
64 private void read_pop(c_macro_t *);
65
66 /* read_init():
67 * Initialize the read stuff
68 */
69 protected int
70 read_init(EditLine *el)
71 {
72 /* builtin read_char */
73 el->el_read.read_char = read_char;
74 return 0;
75 }
76
77
78 /* el_read_setfn():
79 * Set the read char function to the one provided.
80 * If it is set to EL_BUILTIN_GETCFN, then reset to the builtin one.
81 */
82 protected int
83 el_read_setfn(EditLine *el, el_rfunc_t rc)
84 {
85 el->el_read.read_char = (rc == EL_BUILTIN_GETCFN) ? read_char : rc;
86 return 0;
87 }
88
89
90 /* el_read_getfn():
91 * return the current read char function, or EL_BUILTIN_GETCFN
92 * if it is the default one
93 */
94 protected el_rfunc_t
95 el_read_getfn(EditLine *el)
96 {
97 return el->el_read.read_char == read_char ?
98 EL_BUILTIN_GETCFN : el->el_read.read_char;
99 }
100
101
102 #ifndef MIN
103 #define MIN(A,B) ((A) < (B) ? (A) : (B))
104 #endif
105
106 #ifdef DEBUG_EDIT
107 private void
108 read_debug(EditLine *el)
109 {
110
111 if (el->el_line.cursor > el->el_line.lastchar)
112 (void) fprintf(el->el_errfile, "cursor > lastchar\r\n");
113 if (el->el_line.cursor < el->el_line.buffer)
114 (void) fprintf(el->el_errfile, "cursor < buffer\r\n");
115 if (el->el_line.cursor > el->el_line.limit)
116 (void) fprintf(el->el_errfile, "cursor > limit\r\n");
117 if (el->el_line.lastchar > el->el_line.limit)
118 (void) fprintf(el->el_errfile, "lastchar > limit\r\n");
119 if (el->el_line.limit != &el->el_line.buffer[EL_BUFSIZ - 2])
120 (void) fprintf(el->el_errfile, "limit != &buffer[EL_BUFSIZ-2]\r\n");
121 }
122 #endif /* DEBUG_EDIT */
123
124
125 /* read__fixio():
126 * Try to recover from a read error
127 */
128 /* ARGSUSED */
129 private int
130 read__fixio(int fd __attribute__((__unused__)), int e)
131 {
132
133 switch (e) {
134 case -1: /* Make sure that the code is reachable */
135
136 #ifdef EWOULDBLOCK
137 case EWOULDBLOCK:
138 #ifndef TRY_AGAIN
139 #define TRY_AGAIN
140 #endif
141 #endif /* EWOULDBLOCK */
142
143 #if defined(POSIX) && defined(EAGAIN)
144 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
145 case EAGAIN:
146 #ifndef TRY_AGAIN
147 #define TRY_AGAIN
148 #endif
149 #endif /* EWOULDBLOCK && EWOULDBLOCK != EAGAIN */
150 #endif /* POSIX && EAGAIN */
151
152 e = 0;
153 #ifdef TRY_AGAIN
154 #if defined(F_SETFL) && defined(O_NDELAY)
155 if ((e = fcntl(fd, F_GETFL, 0)) == -1)
156 return -1;
157
158 if (fcntl(fd, F_SETFL, e & ~O_NDELAY) == -1)
159 return -1;
160 else
161 e = 1;
162 #endif /* F_SETFL && O_NDELAY */
163
164 #ifdef FIONBIO
165 {
166 int zero = 0;
167
168 if (ioctl(fd, FIONBIO, &zero) == -1)
169 return -1;
170 else
171 e = 1;
172 }
173 #endif /* FIONBIO */
174
175 #endif /* TRY_AGAIN */
176 return e ? 0 : -1;
177
178 case EINTR:
179 return 0;
180
181 default:
182 return -1;
183 }
184 }
185
186
187 /* read_preread():
188 * Try to read the stuff in the input queue;
189 */
190 private int
191 read_preread(EditLine *el)
192 {
193 int chrs = 0;
194
195 if (el->el_tty.t_mode == ED_IO)
196 return 0;
197
198 #ifndef WIDECHAR
199 /* FIONREAD attempts to buffer up multiple bytes, and to make that work
200 * properly with partial wide/UTF-8 characters would need some careful work. */
201 #ifdef FIONREAD
202 (void) ioctl(el->el_infd, FIONREAD, &chrs);
203 if (chrs > 0) {
204 char buf[EL_BUFSIZ];
205
206 chrs = read(el->el_infd, buf,
207 (size_t) MIN(chrs, EL_BUFSIZ - 1));
208 if (chrs > 0) {
209 buf[chrs] = '\0';
210 el_push(el, buf);
211 }
212 }
213 #endif /* FIONREAD */
214 #endif
215 return chrs > 0;
216 }
217
218
219 /* el_push():
220 * Push a macro
221 */
222 public void
223 FUN(el,push)(EditLine *el, const Char *str)
224 {
225 c_macro_t *ma = &el->el_chared.c_macro;
226
227 if (str != NULL && ma->level + 1 < EL_MAXMACRO) {
228 ma->level++;
229 if ((ma->macro[ma->level] = Strdup(str)) != NULL)
230 return;
231 ma->level--;
232 }
233 terminal_beep(el);
234 terminal__flush(el);
235 }
236
237
238 /* read_getcmd():
239 * Get next command from the input stream, return OKCMD on success.
240 * Character values > 255 are not looked up in the map, but inserted.
241 */
242 private int
243 read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch)
244 {
245 static const Char meta = (Char)0x80;
246 el_action_t cmd;
247 int num;
248
249 el->el_errno = 0;
250 do {
251 if ((num = FUN(el,getc)(el, ch)) != 1) {/* if EOF or error */
252 el->el_errno = num == 0 ? 0 : errno;
253 return 0; /* not OKCMD */
254 }
255
256 #ifdef KANJI
257 if ((*ch & meta)) {
258 el->el_state.metanext = 0;
259 cmd = CcViMap[' '];
260 break;
261 } else
262 #endif /* KANJI */
263
264 if (el->el_state.metanext) {
265 el->el_state.metanext = 0;
266 *ch |= meta;
267 }
268 #ifdef WIDECHAR
269 if (*ch >= N_KEYS)
270 cmd = ED_INSERT;
271 else
272 #endif
273 cmd = el->el_map.current[(unsigned char) *ch];
274 if (cmd == ED_SEQUENCE_LEAD_IN) {
275 keymacro_value_t val;
276 switch (keymacro_get(el, ch, &val)) {
277 case XK_CMD:
278 cmd = val.cmd;
279 break;
280 case XK_STR:
281 FUN(el,push)(el, val.str);
282 break;
283 #ifdef notyet
284 case XK_EXE:
285 /* XXX: In the future to run a user function */
286 RunCommand(val.str);
287 break;
288 #endif
289 default:
290 EL_ABORT((el->el_errfile, "Bad XK_ type \n"));
291 break;
292 }
293 }
294 if (el->el_map.alt == NULL)
295 el->el_map.current = el->el_map.key;
296 } while (cmd == ED_SEQUENCE_LEAD_IN);
297 *cmdnum = cmd;
298 return OKCMD;
299 }
300
301 /* read_char():
302 * Read a character from the tty.
303 */
304 private int
305 read_char(EditLine *el, Char *cp)
306 {
307 ssize_t num_read;
308 int tried = 0;
309 char cbuf[MB_LEN_MAX];
310 size_t cbp = 0;
311 int save_errno = errno;
312
313 again:
314 el->el_signal->sig_no = 0;
315 while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) {
316 int e = errno;
317 switch (el->el_signal->sig_no) {
318 case SIGCONT:
319 FUN(el,set)(el, EL_REFRESH);
320 /*FALLTHROUGH*/
321 case SIGWINCH:
322 sig_set(el);
323 goto again;
324 default:
325 break;
326 }
327 if (!tried && read__fixio(el->el_infd, e) == 0) {
328 errno = save_errno;
329 tried = 1;
330 } else {
331 errno = e;
332 *cp = '\0';
333 return -1;
334 }
335 }
336
337 /* Test for EOF */
338 if (num_read == 0) {
339 *cp = '\0';
340 return 0;
341 }
342
343 for (;;) {
344 mbstate_t mbs;
345
346 ++cbp;
347 /* This only works because UTF8 is stateless */
348 memset(&mbs, 0, sizeof(mbs));
349 switch (ct_mbrtowc(cp, cbuf, cbp, &mbs)) {
350 case (size_t)-1:
351 if (cbp > 1) {
352 /*
353 * Invalid sequence, discard all bytes
354 * except the last one.
355 */
356 cbuf[0] = cbuf[cbp - 1];
357 cbp = 0;
358 break;
359 } else {
360 /* Invalid byte, discard it. */
361 cbp = 0;
362 goto again;
363 }
364 case (size_t)-2:
365 /*
366 * We don't support other multibyte charsets.
367 * The second condition shouldn't happen
368 * and is here merely for additional safety.
369 */
370 if ((el->el_flags & CHARSET_IS_UTF8) == 0 ||
371 cbp >= MB_LEN_MAX) {
372 errno = EILSEQ;
373 *cp = '\0';
374 return -1;
375 }
376 /* Incomplete sequence, read another byte. */
377 goto again;
378 default:
379 /* Valid character, process it. */
380 return 1;
381 }
382 }
383 }
384
385 /* read_pop():
386 * Pop a macro from the stack
387 */
388 private void
389 read_pop(c_macro_t *ma)
390 {
391 int i;
392
393 el_free(ma->macro[0]);
394 for (i = 0; i < ma->level; i++)
395 ma->macro[i] = ma->macro[i + 1];
396 ma->level--;
397 ma->offset = 0;
398 }
399
400 /* el_getc():
401 * Read a character
402 */
403 public int
404 FUN(el,getc)(EditLine *el, Char *cp)
405 {
406 int num_read;
407 c_macro_t *ma = &el->el_chared.c_macro;
408
409 terminal__flush(el);
410 for (;;) {
411 if (ma->level < 0) {
412 if (!read_preread(el))
413 break;
414 }
415
416 if (ma->level < 0)
417 break;
418
419 if (ma->macro[0][ma->offset] == '\0') {
420 read_pop(ma);
421 continue;
422 }
423
424 *cp = ma->macro[0][ma->offset++];
425
426 if (ma->macro[0][ma->offset] == '\0') {
427 /* Needed for QuoteMode On */
428 read_pop(ma);
429 }
430
431 return 1;
432 }
433
434 #ifdef DEBUG_READ
435 (void) fprintf(el->el_errfile, "Turning raw mode on\n");
436 #endif /* DEBUG_READ */
437 if (tty_rawmode(el) < 0)/* make sure the tty is set up correctly */
438 return 0;
439
440 #ifdef DEBUG_READ
441 (void) fprintf(el->el_errfile, "Reading a character\n");
442 #endif /* DEBUG_READ */
443 num_read = (*el->el_read.read_char)(el, cp);
444 if (num_read < 0)
445 el->el_errno = errno;
446 #ifdef WIDECHAR
447 if (el->el_flags & NARROW_READ)
448 *cp = *(char *)(void *)cp;
449 #endif
450 #ifdef DEBUG_READ
451 (void) fprintf(el->el_errfile, "Got it %c\n", *cp);
452 #endif /* DEBUG_READ */
453 return num_read;
454 }
455
456 protected void
457 read_prepare(EditLine *el)
458 {
459 if (el->el_flags & HANDLE_SIGNALS)
460 sig_set(el);
461 if (el->el_flags & NO_TTY)
462 return;
463 if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED)
464 tty_rawmode(el);
465
466 /* This is relatively cheap, and things go terribly wrong if
467 we have the wrong size. */
468 el_resize(el);
469 re_clear_display(el); /* reset the display stuff */
470 ch_reset(el, 0);
471 re_refresh(el); /* print the prompt */
472
473 if (el->el_flags & UNBUFFERED)
474 terminal__flush(el);
475 }
476
477 protected void
478 read_finish(EditLine *el)
479 {
480 if ((el->el_flags & UNBUFFERED) == 0)
481 (void) tty_cookedmode(el);
482 if (el->el_flags & HANDLE_SIGNALS)
483 sig_clr(el);
484 }
485
486 public const Char *
487 FUN(el,gets)(EditLine *el, int *nread)
488 {
489 int retval;
490 el_action_t cmdnum = 0;
491 int num; /* how many chars we have read at NL */
492 Char ch, *cp;
493 int crlf = 0;
494 int nrb;
495 #ifdef FIONREAD
496 c_macro_t *ma = &el->el_chared.c_macro;
497 #endif /* FIONREAD */
498
499 if (nread == NULL)
500 nread = &nrb;
501 *nread = 0;
502
503 if (el->el_flags & NO_TTY) {
504 size_t idx;
505
506 cp = el->el_line.buffer;
507 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
508 /* make sure there is space for next character */
509 if (cp + 1 >= el->el_line.limit) {
510 idx = (size_t)(cp - el->el_line.buffer);
511 if (!ch_enlargebufs(el, (size_t)2))
512 break;
513 cp = &el->el_line.buffer[idx];
514 }
515 cp++;
516 if (el->el_flags & UNBUFFERED)
517 break;
518 if (cp[-1] == '\r' || cp[-1] == '\n')
519 break;
520 }
521 if (num == -1) {
522 if (errno == EINTR)
523 cp = el->el_line.buffer;
524 el->el_errno = errno;
525 }
526
527 goto noedit;
528 }
529
530
531 #ifdef FIONREAD
532 if (el->el_tty.t_mode == EX_IO && ma->level < 0) {
533 long chrs = 0;
534
535 (void) ioctl(el->el_infd, FIONREAD, &chrs);
536 if (chrs == 0) {
537 if (tty_rawmode(el) < 0) {
538 errno = 0;
539 *nread = 0;
540 return NULL;
541 }
542 }
543 }
544 #endif /* FIONREAD */
545
546 if ((el->el_flags & UNBUFFERED) == 0)
547 read_prepare(el);
548
549 if (el->el_flags & EDIT_DISABLED) {
550 size_t idx;
551
552 if ((el->el_flags & UNBUFFERED) == 0)
553 cp = el->el_line.buffer;
554 else
555 cp = el->el_line.lastchar;
556
557 terminal__flush(el);
558
559 while ((num = (*el->el_read.read_char)(el, cp)) == 1) {
560 /* make sure there is space next character */
561 if (cp + 1 >= el->el_line.limit) {
562 idx = (size_t)(cp - el->el_line.buffer);
563 if (!ch_enlargebufs(el, (size_t)2))
564 break;
565 cp = &el->el_line.buffer[idx];
566 }
567 cp++;
568 crlf = cp[-1] == '\r' || cp[-1] == '\n';
569 if (el->el_flags & UNBUFFERED)
570 break;
571 if (crlf)
572 break;
573 }
574
575 if (num == -1) {
576 if (errno == EINTR)
577 cp = el->el_line.buffer;
578 el->el_errno = errno;
579 }
580
581 goto noedit;
582 }
583
584 for (num = OKCMD; num == OKCMD;) { /* while still editing this
585 * line */
586 #ifdef DEBUG_EDIT
587 read_debug(el);
588 #endif /* DEBUG_EDIT */
589 /* if EOF or error */
590 if ((num = read_getcmd(el, &cmdnum, &ch)) != OKCMD) {
591 num = -1;
592 #ifdef DEBUG_READ
593 (void) fprintf(el->el_errfile,
594 "Returning from el_gets %d\n", num);
595 #endif /* DEBUG_READ */
596 break;
597 }
598 if (el->el_errno == EINTR) {
599 el->el_line.buffer[0] = '\0';
600 el->el_line.lastchar =
601 el->el_line.cursor = el->el_line.buffer;
602 break;
603 }
604 if ((size_t)cmdnum >= el->el_map.nfunc) { /* BUG CHECK command */
605 #ifdef DEBUG_EDIT
606 (void) fprintf(el->el_errfile,
607 "ERROR: illegal command from key 0%o\r\n", ch);
608 #endif /* DEBUG_EDIT */
609 continue; /* try again */
610 }
611 /* now do the real command */
612 #ifdef DEBUG_READ
613 {
614 el_bindings_t *b;
615 for (b = el->el_map.help; b->name; b++)
616 if (b->func == cmdnum)
617 break;
618 if (b->name)
619 (void) fprintf(el->el_errfile,
620 "Executing %s\n", b->name);
621 else
622 (void) fprintf(el->el_errfile,
623 "Error command = %d\n", cmdnum);
624 }
625 #endif /* DEBUG_READ */
626 /* vi redo needs these way down the levels... */
627 el->el_state.thiscmd = cmdnum;
628 el->el_state.thisch = ch;
629 if (el->el_map.type == MAP_VI &&
630 el->el_map.current == el->el_map.key &&
631 el->el_chared.c_redo.pos < el->el_chared.c_redo.lim) {
632 if (cmdnum == VI_DELETE_PREV_CHAR &&
633 el->el_chared.c_redo.pos != el->el_chared.c_redo.buf
634 && Isprint(el->el_chared.c_redo.pos[-1]))
635 el->el_chared.c_redo.pos--;
636 else
637 *el->el_chared.c_redo.pos++ = ch;
638 }
639 retval = (*el->el_map.func[cmdnum]) (el, ch);
640 #ifdef DEBUG_READ
641 (void) fprintf(el->el_errfile,
642 "Returned state %d\n", retval );
643 #endif /* DEBUG_READ */
644
645 /* save the last command here */
646 el->el_state.lastcmd = cmdnum;
647
648 /* use any return value */
649 switch (retval) {
650 case CC_CURSOR:
651 re_refresh_cursor(el);
652 break;
653
654 case CC_REDISPLAY:
655 re_clear_lines(el);
656 re_clear_display(el);
657 /* FALLTHROUGH */
658
659 case CC_REFRESH:
660 re_refresh(el);
661 break;
662
663 case CC_REFRESH_BEEP:
664 re_refresh(el);
665 terminal_beep(el);
666 break;
667
668 case CC_NORM: /* normal char */
669 break;
670
671 case CC_ARGHACK: /* Suggested by Rich Salz */
672 /* <rsalz (at) pineapple.bbn.com> */
673 continue; /* keep going... */
674
675 case CC_EOF: /* end of file typed */
676 if ((el->el_flags & UNBUFFERED) == 0)
677 num = 0;
678 else if (num == -1) {
679 *el->el_line.lastchar++ = CONTROL('d');
680 el->el_line.cursor = el->el_line.lastchar;
681 num = 1;
682 }
683 break;
684
685 case CC_NEWLINE: /* normal end of line */
686 num = (int)(el->el_line.lastchar - el->el_line.buffer);
687 break;
688
689 case CC_FATAL: /* fatal error, reset to known state */
690 #ifdef DEBUG_READ
691 (void) fprintf(el->el_errfile,
692 "*** editor fatal ERROR ***\r\n\n");
693 #endif /* DEBUG_READ */
694 /* put (real) cursor in a known place */
695 re_clear_display(el); /* reset the display stuff */
696 ch_reset(el, 1); /* reset the input pointers */
697 re_refresh(el); /* print the prompt again */
698 break;
699
700 case CC_ERROR:
701 default: /* functions we don't know about */
702 #ifdef DEBUG_READ
703 (void) fprintf(el->el_errfile,
704 "*** editor ERROR ***\r\n\n");
705 #endif /* DEBUG_READ */
706 terminal_beep(el);
707 terminal__flush(el);
708 break;
709 }
710 el->el_state.argument = 1;
711 el->el_state.doingarg = 0;
712 el->el_chared.c_vcmd.action = NOP;
713 if (el->el_flags & UNBUFFERED)
714 break;
715 }
716
717 terminal__flush(el); /* flush any buffered output */
718 /* make sure the tty is set up correctly */
719 if ((el->el_flags & UNBUFFERED) == 0) {
720 read_finish(el);
721 *nread = num != -1 ? num : 0;
722 } else {
723 *nread = (int)(el->el_line.lastchar - el->el_line.buffer);
724 }
725 goto done;
726 noedit:
727 el->el_line.cursor = el->el_line.lastchar = cp;
728 *cp = '\0';
729 *nread = (int)(el->el_line.cursor - el->el_line.buffer);
730 done:
731 if (*nread == 0) {
732 if (num == -1) {
733 *nread = -1;
734 errno = el->el_errno;
735 }
736 return NULL;
737 } else
738 return el->el_line.buffer;
739 }
740