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