Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * A text-mode VNC like program for Linux virtual terminals. |
| 4 | * |
| 5 | * pascal.bellard@ads-lu.com |
| 6 | * |
| 7 | * Based on Russell Stuart's conspy.c |
| 8 | * http://ace-host.stuart.id.au/russell/files/conspy.c |
| 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 11 | */ |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 12 | //config:config CONSPY |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 13 | //config: bool "conspy (10 kb)" |
Denys Vlasenko | c97578d | 2011-08-13 09:00:29 +0200 | [diff] [blame] | 14 | //config: default y |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 15 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 16 | //config: A text-mode VNC like program for Linux virtual terminals. |
| 17 | //config: example: conspy NUM shared access to console num |
| 18 | //config: or conspy -nd NUM screenshot of console num |
| 19 | //config: or conspy -cs NUM poor man's GNU screen like |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 20 | |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 21 | //applet:IF_CONSPY(APPLET(conspy, BB_DIR_BIN, BB_SUID_DROP)) |
| 22 | |
| 23 | //kbuild:lib-$(CONFIG_CONSPY) += conspy.o |
| 24 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 25 | //usage:#define conspy_trivial_usage |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 26 | //usage: "[-vcsndfFQ] [-x COL] [-y LINE] [CONSOLE_NO]" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 27 | //usage:#define conspy_full_usage "\n\n" |
| 28 | //usage: "A text-mode VNC like program for Linux virtual consoles." |
| 29 | //usage: "\nTo exit, quickly press ESC 3 times." |
| 30 | //usage: "\n" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 31 | //usage: "\n -v Don't send keystrokes to the console" |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 32 | //usage: "\n -c Create missing /dev/{tty,vcsa}N" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 33 | //usage: "\n -s Open a SHELL session" |
| 34 | //usage: "\n -n Black & white" |
| 35 | //usage: "\n -d Dump console to stdout" |
| 36 | //usage: "\n -f Follow cursor" |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 37 | //usage: "\n -F Assume console is on a framebuffer device" |
| 38 | //usage: "\n -Q Disable exit on ESC-ESC-ESC" |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 39 | //usage: "\n -x COL Starting column" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 40 | //usage: "\n -y LINE Starting line" |
| 41 | |
| 42 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 43 | #include "common_bufsiz.h" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 44 | #include <sys/kd.h> |
| 45 | |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 46 | #define ESC "\033" |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 47 | #define CURSOR_ON -1 |
| 48 | #define CURSOR_OFF 1 |
| 49 | |
| 50 | #define DEV_TTY "/dev/tty" |
| 51 | #define DEV_VCSA "/dev/vcsa" |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 52 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 53 | struct screen_info { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 54 | unsigned char lines, cols, cursor_x, cursor_y; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 57 | #define CHAR(x) (*(uint8_t*)(x)) |
| 58 | #define ATTR(x) (((uint8_t*)(x))[1]) |
| 59 | #define NEXT(x) ((x) += 2) |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 60 | #define DATA(x) (*(uint16_t*)(x)) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 61 | |
| 62 | struct globals { |
| 63 | char* data; |
| 64 | int size; |
| 65 | int x, y; |
| 66 | int kbd_fd; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 67 | int ioerror_count; |
| 68 | int key_count; |
| 69 | int escape_count; |
| 70 | int nokeys; |
| 71 | int current; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 72 | int first_line_offset; |
| 73 | int last_attr; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 74 | // cached local tty parameters |
| 75 | unsigned width; |
| 76 | unsigned height; |
| 77 | unsigned col; |
| 78 | unsigned line; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 79 | smallint curoff; // unknown:0 cursor on:-1 cursor off:1 |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 80 | char attrbuf[sizeof("0;1;5;30;40m")]; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 81 | // remote console |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 82 | struct screen_info remote; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 83 | // saved local tty terminfo |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 84 | struct termios term_orig; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 85 | char vcsa_name[sizeof(DEV_VCSA "NN")]; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | #define G (*ptr_to_globals) |
| 89 | #define INIT_G() do { \ |
| 90 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 91 | G.width = G.height = UINT_MAX; \ |
| 92 | G.last_attr--; \ |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 93 | } while (0) |
| 94 | |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 95 | enum { |
| 96 | FLAG_v, // view only |
| 97 | FLAG_c, // create device if need |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 98 | FLAG_Q, // never exit |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 99 | FLAG_s, // session |
| 100 | FLAG_n, // no colors |
| 101 | FLAG_d, // dump screen |
| 102 | FLAG_f, // follow cursor |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 103 | FLAG_F, // framebuffer |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 104 | }; |
| 105 | #define FLAG(x) (1 << FLAG_##x) |
| 106 | #define BW (option_mask32 & FLAG(n)) |
| 107 | |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 108 | static void putcsi(const char *s) |
| 109 | { |
Ron Yorston | cad3fc7 | 2021-02-03 20:47:14 +0100 | [diff] [blame] | 110 | fputs_stdout(ESC"["); |
| 111 | fputs_stdout(s); |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 114 | static void clrscr(void) |
| 115 | { |
| 116 | // Home, clear till end of screen |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 117 | putcsi("1;1H" ESC"[J"); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 118 | G.col = G.line = 0; |
| 119 | } |
| 120 | |
| 121 | static void set_cursor(int state) |
| 122 | { |
| 123 | if (G.curoff != state) { |
| 124 | G.curoff = state; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 125 | putcsi("?25"); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 126 | bb_putchar("h?l"[1 + state]); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static void gotoxy(int col, int line) |
| 131 | { |
| 132 | if (G.col != col || G.line != line) { |
| 133 | G.col = col; |
| 134 | G.line = line; |
Marek Polacek | 7b18107 | 2010-10-28 21:34:56 +0200 | [diff] [blame] | 135 | printf(ESC"[%u;%uH", line + 1, col + 1); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 139 | static void cleanup(int code) NORETURN; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 140 | static void cleanup(int code) |
| 141 | { |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 142 | set_cursor(CURSOR_ON); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 143 | tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig); |
| 144 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 145 | close(G.kbd_fd); |
| 146 | } |
| 147 | // Reset attributes |
| 148 | if (!BW) |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 149 | putcsi("0m"); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 150 | bb_putchar('\n'); |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 151 | if (code > EXIT_FAILURE) |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 152 | kill_myself_with_sig(code); |
| 153 | exit(code); |
| 154 | } |
| 155 | |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 156 | static void screen_read_close(void) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 157 | { |
| 158 | unsigned i, j; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 159 | int vcsa_fd; |
| 160 | char *data; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 161 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 162 | // Close & re-open vcsa in case they have swapped virtual consoles |
| 163 | vcsa_fd = xopen(G.vcsa_name, O_RDONLY); |
| 164 | xread(vcsa_fd, &G.remote, 4); |
| 165 | i = G.remote.cols * 2; |
| 166 | G.first_line_offset = G.y * i; |
| 167 | i *= G.remote.lines; |
| 168 | if (G.data == NULL) { |
| 169 | G.size = i; |
| 170 | G.data = xzalloc(2 * i); |
| 171 | } |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 172 | if (G.size != i) { |
| 173 | cleanup(EXIT_FAILURE); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 174 | } |
| 175 | data = G.data + G.current; |
| 176 | xread(vcsa_fd, data, G.size); |
| 177 | close(vcsa_fd); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 178 | for (i = 0; i < G.remote.lines; i++) { |
| 179 | for (j = 0; j < G.remote.cols; j++, NEXT(data)) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 180 | unsigned x = j - G.x; // if will catch j < G.x too |
| 181 | unsigned y = i - G.y; // if will catch i < G.y too |
| 182 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 183 | if (y >= G.height || x >= G.width) |
| 184 | DATA(data) = 0; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 185 | else { |
| 186 | uint8_t ch = CHAR(data); |
| 187 | if (ch < ' ') |
| 188 | CHAR(data) = ch | 0x40; |
| 189 | else if (ch > 0x7e) |
| 190 | CHAR(data) = '?'; |
| 191 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static void screen_char(char *data) |
| 197 | { |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 198 | if (!BW) { |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 199 | uint8_t attr_diff; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 200 | uint8_t attr = ATTR(data); |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 201 | |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 202 | if (option_mask32 & FLAG(F)) { |
| 203 | attr >>= 1; |
| 204 | } |
| 205 | attr_diff = G.last_attr ^ attr; |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 206 | if (attr_diff) { |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 207 | // Attribute layout for VGA compatible text videobuffer: |
| 208 | // blinking text |
| 209 | // |red bkgd |
| 210 | // ||green bkgd |
| 211 | // |||blue bkgd |
| 212 | // vvvv |
| 213 | // 00000000 <- lsb bit on the right |
| 214 | // bold text / text 8th bit |
| 215 | // red text |
| 216 | // green text |
| 217 | // blue text |
| 218 | // TODO: apparently framebuffer-based console uses different layout |
| 219 | // (bug? attempt to get 8th text bit in better position?) |
| 220 | // red bkgd |
| 221 | // |green bkgd |
| 222 | // ||blue bkgd |
| 223 | // vvv |
| 224 | // 00000000 <- lsb bit on the right |
| 225 | // bold text |
| 226 | // red text |
| 227 | // green text |
| 228 | // blue text |
| 229 | // text 8th bit |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 230 | // converting RGB color bit triad to BGR: |
| 231 | static const char color[8] = "04261537"; |
| 232 | const uint8_t fg_mask = 0x07, bold_mask = 0x08; |
| 233 | const uint8_t bg_mask = 0x70, blink_mask = 0x80; |
| 234 | char *ptr; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 235 | |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 236 | ptr = G.attrbuf; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 237 | |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 238 | // (G.last_attr & ~attr) has 1 only where |
| 239 | // G.last_attr has 1 but attr has 0. |
| 240 | // Here we check whether we have transition |
| 241 | // bold->non-bold or blink->non-blink: |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 242 | if (G.last_attr < 0 // initial value |
| 243 | || ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0 |
| 244 | ) { |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 245 | *ptr++ = '0'; // "reset all attrs" |
| 246 | *ptr++ = ';'; |
| 247 | // must set fg & bg, maybe need to set bold or blink: |
| 248 | attr_diff = attr | ~(bold_mask | blink_mask); |
| 249 | } |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 250 | G.last_attr = attr; |
| 251 | if (attr_diff & bold_mask) { |
| 252 | *ptr++ = '1'; |
| 253 | *ptr++ = ';'; |
| 254 | } |
| 255 | if (attr_diff & blink_mask) { |
| 256 | *ptr++ = '5'; |
| 257 | *ptr++ = ';'; |
| 258 | } |
| 259 | if (attr_diff & fg_mask) { |
| 260 | *ptr++ = '3'; |
| 261 | *ptr++ = color[attr & fg_mask]; |
| 262 | *ptr++ = ';'; |
| 263 | } |
| 264 | if (attr_diff & bg_mask) { |
| 265 | *ptr++ = '4'; |
| 266 | *ptr++ = color[(attr & bg_mask) >> 4]; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 267 | ptr++; // last attribute |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 268 | } |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 269 | if (ptr != G.attrbuf) { |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 270 | ptr[-1] = 'm'; |
| 271 | *ptr = '\0'; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 272 | putcsi(G.attrbuf); |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 273 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 274 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 275 | } |
Denys Vlasenko | a54985b | 2010-06-24 17:50:00 +0200 | [diff] [blame] | 276 | putchar(CHAR(data)); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 277 | G.col++; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 280 | static void screen_dump(void) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 281 | { |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 282 | int linefeed_cnt; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 283 | int line, col; |
| 284 | int linecnt = G.remote.lines - G.y; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 285 | char *data = G.data + G.current + G.first_line_offset; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 286 | |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 287 | linefeed_cnt = 0; |
| 288 | for (line = 0; line < linecnt && line < G.height; line++) { |
| 289 | int space_cnt = 0; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 290 | for (col = 0; col < G.remote.cols; col++, NEXT(data)) { |
| 291 | unsigned tty_col = col - G.x; // if will catch col < G.x too |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 292 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 293 | if (tty_col >= G.width) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 294 | continue; |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 295 | space_cnt++; |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 296 | if (BW && CHAR(data) == ' ') |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 297 | continue; |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 298 | while (linefeed_cnt != 0) { |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 299 | //bb_putchar('\r'); - tty driver does it for us |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 300 | bb_putchar('\n'); |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 301 | linefeed_cnt--; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 302 | } |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 303 | while (--space_cnt) |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 304 | bb_putchar(' '); |
| 305 | screen_char(data); |
| 306 | } |
Denys Vlasenko | 05a550b | 2010-06-21 04:00:16 +0200 | [diff] [blame] | 307 | linefeed_cnt++; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
| 311 | static void curmove(void) |
| 312 | { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 313 | unsigned cx = G.remote.cursor_x - G.x; |
| 314 | unsigned cy = G.remote.cursor_y - G.y; |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 315 | int cursor = CURSOR_OFF; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 316 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 317 | if (cx < G.width && cy < G.height) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 318 | gotoxy(cx, cy); |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 319 | cursor = CURSOR_ON; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 320 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 321 | set_cursor(cursor); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static void create_cdev_if_doesnt_exist(const char* name, dev_t dev) |
| 325 | { |
| 326 | int fd = open(name, O_RDONLY); |
| 327 | if (fd != -1) |
| 328 | close(fd); |
| 329 | else if (errno == ENOENT) |
| 330 | mknod(name, S_IFCHR | 0660, dev); |
| 331 | } |
| 332 | |
| 333 | static NOINLINE void start_shell_in_child(const char* tty_name) |
| 334 | { |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 335 | int pid = xvfork(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 336 | if (pid == 0) { |
| 337 | struct termios termchild; |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 338 | const char *shell = get_shell_name(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 339 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 340 | signal(SIGHUP, SIG_IGN); |
| 341 | // set tty as a controlling tty |
| 342 | setsid(); |
| 343 | // make tty to be input, output, error |
| 344 | close(0); |
| 345 | xopen(tty_name, O_RDWR); // uses fd 0 |
| 346 | xdup2(0, 1); |
| 347 | xdup2(0, 2); |
| 348 | ioctl(0, TIOCSCTTY, 1); |
| 349 | tcsetpgrp(0, getpid()); |
| 350 | tcgetattr(0, &termchild); |
| 351 | termchild.c_lflag |= ECHO; |
| 352 | termchild.c_oflag |= ONLCR | XTABS; |
| 353 | termchild.c_iflag |= ICRNL; |
| 354 | termchild.c_iflag &= ~IXOFF; |
| 355 | tcsetattr_stdin_TCSANOW(&termchild); |
| 356 | execl(shell, shell, "-i", (char *) NULL); |
| 357 | bb_simple_perror_msg_and_die(shell); |
| 358 | } |
| 359 | } |
| 360 | |
Denys Vlasenko | 51fa147 | 2010-06-25 00:57:57 +0200 | [diff] [blame] | 361 | int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 362 | int conspy_main(int argc UNUSED_PARAM, char **argv) |
| 363 | { |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 364 | char tty_name[sizeof(DEV_TTY "NN")]; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 365 | unsigned opts; |
| 366 | unsigned ttynum; |
| 367 | int poll_timeout_ms; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 368 | #if ENABLE_LONG_OPTS |
Denys Vlasenko | 00677b5 | 2017-08-08 14:59:35 +0200 | [diff] [blame] | 369 | static const char conspy_longopts[] ALIGN1 = |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 370 | "viewonly\0" No_argument "v" |
| 371 | "createdevice\0" No_argument "c" |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 372 | "neverquit\0" No_argument "Q" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 373 | "session\0" No_argument "s" |
| 374 | "nocolors\0" No_argument "n" |
| 375 | "dump\0" No_argument "d" |
| 376 | "follow\0" No_argument "f" |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 377 | "framebuffer\0" No_argument "F" |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 378 | ; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 379 | #endif |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 380 | #define keybuf bb_common_bufsiz1 |
| 381 | setup_common_bufsiz(); |
| 382 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 383 | INIT_G(); |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 384 | strcpy(G.vcsa_name, DEV_VCSA); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 385 | |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 386 | // numeric params |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 387 | opts = getopt32long(argv, "vcQsndfFx:+y:+", conspy_longopts, &G.x, &G.y); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 388 | argv += optind; |
| 389 | ttynum = 0; |
| 390 | if (argv[0]) { |
| 391 | ttynum = xatou_range(argv[0], 0, 63); |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 392 | sprintf(G.vcsa_name + sizeof(DEV_VCSA)-1, "%u", ttynum); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 393 | } |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 394 | sprintf(tty_name, "%s%u", DEV_TTY, ttynum); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 395 | if (opts & FLAG(c)) { |
| 396 | if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v)) |
| 397 | create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum)); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 398 | create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum)); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 399 | } |
| 400 | if ((opts & FLAG(s)) && ttynum) { |
| 401 | start_shell_in_child(tty_name); |
| 402 | } |
| 403 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 404 | screen_read_close(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 405 | if (opts & FLAG(d)) { |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 406 | screen_dump(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 407 | bb_putchar('\n'); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | bb_signals(BB_FATAL_SIGS, cleanup); |
Denys Vlasenko | 918f444 | 2010-06-25 14:40:25 +0200 | [diff] [blame] | 412 | |
Denys Vlasenko | 918f444 | 2010-06-25 14:40:25 +0200 | [diff] [blame] | 413 | G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY); |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 414 | |
| 415 | // All characters must be passed through to us unaltered |
| 416 | set_termios_to_raw(G.kbd_fd, &G.term_orig, 0 |
| 417 | | TERMIOS_CLEAR_ISIG // no signals on ^C ^Z etc |
| 418 | | TERMIOS_RAW_INPUT // turn off all input conversions |
| 419 | ); |
| 420 | //Note: termios.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n |
Pascal Bellard | ff37799 | 2010-06-28 15:50:22 +0200 | [diff] [blame] | 421 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 422 | poll_timeout_ms = 250; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 423 | while (1) { |
| 424 | struct pollfd pfd; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 425 | int bytes_read; |
| 426 | int i, j; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 427 | char *data, *old; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 428 | |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 429 | // in the first loop G.width = G.height = 0: refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 430 | i = G.width; |
| 431 | j = G.height; |
| 432 | get_terminal_width_height(G.kbd_fd, &G.width, &G.height); |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 433 | if (option_mask32 & FLAG(f)) { |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 434 | int nx = G.remote.cursor_x - G.width + 1; |
| 435 | int ny = G.remote.cursor_y - G.height + 1; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 436 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 437 | if (G.remote.cursor_x < G.x) { |
| 438 | G.x = G.remote.cursor_x; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 439 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 440 | } |
| 441 | if (nx > G.x) { |
| 442 | G.x = nx; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 443 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 444 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 445 | if (G.remote.cursor_y < G.y) { |
| 446 | G.y = G.remote.cursor_y; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 447 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 448 | } |
| 449 | if (ny > G.y) { |
| 450 | G.y = ny; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 451 | i = 0; // force refresh |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | // Scan console data and redraw our tty where needed |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 456 | old = G.data + G.current; |
| 457 | G.current = G.size - G.current; |
| 458 | data = G.data + G.current; |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 459 | screen_read_close(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 460 | if (i != G.width || j != G.height) { |
| 461 | clrscr(); |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 462 | screen_dump(); |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 463 | } else { |
| 464 | // For each remote line |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 465 | old += G.first_line_offset; |
| 466 | data += G.first_line_offset; |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 467 | for (i = G.y; i < G.remote.lines; i++) { |
| 468 | char *first = NULL; // first char which needs updating |
| 469 | char *last = last; // last char which needs updating |
| 470 | unsigned iy = i - G.y; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 471 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 472 | if (iy >= G.height) |
| 473 | break; |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 474 | for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) { |
| 475 | unsigned jx = j - G.x; // if will catch j >= G.x too |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 476 | |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 477 | if (jx < G.width && DATA(data) != DATA(old)) { |
| 478 | last = data; |
| 479 | if (!first) { |
| 480 | first = data; |
| 481 | gotoxy(jx, iy); |
| 482 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 483 | } |
| 484 | } |
Denys Vlasenko | 1a3b071 | 2010-06-27 20:42:17 +0200 | [diff] [blame] | 485 | if (first) { |
| 486 | // Rewrite updated data on the local screen |
| 487 | for (; first <= last; NEXT(first)) |
| 488 | screen_char(first); |
| 489 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 490 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 491 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 492 | curmove(); |
| 493 | |
| 494 | // Wait for local user keypresses |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 495 | fflush_all(); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 496 | pfd.fd = G.kbd_fd; |
| 497 | pfd.events = POLLIN; |
| 498 | bytes_read = 0; |
| 499 | switch (poll(&pfd, 1, poll_timeout_ms)) { |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 500 | char *k; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 501 | case -1: |
| 502 | if (errno != EINTR) |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 503 | goto abort; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 504 | break; |
| 505 | case 0: |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 506 | if (++G.nokeys >= 4) |
| 507 | G.nokeys = G.escape_count = 0; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 508 | break; |
| 509 | default: |
| 510 | // Read the keys pressed |
Pascal Bellard | 1b8e2b0 | 2010-06-23 20:25:00 +0200 | [diff] [blame] | 511 | k = keybuf + G.key_count; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 512 | bytes_read = read(G.kbd_fd, k, COMMON_BUFSIZE - G.key_count); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 513 | if (bytes_read < 0) |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 514 | goto abort; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 515 | |
| 516 | // Do exit processing |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 517 | if (!(option_mask32 & FLAG(Q))) { |
| 518 | for (i = 0; i < bytes_read; i++) { |
| 519 | if (k[i] != '\033') |
| 520 | G.escape_count = -1; |
| 521 | if (++G.escape_count >= 3) |
| 522 | cleanup(EXIT_SUCCESS); |
| 523 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | poll_timeout_ms = 250; |
Pascal Bellard | 6161cdb | 2011-04-11 03:52:53 +0200 | [diff] [blame] | 527 | if (option_mask32 & FLAG(v)) continue; |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 528 | |
| 529 | // Insert all keys pressed into the virtual console's input |
| 530 | // buffer. Don't do this if the virtual console is in scan |
| 531 | // code mode - giving ASCII characters to a program expecting |
| 532 | // scan codes will confuse it. |
Pascal Bellard | 6161cdb | 2011-04-11 03:52:53 +0200 | [diff] [blame] | 533 | G.key_count += bytes_read; |
| 534 | if (G.escape_count == 0) { |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 535 | int handle, result; |
| 536 | long kbd_mode; |
| 537 | |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 538 | handle = xopen(tty_name, O_WRONLY); |
| 539 | result = ioctl(handle, KDGKBMODE, &kbd_mode); |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 540 | if (result >= 0) { |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 541 | char *p = keybuf; |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 542 | |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 543 | G.ioerror_count = 0; |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 544 | if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) { |
| 545 | G.key_count = 0; // scan code mode |
| 546 | } |
| 547 | for (; G.key_count != 0; p++, G.key_count--) { |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 548 | result = ioctl(handle, TIOCSTI, p); |
Pascal Bellard | 8119967 | 2010-07-01 07:18:41 +0200 | [diff] [blame] | 549 | if (result < 0) { |
| 550 | memmove(keybuf, p, G.key_count); |
| 551 | break; |
| 552 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 553 | // If there is an application on console which reacts |
| 554 | // to keypresses, we need to make our first sleep |
| 555 | // shorter to quickly redraw whatever it printed there. |
| 556 | poll_timeout_ms = 20; |
| 557 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 558 | } |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 559 | // We sometimes get spurious IO errors on the TTY |
| 560 | // as programs close and re-open it |
| 561 | else if (errno != EIO || ++G.ioerror_count > 4) { |
| 562 | if (ENABLE_FEATURE_CLEAN_UP) |
| 563 | close(handle); |
| 564 | goto abort; |
| 565 | } |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 566 | // Close & re-open tty in case they have |
| 567 | // swapped virtual consoles |
| 568 | close(handle); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 569 | } |
Pascal Bellard | fa5ea17 | 2010-06-30 07:05:31 +0200 | [diff] [blame] | 570 | } /* while (1) */ |
Pascal Bellard | f9e07e7 | 2011-09-13 18:39:04 +0200 | [diff] [blame] | 571 | abort: |
| 572 | cleanup(EXIT_FAILURE); |
Pascal Bellard | a8b594f | 2010-06-21 02:17:29 +0200 | [diff] [blame] | 573 | } |