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