wok-6.x view busybox/stuff/busybox-1.16.1-vcsa2txt.u @ rev 5711

busybox: add conspy
author Pascal Bellard <pascal.bellard@slitaz.org>
date Wed Jun 23 21:48:13 2010 +0200 (2010-06-23)
parents 2a5e098a34ac
children 42de4dc1d8d6
line source
1 --- busybox-1.16.1/include/applets.h
2 +++ busybox-1.16.1/include/applets.h
3 @@ -105,6 +105,7 @@
4 IF_CLEAR(APPLET(clear, _BB_DIR_USR_BIN, _BB_SUID_DROP))
5 IF_CMP(APPLET(cmp, _BB_DIR_USR_BIN, _BB_SUID_DROP))
6 IF_COMM(APPLET(comm, _BB_DIR_USR_BIN, _BB_SUID_DROP))
7 +IF_VCSA2TXT(APPLET(conspy, _BB_DIR_USR_BIN, _BB_SUID_DROP))
8 IF_CP(APPLET_NOEXEC(cp, cp, _BB_DIR_BIN, _BB_SUID_DROP, cp))
9 IF_CPIO(APPLET(cpio, _BB_DIR_BIN, _BB_SUID_DROP))
10 IF_CROND(APPLET(crond, _BB_DIR_USR_SBIN, _BB_SUID_DROP))
11 @@ -420,6 +421,7 @@
12 IF_UUDECODE(APPLET(uudecode, _BB_DIR_USR_BIN, _BB_SUID_DROP))
13 IF_UUENCODE(APPLET(uuencode, _BB_DIR_USR_BIN, _BB_SUID_DROP))
14 IF_VCONFIG(APPLET(vconfig, _BB_DIR_SBIN, _BB_SUID_DROP))
15 +IF_VCSA2TXT(APPLET(vcsa2txt, _BB_DIR_USR_BIN, _BB_SUID_DROP))
16 IF_VI(APPLET(vi, _BB_DIR_BIN, _BB_SUID_DROP))
17 IF_VLOCK(APPLET(vlock, _BB_DIR_USR_BIN, _BB_SUID_REQUIRE))
18 IF_VOLNAME(APPLET(volname, _BB_DIR_USR_BIN, _BB_SUID_DROP))
20 --- busybox-1.16.1/include/usage.h
21 +++ busybox-1.16.1/include/usage.h
22 @@ -440,6 +440,23 @@
23 "\n -1 Close stdout" \
24 "\n -2 Close stderr" \
26 +#define conspy_trivial_usage \
27 + "[-vcsnd] [-x rows] [-y lines] [virtual_console]"
28 +#define conspy_full_usage "\n\n" \
29 + "A text-mode VNC like program for Linux virtual terminals.\n" \
30 + "\nTo exit, quickly press escape 3 times.\n" \
31 + "\nvirtual_console:\n" \
32 + " omitted Track the current console.\n" \
33 + " 1..63 Virtual console N.\n" \
34 + "\nOptions:" \
35 + "\n -v Don't send keystrokes to the console." \
36 + "\n -c May create device in /dev." \
37 + "\n -s Open a SHELL session." \
38 + "\n -n No colors. Black & white only." \
39 + "\n -d Dump console to stdout."\
40 + "\n -x r Skip the r first rows."\
41 + "\n -y l Skip the l first lines."\
42 +
43 #define setuidgid_trivial_usage \
44 "USER PROG ARGS"
45 #define setuidgid_full_usage "\n\n" \
46 @@ -5011,6 +5028,13 @@
47 "\n set_ingress_map [vlan-name] [skb_priority] [vlan_qos]" \
48 "\n set_name_type [name-type]" \
50 +#define vcsa2txt_trivial_usage \
51 + "stdin"
52 +#define vcsa2txt_full_usage \
53 + " Filter /dev/vcsa* to ansi escape sequences"
54 +#define vcsa2txt_example_usage \
55 + "# vcsa2txt < /dev/vcsa1\n"
56 +
57 #define vi_trivial_usage \
58 "[OPTIONS] [FILE]..."
59 #define vi_full_usage "\n\n" \
61 --- busybox-1.16.1/miscutils/Config.in
62 +++ busybox-1.16.1/miscutils/Config.in
63 @@ -625,6 +625,12 @@
64 help
65 Write a message to all users that are logged in.
67 +config VCSA2TXT
68 + bool "vcsa2txt/conspy"
69 + default n
70 + help
71 + Filter /dev/vcsa* output to ansi escape sequences.
72 +
73 config WATCHDOG
74 bool "watchdog"
75 default n
77 --- busybox-1.16.1/util-linux/Kbuild
78 +++ busybox-1.16.1/util-linux/Kbuild
79 @@ -43,3 +43,4 @@
80 lib-$(CONFIG_SWITCH_ROOT) += switch_root.o
81 lib-$(CONFIG_MKFS_EXT2) += tune2fs.o
82 lib-$(CONFIG_UMOUNT) += umount.o
83 +lib-$(CONFIG_VCSA2TXT) += vcsa2txt.o
85 --- busybox-1.16.1/util-linux/vcsa2txt.c
86 +++ busybox-1.16.1/util-linux/vcsa2txt.c
87 @@ -0,0 +1,489 @@
88 +/* vi: set sw=4 ts=4: */
89 +/*
90 + * A text-mode VNC like program for Linux virtual terminals.
91 + *
92 + * pascal.bellard@ads-lu.com
93 + *
94 + * Based on Russell Stuart's conspy.c
95 + * http://ace-host.stuart.id.au/russell/files/conspy.c
96 + *
97 + * Licensed under GPLv2 or later, see file License in this tarball for details.
98 + *
99 + * example : conspy num shared access to console num
100 + * or conspy -d num screenshot of console num
101 + * or conspy -cs num poor man's GNU screen like
102 + */
103 +
104 +//applet:IF_CONSPY(APPLET(conspy, _BB_DIR_BIN, _BB_SUID_DROP))
105 +
106 +//kbuild:lib-$(CONFIG_CONSPY) += conspy.o
107 +
108 +//config:config CONSPY
109 +//config: bool "conspy"
110 +//config: default n
111 +//config: help
112 +//config: A text-mode VNC like program for Linux virtual terminals.
113 +//config: example : conspy num shared access to console num
114 +//config: or conspy -d num screenshot of console num
115 +//config: or conspy -cs num poor man's GNU screen like
116 +
117 +//usage:#define conspy_trivial_usage
118 +//usage: "[-vcsndf] [-x ROW] [-y LINE] [CONSOLE_NO]"
119 +//usage:#define conspy_full_usage "\n\n"
120 +//usage: "A text-mode VNC like program for Linux virtual consoles."
121 +//usage: "\nTo exit, quickly press ESC 3 times."
122 +//usage: "\n"
123 +//usage: "\nOptions:"
124 +//usage: "\n -v Don't send keystrokes to the console"
125 +//usage: "\n -c Create missing devices in /dev"
126 +//usage: "\n -s Open a SHELL session"
127 +//usage: "\n -n Black & white"
128 +//usage: "\n -d Dump console to stdout"
129 +//usage: "\n -f Follow cursor"
130 +//usage: "\n -x ROW Starting row"
131 +//usage: "\n -y LINE Starting line"
132 +
133 +#include "libbb.h"
134 +#include <sys/kd.h>
135 +
136 +struct screen_info {
137 + unsigned char lines, rows, cursor_x, cursor_y;
138 +};
139 +
140 +#define CHAR(x) ((x)[0])
141 +#define ATTR(x) ((x)[1])
142 +#define NEXT(x) ((x)+=2)
143 +#define DATA(x) (* (short *) (x))
144 +
145 +struct globals {
146 + char* data;
147 + int size;
148 + int x, y;
149 + int kbd_fd;
150 + unsigned width;
151 + unsigned height;
152 + char last_attr;
153 + int ioerror_count;
154 + int key_count;
155 + int escape_count;
156 + int nokeys;
157 + int current;
158 + int vcsa_fd;
159 + struct screen_info info;
160 + struct termios term_orig;
161 +};
162 +
163 +#define G (*ptr_to_globals)
164 +#define INIT_G() do { \
165 + SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
166 +} while (0)
167 +
168 +enum {
169 + FLAG_v, // view only
170 + FLAG_c, // create device if need
171 + FLAG_s, // session
172 + FLAG_n, // no colors
173 + FLAG_d, // dump screen
174 + FLAG_f, // follow cursor
175 +};
176 +#define FLAG(x) (1 << FLAG_##x)
177 +#define BW (option_mask32 & FLAG(n))
178 +
179 +static void screen_read_close(void)
180 +{
181 + unsigned i, j;
182 + char *data = G.data + G.current;
183 +
184 + xread(G.vcsa_fd, data, G.size);
185 + G.last_attr = 0;
186 + for (i = 0; i < G.info.lines; i++) {
187 + for (j = 0; j < G.info.rows; j++, NEXT(data)) {
188 + unsigned x = j - G.x; // if will catch j < G.x too
189 + unsigned y = i - G.y; // if will catch i < G.y too
190 +
191 + if (CHAR(data) < ' ')
192 + CHAR(data) = ' ';
193 + if (y >= G.height || x >= G.width)
194 + DATA(data) = 0;
195 + }
196 + }
197 + close(G.vcsa_fd);
198 +}
199 +
200 +static void screen_char(char *data)
201 +{
202 + if (!BW && G.last_attr != ATTR(data)) {
203 + // BLGCRMOW
204 + static const char color[8] = "04261537";
205 +
206 + printf("\033[%c;4%c;3%cm",
207 + (ATTR(data) & 8) ? '1' // bold
208 + : '0', // defaults
209 + color[(ATTR(data) >> 4) & 7], color[ATTR(data) & 7]);
210 + G.last_attr = ATTR(data);
211 + }
212 + bb_putchar(CHAR(data));
213 +}
214 +
215 +#define clrscr() printf("\033[1;1H" "\033[0J")
216 +#define curoff() printf("\033[?25l")
217 +
218 +static void curon(void)
219 +{
220 + printf("\033[?25h");
221 +}
222 +
223 +static void gotoxy(int row, int line)
224 +{
225 + printf("\033[%u;%uH", line + 1, row + 1);
226 +}
227 +
228 +static void screen_dump(void)
229 +{
230 + int linefeed_cnt;
231 + int line, row;
232 + int linecnt = G.info.lines - G.y;
233 + char *data = G.data + G.current + (2 * G.y * G.info.rows);
234 +
235 + linefeed_cnt = 0;
236 + for (line = 0; line < linecnt && line < G.height; line++) {
237 + int space_cnt = 0;
238 + for (row = 0; row < G.info.rows; row++, NEXT(data)) {
239 + unsigned tty_row = row - G.x; // if will catch row < G.x too
240 +
241 + if (tty_row >= G.width)
242 + continue;
243 + space_cnt++;
244 + if (BW && (CHAR(data) | ' ') == ' ')
245 + continue;
246 + while (linefeed_cnt != 0) {
247 + bb_putchar('\r');
248 + bb_putchar('\n');
249 + linefeed_cnt--;
250 + }
251 + while (--space_cnt)
252 + bb_putchar(' ');
253 + screen_char(data);
254 + }
255 + linefeed_cnt++;
256 + }
257 +}
258 +
259 +static void curmove(void)
260 +{
261 + unsigned cx = G.info.cursor_x - G.x;
262 + unsigned cy = G.info.cursor_y - G.y;
263 +
264 + if (cx >= G.width || cy >= G.height) {
265 + curoff();
266 + } else {
267 + curon();
268 + gotoxy(cx, cy);
269 + }
270 + fflush_all();
271 +}
272 +
273 +static void cleanup(int code)
274 +{
275 + curon();
276 + fflush_all();
277 + tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
278 + if (ENABLE_FEATURE_CLEAN_UP) {
279 + free(ptr_to_globals);
280 + close(G.kbd_fd);
281 + }
282 + // Reset attributes
283 + if (!BW)
284 + printf("\033[0m");
285 + bb_putchar('\n');
286 + if (code > 1)
287 + kill_myself_with_sig(code); // does not return
288 + exit(code);
289 +}
290 +
291 +static void get_initial_data(const char* vcsa_name)
292 +{
293 + G.vcsa_fd = xopen(vcsa_name, O_RDONLY);
294 + xread(G.vcsa_fd, &G.info, 4);
295 + G.size = G.info.rows * G.info.lines * 2;
296 + G.width = G.height = UINT_MAX;
297 + G.data = xzalloc(2 * G.size);
298 + screen_read_close();
299 +}
300 +
301 +static void create_cdev_if_doesnt_exist(const char* name, dev_t dev)
302 +{
303 + int fd = open(name, O_RDONLY);
304 + if (fd != -1)
305 + close(fd);
306 + else if (errno == ENOENT)
307 + mknod(name, S_IFCHR | 0660, dev);
308 +}
309 +
310 +static NOINLINE void start_shell_in_child(const char* tty_name)
311 +{
312 + int pid = vfork();
313 + if (pid < 0) {
314 + bb_perror_msg_and_die("vfork");
315 + }
316 + if (pid == 0) {
317 + struct termios termchild;
318 + char *shell = getenv("SHELL");
319 +
320 + if (!shell)
321 + shell = (char *) DEFAULT_SHELL;
322 + signal(SIGHUP, SIG_IGN);
323 + // set tty as a controlling tty
324 + setsid();
325 + // make tty to be input, output, error
326 + close(0);
327 + xopen(tty_name, O_RDWR); // uses fd 0
328 + xdup2(0, 1);
329 + xdup2(0, 2);
330 + ioctl(0, TIOCSCTTY, 1);
331 + tcsetpgrp(0, getpid());
332 + tcgetattr(0, &termchild);
333 + termchild.c_lflag |= ECHO;
334 + termchild.c_oflag |= ONLCR | XTABS;
335 + termchild.c_iflag |= ICRNL;
336 + termchild.c_iflag &= ~IXOFF;
337 + tcsetattr_stdin_TCSANOW(&termchild);
338 + execl(shell, shell, "-i", (char *) NULL);
339 + bb_simple_perror_msg_and_die(shell);
340 + }
341 +}
342 +
343 +int vcsa2txt_main(int argc) MAIN_EXTERNALLY_VISIBLE;
344 +int vcsa2txt_main(int argc)
345 +{
346 + INIT_G();
347 + option_mask32 = FLAG(n);
348 + if (argc < 2) option_mask32 = 0;
349 + xread(0, &G.info, 4);
350 + G.size = G.info.rows * G.info.lines * 2;
351 + G.width = G.height = UINT_MAX;
352 + G.data = xzalloc(G.size);
353 + screen_read_close();
354 + screen_dump();
355 + bb_putchar('\n');
356 + if (ENABLE_FEATURE_CLEAN_UP) {
357 + free(ptr_to_globals);
358 + close(G.kbd_fd);
359 + }
360 + return 0;
361 +}
362 +
363 +int conspy_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE;
364 +int conspy_main(int argc UNUSED_PARAM, char **argv)
365 +{
366 + char vcsa_name[sizeof("/dev/vcsa") + 2];
367 + char tty_name[sizeof("/dev/tty") + 2];
368 +#define keybuf bb_common_bufsiz1
369 + struct termios termbuf;
370 + unsigned opts;
371 + unsigned ttynum;
372 + int poll_timeout_ms;
373 +#if ENABLE_LONG_OPTS
374 + static const char getopt_longopts[] ALIGN1 =
375 + "viewonly\0" No_argument "v"
376 + "createdevice\0" No_argument "c"
377 + "session\0" No_argument "s"
378 + "nocolors\0" No_argument "n"
379 + "dump\0" No_argument "d"
380 + "follow\0" No_argument "f"
381 + ;
382 +
383 + applet_long_options = getopt_longopts;
384 +#endif
385 + INIT_G();
386 +
387 + opt_complementary = "x+:y+"; // numeric params
388 + opts = getopt32(argv, "vcsndfx:y:", &G.x, &G.y);
389 + argv += optind;
390 + ttynum = 0;
391 + if (argv[0]) {
392 + ttynum = xatou_range(argv[0], 0, 63);
393 + }
394 + sprintf(vcsa_name, "/dev/vcsa%u", ttynum);
395 + sprintf(tty_name, "%s%u", "/dev/tty", ttynum);
396 + if (opts & FLAG(c)) {
397 + if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v))
398 + create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum));
399 + create_cdev_if_doesnt_exist(vcsa_name, makedev(7, 128 + ttynum));
400 + }
401 + if ((opts & FLAG(s)) && ttynum) {
402 + start_shell_in_child(tty_name);
403 + }
404 +
405 + get_initial_data(vcsa_name);
406 + G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
407 + if (opts & FLAG(d)) {
408 + screen_dump();
409 + bb_putchar('\n');
410 + if (ENABLE_FEATURE_CLEAN_UP) {
411 + free(ptr_to_globals);
412 + close(G.kbd_fd);
413 + }
414 + return 0;
415 + }
416 +
417 + bb_signals(BB_FATAL_SIGS, cleanup);
418 + // All characters must be passed through to us unaltered
419 + tcgetattr(G.kbd_fd, &G.term_orig);
420 + termbuf = G.term_orig;
421 + termbuf.c_iflag &= ~(BRKINT|INLCR|ICRNL|IXON|IXOFF|IUCLC|IXANY|IMAXBEL);
422 + termbuf.c_oflag &= ~(OPOST);
423 + termbuf.c_lflag &= ~(ISIG|ICANON|ECHO);
424 + termbuf.c_cc[VMIN] = 1;
425 + termbuf.c_cc[VTIME] = 0;
426 + tcsetattr(G.kbd_fd, TCSANOW, &termbuf);
427 + poll_timeout_ms = 250;
428 + while (1) {
429 + struct pollfd pfd;
430 + int bytes_read;
431 + int i, j;
432 + char *data, *old;
433 +
434 + old = G.data + G.current;
435 + G.current = G.size - G.current;
436 + data = G.data + G.current;
437 +
438 + // Close & re-open vcsa in case they have
439 + // swapped virtual consoles
440 + G.vcsa_fd = xopen(vcsa_name, O_RDONLY);
441 + xread(G.vcsa_fd, &G.info, 4);
442 + if (G.size != (G.info.rows * G.info.lines * 2)) {
443 + cleanup(1);
444 + }
445 + i = G.width;
446 + j = G.height;
447 + get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
448 + if ((option_mask32 & FLAG(f))) {
449 + int nx = G.info.cursor_x - G.width + 1;
450 + int ny = G.info.cursor_y - G.height + 1;
451 +
452 + if (G.info.cursor_x < G.x) {
453 + G.x = G.info.cursor_x;
454 + i = 0; // force refresh
455 + }
456 + if (nx > G.x) {
457 + G.x = nx;
458 + i = 0; // force refresh
459 + }
460 + if (G.info.cursor_y < G.y) {
461 + G.y = G.info.cursor_y;
462 + i = 0; // force refresh
463 + }
464 + if (ny > G.y) {
465 + G.y = ny;
466 + i = 0; // force refresh
467 + }
468 + }
469 +
470 + // Scan console data and redraw our tty where needed
471 + screen_read_close();
472 + if (i != G.width || j != G.height) {
473 + clrscr();
474 + screen_dump();
475 + }
476 + else for (i = 0; i < G.info.lines; i++) {
477 + char *last = last;
478 + char *first = NULL;
479 + int iy = i - G.y;
480 +
481 + if (iy >= (int) G.height)
482 + break;
483 + for (j = 0; j < G.info.rows; j++) {
484 + last = data;
485 + if (DATA(data) != DATA(old) && iy >= 0) {
486 + unsigned jx = j - G.x;
487 +
488 + last = NULL;
489 + if (first == NULL && jx < G.width) {
490 + first = data;
491 + gotoxy(jx, iy);
492 + }
493 + }
494 + NEXT(old);
495 + NEXT(data);
496 + }
497 + if (first == NULL)
498 + continue;
499 + if (last == NULL)
500 + last = data;
501 +
502 + // Write the data to the screen
503 + for (; first < last; NEXT(first))
504 + screen_char(first);
505 + }
506 + curmove();
507 +
508 + // Wait for local user keypresses
509 + pfd.fd = G.kbd_fd;
510 + pfd.events = POLLIN;
511 + bytes_read = 0;
512 + switch (poll(&pfd, 1, poll_timeout_ms)) {
513 + char *k;
514 + case -1:
515 + if (errno != EINTR)
516 + cleanup(1);
517 + break;
518 + case 0:
519 + if (++G.nokeys >= 4)
520 + G.nokeys = G.escape_count = 0;
521 + break;
522 + default:
523 + // Read the keys pressed
524 + k = keybuf + G.key_count;
525 + bytes_read = read(G.kbd_fd, k, sizeof(keybuf) - G.key_count);
526 + if (bytes_read < 0)
527 + cleanup(1);
528 +
529 + // Do exit processing
530 + for (i = 0; i < bytes_read; i++) {
531 + if (k[i] != '\033') G.escape_count = 0;
532 + else if (++G.escape_count >= 3)
533 + cleanup(0);
534 + }
535 + }
536 + poll_timeout_ms = 250;
537 +
538 + // Insert all keys pressed into the virtual console's input
539 + // buffer. Don't do this if the virtual console is in scan
540 + // code mode - giving ASCII characters to a program expecting
541 + // scan codes will confuse it.
542 + if (!(option_mask32 & FLAG(v)) && G.escape_count == 0) {
543 + int handle, result;
544 + long kbd_mode;
545 +
546 + G.key_count += bytes_read;
547 + handle = xopen(tty_name, O_WRONLY);
548 + result = ioctl(handle, KDGKBMODE, &kbd_mode);
549 + if (result == -1)
550 + /* nothing */;
551 + else if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE)
552 + G.key_count = 0; // scan code mode
553 + else {
554 + for (i = 0; i < G.key_count && result != -1; i++)
555 + result = ioctl(handle, TIOCSTI, keybuf + i);
556 + G.key_count -= i;
557 + if (G.key_count)
558 + memmove(keybuf, keybuf + i, G.key_count);
559 + // If there is an application on console which reacts
560 + // to keypresses, we need to make our first sleep
561 + // shorter to quickly redraw whatever it printed there.
562 + poll_timeout_ms = 20;
563 + }
564 + // Close & re-open tty in case they have
565 + // swapped virtual consoles
566 + close(handle);
567 +
568 + // We sometimes get spurious IO errors on the TTY
569 + // as programs close and re-open it
570 + if (result != -1)
571 + G.ioerror_count = 0;
572 + else if (errno != EIO || ++G.ioerror_count > 4)
573 + cleanup(1);
574 + }
575 + }
576 +}