1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include "libbb.h"
43#include "common_bufsiz.h"
44#include <sys/kd.h>
45
46#define ESC "\033"
47#define CURSOR_ON -1
48#define CURSOR_OFF 1
49
50#define DEV_TTY "/dev/tty"
51#define DEV_VCSA "/dev/vcsa"
52
53struct screen_info {
54 unsigned char lines, cols, cursor_x, cursor_y;
55};
56
57#define CHAR(x) (*(uint8_t*)(x))
58#define ATTR(x) (((uint8_t*)(x))[1])
59#define NEXT(x) ((x) += 2)
60#define DATA(x) (*(uint16_t*)(x))
61
62struct globals {
63 char* data;
64 int size;
65 int x, y;
66 int kbd_fd;
67 int ioerror_count;
68 int key_count;
69 int escape_count;
70 int nokeys;
71 int current;
72 int first_line_offset;
73 int last_attr;
74
75 unsigned width;
76 unsigned height;
77 unsigned col;
78 unsigned line;
79 smallint curoff;
80 char attrbuf[sizeof("0;1;5;30;40m")];
81
82 struct screen_info remote;
83
84 struct termios term_orig;
85 char vcsa_name[sizeof(DEV_VCSA "NN")];
86};
87
88#define G (*ptr_to_globals)
89#define INIT_G() do { \
90 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
91 G.width = G.height = UINT_MAX; \
92 G.last_attr--; \
93} while (0)
94
95enum {
96 FLAG_v,
97 FLAG_c,
98 FLAG_Q,
99 FLAG_s,
100 FLAG_n,
101 FLAG_d,
102 FLAG_f,
103 FLAG_F,
104};
105#define FLAG(x) (1 << FLAG_##x)
106#define BW (option_mask32 & FLAG(n))
107
108static void putcsi(const char *s)
109{
110 fputs_stdout(ESC"[");
111 fputs_stdout(s);
112}
113
114static void clrscr(void)
115{
116
117 putcsi("1;1H" ESC"[J");
118 G.col = G.line = 0;
119}
120
121static void set_cursor(int state)
122{
123 if (G.curoff != state) {
124 G.curoff = state;
125 putcsi("?25");
126 bb_putchar("h?l"[1 + state]);
127 }
128}
129
130static void gotoxy(int col, int line)
131{
132 if (G.col != col || G.line != line) {
133 G.col = col;
134 G.line = line;
135 printf(ESC"[%u;%uH", line + 1, col + 1);
136 }
137}
138
139static void cleanup(int code) NORETURN;
140static void cleanup(int code)
141{
142 set_cursor(CURSOR_ON);
143 tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
144 if (ENABLE_FEATURE_CLEAN_UP) {
145 close(G.kbd_fd);
146 }
147
148 if (!BW)
149 putcsi("0m");
150 bb_putchar('\n');
151 if (code > EXIT_FAILURE)
152 kill_myself_with_sig(code);
153 exit(code);
154}
155
156static void screen_read_close(void)
157{
158 unsigned i, j;
159 int vcsa_fd;
160 char *data;
161
162
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 }
172 if (G.size != i) {
173 cleanup(EXIT_FAILURE);
174 }
175 data = G.data + G.current;
176 xread(vcsa_fd, data, G.size);
177 close(vcsa_fd);
178 for (i = 0; i < G.remote.lines; i++) {
179 for (j = 0; j < G.remote.cols; j++, NEXT(data)) {
180 unsigned x = j - G.x;
181 unsigned y = i - G.y;
182
183 if (y >= G.height || x >= G.width)
184 DATA(data) = 0;
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 }
192 }
193 }
194}
195
196static void screen_char(char *data)
197{
198 if (!BW) {
199 uint8_t attr_diff;
200 uint8_t attr = ATTR(data);
201
202 if (option_mask32 & FLAG(F)) {
203 attr >>= 1;
204 }
205 attr_diff = G.last_attr ^ attr;
206 if (attr_diff) {
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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;
235
236 ptr = G.attrbuf;
237
238
239
240
241
242 if (G.last_attr < 0
243 || ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0
244 ) {
245 *ptr++ = '0';
246 *ptr++ = ';';
247
248 attr_diff = attr | ~(bold_mask | blink_mask);
249 }
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];
267 ptr++;
268 }
269 if (ptr != G.attrbuf) {
270 ptr[-1] = 'm';
271 *ptr = '\0';
272 putcsi(G.attrbuf);
273 }
274 }
275 }
276 putchar(CHAR(data));
277 G.col++;
278}
279
280static void screen_dump(void)
281{
282 int linefeed_cnt;
283 int line, col;
284 int linecnt = G.remote.lines - G.y;
285 char *data = G.data + G.current + G.first_line_offset;
286
287 linefeed_cnt = 0;
288 for (line = 0; line < linecnt && line < G.height; line++) {
289 int space_cnt = 0;
290 for (col = 0; col < G.remote.cols; col++, NEXT(data)) {
291 unsigned tty_col = col - G.x;
292
293 if (tty_col >= G.width)
294 continue;
295 space_cnt++;
296 if (BW && CHAR(data) == ' ')
297 continue;
298 while (linefeed_cnt != 0) {
299
300 bb_putchar('\n');
301 linefeed_cnt--;
302 }
303 while (--space_cnt)
304 bb_putchar(' ');
305 screen_char(data);
306 }
307 linefeed_cnt++;
308 }
309}
310
311static void curmove(void)
312{
313 unsigned cx = G.remote.cursor_x - G.x;
314 unsigned cy = G.remote.cursor_y - G.y;
315 int cursor = CURSOR_OFF;
316
317 if (cx < G.width && cy < G.height) {
318 gotoxy(cx, cy);
319 cursor = CURSOR_ON;
320 }
321 set_cursor(cursor);
322}
323
324static 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
333static NOINLINE void start_shell_in_child(const char* tty_name)
334{
335 int pid = xvfork();
336 if (pid == 0) {
337 struct termios termchild;
338 const char *shell = get_shell_name();
339
340 signal(SIGHUP, SIG_IGN);
341
342 setsid();
343
344 close(0);
345 xopen(tty_name, O_RDWR);
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
361int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
362int conspy_main(int argc UNUSED_PARAM, char **argv)
363{
364 char tty_name[sizeof(DEV_TTY "NN")];
365 unsigned opts;
366 unsigned ttynum;
367 int poll_timeout_ms;
368#if ENABLE_LONG_OPTS
369 static const char conspy_longopts[] ALIGN1 =
370 "viewonly\0" No_argument "v"
371 "createdevice\0" No_argument "c"
372 "neverquit\0" No_argument "Q"
373 "session\0" No_argument "s"
374 "nocolors\0" No_argument "n"
375 "dump\0" No_argument "d"
376 "follow\0" No_argument "f"
377 "framebuffer\0" No_argument "F"
378 ;
379#endif
380#define keybuf bb_common_bufsiz1
381 setup_common_bufsiz();
382
383 INIT_G();
384 strcpy(G.vcsa_name, DEV_VCSA);
385
386
387 opts = getopt32long(argv, "vcQsndfFx:+y:+", conspy_longopts, &G.x, &G.y);
388 argv += optind;
389 ttynum = 0;
390 if (argv[0]) {
391 ttynum = xatou_range(argv[0], 0, 63);
392 sprintf(G.vcsa_name + sizeof(DEV_VCSA)-1, "%u", ttynum);
393 }
394 sprintf(tty_name, "%s%u", DEV_TTY, ttynum);
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));
398 create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum));
399 }
400 if ((opts & FLAG(s)) && ttynum) {
401 start_shell_in_child(tty_name);
402 }
403
404 screen_read_close();
405 if (opts & FLAG(d)) {
406 screen_dump();
407 bb_putchar('\n');
408 return 0;
409 }
410
411 bb_signals(BB_FATAL_SIGS, cleanup);
412
413 G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
414
415
416 set_termios_to_raw(G.kbd_fd, &G.term_orig, 0
417 | TERMIOS_CLEAR_ISIG
418 | TERMIOS_RAW_INPUT
419 );
420
421
422 poll_timeout_ms = 250;
423 while (1) {
424 struct pollfd pfd;
425 int bytes_read;
426 int i, j;
427 char *data, *old;
428
429
430 i = G.width;
431 j = G.height;
432 get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
433 if (option_mask32 & FLAG(f)) {
434 int nx = G.remote.cursor_x - G.width + 1;
435 int ny = G.remote.cursor_y - G.height + 1;
436
437 if (G.remote.cursor_x < G.x) {
438 G.x = G.remote.cursor_x;
439 i = 0;
440 }
441 if (nx > G.x) {
442 G.x = nx;
443 i = 0;
444 }
445 if (G.remote.cursor_y < G.y) {
446 G.y = G.remote.cursor_y;
447 i = 0;
448 }
449 if (ny > G.y) {
450 G.y = ny;
451 i = 0;
452 }
453 }
454
455
456 old = G.data + G.current;
457 G.current = G.size - G.current;
458 data = G.data + G.current;
459 screen_read_close();
460 if (i != G.width || j != G.height) {
461 clrscr();
462 screen_dump();
463 } else {
464
465 old += G.first_line_offset;
466 data += G.first_line_offset;
467 for (i = G.y; i < G.remote.lines; i++) {
468 char *first = NULL;
469 char *last = last;
470 unsigned iy = i - G.y;
471
472 if (iy >= G.height)
473 break;
474 for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) {
475 unsigned jx = j - G.x;
476
477 if (jx < G.width && DATA(data) != DATA(old)) {
478 last = data;
479 if (!first) {
480 first = data;
481 gotoxy(jx, iy);
482 }
483 }
484 }
485 if (first) {
486
487 for (; first <= last; NEXT(first))
488 screen_char(first);
489 }
490 }
491 }
492 curmove();
493
494
495 fflush_all();
496 pfd.fd = G.kbd_fd;
497 pfd.events = POLLIN;
498 bytes_read = 0;
499 switch (poll(&pfd, 1, poll_timeout_ms)) {
500 char *k;
501 case -1:
502 if (errno != EINTR)
503 goto abort;
504 break;
505 case 0:
506 if (++G.nokeys >= 4)
507 G.nokeys = G.escape_count = 0;
508 break;
509 default:
510
511 k = keybuf + G.key_count;
512 bytes_read = read(G.kbd_fd, k, COMMON_BUFSIZE - G.key_count);
513 if (bytes_read < 0)
514 goto abort;
515
516
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 }
524 }
525 }
526 poll_timeout_ms = 250;
527 if (option_mask32 & FLAG(v)) continue;
528
529
530
531
532
533 G.key_count += bytes_read;
534 if (G.escape_count == 0) {
535 int handle, result;
536 long kbd_mode;
537
538 handle = xopen(tty_name, O_WRONLY);
539 result = ioctl(handle, KDGKBMODE, &kbd_mode);
540 if (result >= 0) {
541 char *p = keybuf;
542
543 G.ioerror_count = 0;
544 if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) {
545 G.key_count = 0;
546 }
547 for (; G.key_count != 0; p++, G.key_count--) {
548 result = ioctl(handle, TIOCSTI, p);
549 if (result < 0) {
550 memmove(keybuf, p, G.key_count);
551 break;
552 }
553
554
555
556 poll_timeout_ms = 20;
557 }
558 }
559
560
561 else if (errno != EIO || ++G.ioerror_count > 4) {
562 if (ENABLE_FEATURE_CLEAN_UP)
563 close(handle);
564 goto abort;
565 }
566
567
568 close(handle);
569 }
570 }
571 abort:
572 cleanup(EXIT_FAILURE);
573}
574