- Rearrange reactos.dff according to rosapps rearrange.
[reactos.git] / rosapps / mc / src / rxvt.c
1 /* rxvt.c - gives output lines on rxvt with a special rxvt patch
2 Copyright (C) 1997 Paul Sheer
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <config.h>
20 #include <stdio.h> /* read, printf */
21 #include <stdlib.h> /* getenv */
22 #include <sys/types.h>
23 #include <string.h>
24 #ifdef HAVE_UNISTD_H
25 # include <unistd.h>
26 #endif
27 #include <malloc.h> /* malloc */
28
29 #ifndef SCO_FLAVOR
30 # include <sys/time.h> /* struct timeval */
31 #endif /* SCO_FLAVOR */
32
33 #if HAVE_SYS_SELECT_H
34 # include <sys/select.h>
35 #endif
36
37 #include "tty.h" /* move, addch */
38 #include "util.h" /* is_printable */
39 #include "cons.saver.h"
40
41 int rxvt_extensions = 0;
42
43 int look_for_rxvt_extensions (void)
44 {
45 static int been_called = 0;
46 char *e;
47 if (!been_called) {
48 rxvt_extensions = 0;
49 e = getenv ("RXVT_EXT");
50 if (e)
51 if (!strcmp (e, "1.0"))
52 rxvt_extensions = 1;
53 been_called = 1;
54 }
55 if (rxvt_extensions)
56 console_flag = 4;
57 return rxvt_extensions;
58 }
59
60 /* my own wierd protocol base 16 - paul */
61 static int rxvt_getc (void)
62 {
63 int r;
64 unsigned char c;
65 while (read (0, &c, 1) != 1);
66 if (c == '\n')
67 return -1;
68 r = (c - 'A') * 16;
69 while (read (0, &c, 1) != 1);
70 r += (c - 'A');
71 return r;
72 }
73
74 extern int keybar_visible;
75
76 static int anything_ready ()
77 {
78 fd_set fds;
79 struct timeval tv;
80
81 FD_ZERO (&fds);
82 FD_SET (0, &fds);
83 tv.tv_sec = 0;
84 tv.tv_usec = 0;
85 return select (1, &fds, 0, 0, &tv);
86 }
87
88 void show_rxvt_contents (int starty, unsigned char y1, unsigned char y2)
89 {
90 unsigned char *k;
91 int bytes, i, j, cols = 0;
92 y1 += (keybar_visible != 0); /* i don't knwo why we need this - paul */
93 y2 += (keybar_visible != 0);
94 while (anything_ready ())
95 getch ();
96
97 /* my own wierd protocol base 26 - paul */
98 printf ("\033CL%c%c%c%c\n",
99 (y1 / 26) + 'A', (y1 % 26) + 'A',
100 (y2 / 26) + 'A', (y2 % 26) + 'A');
101
102 bytes = (y2 - y1) * (COLS + 1) + 1; /* *should* be the number of bytes read */
103 j = 0;
104 k = malloc (bytes);
105 for (;;) {
106 int c;
107 c = rxvt_getc ();
108 if (c < 0)
109 break;
110 if (j < bytes)
111 k[j++] = c;
112 for (cols = 1;;cols++) {
113 c = rxvt_getc ();
114 if (c < 0)
115 break;
116 if (j < bytes)
117 k[j++] = c;
118 }
119 }
120 for (i = 0; i < j; i++) {
121 if ((i % cols) == 0)
122 move (starty + (i / cols), 0);
123 addch (is_printable (k[i]) ? k[i] : ' ');
124 }
125 free (k);
126 }
127