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