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