blob: a0e0d4e4b5c6f3cb9a01ffa0e1fba41b2fe22acf [file] [log] [blame]
Pascal Bellarda8b594f2010-06-21 02:17:29 +02001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Pascal Bellarda8b594f2010-06-21 02:17:29 +020011 */
Pascal Bellarda8b594f2010-06-21 02:17:29 +020012//config:config CONSPY
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020013//config: bool "conspy (10 kb)"
Denys Vlasenkoc97578d2011-08-13 09:00:29 +020014//config: default y
Denys Vlasenkoe3b1a1f2011-02-26 22:24:08 +010015//config: select PLATFORM_LINUX
Pascal Bellarda8b594f2010-06-21 02:17:29 +020016//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020017//config: A text-mode VNC like program for Linux virtual terminals.
18//config: example: conspy NUM shared access to console num
19//config: or conspy -nd NUM screenshot of console num
20//config: or conspy -cs NUM poor man's GNU screen like
Pascal Bellarda8b594f2010-06-21 02:17:29 +020021
Denys Vlasenkofb4da162016-11-22 23:14:24 +010022//applet:IF_CONSPY(APPLET(conspy, BB_DIR_BIN, BB_SUID_DROP))
23
24//kbuild:lib-$(CONFIG_CONSPY) += conspy.o
25
Pascal Bellarda8b594f2010-06-21 02:17:29 +020026//usage:#define conspy_trivial_usage
Pascal Bellardf9e07e72011-09-13 18:39:04 +020027//usage: "[-vcsndfFQ] [-x COL] [-y LINE] [CONSOLE_NO]"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020028//usage:#define conspy_full_usage "\n\n"
29//usage: "A text-mode VNC like program for Linux virtual consoles."
30//usage: "\nTo exit, quickly press ESC 3 times."
31//usage: "\n"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020032//usage: "\n -v Don't send keystrokes to the console"
Pascal Bellardf9e07e72011-09-13 18:39:04 +020033//usage: "\n -c Create missing /dev/{tty,vcsa}N"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020034//usage: "\n -s Open a SHELL session"
35//usage: "\n -n Black & white"
36//usage: "\n -d Dump console to stdout"
37//usage: "\n -f Follow cursor"
Pascal Bellardf9e07e72011-09-13 18:39:04 +020038//usage: "\n -F Assume console is on a framebuffer device"
39//usage: "\n -Q Disable exit on ESC-ESC-ESC"
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020040//usage: "\n -x COL Starting column"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020041//usage: "\n -y LINE Starting line"
42
43#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020044#include "common_bufsiz.h"
Pascal Bellarda8b594f2010-06-21 02:17:29 +020045#include <sys/kd.h>
46
Marek Polacek7b181072010-10-28 21:34:56 +020047#define ESC "\033"
Pascal Bellardf9e07e72011-09-13 18:39:04 +020048#define CURSOR_ON -1
49#define CURSOR_OFF 1
50
51#define DEV_TTY "/dev/tty"
52#define DEV_VCSA "/dev/vcsa"
Marek Polacek7b181072010-10-28 21:34:56 +020053
Pascal Bellarda8b594f2010-06-21 02:17:29 +020054struct screen_info {
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020055 unsigned char lines, cols, cursor_x, cursor_y;
Pascal Bellarda8b594f2010-06-21 02:17:29 +020056};
57
Pascal Bellardfa5ea172010-06-30 07:05:31 +020058#define CHAR(x) (*(uint8_t*)(x))
59#define ATTR(x) (((uint8_t*)(x))[1])
60#define NEXT(x) ((x) += 2)
Denys Vlasenko51fa1472010-06-25 00:57:57 +020061#define DATA(x) (*(uint16_t*)(x))
Pascal Bellarda8b594f2010-06-21 02:17:29 +020062
63struct globals {
64 char* data;
65 int size;
66 int x, y;
67 int kbd_fd;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +020068 int ioerror_count;
69 int key_count;
70 int escape_count;
71 int nokeys;
72 int current;
Pascal Bellardfa5ea172010-06-30 07:05:31 +020073 int first_line_offset;
74 int last_attr;
Pascal Bellardff377992010-06-28 15:50:22 +020075 // cached local tty parameters
76 unsigned width;
77 unsigned height;
78 unsigned col;
79 unsigned line;
Pascal Bellardfa5ea172010-06-30 07:05:31 +020080 smallint curoff; // unknown:0 cursor on:-1 cursor off:1
Pascal Bellardf9e07e72011-09-13 18:39:04 +020081 char attrbuf[sizeof("0;1;5;30;40m")];
Pascal Bellardff377992010-06-28 15:50:22 +020082 // remote console
Denys Vlasenko1a3b0712010-06-27 20:42:17 +020083 struct screen_info remote;
Pascal Bellardff377992010-06-28 15:50:22 +020084 // saved local tty terminfo
Pascal Bellarda8b594f2010-06-21 02:17:29 +020085 struct termios term_orig;
Pascal Bellardf9e07e72011-09-13 18:39:04 +020086 char vcsa_name[sizeof(DEV_VCSA "NN")];
Pascal Bellarda8b594f2010-06-21 02:17:29 +020087};
88
89#define G (*ptr_to_globals)
90#define INIT_G() do { \
91 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Pascal Bellardfa5ea172010-06-30 07:05:31 +020092 G.width = G.height = UINT_MAX; \
93 G.last_attr--; \
Pascal Bellarda8b594f2010-06-21 02:17:29 +020094} while (0)
95
Denys Vlasenko05a550b2010-06-21 04:00:16 +020096enum {
97 FLAG_v, // view only
98 FLAG_c, // create device if need
Pascal Bellardf9e07e72011-09-13 18:39:04 +020099 FLAG_Q, // never exit
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200100 FLAG_s, // session
101 FLAG_n, // no colors
102 FLAG_d, // dump screen
103 FLAG_f, // follow cursor
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200104 FLAG_F, // framebuffer
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200105};
106#define FLAG(x) (1 << FLAG_##x)
107#define BW (option_mask32 & FLAG(n))
108
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200109static void putcsi(const char *s)
110{
111 fputs(ESC"[", stdout);
112 fputs(s, stdout);
113}
114
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200115static void clrscr(void)
116{
117 // Home, clear till end of screen
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200118 putcsi("1;1H" ESC"[J");
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200119 G.col = G.line = 0;
120}
121
122static void set_cursor(int state)
123{
124 if (G.curoff != state) {
125 G.curoff = state;
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200126 putcsi("?25");
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200127 bb_putchar("h?l"[1 + state]);
128 }
129}
130
131static void gotoxy(int col, int line)
132{
133 if (G.col != col || G.line != line) {
134 G.col = col;
135 G.line = line;
Marek Polacek7b181072010-10-28 21:34:56 +0200136 printf(ESC"[%u;%uH", line + 1, col + 1);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200137 }
138}
139
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200140static void cleanup(int code) NORETURN;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200141static void cleanup(int code)
142{
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200143 set_cursor(CURSOR_ON);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200144 tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
145 if (ENABLE_FEATURE_CLEAN_UP) {
146 close(G.kbd_fd);
147 }
148 // Reset attributes
149 if (!BW)
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200150 putcsi("0m");
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200151 bb_putchar('\n');
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200152 if (code > EXIT_FAILURE)
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200153 kill_myself_with_sig(code);
154 exit(code);
155}
156
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200157static void screen_read_close(void)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200158{
159 unsigned i, j;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200160 int vcsa_fd;
161 char *data;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200162
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200163 // Close & re-open vcsa in case they have swapped virtual consoles
164 vcsa_fd = xopen(G.vcsa_name, O_RDONLY);
165 xread(vcsa_fd, &G.remote, 4);
166 i = G.remote.cols * 2;
167 G.first_line_offset = G.y * i;
168 i *= G.remote.lines;
169 if (G.data == NULL) {
170 G.size = i;
171 G.data = xzalloc(2 * i);
172 }
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200173 if (G.size != i) {
174 cleanup(EXIT_FAILURE);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200175 }
176 data = G.data + G.current;
177 xread(vcsa_fd, data, G.size);
178 close(vcsa_fd);
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200179 for (i = 0; i < G.remote.lines; i++) {
180 for (j = 0; j < G.remote.cols; j++, NEXT(data)) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200181 unsigned x = j - G.x; // if will catch j < G.x too
182 unsigned y = i - G.y; // if will catch i < G.y too
183
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200184 if (y >= G.height || x >= G.width)
185 DATA(data) = 0;
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200186 else {
187 uint8_t ch = CHAR(data);
188 if (ch < ' ')
189 CHAR(data) = ch | 0x40;
190 else if (ch > 0x7e)
191 CHAR(data) = '?';
192 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200193 }
194 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200195}
196
197static void screen_char(char *data)
198{
Pascal Bellardff377992010-06-28 15:50:22 +0200199 if (!BW) {
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200200 uint8_t attr_diff;
Pascal Bellardff377992010-06-28 15:50:22 +0200201 uint8_t attr = ATTR(data);
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200202
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200203 if (option_mask32 & FLAG(F)) {
204 attr >>= 1;
205 }
206 attr_diff = G.last_attr ^ attr;
Pascal Bellardff377992010-06-28 15:50:22 +0200207 if (attr_diff) {
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200208// Attribute layout for VGA compatible text videobuffer:
209// blinking text
210// |red bkgd
211// ||green bkgd
212// |||blue bkgd
213// vvvv
214// 00000000 <- lsb bit on the right
215// bold text / text 8th bit
216// red text
217// green text
218// blue text
219// TODO: apparently framebuffer-based console uses different layout
220// (bug? attempt to get 8th text bit in better position?)
221// red bkgd
222// |green bkgd
223// ||blue bkgd
224// vvv
225// 00000000 <- lsb bit on the right
226// bold text
227// red text
228// green text
229// blue text
230// text 8th bit
Pascal Bellardff377992010-06-28 15:50:22 +0200231 // converting RGB color bit triad to BGR:
232 static const char color[8] = "04261537";
233 const uint8_t fg_mask = 0x07, bold_mask = 0x08;
234 const uint8_t bg_mask = 0x70, blink_mask = 0x80;
235 char *ptr;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200236
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200237 ptr = G.attrbuf;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200238
Pascal Bellardff377992010-06-28 15:50:22 +0200239 // (G.last_attr & ~attr) has 1 only where
240 // G.last_attr has 1 but attr has 0.
241 // Here we check whether we have transition
242 // bold->non-bold or blink->non-blink:
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200243 if (G.last_attr < 0 // initial value
244 || ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0
245 ) {
Pascal Bellardff377992010-06-28 15:50:22 +0200246 *ptr++ = '0'; // "reset all attrs"
247 *ptr++ = ';';
248 // must set fg & bg, maybe need to set bold or blink:
249 attr_diff = attr | ~(bold_mask | blink_mask);
250 }
Pascal Bellardff377992010-06-28 15:50:22 +0200251 G.last_attr = attr;
252 if (attr_diff & bold_mask) {
253 *ptr++ = '1';
254 *ptr++ = ';';
255 }
256 if (attr_diff & blink_mask) {
257 *ptr++ = '5';
258 *ptr++ = ';';
259 }
260 if (attr_diff & fg_mask) {
261 *ptr++ = '3';
262 *ptr++ = color[attr & fg_mask];
263 *ptr++ = ';';
264 }
265 if (attr_diff & bg_mask) {
266 *ptr++ = '4';
267 *ptr++ = color[(attr & bg_mask) >> 4];
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200268 ptr++; // last attribute
Pascal Bellardff377992010-06-28 15:50:22 +0200269 }
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200270 if (ptr != G.attrbuf) {
Pascal Bellardff377992010-06-28 15:50:22 +0200271 ptr[-1] = 'm';
272 *ptr = '\0';
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200273 putcsi(G.attrbuf);
Pascal Bellardff377992010-06-28 15:50:22 +0200274 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200275 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200276 }
Denys Vlasenkoa54985b2010-06-24 17:50:00 +0200277 putchar(CHAR(data));
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200278 G.col++;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200279}
280
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200281static void screen_dump(void)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200282{
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200283 int linefeed_cnt;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200284 int line, col;
285 int linecnt = G.remote.lines - G.y;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200286 char *data = G.data + G.current + G.first_line_offset;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200287
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200288 linefeed_cnt = 0;
289 for (line = 0; line < linecnt && line < G.height; line++) {
290 int space_cnt = 0;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200291 for (col = 0; col < G.remote.cols; col++, NEXT(data)) {
292 unsigned tty_col = col - G.x; // if will catch col < G.x too
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200293
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200294 if (tty_col >= G.width)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200295 continue;
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200296 space_cnt++;
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200297 if (BW && CHAR(data) == ' ')
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200298 continue;
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200299 while (linefeed_cnt != 0) {
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200300 //bb_putchar('\r'); - tty driver does it for us
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200301 bb_putchar('\n');
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200302 linefeed_cnt--;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200303 }
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200304 while (--space_cnt)
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200305 bb_putchar(' ');
306 screen_char(data);
307 }
Denys Vlasenko05a550b2010-06-21 04:00:16 +0200308 linefeed_cnt++;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200309 }
310}
311
312static void curmove(void)
313{
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200314 unsigned cx = G.remote.cursor_x - G.x;
315 unsigned cy = G.remote.cursor_y - G.y;
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200316 int cursor = CURSOR_OFF;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200317
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200318 if (cx < G.width && cy < G.height) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200319 gotoxy(cx, cy);
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200320 cursor = CURSOR_ON;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200321 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200322 set_cursor(cursor);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200323}
324
325static void create_cdev_if_doesnt_exist(const char* name, dev_t dev)
326{
327 int fd = open(name, O_RDONLY);
328 if (fd != -1)
329 close(fd);
330 else if (errno == ENOENT)
331 mknod(name, S_IFCHR | 0660, dev);
332}
333
334static NOINLINE void start_shell_in_child(const char* tty_name)
335{
Pascal Bellard926031b2010-07-04 15:32:38 +0200336 int pid = xvfork();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200337 if (pid == 0) {
338 struct termios termchild;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100339 const char *shell = get_shell_name();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200340
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200341 signal(SIGHUP, SIG_IGN);
342 // set tty as a controlling tty
343 setsid();
344 // make tty to be input, output, error
345 close(0);
346 xopen(tty_name, O_RDWR); // uses fd 0
347 xdup2(0, 1);
348 xdup2(0, 2);
349 ioctl(0, TIOCSCTTY, 1);
350 tcsetpgrp(0, getpid());
351 tcgetattr(0, &termchild);
352 termchild.c_lflag |= ECHO;
353 termchild.c_oflag |= ONLCR | XTABS;
354 termchild.c_iflag |= ICRNL;
355 termchild.c_iflag &= ~IXOFF;
356 tcsetattr_stdin_TCSANOW(&termchild);
357 execl(shell, shell, "-i", (char *) NULL);
358 bb_simple_perror_msg_and_die(shell);
359 }
360}
361
Denys Vlasenko51fa1472010-06-25 00:57:57 +0200362int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200363int conspy_main(int argc UNUSED_PARAM, char **argv)
364{
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200365 char tty_name[sizeof(DEV_TTY "NN")];
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200366 unsigned opts;
367 unsigned ttynum;
368 int poll_timeout_ms;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200369#if ENABLE_LONG_OPTS
Denys Vlasenko00677b52017-08-08 14:59:35 +0200370 static const char conspy_longopts[] ALIGN1 =
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200371 "viewonly\0" No_argument "v"
372 "createdevice\0" No_argument "c"
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200373 "neverquit\0" No_argument "Q"
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200374 "session\0" No_argument "s"
375 "nocolors\0" No_argument "n"
376 "dump\0" No_argument "d"
377 "follow\0" No_argument "f"
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200378 "framebuffer\0" No_argument "F"
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200379 ;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200380#endif
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200381#define keybuf bb_common_bufsiz1
382 setup_common_bufsiz();
383
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200384 INIT_G();
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200385 strcpy(G.vcsa_name, DEV_VCSA);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200386
Denys Vlasenko237bedd2016-07-06 21:58:02 +0200387 // numeric params
Denys Vlasenko036585a2017-08-08 16:38:18 +0200388 opts = getopt32long(argv, "vcQsndfFx:+y:+", conspy_longopts, &G.x, &G.y);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200389 argv += optind;
390 ttynum = 0;
391 if (argv[0]) {
392 ttynum = xatou_range(argv[0], 0, 63);
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200393 sprintf(G.vcsa_name + sizeof(DEV_VCSA)-1, "%u", ttynum);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200394 }
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200395 sprintf(tty_name, "%s%u", DEV_TTY, ttynum);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200396 if (opts & FLAG(c)) {
397 if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v))
398 create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum));
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200399 create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum));
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200400 }
401 if ((opts & FLAG(s)) && ttynum) {
402 start_shell_in_child(tty_name);
403 }
404
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200405 screen_read_close();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200406 if (opts & FLAG(d)) {
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200407 screen_dump();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200408 bb_putchar('\n');
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200409 return 0;
410 }
411
412 bb_signals(BB_FATAL_SIGS, cleanup);
Denys Vlasenko918f4442010-06-25 14:40:25 +0200413
Denys Vlasenko918f4442010-06-25 14:40:25 +0200414 G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
Denys Vlasenko01ccdd12017-01-11 16:17:59 +0100415
416 // All characters must be passed through to us unaltered
417 set_termios_to_raw(G.kbd_fd, &G.term_orig, 0
418 | TERMIOS_CLEAR_ISIG // no signals on ^C ^Z etc
419 | TERMIOS_RAW_INPUT // turn off all input conversions
420 );
421 //Note: termios.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n
Pascal Bellardff377992010-06-28 15:50:22 +0200422
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200423 poll_timeout_ms = 250;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200424 while (1) {
425 struct pollfd pfd;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200426 int bytes_read;
427 int i, j;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200428 char *data, *old;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200429
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200430 // in the first loop G.width = G.height = 0: refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200431 i = G.width;
432 j = G.height;
433 get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200434 if (option_mask32 & FLAG(f)) {
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200435 int nx = G.remote.cursor_x - G.width + 1;
436 int ny = G.remote.cursor_y - G.height + 1;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200437
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200438 if (G.remote.cursor_x < G.x) {
439 G.x = G.remote.cursor_x;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200440 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200441 }
442 if (nx > G.x) {
443 G.x = nx;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200444 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200445 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200446 if (G.remote.cursor_y < G.y) {
447 G.y = G.remote.cursor_y;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200448 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200449 }
450 if (ny > G.y) {
451 G.y = ny;
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200452 i = 0; // force refresh
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200453 }
454 }
455
456 // Scan console data and redraw our tty where needed
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200457 old = G.data + G.current;
458 G.current = G.size - G.current;
459 data = G.data + G.current;
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200460 screen_read_close();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200461 if (i != G.width || j != G.height) {
462 clrscr();
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200463 screen_dump();
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200464 } else {
465 // For each remote line
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200466 old += G.first_line_offset;
467 data += G.first_line_offset;
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200468 for (i = G.y; i < G.remote.lines; i++) {
469 char *first = NULL; // first char which needs updating
470 char *last = last; // last char which needs updating
471 unsigned iy = i - G.y;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200472
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200473 if (iy >= G.height)
474 break;
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200475 for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) {
476 unsigned jx = j - G.x; // if will catch j >= G.x too
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200477
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200478 if (jx < G.width && DATA(data) != DATA(old)) {
479 last = data;
480 if (!first) {
481 first = data;
482 gotoxy(jx, iy);
483 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200484 }
485 }
Denys Vlasenko1a3b0712010-06-27 20:42:17 +0200486 if (first) {
487 // Rewrite updated data on the local screen
488 for (; first <= last; NEXT(first))
489 screen_char(first);
490 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200491 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200492 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200493 curmove();
494
495 // Wait for local user keypresses
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200496 fflush_all();
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200497 pfd.fd = G.kbd_fd;
498 pfd.events = POLLIN;
499 bytes_read = 0;
500 switch (poll(&pfd, 1, poll_timeout_ms)) {
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200501 char *k;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200502 case -1:
503 if (errno != EINTR)
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200504 goto abort;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200505 break;
506 case 0:
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200507 if (++G.nokeys >= 4)
508 G.nokeys = G.escape_count = 0;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200509 break;
510 default:
511 // Read the keys pressed
Pascal Bellard1b8e2b02010-06-23 20:25:00 +0200512 k = keybuf + G.key_count;
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200513 bytes_read = read(G.kbd_fd, k, COMMON_BUFSIZE - G.key_count);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200514 if (bytes_read < 0)
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200515 goto abort;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200516
517 // Do exit processing
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200518 if (!(option_mask32 & FLAG(Q))) {
519 for (i = 0; i < bytes_read; i++) {
520 if (k[i] != '\033')
521 G.escape_count = -1;
522 if (++G.escape_count >= 3)
523 cleanup(EXIT_SUCCESS);
524 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200525 }
526 }
527 poll_timeout_ms = 250;
Pascal Bellard6161cdb2011-04-11 03:52:53 +0200528 if (option_mask32 & FLAG(v)) continue;
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200529
530 // Insert all keys pressed into the virtual console's input
531 // buffer. Don't do this if the virtual console is in scan
532 // code mode - giving ASCII characters to a program expecting
533 // scan codes will confuse it.
Pascal Bellard6161cdb2011-04-11 03:52:53 +0200534 G.key_count += bytes_read;
535 if (G.escape_count == 0) {
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200536 int handle, result;
537 long kbd_mode;
538
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200539 handle = xopen(tty_name, O_WRONLY);
540 result = ioctl(handle, KDGKBMODE, &kbd_mode);
Pascal Bellard81199672010-07-01 07:18:41 +0200541 if (result >= 0) {
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200542 char *p = keybuf;
Pascal Bellard81199672010-07-01 07:18:41 +0200543
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200544 G.ioerror_count = 0;
Pascal Bellard81199672010-07-01 07:18:41 +0200545 if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) {
546 G.key_count = 0; // scan code mode
547 }
548 for (; G.key_count != 0; p++, G.key_count--) {
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200549 result = ioctl(handle, TIOCSTI, p);
Pascal Bellard81199672010-07-01 07:18:41 +0200550 if (result < 0) {
551 memmove(keybuf, p, G.key_count);
552 break;
553 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200554 // If there is an application on console which reacts
555 // to keypresses, we need to make our first sleep
556 // shorter to quickly redraw whatever it printed there.
557 poll_timeout_ms = 20;
558 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200559 }
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200560 // We sometimes get spurious IO errors on the TTY
561 // as programs close and re-open it
562 else if (errno != EIO || ++G.ioerror_count > 4) {
563 if (ENABLE_FEATURE_CLEAN_UP)
564 close(handle);
565 goto abort;
566 }
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200567 // Close & re-open tty in case they have
568 // swapped virtual consoles
569 close(handle);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200570 }
Pascal Bellardfa5ea172010-06-30 07:05:31 +0200571 } /* while (1) */
Pascal Bellardf9e07e72011-09-13 18:39:04 +0200572 abort:
573 cleanup(EXIT_FAILURE);
Pascal Bellarda8b594f2010-06-21 02:17:29 +0200574}