ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/command.C
(Generate patch)

Comparing rxvt-unicode/src/command.C (file contents):
Revision 1.4 by pcg, Tue Nov 25 15:25:16 2003 UTC vs.
Revision 1.11 by pcg, Tue Dec 2 21:49:46 2003 UTC

1/*--------------------------------*-C-*---------------------------------* 1/*--------------------------------*-C-*---------------------------------*
2 * File: command.c 2 * File: command.c
3 *----------------------------------------------------------------------* 3 *----------------------------------------------------------------------*
4 * $Id: command.C,v 1.4 2003/11/25 15:25:16 pcg Exp $ 4 * $Id: command.C,v 1.11 2003/12/02 21:49:46 pcg Exp $
5 * 5 *
6 * All portions of code are copyright by their respective author/s. 6 * All portions of code are copyright by their respective author/s.
7 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk> 7 * Copyright (c) 1992 John Bovey, University of Kent at Canterbury <jdb@ukc.ac.uk>
8 * - original version 8 * - original version
9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com> 9 * Copyright (c) 1994 Robert Nation <nation@rocket.sanders.lockheed.com>
237 /* escape prefix */ 237 /* escape prefix */
238 if (meta) 238 if (meta)
239# ifdef META8_OPTION 239# ifdef META8_OPTION
240 if (R->meta_char == C0_ESC) 240 if (R->meta_char == C0_ESC)
241# endif 241# endif
242 rxvt_tt_write(aR_ &ch, 1); 242 R->tt_write(&ch, 1);
243 rxvt_tt_write(aR_ kbuf0, l); 243 R->tt_write(kbuf0, l);
244 return; 244 return;
245 } else 245 } else
246#endif 246#endif
247 { 247 {
248 newlen = 1; 248 newlen = 1;
605 && (R->meta_char == C0_ESC) 605 && (R->meta_char == C0_ESC)
606#endif 606#endif
607 ) { 607 ) {
608 const unsigned char ch = C0_ESC; 608 const unsigned char ch = C0_ESC;
609 609
610 rxvt_tt_write(aR_ &ch, 1); 610 R->tt_write(&ch, 1);
611 } 611 }
612#ifdef DEBUG_CMD 612#ifdef DEBUG_CMD
613 if (debug_key) { /* Display keyboard buffer contents */ 613 if (debug_key) { /* Display keyboard buffer contents */
614 char *p; 614 char *p;
615 int i; 615 int i;
618 for (i = 0, p = kbuf; i < len; i++, p++) 618 for (i = 0, p = kbuf; i < len; i++, p++)
619 fprintf(stderr, (*p >= ' ' && *p < '\177' ? "%c" : "\\%03o"), *p); 619 fprintf(stderr, (*p >= ' ' && *p < '\177' ? "%c" : "\\%03o"), *p);
620 fprintf(stderr, "'\n"); 620 fprintf(stderr, "'\n");
621 } 621 }
622#endif /* DEBUG_CMD */ 622#endif /* DEBUG_CMD */
623 rxvt_tt_write(aR_ kbuf, (unsigned int)len); 623 R->tt_write(kbuf, (unsigned int)len);
624} 624}
625/*}}} */ 625/*}}} */
626 626
627#if (MENUBAR_MAX) 627#if (MENUBAR_MAX)
628/*{{{ rxvt_cmd_write(), rxvt_cmd_getc() */ 628/*{{{ rxvt_cmd_write(), rxvt_cmd_getc() */
656 return 0; 656 return 0;
657} 657}
658#endif /* MENUBAR_MAX */ 658#endif /* MENUBAR_MAX */
659 659
660void 660void
661rxvt_term::flush ()
662{
663#ifdef TRANSPARENT
664 if (want_full_refresh)
665 {
666 want_full_refresh = 0;
667 rxvt_scr_clear (this);
668 rxvt_scr_touch (this, False);
669 want_refresh = 1;
670 }
671#endif
672
673 if (want_refresh)
674 {
675 rxvt_scr_refresh (this, refresh_type);
676 rxvt_scrollbar_show (this, 1);
677#ifdef USE_XIM
678 rxvt_IMSendSpot (this);
679#endif
680 }
681
682 XFlush (Xdisplay);
683#if 0
684 if (XPending (Xdisplay)) process_x_events ();
685 if (XPending (Xdisplay)) process_x_events ();
686#endif
687}
688
689void
690rxvt_term::check_cb (check_watcher &w)
691{
692 SET_R (this);
693
694 flush ();
695}
696
697void
661rxvt_term::process_x_events () 698rxvt_term::process_x_events ()
662{ 699{
663 do 700 do
664 { 701 {
665 XEvent xev; 702 XEvent xev;
666 703
667 XNextEvent (Xdisplay, &xev); 704 XNextEvent (Xdisplay, &xev);
705
668#if defined(CURSOR_BLINK) 706#if defined(CURSOR_BLINK)
669 if ((Options & Opt_cursorBlink) 707 if ((Options & Opt_cursorBlink)
670 && xev.type == KeyPress) { 708 && xev.type == KeyPress)
709 {
671 if (hidden_cursor) { 710 if (hidden_cursor)
711 {
672 hidden_cursor = 0; 712 hidden_cursor = 0;
673 want_refresh = 1; 713 want_refresh = 1;
674 } 714 }
675 want_keypress_time = 1; 715
716 blink_ev.start (NOW + BLINK_INTERVAL);
676 } 717 }
677#endif 718#endif
678 719
679#if defined(POINTER_BLANK) 720#if defined(POINTER_BLANK)
680 if ((Options & Opt_pointerBlank) 721 if ((Options & Opt_pointerBlank)
681 && (pointerBlankDelay > 0)) { 722 && (pointerBlankDelay > 0))
723 {
682 if (xev.type == MotionNotify 724 if (xev.type == MotionNotify
683 || xev.type == ButtonPress 725 || xev.type == ButtonPress
684 || xev.type == ButtonRelease) { 726 || xev.type == ButtonRelease)
685 if (hidden_pointer) 727 if (hidden_pointer)
686 rxvt_pointer_unblank(aR); 728 pointer_unblank ();
687 want_motion_time = 1; 729
688 }
689 if (xev.type == KeyPress && hidden_pointer == 0) 730 if (xev.type == KeyPress && hidden_pointer == 0)
690 rxvt_pointer_blank (this); 731 pointer_blank ();
691 } 732 }
692#endif 733#endif
693 734
694#ifdef USE_XIM 735#ifdef USE_XIM
695 if (!XFilterEvent (&xev, xev.xany.window)) 736 if (!XFilterEvent (&xev, xev.xany.window))
696#endif 737#endif
698 } 739 }
699 while (XPending (Xdisplay)); 740 while (XPending (Xdisplay));
700} 741}
701 742
702void 743void
744rxvt_term::blink_cb (time_watcher &w)
745{
746 SET_R (this);
747
748 w.at += BLINK_INTERVAL;
749 hidden_cursor = !hidden_cursor;
750 want_refresh = 1;
751}
752
753void
703rxvt_term::x_cb (io_watcher &w, short revents) 754rxvt_term::x_cb (io_watcher &w, short revents)
704{ 755{
756 SET_R (this);
757
705 process_x_events (); 758 process_x_events ();
759}
706 760
707 flush (); 761bool
762rxvt_term::pty_fill ()
763{
764 ssize_t n = cmdbuf_endp - cmdbuf_ptr;
765
766 memmove (cmdbuf_base, cmdbuf_ptr, n);
767 cmdbuf_ptr = cmdbuf_base;
768 cmdbuf_endp = cmdbuf_ptr + n;
769
770 n = read (cmd_fd, cmdbuf_endp, BUFSIZ - n);
771
772 if (n > 0)
773 {
774 cmdbuf_endp += n;
775 return true;
776 }
777 else if (n < 0 && errno == EAGAIN)
778 return false;
779
780 destroy ();
781}
782
783void
784rxvt_term::pty_cb (io_watcher &w, short revents)
785{
786 SET_R (this);
787
788 if (revents & EVENT_WRITE)
789 tt_write (0, 0);
790 else if (revents & EVENT_READ)
791 // loop, but don't allow a single term to monopolize us
792 // the number of loops is fully arbitrary, and thus wrong
793 for (int i = 1; i-- && pty_fill (); )
794 {
795 if (!seen_input)
796 {
797 seen_input = 1;
798 /* once we know the shell is running, send the screen size. Again! */
799 tt_winch ();
800 }
801
802 uint32_t ch = NOCHAR;
803
804 for (;;)
805 {
806 if (ch == NOCHAR)
807 ch = next_char ();
808
809 if (ch == NOCHAR) // TODO: improve
810 break;
811
812 if (ch >= ' ' || ch == '\t' || ch == '\n' || ch == '\r')
813 {
814 /* Read a text string from the input buffer */
815 uint32_t buf[BUFSIZ];
816 bool refreshnow = false;
817 int nlines = 0;
818 uint32_t *str = buf;
819
820 *str++ = ch;
821
822 for (;;)
823 {
824 ch = next_char ();
825
826 if (ch == NOCHAR || (ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r'))
827 break;
828 else
829 {
830 *str++ = ch;
831
832 if (ch == '\n')
833 {
834 nlines++;
835 refresh_count++;
836
837 if (!(Options & Opt_jumpScroll)
838 || (refresh_count >= (refresh_limit * (TermWin.nrow - 1))))
839 {
840 refreshnow = true;
841 ch = NOCHAR;
842 break;
843 }
844 }
845
846 if (str >= buf + BUFSIZ)
847 {
848 ch = NOCHAR;
849 break;
850 }
851 }
852 }
853
854 rxvt_scr_add_lines (this, buf, nlines, str - buf);
855
856 /*
857 * If there have been a lot of new lines, then update the screen
858 * What the heck I'll cheat and only refresh less than every page-full.
859 * the number of pages between refreshes is refresh_limit, which
860 * is incremented here because we must be doing flat-out scrolling.
861 *
862 * refreshing should be correct for small scrolls, because of the
863 * time-out
864 */
865 if (refreshnow)
866 {
867 if ((Options & Opt_jumpScroll) && refresh_limit < REFRESH_PERIOD)
868 refresh_limit++;
869
870 rxvt_scr_refresh (this, refresh_type);
871 }
872
873 }
874 else
875 {
876 switch (ch)
877 {
878 default:
879 rxvt_process_nonprinting (this, ch);
880 break;
881 case C0_ESC: /* escape char */
882 rxvt_process_escape_seq (this);
883 break;
884 /*case 0x9b: */ /* CSI */
885 /* rxvt_process_csi_seq (this); */
886 }
887
888 ch = NOCHAR;
889 }
890 }
891 }
708} 892}
709 893
710// read the next character, currently handles UTF-8 894// read the next character, currently handles UTF-8
711// will probably handle all sorts of other stuff in the future 895// will probably handle all sorts of other stuff in the future
712uint32_t 896uint32_t
760 } 944 }
761 945
762 return NOCHAR; 946 return NOCHAR;
763} 947}
764 948
765bool
766rxvt_term::pty_fill (size_t count)
767{
768 ssize_t n = cmdbuf_endp - cmdbuf_ptr;
769
770 memmove (cmdbuf_base, cmdbuf_ptr, n);
771 cmdbuf_ptr = cmdbuf_base;
772 cmdbuf_endp = cmdbuf_ptr + n;
773
774 n = read (cmd_fd, cmdbuf_endp, count);
775
776 if (n > 0)
777 {
778 cmdbuf_endp += n;
779 return true;
780 }
781 else if (n < 0 && errno == EAGAIN)
782 return false;
783
784 rxvt_clean_exit();
785 exit(EXIT_FAILURE); /* bad order of events? */
786}
787
788void
789rxvt_term::pty_cb (io_watcher &w, short revents)
790{
791 while (pty_fill (BUFSIZ - (cmdbuf_endp - cmdbuf_ptr)))
792 {
793 /* once we know the shell is running, send the screen size. Again! */
794 //ch = rxvt_cmd_getc(aR); /* wait for something */
795 //rxvt_tt_winsize(cmd_fd, TermWin.ncol, TermWin.nrow, cmd_pid);
796
797 uint32_t ch = NOCHAR;
798
799 for (;;)
800 {
801 if (ch == NOCHAR)
802 ch = next_char ();
803
804 if (ch == NOCHAR) // TODO: improve
805 break;
806
807 if (ch >= ' ' || ch == '\t' || ch == '\n' || ch == '\r')
808 {
809 /* Read a text string from the input buffer */
810 uint32_t buf[BUFSIZ];
811 bool refreshnow = false;
812 int nlines = 0;
813 uint32_t *str = buf;
814
815 *str++ = ch;
816
817 for (;;)
818 {
819 ch = next_char ();
820
821 if (ch == NOCHAR || (ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r'))
822 break;
823 else
824 {
825 *str++ = ch;
826
827 if (ch == '\n')
828 {
829 nlines++;
830 refresh_count++;
831
832 if (!(Options & Opt_jumpScroll)
833 || (refresh_count >= (refresh_limit * (TermWin.nrow - 1))))
834 {
835 refreshnow = true;
836 ch = NOCHAR;
837 break;
838 }
839 }
840
841 if (str >= buf + BUFSIZ)
842 {
843 ch = NOCHAR;
844 break;
845 }
846 }
847 }
848
849 rxvt_scr_add_lines (this, buf, nlines, str - buf);
850
851 /*
852 * If there have been a lot of new lines, then update the screen
853 * What the heck I'll cheat and only refresh less than every page-full.
854 * the number of pages between refreshes is refresh_limit, which
855 * is incremented here because we must be doing flat-out scrolling.
856 *
857 * refreshing should be correct for small scrolls, because of the
858 * time-out
859 */
860 if (refreshnow)
861 {
862 if ((Options & Opt_jumpScroll) && refresh_limit < REFRESH_PERIOD)
863 refresh_limit++;
864
865 rxvt_scr_refresh (this, refresh_type);
866 }
867
868 }
869 else
870 {
871 switch (ch)
872 {
873 default:
874 rxvt_process_nonprinting (this, ch);
875 break;
876 case C0_ESC: /* escape char */
877 rxvt_process_escape_seq (this);
878 break;
879 /*case 0x9b: */ /* CSI */
880 /* rxvt_process_csi_seq (this); */
881 }
882
883 ch = NOCHAR;
884 }
885 }
886 }
887
888 flush ();
889}
890
891void
892rxvt_term::flush ()
893{
894#ifdef TRANSPARENT
895 if (want_full_refresh)
896 {
897 want_full_refresh = 0;
898 rxvt_scr_clear (this);
899 rxvt_scr_touch (this, False);
900 want_refresh = 1;
901 }
902#endif
903
904 if (want_refresh)
905 {
906 rxvt_scr_refresh (this, refresh_type);
907 rxvt_scrollbar_show (this, 1);
908#ifdef USE_XIM
909 rxvt_IMSendSpot (this);
910#endif
911 }
912
913 XFlush (Xdisplay);
914}
915
916/* rxvt_cmd_getc() - Return next input character */ 949/* rxvt_cmd_getc() - Return next input character */
917/* 950/*
918 * Return the next input character after first passing any keyboard input 951 * Return the next input character after first passing any keyboard input
919 * to the command. 952 * to the command.
920 */ 953 */
929 return c; 962 return c;
930 963
931 // incomplete sequences should occur rarely, still, a better solution 964 // incomplete sequences should occur rarely, still, a better solution
932 // would be preferred. either setjmp/longjmp or better design. 965 // would be preferred. either setjmp/longjmp or better design.
933 fcntl (R->cmd_fd, F_SETFL, 0); 966 fcntl (R->cmd_fd, F_SETFL, 0);
934 R->pty_fill (1); 967 R->pty_fill ();
935 fcntl (R->cmd_fd, F_SETFL, O_NONBLOCK); 968 fcntl (R->cmd_fd, F_SETFL, O_NONBLOCK);
936 } 969 }
937 970
971#if 0
938#define TIMEOUT_USEC 5000 972#define TIMEOUT_USEC 5000
939 fd_set readfds; 973 fd_set readfds;
940 int quick_timeout, select_res; 974 int quick_timeout, select_res;
941 int want_motion_time, want_keypress_time; 975 int want_motion_time, want_keypress_time;
942 struct timeval value; 976 struct timeval value;
946 980
947 for (;;) { 981 for (;;) {
948 /* loop until we can return something */ 982 /* loop until we can return something */
949 983
950 if (R->v_bufstr < R->v_bufptr) /* output any pending chars */ 984 if (R->v_bufstr < R->v_bufptr) /* output any pending chars */
951 rxvt_tt_write(aR_ NULL, 0); 985 R->tt_write(NULL, 0);
952
953#if defined(POINTER_BLANK) || defined(CURSOR_BLINK)
954 tp.tv_sec = 0; /* presume == 0 implies time not yet retrieved */
955#endif
956#if defined(CURSOR_BLINK)
957 want_keypress_time = 0;
958#endif
959#if defined(POINTER_BLANK)
960 want_motion_time = 0;
961#endif
962
963 if (XPending (R->Xdisplay))
964 {
965 R->process_x_events ();
966
967 /* in case button actions pushed chars to cmdbuf */
968 if (R->cmdbuf_ptr < R->cmdbuf_endp)
969 return *R->cmdbuf_ptr++;
970 }
971
972#if defined(CURSOR_BLINK)
973 if (want_keypress_time) {
974 (void)gettimeofday(&tp, NULL);
975 R->lastcursorchange.tv_sec = tp.tv_sec;
976 R->lastcursorchange.tv_usec = tp.tv_usec;
977 }
978#endif
979#if defined(POINTER_BLANK)
980 if (want_motion_time) {
981 if (!tp.tv_sec)
982 (void)gettimeofday(&tp, NULL);
983 R->lastmotion.tv_sec = tp.tv_sec;
984 R->lastmotion.tv_usec = tp.tv_usec;
985 }
986#endif
987
988/*
989 * the command input buffer is empty and we have no pending X events
990 */
991 quick_timeout = 0;
992 986
993#if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING) 987#if defined(MOUSE_WHEEL) && defined(MOUSE_SLIP_WHEELING)
994 if (R->mouse_slip_wheel_speed) { 988 if (R->mouse_slip_wheel_speed) {
995 quick_timeout = 1; 989 quick_timeout = 1;
996 if (!R->mouse_slip_wheel_delay-- 990 if (!R->mouse_slip_wheel_delay--
1026 R->want_refresh = 1; 1020 R->want_refresh = 1;
1027 } 1021 }
1028 } 1022 }
1029#endif /* NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING */ 1023#endif /* NO_SCROLLBAR_BUTTON_CONTINUAL_SCROLLING */
1030 1024
1031 FD_ZERO(&readfds);
1032 FD_SET(R->cmd_fd, &readfds);
1033 FD_SET(R->Xfd, &readfds);
1034 value.tv_usec = TIMEOUT_USEC;
1035 value.tv_sec = 0;
1036
1037 if (!R->TermWin.mapped)
1038 quick_timeout = 0;
1039 else {
1040 quick_timeout |= R->want_refresh;
1041#ifdef TRANSPARENT 1025#ifdef TRANSPARENT
1042 quick_timeout |= R->want_full_refresh; 1026 quick_timeout |= R->want_full_refresh;
1043#endif 1027#endif
1044 }
1045
1046#if defined(POINTER_BLANK) || defined(CURSOR_BLINK)
1047 {
1048 int set_quick_timeout = 0;
1049 long csdiff, psdiff;
1050
1051#define BLINK_TIME 500000L
1052 csdiff = psdiff = 60000000L; /* or, say, LONG_MAX */
1053# if defined(CURSOR_BLINK)
1054 if (R->Options & Opt_cursorBlink) {
1055 if (!tp.tv_sec) /* didn't get it before so get it now */
1056 (void)gettimeofday(&tp, NULL);
1057
1058 csdiff = (tp.tv_sec - R->lastcursorchange.tv_sec) * 1000000L
1059 + tp.tv_usec - R->lastcursorchange.tv_usec;
1060 if (csdiff > BLINK_TIME) { /* XXX: settable blink times */
1061 R->lastcursorchange.tv_sec = tp.tv_sec;
1062 R->lastcursorchange.tv_usec = tp.tv_usec;
1063 R->hidden_cursor = !R->hidden_cursor;
1064 csdiff = 0;
1065 } else
1066 csdiff = BLINK_TIME - csdiff;
1067 set_quick_timeout = 1;
1068 }
1069# endif 1028#endif
1070# if defined(POINTER_BLANK)
1071 /*
1072 * If we haven't moved the pointer for a while
1073 */
1074 if ((R->Options & Opt_pointerBlank)
1075 && (R->pointerBlankDelay > 0)
1076 && (R->hidden_pointer == 0)) {
1077 long pdelay;
1078
1079 if (!tp.tv_sec) /* didn't get it before so get it now */
1080 (void)gettimeofday(&tp, NULL);
1081 psdiff = (tp.tv_sec - R->lastmotion.tv_sec) * 1000000L
1082 + tp.tv_usec - R->lastmotion.tv_usec;
1083 pdelay = R->pointerBlankDelay * 1000000L;
1084 if (psdiff >= pdelay)
1085 rxvt_pointer_blank(aR);
1086 else {
1087 set_quick_timeout = 1;
1088 psdiff = pdelay - psdiff;
1089 }
1090 }
1091# endif
1092 if (!quick_timeout && set_quick_timeout) {
1093 MIN_IT(csdiff, psdiff);
1094 value.tv_sec = csdiff / 1000000L;
1095 value.tv_usec = csdiff % 1000000L;
1096 quick_timeout = 1;
1097 }
1098 }
1099#endif
1100
1101 if ((select_res = select(R->num_fds, &readfds, NULL, NULL,
1102 (quick_timeout ? &value : NULL))) == 0) {
1103 /* select statement timed out - we're not hard and fast scrolling */
1104 R->refresh_limit = 1;
1105 }
1106#if defined(CURSOR_BLINK)
1107 if (R->Options & Opt_cursorBlink)
1108 R->want_refresh = 1;
1109#endif
1110 }
1111} 1029}
1112 1030
1113/* EXTPROTO */
1114void 1031void
1115rxvt_pointer_unblank(pR) 1032rxvt_term::pointer_unblank ()
1116{ 1033{
1117 XDefineCursor(R->Xdisplay, R->TermWin.vt, R->TermWin_cursor); 1034 XDefineCursor (Xdisplay, TermWin.vt, TermWin_cursor);
1118 rxvt_recolour_cursor(aR); 1035 rxvt_recolour_cursor (this);
1036
1119#ifdef POINTER_BLANK 1037#ifdef POINTER_BLANK
1120 R->hidden_pointer = 0; 1038 hidden_pointer = 0;
1121 if (R->pointerBlankDelay > 0) {
1122 struct timeval tp;
1123 1039
1124 (void)gettimeofday(&tp, NULL); 1040 if (Options & Opt_pointerBlank)
1125 R->lastmotion.tv_sec = tp.tv_sec; 1041 pointer_ev.start (NOW + pointerBlankDelay);
1126 R->lastmotion.tv_usec = tp.tv_usec;
1127 }
1128#endif 1042#endif
1129} 1043}
1130 1044
1131#ifdef POINTER_BLANK 1045#ifdef POINTER_BLANK
1132/* INTPROTO */
1133void 1046void
1134rxvt_pointer_blank(pR) 1047rxvt_term::pointer_blank ()
1135{ 1048{
1049 pointer_ev.stop ();
1050
1051 if (!(Options & Opt_pointerBlank))
1052 return;
1053
1136 XDefineCursor(R->Xdisplay, R->TermWin.vt, R->pointer_blank); 1054 XDefineCursor (Xdisplay, TermWin.vt, blank_cursor);
1137 XFlush(R->Xdisplay); 1055 XFlush (Xdisplay);
1056
1138 R->hidden_pointer = 1; 1057 hidden_pointer = 1;
1058}
1059
1060void
1061rxvt_term::pointer_cb (time_watcher &w)
1062{
1063 SET_R (this);
1064
1065 pointer_blank ();
1139} 1066}
1140#endif 1067#endif
1141 1068
1142/* INTPROTO */ 1069/* INTPROTO */
1143void 1070void
1196 fprintf(stderr, "]: <%d>, %d/%d\n", 1123 fprintf(stderr, "]: <%d>, %d/%d\n",
1197 button_number, 1124 button_number,
1198 x + 1, 1125 x + 1,
1199 y + 1); 1126 y + 1);
1200#else 1127#else
1201 rxvt_tt_printf(aR_ "\033[M%c%c%c", 1128 R->tt_printf("\033[M%c%c%c",
1202 (32 + button_number + key_state), 1129 (32 + button_number + key_state),
1203 (32 + x + 1), 1130 (32 + x + 1),
1204 (32 + y + 1)); 1131 (32 + y + 1));
1205#endif 1132#endif
1206} 1133}
1339 break; 1266 break;
1340 1267
1341 case ClientMessage: 1268 case ClientMessage:
1342 if (ev->xclient.format == 32 1269 if (ev->xclient.format == 32
1343 && (Atom)ev->xclient.data.l[0] == R->xa[XA_WMDELETEWINDOW]) 1270 && (Atom)ev->xclient.data.l[0] == R->xa[XA_WMDELETEWINDOW])
1344 exit(EXIT_SUCCESS); 1271 R->destroy ();
1345#ifdef OFFIX_DND 1272#ifdef OFFIX_DND
1346 /* OffiX Dnd (drag 'n' drop) protocol */ 1273 /* OffiX Dnd (drag 'n' drop) protocol */
1347 if (ev->xclient.message_type == R->xa[XA_DNDPROTOCOL] 1274 else if (ev->xclient.message_type == R->xa[XA_DNDPROTOCOL]
1348 && (ev->xclient.data.l[0] == DndFile 1275 && (ev->xclient.data.l[0] == DndFile
1349 || ev->xclient.data.l[0] == DndDir 1276 || ev->xclient.data.l[0] == DndDir
1350 || ev->xclient.data.l[0] == DndLink)) { 1277 || ev->xclient.data.l[0] == DndLink)) {
1351 /* Get Dnd data */ 1278 /* Get Dnd data */
1352 Atom ActualType; 1279 Atom ActualType;
1384 * in the GraphicsExpose parts, which means that after each refresh, 1311 * in the GraphicsExpose parts, which means that after each refresh,
1385 * we need to wait for the graphics expose or Noexpose events, 1312 * we need to wait for the graphics expose or Noexpose events,
1386 * which ought to make things real slow! 1313 * which ought to make things real slow!
1387 */ 1314 */
1388 case VisibilityNotify: 1315 case VisibilityNotify:
1389 switch (ev->xvisibility.state) { 1316 switch (ev->xvisibility.state)
1317 {
1390 case VisibilityUnobscured: 1318 case VisibilityUnobscured:
1391 R->refresh_type = FAST_REFRESH; 1319 R->refresh_type = FAST_REFRESH;
1392 break; 1320 break;
1393 case VisibilityPartiallyObscured: 1321 case VisibilityPartiallyObscured:
1394 R->refresh_type = SLOW_REFRESH; 1322 R->refresh_type = SLOW_REFRESH;
1395 break; 1323 break;
1396 default: 1324 default:
1397 R->refresh_type = NO_REFRESH; 1325 R->refresh_type = NO_REFRESH;
1398 break; 1326 break;
1399 } 1327 }
1400 break; 1328 break;
1401 1329
1402 case FocusIn: 1330 case FocusIn:
1403 if (!R->TermWin.focus) { 1331 if (!R->TermWin.focus) {
1404 R->TermWin.focus = 1; 1332 R->TermWin.focus = 1;
1405 R->want_refresh = 1; 1333 R->want_refresh = 1;
1406#ifdef USE_XIM 1334#ifdef USE_XIM
1407 if (R->Input_Context != NULL) 1335 if (R->Input_Context != NULL)
1408 XSetICFocus(R->Input_Context); 1336 XSetICFocus(R->Input_Context);
1409#endif 1337#endif
1338#ifdef CURSOR_BLINK
1339 if (R->Options & Opt_cursorBlink)
1340 R->blink_ev.start (NOW + BLINK_INTERVAL);
1341#endif
1410 } 1342 }
1411 break; 1343 break;
1412 1344
1413 case FocusOut: 1345 case FocusOut:
1414 if (R->TermWin.focus) { 1346 if (R->TermWin.focus) {
1415 R->TermWin.focus = 0; 1347 R->TermWin.focus = 0;
1416 R->want_refresh = 1; 1348 R->want_refresh = 1;
1417#ifdef USE_XIM 1349#ifdef USE_XIM
1418 if (R->Input_Context != NULL) 1350 if (R->Input_Context != NULL)
1419 XUnsetICFocus(R->Input_Context); 1351 XUnsetICFocus(R->Input_Context);
1352#endif
1353#ifdef CURSOR_BLINK
1354 if (R->Options & Opt_cursorBlink)
1355 R->blink_ev.stop ();
1356 R->hidden_cursor = 0;
1420#endif 1357#endif
1421 } 1358 }
1422 break; 1359 break;
1423 1360
1424 case ConfigureNotify: 1361 case ConfigureNotify:
1537 break; 1474 break;
1538 1475
1539 case MotionNotify: 1476 case MotionNotify:
1540#ifdef POINTER_BLANK 1477#ifdef POINTER_BLANK
1541 if (R->hidden_pointer) 1478 if (R->hidden_pointer)
1542 rxvt_pointer_unblank(aR); 1479 R->pointer_unblank ();
1543#endif 1480#endif
1544#if MENUBAR 1481#if MENUBAR
1545 if (isMenuBarWindow(ev->xany.window)) { 1482 if (isMenuBarWindow(ev->xany.window)) {
1546 rxvt_menubar_control(aR_ &(ev->xbutton)); 1483 rxvt_menubar_control(aR_ &(ev->xbutton));
1547 break; 1484 break;
1738 */ 1675 */
1739 if ((R->scrollBar.style == R_SB_NEXT 1676 if ((R->scrollBar.style == R_SB_NEXT
1740 && scrollbarnext_upButton(ev->y)) 1677 && scrollbarnext_upButton(ev->y))
1741 || (R->scrollBar.style == R_SB_RXVT 1678 || (R->scrollBar.style == R_SB_RXVT
1742 && scrollbarrxvt_upButton(ev->y))) 1679 && scrollbarrxvt_upButton(ev->y)))
1743 rxvt_tt_printf(aR_ "\033[A"); 1680 R->tt_printf("\033[A");
1744 else if ((R->scrollBar.style == R_SB_NEXT 1681 else if ((R->scrollBar.style == R_SB_NEXT
1745 && scrollbarnext_dnButton(ev->y)) 1682 && scrollbarnext_dnButton(ev->y))
1746 || (R->scrollBar.style == R_SB_RXVT 1683 || (R->scrollBar.style == R_SB_RXVT
1747 && scrollbarrxvt_dnButton(ev->y))) 1684 && scrollbarrxvt_dnButton(ev->y)))
1748 rxvt_tt_printf(aR_ "\033[B"); 1685 R->tt_printf("\033[B");
1749 else 1686 else
1750 switch (ev->button) { 1687 switch (ev->button) {
1751 case Button2: 1688 case Button2:
1752 rxvt_tt_printf(aR_ "\014"); 1689 R->tt_printf("\014");
1753 break; 1690 break;
1754 case Button1: 1691 case Button1:
1755 rxvt_tt_printf(aR_ "\033[6~"); 1692 R->tt_printf("\033[6~");
1756 break; 1693 break;
1757 case Button3: 1694 case Button3:
1758 rxvt_tt_printf(aR_ "\033[5~"); 1695 R->tt_printf("\033[5~");
1759 break; 1696 break;
1760 } 1697 }
1761 } 1698 }
1762 else 1699 else
1763#endif /* NO_SCROLLBAR_REPORT */ 1700#endif /* NO_SCROLLBAR_REPORT */
2164 return stream; 2101 return stream;
2165} 2102}
2166 2103
2167/* EXTPROTO */ 2104/* EXTPROTO */
2168int 2105int
2169rxvt_pclose_printer(FILE *stream) 2106rxvt_pclose_printer (FILE *stream)
2170{ 2107{
2171 fflush(stream); 2108 fflush (stream);
2172/* pclose() reported not to work on SunOS 4.1.3 */ 2109/* pclose() reported not to work on SunOS 4.1.3 */
2173# if defined (__sun__) /* TODO: RESOLVE THIS */ 2110# if defined (__sun__) /* TODO: RESOLVE THIS */
2174/* pclose works provided SIGCHLD handler uses waitpid */ 2111/* pclose works provided SIGCHLD handler uses waitpid */
2175 return pclose(stream); /* return fclose (stream); */ 2112 return pclose (stream); /* return fclose (stream); */
2176# else 2113# else
2177 return pclose(stream); 2114 return pclose (stream);
2178# endif 2115# endif
2179} 2116}
2180 2117
2181/* 2118/*
2182 * simulate attached vt100 printer 2119 * simulate attached vt100 printer
2245rxvt_process_nonprinting(pR_ unsigned char ch) 2182rxvt_process_nonprinting(pR_ unsigned char ch)
2246{ 2183{
2247 switch (ch) { 2184 switch (ch) {
2248 case C0_ENQ: /* terminal Status */ 2185 case C0_ENQ: /* terminal Status */
2249 if (R->rs[Rs_answerbackstring]) 2186 if (R->rs[Rs_answerbackstring])
2250 rxvt_tt_write(aR_ 2187 R->tt_write(
2251 (const unsigned char *)R->rs[Rs_answerbackstring], 2188 (const unsigned char *)R->rs[Rs_answerbackstring],
2252 (unsigned int)STRLEN(R->rs[Rs_answerbackstring])); 2189 (unsigned int)STRLEN(R->rs[Rs_answerbackstring]));
2253 else 2190 else
2254 rxvt_tt_write(aR_ (unsigned char *)VT100_ANS, 2191 R->tt_write((unsigned char *)VT100_ANS,
2255 (unsigned int)STRLEN(VT100_ANS)); 2192 (unsigned int)STRLEN(VT100_ANS));
2256 break; 2193 break;
2257 case C0_BEL: /* bell */ 2194 case C0_BEL: /* bell */
2258 rxvt_scr_bell(aR); 2195 rxvt_scr_bell(aR);
2259 break; 2196 break;
2322 row = rxvt_cmd_getc(aR) - ' '; 2259 row = rxvt_cmd_getc(aR) - ' ';
2323 col = rxvt_cmd_getc(aR) - ' '; 2260 col = rxvt_cmd_getc(aR) - ' ';
2324 rxvt_scr_gotorc(aR_ row, col, 0); 2261 rxvt_scr_gotorc(aR_ row, col, 0);
2325 break; 2262 break;
2326 case 'Z': /* identify the terminal type */ 2263 case 'Z': /* identify the terminal type */
2327 rxvt_tt_printf(aR_ "\033/Z"); /* I am a VT100 emulating a VT52 */ 2264 R->tt_printf("\033/Z"); /* I am a VT100 emulating a VT52 */
2328 break; 2265 break;
2329 case '<': /* turn off VT52 mode */ 2266 case '<': /* turn off VT52 mode */
2330 PrivMode(0, PrivMode_vt52); 2267 PrivMode(0, PrivMode_vt52);
2331 break; 2268 break;
2332 case 'F': /* use special graphics character set */ 2269 case 'F': /* use special graphics character set */
2439 rxvt_process_dcs_seq(aR); 2376 rxvt_process_dcs_seq(aR);
2440 break; 2377 break;
2441 2378
2442 /* 8.3.110: SINGLE CHARACTER INTRODUCER */ 2379 /* 8.3.110: SINGLE CHARACTER INTRODUCER */
2443 case C1_SCI: /* ESC Z */ 2380 case C1_SCI: /* ESC Z */
2444 rxvt_tt_write(aR_ (const unsigned char *)ESCZ_ANSWER, 2381 R->tt_write((const unsigned char *)ESCZ_ANSWER,
2445 (unsigned int)(sizeof(ESCZ_ANSWER) - 1)); 2382 (unsigned int)(sizeof(ESCZ_ANSWER) - 1));
2446 break; /* steal obsolete ESC [ c */ 2383 break; /* steal obsolete ESC [ c */
2447 2384
2448 /* 8.3.16: CONTROL SEQUENCE INTRODUCER */ 2385 /* 8.3.16: CONTROL SEQUENCE INTRODUCER */
2449 case C1_CSI: /* ESC [ */ 2386 case C1_CSI: /* ESC [ */
2570 */ 2507 */
2571 if (priv) { 2508 if (priv) {
2572 switch (priv) { 2509 switch (priv) {
2573 case '>': 2510 case '>':
2574 if (ch == CSI_DA) /* secondary device attributes */ 2511 if (ch == CSI_DA) /* secondary device attributes */
2575 rxvt_tt_printf(aR_ "\033[>%d;%-.8s;0c", 'R', VSTRING); 2512 R->tt_printf("\033[>%d;%-.8s;0c", 'R', VSTRING);
2576 break; 2513 break;
2577 case '?': 2514 case '?':
2578 if (ch == 'h' || ch == 'l' || ch == 'r' || ch == 's' || ch == 't') 2515 if (ch == 'h' || ch == 'l' || ch == 'r' || ch == 's' || ch == 't')
2579 rxvt_process_terminal_mode(aR_ ch, priv, nargs, arg); 2516 rxvt_process_terminal_mode(aR_ ch, priv, nargs, arg);
2580 break; 2517 break;
2687 case CSI_SU: /* 8.3.148: (1) SCROLL UP */ 2624 case CSI_SU: /* 8.3.148: (1) SCROLL UP */
2688 rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, arg[0], 0); 2625 rxvt_scroll_text(aR_ R->screen.tscroll, R->screen.bscroll, arg[0], 0);
2689 break; 2626 break;
2690 2627
2691 case CSI_DA: /* 8.3.24: (0) DEVICE ATTRIBUTES */ 2628 case CSI_DA: /* 8.3.24: (0) DEVICE ATTRIBUTES */
2692 rxvt_tt_write(aR_ (const unsigned char *)VT100_ANS, 2629 R->tt_write((const unsigned char *)VT100_ANS,
2693 (unsigned int)(sizeof(VT100_ANS) - 1)); 2630 (unsigned int)(sizeof(VT100_ANS) - 1));
2694 break; 2631 break;
2695 2632
2696 case CSI_SGR: /* 8.3.118: (0) SELECT GRAPHIC RENDITION */ 2633 case CSI_SGR: /* 8.3.118: (0) SELECT GRAPHIC RENDITION */
2697 rxvt_process_sgr_mode(aR_ nargs, arg); 2634 rxvt_process_sgr_mode(aR_ nargs, arg);
2698 break; 2635 break;
2699 2636
2700 case CSI_DSR: /* 8.3.36: (0) DEVICE STATUS REPORT */ 2637 case CSI_DSR: /* 8.3.36: (0) DEVICE STATUS REPORT */
2701 switch (arg[0]) { 2638 switch (arg[0]) {
2702 case 5: /* DSR requested */ 2639 case 5: /* DSR requested */
2703 rxvt_tt_printf(aR_ "\033[0n"); 2640 R->tt_printf("\033[0n");
2704 break; 2641 break;
2705 case 6: /* CPR requested */ 2642 case 6: /* CPR requested */
2706 rxvt_scr_report_position(aR); 2643 rxvt_scr_report_position(aR);
2707 break; 2644 break;
2708#if defined (ENABLE_DISPLAY_ANSWER) 2645#if defined (ENABLE_DISPLAY_ANSWER)
2709 case 7: /* unofficial extension */ 2646 case 7: /* unofficial extension */
2710 rxvt_tt_printf(aR_ "%-.250s\n", R->rs[Rs_display_name]); 2647 R->tt_printf("%-.250s\n", R->rs[Rs_display_name]);
2711 break; 2648 break;
2712#endif 2649#endif
2713 case 8: /* unofficial extension */ 2650 case 8: /* unofficial extension */
2714 rxvt_xterm_seq(aR_ XTerm_title, APL_NAME "-" VERSION, CHAR_ST); 2651 rxvt_xterm_seq(aR_ XTerm_title, APL_NAME "-" VERSION, CHAR_ST);
2715 break; 2652 break;
2784 break; 2721 break;
2785#endif 2722#endif
2786 2723
2787 case CSI_78: /* DECREQTPARM */ 2724 case CSI_78: /* DECREQTPARM */
2788 if (arg[0] == 0 || arg[0] == 1) 2725 if (arg[0] == 0 || arg[0] == 1)
2789 rxvt_tt_printf(aR_ "\033[%d;1;1;112;112;1;0x", arg[0] + 2); 2726 R->tt_printf("\033[%d;1;1;112;112;1;0x", arg[0] + 2);
2790 /* FALLTHROUGH */ 2727 /* FALLTHROUGH */
2791 2728
2792 default: 2729 default:
2793 break; 2730 break;
2794 } 2731 }
2848 /* 2785 /*
2849 * reports - some output format copied from XTerm 2786 * reports - some output format copied from XTerm
2850 */ 2787 */
2851 case 11: /* report window state */ 2788 case 11: /* report window state */
2852 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr); 2789 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr);
2853 rxvt_tt_printf(aR_ "\033[%dt", wattr.map_state == IsViewable ? 1 : 2); 2790 R->tt_printf("\033[%dt", wattr.map_state == IsViewable ? 1 : 2);
2854 break; 2791 break;
2855 case 13: /* report window position */ 2792 case 13: /* report window position */
2856 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr); 2793 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr);
2857 XTranslateCoordinates(R->Xdisplay, R->TermWin.parent[0], wattr.root, 2794 XTranslateCoordinates(R->Xdisplay, R->TermWin.parent[0], wattr.root,
2858 -wattr.border_width, -wattr.border_width, 2795 -wattr.border_width, -wattr.border_width,
2859 &x, &y, &wdummy); 2796 &x, &y, &wdummy);
2860 rxvt_tt_printf(aR_ "\033[3;%d;%dt", x, y); 2797 R->tt_printf("\033[3;%d;%dt", x, y);
2861 break; 2798 break;
2862 case 14: /* report window size (pixels) */ 2799 case 14: /* report window size (pixels) */
2863 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr); 2800 XGetWindowAttributes(R->Xdisplay, R->TermWin.parent[0], &wattr);
2864 rxvt_tt_printf(aR_ "\033[4;%d;%dt", wattr.height, wattr.width); 2801 R->tt_printf("\033[4;%d;%dt", wattr.height, wattr.width);
2865 break; 2802 break;
2866 case 18: /* report window size (chars) */ 2803 case 18: /* report window size (chars) */
2867 rxvt_tt_printf(aR_ "\033[8;%d;%dt", R->TermWin.nrow, R->TermWin.ncol); 2804 R->tt_printf("\033[8;%d;%dt", R->TermWin.nrow, R->TermWin.ncol);
2868 break; 2805 break;
2869#if 0 /* XXX: currently disabled due to security concerns */ 2806#if 0 /* XXX: currently disabled due to security concerns */
2870 case 20: /* report icon label */ 2807 case 20: /* report icon label */
2871 XGetIconName(R->Xdisplay, R->TermWin.parent[0], &s); 2808 XGetIconName(R->Xdisplay, R->TermWin.parent[0], &s);
2872 rxvt_tt_printf(aR_ "\033]L%-.200s\234", s ? s : ""); /* 8bit ST */ 2809 R->tt_printf("\033]L%-.200s\234", s ? s : ""); /* 8bit ST */
2873 break; 2810 break;
2874 case 21: /* report window title */ 2811 case 21: /* report window title */
2875 XFetchName(R->Xdisplay, R->TermWin.parent[0], &s); 2812 XFetchName(R->Xdisplay, R->TermWin.parent[0], &s);
2876 rxvt_tt_printf(aR_ "\033]l%-.200s\234", s ? s : ""); /* 8bit ST */ 2813 R->tt_printf("\033]l%-.200s\234", s ? s : ""); /* 8bit ST */
2877 break; 2814 break;
2878#endif 2815#endif
2879 } 2816 }
2880} 2817}
2881#endif 2818#endif
3424{ 3361{
3425 unsigned char ch, cmd = rxvt_cmd_getc(aR); 3362 unsigned char ch, cmd = rxvt_cmd_getc(aR);
3426 3363
3427#ifndef RXVT_GRAPHICS 3364#ifndef RXVT_GRAPHICS
3428 if (cmd == 'Q') { /* query graphics */ 3365 if (cmd == 'Q') { /* query graphics */
3429 rxvt_tt_printf(aR_ "\033G0\n"); /* no graphics */ 3366 R->tt_printf("\033G0\n"); /* no graphics */
3430 return; 3367 return;
3431 } 3368 }
3432/* swallow other graphics sequences until terminating ':' */ 3369/* swallow other graphics sequences until terminating ':' */
3433 do 3370 do
3434 ch = rxvt_cmd_getc(aR); 3371 ch = rxvt_cmd_getc(aR);
3437 unsigned int nargs; 3374 unsigned int nargs;
3438 int args[NGRX_PTS]; 3375 int args[NGRX_PTS];
3439 unsigned char *text = NULL; 3376 unsigned char *text = NULL;
3440 3377
3441 if (cmd == 'Q') { /* query graphics */ 3378 if (cmd == 'Q') { /* query graphics */
3442 rxvt_tt_printf(aR_ "\033G1\n"); /* yes, graphics (color) */ 3379 R->tt_printf("\033G1\n"); /* yes, graphics (color) */
3443 return; 3380 return;
3444 } 3381 }
3445 for (nargs = 0; nargs < (sizeof(args) / sizeof(args[0])) - 1;) { 3382 for (nargs = 0; nargs < (sizeof(args) / sizeof(args[0])) - 1;) {
3446 int neg; 3383 int neg;
3447 3384
3481 3418
3482/* 3419/*
3483 * Send printf() formatted output to the command. 3420 * Send printf() formatted output to the command.
3484 * Only use for small amounts of data. 3421 * Only use for small amounts of data.
3485 */ 3422 */
3486/* EXTPROTO */
3487void 3423void
3488rxvt_tt_printf(pR_ const char *fmt,...) 3424rxvt_term::tt_printf (const char *fmt,...)
3489{ 3425{
3490 va_list arg_ptr; 3426 va_list arg_ptr;
3491 unsigned char buf[256]; 3427 unsigned char buf[256];
3492 3428
3493 va_start(arg_ptr, fmt); 3429 va_start (arg_ptr, fmt);
3494 vsprintf((char *)buf, fmt, arg_ptr); 3430 vsnprintf ((char *)buf, 256, fmt, arg_ptr);
3495 va_end(arg_ptr); 3431 va_end (arg_ptr);
3496 rxvt_tt_write(aR_ buf, (unsigned int)STRLEN(buf)); 3432 tt_write (buf, STRLEN (buf));
3497} 3433}
3498 3434
3499/* ---------------------------------------------------------------------- */ 3435/* ---------------------------------------------------------------------- */
3500/* Addresses pasting large amounts of data and rxvt hang
3501 * code pinched from xterm (v_write()) and applied originally to
3502 * rxvt-2.18 - Hops
3503 * Write data to the pty as typed by the user, pasted with the mouse, 3436/* Write data to the pty as typed by the user, pasted with the mouse,
3504 * or generated by us in response to a query ESC sequence. 3437 * or generated by us in response to a query ESC sequence.
3505 */ 3438 */
3506/* EXTPROTO */
3507void 3439void
3508rxvt_tt_write(pR_ const unsigned char *d, unsigned int len) 3440rxvt_term::tt_write (const unsigned char *data, unsigned int len)
3509{ 3441{
3510#define MAX_PTY_WRITE 256 /* POSIX minimum MAX_INPUT */ 3442 enum { MAX_PTY_WRITE = 255 }; // minimum MAX_INPUT
3511 int riten;
3512 unsigned int p;
3513 unsigned char *v_buffer, /* start of physical buffer */
3514 *v_bufstr, /* start of current buffer pending */
3515 *v_bufptr, /* end of current buffer pending */
3516 *v_bufend; /* end of physical buffer */
3517 3443
3518 if (R->v_bufstr == NULL && len > 0) { 3444 if (len)
3519 p = (len / MAX_PTY_WRITE + 1) * MAX_PTY_WRITE;
3520 v_buffer = v_bufstr = v_bufptr = (unsigned char *)rxvt_malloc(p);
3521 v_bufend = v_buffer + p;
3522 } else {
3523 v_buffer = R->v_buffer;
3524 v_bufstr = R->v_bufstr;
3525 v_bufptr = R->v_bufptr;
3526 v_bufend = R->v_bufend;
3527 } 3445 {
3446 if (v_buflen == 0)
3447 {
3448 int written = write (cmd_fd, data, min (MAX_PTY_WRITE, len));
3449 if (written == len)
3450 return;
3528 3451
3529 /* 3452 data += written;
3530 * Append to the block we already have. Always doing this simplifies the 3453 len -= written;
3531 * code, and isn't too bad, either. If this is a short block, it isn't 3454 }
3532 * too expensive, and if this is a long block, we won't be able to write 3455
3533 * it all anyway. 3456
3534 */ 3457 v_buffer = (unsigned char *)realloc (v_buffer, v_buflen + len);
3535 if (len > 0) { 3458
3536 if (v_bufend < v_bufptr + len) { /* run out of room */ 3459 memcpy (v_buffer + v_buflen, data, len);
3537 if (v_bufstr != v_buffer) { 3460 v_buflen += len;
3538 /* there is unused space, move everything down */
3539 MEMMOVE(v_buffer, v_bufstr,
3540 (unsigned int)(v_bufptr - v_bufstr));
3541 v_bufptr -= v_bufstr - v_buffer;
3542 v_bufstr = v_buffer;
3543 } 3461 }
3544 if (v_bufend < v_bufptr + len) {
3545 /* still won't fit: get more space */
3546 /* use most basic realloc because an error is not fatal. */
3547 unsigned int size = v_bufptr - v_buffer;
3548 unsigned int reallocto;
3549 3462
3550 reallocto = ((size + len) / MAX_PTY_WRITE + 1) * MAX_PTY_WRITE; 3463 for (;;)
3551 v_buffer = (unsigned char *)realloc(v_buffer, reallocto); 3464 {
3552 /* save across realloc */ 3465 int written = write (cmd_fd, v_buffer, min (MAX_PTY_WRITE, v_buflen));
3553 if (v_buffer) { 3466
3554 v_bufstr = v_buffer; 3467 if (written > 0)
3555 v_bufptr = v_buffer + size; 3468 {
3556 v_bufend = v_buffer + reallocto; 3469 v_buflen -= written;
3557 } else { /* no memory: ignore entire write request */ 3470
3558 rxvt_print_error("data loss: cannot allocate buffer space"); 3471 if (v_buflen == 0)
3559 v_buffer = v_bufstr; /* restore clobbered pointer */ 3472 {
3560 } 3473 free (v_buffer);
3474 v_buffer = 0;
3475 v_buflen = 0;
3476
3477 pty_ev.set (EVENT_READ);
3478 return;
3479 }
3480
3481 memmove (v_buffer, v_buffer + written, v_buflen);
3482 }
3483 else if (written != -1 || (errno != EAGAIN && errno != EINTR))
3484 // original code just ignores this...
3485 destroy ();
3486 else
3487 {
3488 pty_ev.set (EVENT_READ | EVENT_WRITE);
3489 return;
3490 }
3561 } 3491 }
3562 }
3563 if (v_bufend >= v_bufptr + len) { /* new stuff will fit */
3564 MEMCPY(v_bufptr, d, len);
3565 v_bufptr += len;
3566 }
3567 }
3568
3569 /*
3570 * Write out as much of the buffer as we can.
3571 * Be careful not to overflow the pty's input silo.
3572 * We are conservative here and only write a small amount at a time.
3573 *
3574 * If we can't push all the data into the pty yet, we expect write
3575 * to return a non-negative number less than the length requested
3576 * (if some data written) or -1 and set errno to EAGAIN,
3577 * EWOULDBLOCK, or EINTR (if no data written).
3578 *
3579 * (Not all systems do this, sigh, so the code is actually
3580 * a little more forgiving.)
3581 */
3582 if ((p = v_bufptr - v_bufstr) > 0) {
3583 riten = write(R->cmd_fd, v_bufstr, min(p, MAX_PTY_WRITE));
3584 if (riten < 0)
3585 riten = 0;
3586 v_bufstr += riten;
3587 if (v_bufstr >= v_bufptr) /* we wrote it all */
3588 v_bufstr = v_bufptr = v_buffer;
3589 }
3590 /*
3591 * If we have lots of unused memory allocated, return it
3592 */
3593 if (v_bufend - v_bufptr > MAX_PTY_WRITE * 8) { /* arbitrary hysteresis */
3594 /* save pointers across realloc */
3595 unsigned int start = v_bufstr - v_buffer;
3596 unsigned int size = v_bufptr - v_buffer;
3597 unsigned int reallocto;
3598
3599 reallocto = (size / MAX_PTY_WRITE + 1) * MAX_PTY_WRITE;
3600 v_buffer = (unsigned char *)realloc(v_buffer, reallocto);
3601 if (v_buffer) {
3602 v_bufstr = v_buffer + start;
3603 v_bufptr = v_buffer + size;
3604 v_bufend = v_buffer + reallocto;
3605 } else {
3606 /* should we print a warning if couldn't return memory? */
3607 v_buffer = v_bufstr - start; /* restore clobbered pointer */
3608 }
3609 }
3610 R->v_buffer = v_buffer;
3611 R->v_bufstr = v_bufstr;
3612 R->v_bufptr = v_bufptr;
3613 R->v_bufend = v_bufend;
3614} 3492}
3493
3615/*----------------------- end-of-file (C source) -----------------------*/ 3494/*----------------------- end-of-file (C source) -----------------------*/
3616 3495

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines