| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: read.c,v 1.61 2011/07/09 23:54:39 christos Exp $ */ | | 1 | /* $NetBSD: read.c,v 1.62 2011/07/28 00:44:35 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1992, 1993 | | 4 | * Copyright (c) 1992, 1993 |
5 | * The Regents of the University of California. All rights reserved. | | 5 | * The Regents of the University of California. All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to Berkeley by | | 7 | * This code is derived from software contributed to Berkeley by |
8 | * Christos Zoulas of Cornell University. | | 8 | * Christos Zoulas of Cornell University. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
| @@ -27,27 +27,27 @@ | | | @@ -27,27 +27,27 @@ |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 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 | | 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 | | 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 | | 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * SUCH DAMAGE. | | 32 | * SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #include "config.h" | | 35 | #include "config.h" |
36 | #if !defined(lint) && !defined(SCCSID) | | 36 | #if !defined(lint) && !defined(SCCSID) |
37 | #if 0 | | 37 | #if 0 |
38 | static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; | | 38 | static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/4/93"; |
39 | #else | | 39 | #else |
40 | __RCSID("$NetBSD: read.c,v 1.61 2011/07/09 23:54:39 christos Exp $"); | | 40 | __RCSID("$NetBSD: read.c,v 1.62 2011/07/28 00:44:35 christos Exp $"); |
41 | #endif | | 41 | #endif |
42 | #endif /* not lint && not SCCSID */ | | 42 | #endif /* not lint && not SCCSID */ |
43 | | | 43 | |
44 | /* | | 44 | /* |
45 | * read.c: Clean this junk up! This is horrible code. | | 45 | * read.c: Clean this junk up! This is horrible code. |
46 | * Terminal read functions | | 46 | * Terminal read functions |
47 | */ | | 47 | */ |
48 | #include <errno.h> | | 48 | #include <errno.h> |
49 | #include <fcntl.h> | | 49 | #include <fcntl.h> |
50 | #include <unistd.h> | | 50 | #include <unistd.h> |
51 | #include <stdlib.h> | | 51 | #include <stdlib.h> |
52 | #include <limits.h> | | 52 | #include <limits.h> |
53 | #include "el.h" | | 53 | #include "el.h" |
| @@ -217,28 +217,28 @@ read_preread(EditLine *el) | | | @@ -217,28 +217,28 @@ read_preread(EditLine *el) |
217 | * Push a macro | | 217 | * Push a macro |
218 | */ | | 218 | */ |
219 | public void | | 219 | public void |
220 | FUN(el,push)(EditLine *el, const Char *str) | | 220 | FUN(el,push)(EditLine *el, const Char *str) |
221 | { | | 221 | { |
222 | c_macro_t *ma = &el->el_chared.c_macro; | | 222 | c_macro_t *ma = &el->el_chared.c_macro; |
223 | | | 223 | |
224 | if (str != NULL && ma->level + 1 < EL_MAXMACRO) { | | 224 | if (str != NULL && ma->level + 1 < EL_MAXMACRO) { |
225 | ma->level++; | | 225 | ma->level++; |
226 | if ((ma->macro[ma->level] = Strdup(str)) != NULL) | | 226 | if ((ma->macro[ma->level] = Strdup(str)) != NULL) |
227 | return; | | 227 | return; |
228 | ma->level--; | | 228 | ma->level--; |
229 | } | | 229 | } |
230 | term_beep(el); | | 230 | terminal_beep(el); |
231 | term__flush(el); | | 231 | terminal__flush(el); |
232 | } | | 232 | } |
233 | | | 233 | |
234 | | | 234 | |
235 | /* read_getcmd(): | | 235 | /* read_getcmd(): |
236 | * Return next command from the input stream. | | 236 | * Return next command from the input stream. |
237 | * Character values > 255 are not looked up in the map, but inserted. | | 237 | * Character values > 255 are not looked up in the map, but inserted. |
238 | */ | | 238 | */ |
239 | private int | | 239 | private int |
240 | read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch) | | 240 | read_getcmd(EditLine *el, el_action_t *cmdnum, Char *ch) |
241 | { | | 241 | { |
242 | el_action_t cmd; | | 242 | el_action_t cmd; |
243 | int num; | | 243 | int num; |
244 | | | 244 | |
| @@ -313,27 +313,27 @@ private int | | | @@ -313,27 +313,27 @@ private int |
313 | read_char(EditLine *el, Char *cp) | | 313 | read_char(EditLine *el, Char *cp) |
314 | { | | 314 | { |
315 | ssize_t num_read; | | 315 | ssize_t num_read; |
316 | int tried = 0; | | 316 | int tried = 0; |
317 | char cbuf[MB_LEN_MAX]; | | 317 | char cbuf[MB_LEN_MAX]; |
318 | int cbp = 0; | | 318 | int cbp = 0; |
319 | int bytes = 0; | | 319 | int bytes = 0; |
320 | | | 320 | |
321 | again: | | 321 | again: |
322 | el->el_signal->sig_no = 0; | | 322 | el->el_signal->sig_no = 0; |
323 | while ((num_read = read(el->el_infd, cbuf + cbp, 1)) == -1) { | | 323 | while ((num_read = read(el->el_infd, cbuf + cbp, 1)) == -1) { |
324 | switch (el->el_signal->sig_no) { | | 324 | switch (el->el_signal->sig_no) { |
325 | case SIGCONT: | | 325 | case SIGCONT: |
326 | el_set(el, EL_REFRESH); | | 326 | FUN(el,set)(el, EL_REFRESH); |
327 | /*FALLTHROUGH*/ | | 327 | /*FALLTHROUGH*/ |
328 | case SIGWINCH: | | 328 | case SIGWINCH: |
329 | sig_set(el); | | 329 | sig_set(el); |
330 | goto again; | | 330 | goto again; |
331 | default: | | 331 | default: |
332 | break; | | 332 | break; |
333 | } | | 333 | } |
334 | if (!tried && read__fixio(el->el_infd, errno) == 0) | | 334 | if (!tried && read__fixio(el->el_infd, errno) == 0) |
335 | tried = 1; | | 335 | tried = 1; |
336 | else { | | 336 | else { |
337 | *cp = '\0'; | | 337 | *cp = '\0'; |
338 | return (-1); | | 338 | return (-1); |
339 | } | | 339 | } |
| @@ -382,27 +382,27 @@ read_pop(c_macro_t *ma) | | | @@ -382,27 +382,27 @@ read_pop(c_macro_t *ma) |
382 | ma->level--; | | 382 | ma->level--; |
383 | ma->offset = 0; | | 383 | ma->offset = 0; |
384 | } | | 384 | } |
385 | | | 385 | |
386 | /* el_getc(): | | 386 | /* el_getc(): |
387 | * Read a character | | 387 | * Read a character |
388 | */ | | 388 | */ |
389 | public int | | 389 | public int |
390 | FUN(el,getc)(EditLine *el, Char *cp) | | 390 | FUN(el,getc)(EditLine *el, Char *cp) |
391 | { | | 391 | { |
392 | int num_read; | | 392 | int num_read; |
393 | c_macro_t *ma = &el->el_chared.c_macro; | | 393 | c_macro_t *ma = &el->el_chared.c_macro; |
394 | | | 394 | |
395 | term__flush(el); | | 395 | terminal__flush(el); |
396 | for (;;) { | | 396 | for (;;) { |
397 | if (ma->level < 0) { | | 397 | if (ma->level < 0) { |
398 | if (!read_preread(el)) | | 398 | if (!read_preread(el)) |
399 | break; | | 399 | break; |
400 | } | | 400 | } |
401 | | | 401 | |
402 | if (ma->level < 0) | | 402 | if (ma->level < 0) |
403 | break; | | 403 | break; |
404 | | | 404 | |
405 | if (ma->macro[0][ma->offset] == '\0') { | | 405 | if (ma->macro[0][ma->offset] == '\0') { |
406 | read_pop(ma); | | 406 | read_pop(ma); |
407 | continue; | | 407 | continue; |
408 | } | | 408 | } |
| @@ -445,27 +445,27 @@ read_prepare(EditLine *el) | | | @@ -445,27 +445,27 @@ read_prepare(EditLine *el) |
445 | if (el->el_flags & NO_TTY) | | 445 | if (el->el_flags & NO_TTY) |
446 | return; | | 446 | return; |
447 | if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED) | | 447 | if ((el->el_flags & (UNBUFFERED|EDIT_DISABLED)) == UNBUFFERED) |
448 | tty_rawmode(el); | | 448 | tty_rawmode(el); |
449 | | | 449 | |
450 | /* This is relatively cheap, and things go terribly wrong if | | 450 | /* This is relatively cheap, and things go terribly wrong if |
451 | we have the wrong size. */ | | 451 | we have the wrong size. */ |
452 | el_resize(el); | | 452 | el_resize(el); |
453 | re_clear_display(el); /* reset the display stuff */ | | 453 | re_clear_display(el); /* reset the display stuff */ |
454 | ch_reset(el, 0); | | 454 | ch_reset(el, 0); |
455 | re_refresh(el); /* print the prompt */ | | 455 | re_refresh(el); /* print the prompt */ |
456 | | | 456 | |
457 | if (el->el_flags & UNBUFFERED) | | 457 | if (el->el_flags & UNBUFFERED) |
458 | term__flush(el); | | 458 | terminal__flush(el); |
459 | } | | 459 | } |
460 | | | 460 | |
461 | protected void | | 461 | protected void |
462 | read_finish(EditLine *el) | | 462 | read_finish(EditLine *el) |
463 | { | | 463 | { |
464 | if ((el->el_flags & UNBUFFERED) == 0) | | 464 | if ((el->el_flags & UNBUFFERED) == 0) |
465 | (void) tty_cookedmode(el); | | 465 | (void) tty_cookedmode(el); |
466 | if (el->el_flags & HANDLE_SIGNALS) | | 466 | if (el->el_flags & HANDLE_SIGNALS) |
467 | sig_clr(el); | | 467 | sig_clr(el); |
468 | } | | 468 | } |
469 | | | 469 | |
470 | public const Char * | | 470 | public const Char * |
471 | FUN(el,gets)(EditLine *el, int *nread) | | 471 | FUN(el,gets)(EditLine *el, int *nread) |
| @@ -528,27 +528,27 @@ FUN(el,gets)(EditLine *el, int *nread) | | | @@ -528,27 +528,27 @@ FUN(el,gets)(EditLine *el, int *nread) |
528 | #endif /* FIONREAD */ | | 528 | #endif /* FIONREAD */ |
529 | | | 529 | |
530 | if ((el->el_flags & UNBUFFERED) == 0) | | 530 | if ((el->el_flags & UNBUFFERED) == 0) |
531 | read_prepare(el); | | 531 | read_prepare(el); |
532 | | | 532 | |
533 | if (el->el_flags & EDIT_DISABLED) { | | 533 | if (el->el_flags & EDIT_DISABLED) { |
534 | size_t idx; | | 534 | size_t idx; |
535 | | | 535 | |
536 | if ((el->el_flags & UNBUFFERED) == 0) | | 536 | if ((el->el_flags & UNBUFFERED) == 0) |
537 | cp = el->el_line.buffer; | | 537 | cp = el->el_line.buffer; |
538 | else | | 538 | else |
539 | cp = el->el_line.lastchar; | | 539 | cp = el->el_line.lastchar; |
540 | | | 540 | |
541 | term__flush(el); | | 541 | terminal__flush(el); |
542 | | | 542 | |
543 | while ((num = (*el->el_read.read_char)(el, cp)) == 1) { | | 543 | while ((num = (*el->el_read.read_char)(el, cp)) == 1) { |
544 | /* make sure there is space next character */ | | 544 | /* make sure there is space next character */ |
545 | if (cp + 1 >= el->el_line.limit) { | | 545 | if (cp + 1 >= el->el_line.limit) { |
546 | idx = (cp - el->el_line.buffer); | | 546 | idx = (cp - el->el_line.buffer); |
547 | if (!ch_enlargebufs(el, 2)) | | 547 | if (!ch_enlargebufs(el, 2)) |
548 | break; | | 548 | break; |
549 | cp = &el->el_line.buffer[idx]; | | 549 | cp = &el->el_line.buffer[idx]; |
550 | } | | 550 | } |
551 | cp++; | | 551 | cp++; |
552 | crlf = cp[-1] == '\r' || cp[-1] == '\n'; | | 552 | crlf = cp[-1] == '\r' || cp[-1] == '\n'; |
553 | if (el->el_flags & UNBUFFERED) | | 553 | if (el->el_flags & UNBUFFERED) |
554 | break; | | 554 | break; |
| @@ -635,27 +635,27 @@ FUN(el,gets)(EditLine *el, int *nread) | | | @@ -635,27 +635,27 @@ FUN(el,gets)(EditLine *el, int *nread) |
635 | break; | | 635 | break; |
636 | | | 636 | |
637 | case CC_REDISPLAY: | | 637 | case CC_REDISPLAY: |
638 | re_clear_lines(el); | | 638 | re_clear_lines(el); |
639 | re_clear_display(el); | | 639 | re_clear_display(el); |
640 | /* FALLTHROUGH */ | | 640 | /* FALLTHROUGH */ |
641 | | | 641 | |
642 | case CC_REFRESH: | | 642 | case CC_REFRESH: |
643 | re_refresh(el); | | 643 | re_refresh(el); |
644 | break; | | 644 | break; |
645 | | | 645 | |
646 | case CC_REFRESH_BEEP: | | 646 | case CC_REFRESH_BEEP: |
647 | re_refresh(el); | | 647 | re_refresh(el); |
648 | term_beep(el); | | 648 | terminal_beep(el); |
649 | break; | | 649 | break; |
650 | | | 650 | |
651 | case CC_NORM: /* normal char */ | | 651 | case CC_NORM: /* normal char */ |
652 | break; | | 652 | break; |
653 | | | 653 | |
654 | case CC_ARGHACK: /* Suggested by Rich Salz */ | | 654 | case CC_ARGHACK: /* Suggested by Rich Salz */ |
655 | /* <rsalz@pineapple.bbn.com> */ | | 655 | /* <rsalz@pineapple.bbn.com> */ |
656 | continue; /* keep going... */ | | 656 | continue; /* keep going... */ |
657 | | | 657 | |
658 | case CC_EOF: /* end of file typed */ | | 658 | case CC_EOF: /* end of file typed */ |
659 | if ((el->el_flags & UNBUFFERED) == 0) | | 659 | if ((el->el_flags & UNBUFFERED) == 0) |
660 | num = 0; | | 660 | num = 0; |
661 | else if (num == -1) { | | 661 | else if (num == -1) { |
| @@ -676,38 +676,38 @@ FUN(el,gets)(EditLine *el, int *nread) | | | @@ -676,38 +676,38 @@ FUN(el,gets)(EditLine *el, int *nread) |
676 | #endif /* DEBUG_READ */ | | 676 | #endif /* DEBUG_READ */ |
677 | /* put (real) cursor in a known place */ | | 677 | /* put (real) cursor in a known place */ |
678 | re_clear_display(el); /* reset the display stuff */ | | 678 | re_clear_display(el); /* reset the display stuff */ |
679 | ch_reset(el, 1); /* reset the input pointers */ | | 679 | ch_reset(el, 1); /* reset the input pointers */ |
680 | re_refresh(el); /* print the prompt again */ | | 680 | re_refresh(el); /* print the prompt again */ |
681 | break; | | 681 | break; |
682 | | | 682 | |
683 | case CC_ERROR: | | 683 | case CC_ERROR: |
684 | default: /* functions we don't know about */ | | 684 | default: /* functions we don't know about */ |
685 | #ifdef DEBUG_READ | | 685 | #ifdef DEBUG_READ |
686 | (void) fprintf(el->el_errfile, | | 686 | (void) fprintf(el->el_errfile, |
687 | "*** editor ERROR ***\r\n\n"); | | 687 | "*** editor ERROR ***\r\n\n"); |
688 | #endif /* DEBUG_READ */ | | 688 | #endif /* DEBUG_READ */ |
689 | term_beep(el); | | 689 | terminal_beep(el); |
690 | term__flush(el); | | 690 | terminal__flush(el); |
691 | break; | | 691 | break; |
692 | } | | 692 | } |
693 | el->el_state.argument = 1; | | 693 | el->el_state.argument = 1; |
694 | el->el_state.doingarg = 0; | | 694 | el->el_state.doingarg = 0; |
695 | el->el_chared.c_vcmd.action = NOP; | | 695 | el->el_chared.c_vcmd.action = NOP; |
696 | if (el->el_flags & UNBUFFERED) | | 696 | if (el->el_flags & UNBUFFERED) |
697 | break; | | 697 | break; |
698 | } | | 698 | } |
699 | | | 699 | |
700 | term__flush(el); /* flush any buffered output */ | | 700 | terminal__flush(el); /* flush any buffered output */ |
701 | /* make sure the tty is set up correctly */ | | 701 | /* make sure the tty is set up correctly */ |
702 | if ((el->el_flags & UNBUFFERED) == 0) { | | 702 | if ((el->el_flags & UNBUFFERED) == 0) { |
703 | read_finish(el); | | 703 | read_finish(el); |
704 | *nread = num != -1 ? num : 0; | | 704 | *nread = num != -1 ? num : 0; |
705 | } else { | | 705 | } else { |
706 | *nread = (int)(el->el_line.lastchar - el->el_line.buffer); | | 706 | *nread = (int)(el->el_line.lastchar - el->el_line.buffer); |
707 | } | | 707 | } |
708 | goto done; | | 708 | goto done; |
709 | noedit: | | 709 | noedit: |
710 | el->el_line.cursor = el->el_line.lastchar = cp; | | 710 | el->el_line.cursor = el->el_line.lastchar = cp; |
711 | *cp = '\0'; | | 711 | *cp = '\0'; |
712 | *nread = (int)(el->el_line.cursor - el->el_line.buffer); | | 712 | *nread = (int)(el->el_line.cursor - el->el_line.buffer); |
713 | done: | | 713 | done: |