2010-06-21 05:47:29 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* A text-mode VNC like program for Linux virtual terminals.
|
|
|
|
*
|
|
|
|
* pascal.bellard@ads-lu.com
|
|
|
|
*
|
|
|
|
* Based on Russell Stuart's conspy.c
|
|
|
|
* http://ace-host.stuart.id.au/russell/files/conspy.c
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2010-06-21 05:47:29 +05:30
|
|
|
*/
|
|
|
|
//config:config CONSPY
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "conspy (10 kb)"
|
2011-08-13 12:30:29 +05:30
|
|
|
//config: default y
|
2010-06-21 05:47:29 +05:30
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: A text-mode VNC like program for Linux virtual terminals.
|
|
|
|
//config: example: conspy NUM shared access to console num
|
|
|
|
//config: or conspy -nd NUM screenshot of console num
|
|
|
|
//config: or conspy -cs NUM poor man's GNU screen like
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2016-11-23 03:44:24 +05:30
|
|
|
//applet:IF_CONSPY(APPLET(conspy, BB_DIR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CONSPY) += conspy.o
|
|
|
|
|
2010-06-21 05:47:29 +05:30
|
|
|
//usage:#define conspy_trivial_usage
|
2011-09-13 22:09:04 +05:30
|
|
|
//usage: "[-vcsndfFQ] [-x COL] [-y LINE] [CONSOLE_NO]"
|
2010-06-21 05:47:29 +05:30
|
|
|
//usage:#define conspy_full_usage "\n\n"
|
|
|
|
//usage: "A text-mode VNC like program for Linux virtual consoles."
|
|
|
|
//usage: "\nTo exit, quickly press ESC 3 times."
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\n -v Don't send keystrokes to the console"
|
2011-09-13 22:09:04 +05:30
|
|
|
//usage: "\n -c Create missing /dev/{tty,vcsa}N"
|
2010-06-21 05:47:29 +05:30
|
|
|
//usage: "\n -s Open a SHELL session"
|
|
|
|
//usage: "\n -n Black & white"
|
|
|
|
//usage: "\n -d Dump console to stdout"
|
|
|
|
//usage: "\n -f Follow cursor"
|
2011-09-13 22:09:04 +05:30
|
|
|
//usage: "\n -F Assume console is on a framebuffer device"
|
|
|
|
//usage: "\n -Q Disable exit on ESC-ESC-ESC"
|
2010-06-28 00:12:17 +05:30
|
|
|
//usage: "\n -x COL Starting column"
|
2010-06-21 05:47:29 +05:30
|
|
|
//usage: "\n -y LINE Starting line"
|
|
|
|
|
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2010-06-21 05:47:29 +05:30
|
|
|
#include <sys/kd.h>
|
|
|
|
|
2010-10-29 01:04:56 +05:30
|
|
|
#define ESC "\033"
|
2011-09-13 22:09:04 +05:30
|
|
|
#define CURSOR_ON -1
|
|
|
|
#define CURSOR_OFF 1
|
|
|
|
|
|
|
|
#define DEV_TTY "/dev/tty"
|
|
|
|
#define DEV_VCSA "/dev/vcsa"
|
2010-10-29 01:04:56 +05:30
|
|
|
|
2010-06-21 05:47:29 +05:30
|
|
|
struct screen_info {
|
2010-06-28 00:12:17 +05:30
|
|
|
unsigned char lines, cols, cursor_x, cursor_y;
|
2010-06-21 05:47:29 +05:30
|
|
|
};
|
|
|
|
|
2010-06-30 10:35:31 +05:30
|
|
|
#define CHAR(x) (*(uint8_t*)(x))
|
|
|
|
#define ATTR(x) (((uint8_t*)(x))[1])
|
|
|
|
#define NEXT(x) ((x) += 2)
|
2010-06-25 04:27:57 +05:30
|
|
|
#define DATA(x) (*(uint16_t*)(x))
|
2010-06-21 05:47:29 +05:30
|
|
|
|
|
|
|
struct globals {
|
|
|
|
char* data;
|
|
|
|
int size;
|
|
|
|
int x, y;
|
|
|
|
int kbd_fd;
|
2010-06-23 23:55:00 +05:30
|
|
|
int ioerror_count;
|
|
|
|
int key_count;
|
|
|
|
int escape_count;
|
|
|
|
int nokeys;
|
|
|
|
int current;
|
2010-06-30 10:35:31 +05:30
|
|
|
int first_line_offset;
|
|
|
|
int last_attr;
|
2010-06-28 19:20:22 +05:30
|
|
|
// cached local tty parameters
|
|
|
|
unsigned width;
|
|
|
|
unsigned height;
|
|
|
|
unsigned col;
|
|
|
|
unsigned line;
|
2010-06-30 10:35:31 +05:30
|
|
|
smallint curoff; // unknown:0 cursor on:-1 cursor off:1
|
2011-09-13 22:09:04 +05:30
|
|
|
char attrbuf[sizeof("0;1;5;30;40m")];
|
2010-06-28 19:20:22 +05:30
|
|
|
// remote console
|
2010-06-28 00:12:17 +05:30
|
|
|
struct screen_info remote;
|
2010-06-28 19:20:22 +05:30
|
|
|
// saved local tty terminfo
|
2010-06-21 05:47:29 +05:30
|
|
|
struct termios term_orig;
|
2011-09-13 22:09:04 +05:30
|
|
|
char vcsa_name[sizeof(DEV_VCSA "NN")];
|
2010-06-21 05:47:29 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
#define G (*ptr_to_globals)
|
|
|
|
#define INIT_G() do { \
|
|
|
|
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
|
2010-06-30 10:35:31 +05:30
|
|
|
G.width = G.height = UINT_MAX; \
|
|
|
|
G.last_attr--; \
|
2010-06-21 05:47:29 +05:30
|
|
|
} while (0)
|
|
|
|
|
2010-06-21 07:30:16 +05:30
|
|
|
enum {
|
|
|
|
FLAG_v, // view only
|
|
|
|
FLAG_c, // create device if need
|
2011-09-13 22:09:04 +05:30
|
|
|
FLAG_Q, // never exit
|
2010-06-21 07:30:16 +05:30
|
|
|
FLAG_s, // session
|
|
|
|
FLAG_n, // no colors
|
|
|
|
FLAG_d, // dump screen
|
|
|
|
FLAG_f, // follow cursor
|
2011-09-13 22:09:04 +05:30
|
|
|
FLAG_F, // framebuffer
|
2010-06-21 07:30:16 +05:30
|
|
|
};
|
|
|
|
#define FLAG(x) (1 << FLAG_##x)
|
|
|
|
#define BW (option_mask32 & FLAG(n))
|
|
|
|
|
2011-09-13 22:09:04 +05:30
|
|
|
static void putcsi(const char *s)
|
|
|
|
{
|
|
|
|
fputs(ESC"[", stdout);
|
|
|
|
fputs(s, stdout);
|
|
|
|
}
|
|
|
|
|
2010-06-30 10:35:31 +05:30
|
|
|
static void clrscr(void)
|
|
|
|
{
|
|
|
|
// Home, clear till end of screen
|
2011-09-13 22:09:04 +05:30
|
|
|
putcsi("1;1H" ESC"[J");
|
2010-06-30 10:35:31 +05:30
|
|
|
G.col = G.line = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_cursor(int state)
|
|
|
|
{
|
|
|
|
if (G.curoff != state) {
|
|
|
|
G.curoff = state;
|
2011-09-13 22:09:04 +05:30
|
|
|
putcsi("?25");
|
2010-06-30 10:35:31 +05:30
|
|
|
bb_putchar("h?l"[1 + state]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gotoxy(int col, int line)
|
|
|
|
{
|
|
|
|
if (G.col != col || G.line != line) {
|
|
|
|
G.col = col;
|
|
|
|
G.line = line;
|
2010-10-29 01:04:56 +05:30
|
|
|
printf(ESC"[%u;%uH", line + 1, col + 1);
|
2010-06-30 10:35:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-13 22:09:04 +05:30
|
|
|
static void cleanup(int code) NORETURN;
|
2010-06-30 10:35:31 +05:30
|
|
|
static void cleanup(int code)
|
|
|
|
{
|
2011-09-13 22:09:04 +05:30
|
|
|
set_cursor(CURSOR_ON);
|
2010-06-30 10:35:31 +05:30
|
|
|
tcsetattr(G.kbd_fd, TCSANOW, &G.term_orig);
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
close(G.kbd_fd);
|
|
|
|
}
|
|
|
|
// Reset attributes
|
|
|
|
if (!BW)
|
2011-09-13 22:09:04 +05:30
|
|
|
putcsi("0m");
|
2010-06-30 10:35:31 +05:30
|
|
|
bb_putchar('\n');
|
2011-09-13 22:09:04 +05:30
|
|
|
if (code > EXIT_FAILURE)
|
2010-06-30 10:35:31 +05:30
|
|
|
kill_myself_with_sig(code);
|
|
|
|
exit(code);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:55:00 +05:30
|
|
|
static void screen_read_close(void)
|
2010-06-21 05:47:29 +05:30
|
|
|
{
|
|
|
|
unsigned i, j;
|
2010-06-30 10:35:31 +05:30
|
|
|
int vcsa_fd;
|
|
|
|
char *data;
|
|
|
|
|
|
|
|
// Close & re-open vcsa in case they have swapped virtual consoles
|
|
|
|
vcsa_fd = xopen(G.vcsa_name, O_RDONLY);
|
|
|
|
xread(vcsa_fd, &G.remote, 4);
|
|
|
|
i = G.remote.cols * 2;
|
|
|
|
G.first_line_offset = G.y * i;
|
|
|
|
i *= G.remote.lines;
|
|
|
|
if (G.data == NULL) {
|
|
|
|
G.size = i;
|
|
|
|
G.data = xzalloc(2 * i);
|
|
|
|
}
|
2011-09-13 22:09:04 +05:30
|
|
|
if (G.size != i) {
|
|
|
|
cleanup(EXIT_FAILURE);
|
2010-06-30 10:35:31 +05:30
|
|
|
}
|
|
|
|
data = G.data + G.current;
|
|
|
|
xread(vcsa_fd, data, G.size);
|
|
|
|
close(vcsa_fd);
|
2010-06-28 00:12:17 +05:30
|
|
|
for (i = 0; i < G.remote.lines; i++) {
|
|
|
|
for (j = 0; j < G.remote.cols; j++, NEXT(data)) {
|
2010-06-21 05:47:29 +05:30
|
|
|
unsigned x = j - G.x; // if will catch j < G.x too
|
|
|
|
unsigned y = i - G.y; // if will catch i < G.y too
|
|
|
|
|
|
|
|
if (y >= G.height || x >= G.width)
|
|
|
|
DATA(data) = 0;
|
2011-09-13 22:09:04 +05:30
|
|
|
else {
|
|
|
|
uint8_t ch = CHAR(data);
|
|
|
|
if (ch < ' ')
|
|
|
|
CHAR(data) = ch | 0x40;
|
|
|
|
else if (ch > 0x7e)
|
|
|
|
CHAR(data) = '?';
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void screen_char(char *data)
|
|
|
|
{
|
2010-06-28 19:20:22 +05:30
|
|
|
if (!BW) {
|
2011-09-13 22:09:04 +05:30
|
|
|
uint8_t attr_diff;
|
2010-06-28 19:20:22 +05:30
|
|
|
uint8_t attr = ATTR(data);
|
2010-06-24 21:20:00 +05:30
|
|
|
|
2011-09-13 22:09:04 +05:30
|
|
|
if (option_mask32 & FLAG(F)) {
|
|
|
|
attr >>= 1;
|
|
|
|
}
|
|
|
|
attr_diff = G.last_attr ^ attr;
|
2010-06-28 19:20:22 +05:30
|
|
|
if (attr_diff) {
|
2010-06-24 21:20:00 +05:30
|
|
|
// Attribute layout for VGA compatible text videobuffer:
|
|
|
|
// blinking text
|
|
|
|
// |red bkgd
|
|
|
|
// ||green bkgd
|
|
|
|
// |||blue bkgd
|
|
|
|
// vvvv
|
|
|
|
// 00000000 <- lsb bit on the right
|
|
|
|
// bold text / text 8th bit
|
|
|
|
// red text
|
|
|
|
// green text
|
|
|
|
// blue text
|
|
|
|
// TODO: apparently framebuffer-based console uses different layout
|
|
|
|
// (bug? attempt to get 8th text bit in better position?)
|
|
|
|
// red bkgd
|
|
|
|
// |green bkgd
|
|
|
|
// ||blue bkgd
|
|
|
|
// vvv
|
|
|
|
// 00000000 <- lsb bit on the right
|
|
|
|
// bold text
|
|
|
|
// red text
|
|
|
|
// green text
|
|
|
|
// blue text
|
|
|
|
// text 8th bit
|
2010-06-28 19:20:22 +05:30
|
|
|
// converting RGB color bit triad to BGR:
|
|
|
|
static const char color[8] = "04261537";
|
|
|
|
const uint8_t fg_mask = 0x07, bold_mask = 0x08;
|
|
|
|
const uint8_t bg_mask = 0x70, blink_mask = 0x80;
|
|
|
|
char *ptr;
|
|
|
|
|
2011-09-13 22:09:04 +05:30
|
|
|
ptr = G.attrbuf;
|
2010-06-28 19:20:22 +05:30
|
|
|
|
|
|
|
// (G.last_attr & ~attr) has 1 only where
|
|
|
|
// G.last_attr has 1 but attr has 0.
|
|
|
|
// Here we check whether we have transition
|
|
|
|
// bold->non-bold or blink->non-blink:
|
2010-06-30 10:35:31 +05:30
|
|
|
if (G.last_attr < 0 // initial value
|
|
|
|
|| ((G.last_attr & ~attr) & (bold_mask | blink_mask)) != 0
|
|
|
|
) {
|
2010-06-28 19:20:22 +05:30
|
|
|
*ptr++ = '0'; // "reset all attrs"
|
|
|
|
*ptr++ = ';';
|
|
|
|
// must set fg & bg, maybe need to set bold or blink:
|
|
|
|
attr_diff = attr | ~(bold_mask | blink_mask);
|
|
|
|
}
|
|
|
|
G.last_attr = attr;
|
|
|
|
if (attr_diff & bold_mask) {
|
|
|
|
*ptr++ = '1';
|
|
|
|
*ptr++ = ';';
|
|
|
|
}
|
|
|
|
if (attr_diff & blink_mask) {
|
|
|
|
*ptr++ = '5';
|
|
|
|
*ptr++ = ';';
|
|
|
|
}
|
|
|
|
if (attr_diff & fg_mask) {
|
|
|
|
*ptr++ = '3';
|
|
|
|
*ptr++ = color[attr & fg_mask];
|
|
|
|
*ptr++ = ';';
|
|
|
|
}
|
|
|
|
if (attr_diff & bg_mask) {
|
|
|
|
*ptr++ = '4';
|
|
|
|
*ptr++ = color[(attr & bg_mask) >> 4];
|
2011-09-13 22:09:04 +05:30
|
|
|
ptr++; // last attribute
|
2010-06-28 19:20:22 +05:30
|
|
|
}
|
2011-09-13 22:09:04 +05:30
|
|
|
if (ptr != G.attrbuf) {
|
2010-06-28 19:20:22 +05:30
|
|
|
ptr[-1] = 'm';
|
|
|
|
*ptr = '\0';
|
2011-09-13 22:09:04 +05:30
|
|
|
putcsi(G.attrbuf);
|
2010-06-28 19:20:22 +05:30
|
|
|
}
|
2010-06-28 00:12:17 +05:30
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2010-06-24 21:20:00 +05:30
|
|
|
putchar(CHAR(data));
|
2010-06-28 00:12:17 +05:30
|
|
|
G.col++;
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
|
2010-06-23 23:55:00 +05:30
|
|
|
static void screen_dump(void)
|
2010-06-21 05:47:29 +05:30
|
|
|
{
|
2010-06-21 07:30:16 +05:30
|
|
|
int linefeed_cnt;
|
2010-06-28 00:12:17 +05:30
|
|
|
int line, col;
|
|
|
|
int linecnt = G.remote.lines - G.y;
|
2010-06-30 10:35:31 +05:30
|
|
|
char *data = G.data + G.current + G.first_line_offset;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-21 07:30:16 +05:30
|
|
|
linefeed_cnt = 0;
|
|
|
|
for (line = 0; line < linecnt && line < G.height; line++) {
|
|
|
|
int space_cnt = 0;
|
2010-06-28 00:12:17 +05:30
|
|
|
for (col = 0; col < G.remote.cols; col++, NEXT(data)) {
|
|
|
|
unsigned tty_col = col - G.x; // if will catch col < G.x too
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-28 00:12:17 +05:30
|
|
|
if (tty_col >= G.width)
|
2010-06-21 05:47:29 +05:30
|
|
|
continue;
|
2010-06-21 07:30:16 +05:30
|
|
|
space_cnt++;
|
2010-06-25 04:27:57 +05:30
|
|
|
if (BW && CHAR(data) == ' ')
|
2010-06-21 05:47:29 +05:30
|
|
|
continue;
|
2010-06-21 07:30:16 +05:30
|
|
|
while (linefeed_cnt != 0) {
|
2010-06-25 04:27:57 +05:30
|
|
|
//bb_putchar('\r'); - tty driver does it for us
|
2010-06-21 05:47:29 +05:30
|
|
|
bb_putchar('\n');
|
2010-06-21 07:30:16 +05:30
|
|
|
linefeed_cnt--;
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2010-06-21 07:30:16 +05:30
|
|
|
while (--space_cnt)
|
2010-06-21 05:47:29 +05:30
|
|
|
bb_putchar(' ');
|
|
|
|
screen_char(data);
|
|
|
|
}
|
2010-06-21 07:30:16 +05:30
|
|
|
linefeed_cnt++;
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void curmove(void)
|
|
|
|
{
|
2010-06-28 00:12:17 +05:30
|
|
|
unsigned cx = G.remote.cursor_x - G.x;
|
|
|
|
unsigned cy = G.remote.cursor_y - G.y;
|
2011-09-13 22:09:04 +05:30
|
|
|
int cursor = CURSOR_OFF;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-30 10:35:31 +05:30
|
|
|
if (cx < G.width && cy < G.height) {
|
2010-06-21 05:47:29 +05:30
|
|
|
gotoxy(cx, cy);
|
2011-09-13 22:09:04 +05:30
|
|
|
cursor = CURSOR_ON;
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2010-06-30 10:35:31 +05:30
|
|
|
set_cursor(cursor);
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void create_cdev_if_doesnt_exist(const char* name, dev_t dev)
|
|
|
|
{
|
|
|
|
int fd = open(name, O_RDONLY);
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
else if (errno == ENOENT)
|
|
|
|
mknod(name, S_IFCHR | 0660, dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NOINLINE void start_shell_in_child(const char* tty_name)
|
|
|
|
{
|
2010-07-04 19:02:38 +05:30
|
|
|
int pid = xvfork();
|
2010-06-21 05:47:29 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
struct termios termchild;
|
2011-03-09 01:30:36 +05:30
|
|
|
const char *shell = get_shell_name();
|
2010-06-21 05:47:29 +05:30
|
|
|
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
// set tty as a controlling tty
|
|
|
|
setsid();
|
|
|
|
// make tty to be input, output, error
|
|
|
|
close(0);
|
|
|
|
xopen(tty_name, O_RDWR); // uses fd 0
|
|
|
|
xdup2(0, 1);
|
|
|
|
xdup2(0, 2);
|
|
|
|
ioctl(0, TIOCSCTTY, 1);
|
|
|
|
tcsetpgrp(0, getpid());
|
|
|
|
tcgetattr(0, &termchild);
|
|
|
|
termchild.c_lflag |= ECHO;
|
|
|
|
termchild.c_oflag |= ONLCR | XTABS;
|
|
|
|
termchild.c_iflag |= ICRNL;
|
|
|
|
termchild.c_iflag &= ~IXOFF;
|
|
|
|
tcsetattr_stdin_TCSANOW(&termchild);
|
|
|
|
execl(shell, shell, "-i", (char *) NULL);
|
|
|
|
bb_simple_perror_msg_and_die(shell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 04:27:57 +05:30
|
|
|
int conspy_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2010-06-21 05:47:29 +05:30
|
|
|
int conspy_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
2011-09-13 22:09:04 +05:30
|
|
|
char tty_name[sizeof(DEV_TTY "NN")];
|
2010-06-21 05:47:29 +05:30
|
|
|
unsigned opts;
|
|
|
|
unsigned ttynum;
|
|
|
|
int poll_timeout_ms;
|
|
|
|
#if ENABLE_LONG_OPTS
|
2017-08-08 18:29:35 +05:30
|
|
|
static const char conspy_longopts[] ALIGN1 =
|
2010-06-21 05:47:29 +05:30
|
|
|
"viewonly\0" No_argument "v"
|
|
|
|
"createdevice\0" No_argument "c"
|
2011-09-13 22:09:04 +05:30
|
|
|
"neverquit\0" No_argument "Q"
|
2010-06-21 05:47:29 +05:30
|
|
|
"session\0" No_argument "s"
|
|
|
|
"nocolors\0" No_argument "n"
|
|
|
|
"dump\0" No_argument "d"
|
|
|
|
"follow\0" No_argument "f"
|
2011-09-13 22:09:04 +05:30
|
|
|
"framebuffer\0" No_argument "F"
|
2010-06-21 05:47:29 +05:30
|
|
|
;
|
|
|
|
#endif
|
2016-04-21 22:08:51 +05:30
|
|
|
#define keybuf bb_common_bufsiz1
|
|
|
|
setup_common_bufsiz();
|
|
|
|
|
2010-06-21 05:47:29 +05:30
|
|
|
INIT_G();
|
2011-09-13 22:09:04 +05:30
|
|
|
strcpy(G.vcsa_name, DEV_VCSA);
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2016-07-07 01:28:02 +05:30
|
|
|
// numeric params
|
getopt32: remove applet_long_options
FEATURE_GETOPT_LONG made dependent on LONG_OPTS.
The folloving options are removed, now LONG_OPTS enables long options
for affected applets:
FEATURE_ENV_LONG_OPTIONS FEATURE_EXPAND_LONG_OPTIONS
FEATURE_UNEXPAND_LONG_OPTIONS FEATURE_MKDIR_LONG_OPTIONS
FEATURE_MV_LONG_OPTIONS FEATURE_RMDIR_LONG_OPTIONS
FEATURE_ADDGROUP_LONG_OPTIONS FEATURE_ADDUSER_LONG_OPTIONS
FEATURE_HWCLOCK_LONG_OPTIONS FEATURE_NSENTER_LONG_OPTS
FEATURE_CHCON_LONG_OPTIONS FEATURE_RUNCON_LONG_OPTIONS
They either had a small number of long options, or their long options are
essential.
Example: upstream addgroup and adduser have ONLY longopts,
we should probably go further and get rid
of non-standard short options.
To this end, make addgroup and adduser "select LONG_OPTS".
We had this breakage caused by us even in our own package!
#if ENABLE_LONG_OPTS || !ENABLE_ADDGROUP
/* We try to use --gid, not -g, because "standard" addgroup
* has no short option -g, it has only long --gid.
*/
argv[1] = (char*)"--gid";
#else
/* Breaks if system in fact does NOT use busybox addgroup */
argv[1] = (char*)"-g";
#endif
xargs: its lone longopt no longer depends on DESKTOP, only on LONG_OPTS.
hwclock TODO: get rid of incompatible -t, -l aliases to --systz, --localtime
Shorten help texts by omitting long option when short opt alternative exists.
Reduction of size comes from the fact that store of an immediate
(an address of longopts) to a fixed address (global variable)
is a longer insn than pushing that immediate or passing it in a register.
This effect is CPU-agnostic.
function old new delta
getopt32 1350 22 -1328
vgetopt32 - 1318 +1318
getopt32long - 24 +24
tftpd_main 562 567 +5
scan_recursive 376 380 +4
collect_cpu 545 546 +1
date_main 1096 1095 -1
hostname_main 262 259 -3
uname_main 259 255 -4
setpriv_main 362 358 -4
rmdir_main 191 187 -4
mv_main 562 558 -4
ipcalc_main 548 544 -4
ifenslave_main 641 637 -4
gzip_main 192 188 -4
gunzip_main 77 73 -4
fsfreeze_main 81 77 -4
flock_main 318 314 -4
deluser_main 337 333 -4
cp_main 374 370 -4
chown_main 175 171 -4
applet_long_options 4 - -4
xargs_main 894 889 -5
wget_main 2540 2535 -5
udhcpc_main 2767 2762 -5
touch_main 436 431 -5
tar_main 1014 1009 -5
start_stop_daemon_main 1033 1028 -5
sed_main 682 677 -5
script_main 1082 1077 -5
run_parts_main 330 325 -5
rtcwake_main 459 454 -5
od_main 2169 2164 -5
nl_main 201 196 -5
modprobe_main 773 768 -5
mkdir_main 160 155 -5
ls_main 568 563 -5
install_main 773 768 -5
hwclock_main 411 406 -5
getopt_main 622 617 -5
fstrim_main 256 251 -5
env_main 198 193 -5
dumpleases_main 635 630 -5
dpkg_main 3991 3986 -5
diff_main 1355 1350 -5
cryptpw_main 233 228 -5
cpio_main 593 588 -5
conspy_main 1135 1130 -5
chpasswd_main 313 308 -5
adduser_main 887 882 -5
addgroup_main 416 411 -5
ftpgetput_main 351 345 -6
get_terminal_width_height 242 234 -8
expand_main 690 680 -10
static.expand_longopts 18 - -18
static.unexpand_longopts 27 - -27
mkdir_longopts 28 - -28
env_longopts 30 - -30
static.ifenslave_longopts 34 - -34
mv_longopts 46 - -46
static.rmdir_longopts 48 - -48
packed_usage 31739 31687 -52
------------------------------------------------------------------------------
(add/remove: 2/8 grow/shrink: 3/49 up/down: 1352/-1840) Total: -488 bytes
text data bss dec hex filename
915681 485 6880 923046 e15a6 busybox_old
915428 485 6876 922789 e14a5 busybox_unstripped
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-08 20:08:18 +05:30
|
|
|
opts = getopt32long(argv, "vcQsndfFx:+y:+", conspy_longopts, &G.x, &G.y);
|
2010-06-21 05:47:29 +05:30
|
|
|
argv += optind;
|
|
|
|
ttynum = 0;
|
|
|
|
if (argv[0]) {
|
|
|
|
ttynum = xatou_range(argv[0], 0, 63);
|
2011-09-13 22:09:04 +05:30
|
|
|
sprintf(G.vcsa_name + sizeof(DEV_VCSA)-1, "%u", ttynum);
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2011-09-13 22:09:04 +05:30
|
|
|
sprintf(tty_name, "%s%u", DEV_TTY, ttynum);
|
2010-06-21 05:47:29 +05:30
|
|
|
if (opts & FLAG(c)) {
|
|
|
|
if ((opts & (FLAG(s)|FLAG(v))) != FLAG(v))
|
|
|
|
create_cdev_if_doesnt_exist(tty_name, makedev(4, ttynum));
|
2010-06-30 10:35:31 +05:30
|
|
|
create_cdev_if_doesnt_exist(G.vcsa_name, makedev(7, 128 + ttynum));
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
if ((opts & FLAG(s)) && ttynum) {
|
|
|
|
start_shell_in_child(tty_name);
|
|
|
|
}
|
|
|
|
|
2010-06-30 10:35:31 +05:30
|
|
|
screen_read_close();
|
2010-06-21 05:47:29 +05:30
|
|
|
if (opts & FLAG(d)) {
|
2010-06-23 23:55:00 +05:30
|
|
|
screen_dump();
|
2010-06-21 05:47:29 +05:30
|
|
|
bb_putchar('\n');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bb_signals(BB_FATAL_SIGS, cleanup);
|
2010-06-25 18:10:25 +05:30
|
|
|
|
|
|
|
G.kbd_fd = xopen(CURRENT_TTY, O_RDONLY);
|
2017-01-11 20:47:59 +05:30
|
|
|
|
|
|
|
// All characters must be passed through to us unaltered
|
|
|
|
set_termios_to_raw(G.kbd_fd, &G.term_orig, 0
|
|
|
|
| TERMIOS_CLEAR_ISIG // no signals on ^C ^Z etc
|
|
|
|
| TERMIOS_RAW_INPUT // turn off all input conversions
|
|
|
|
);
|
|
|
|
//Note: termios.c_oflag &= ~(OPOST); - no, we still want \n -> \r\n
|
2010-06-28 19:20:22 +05:30
|
|
|
|
2010-06-21 05:47:29 +05:30
|
|
|
poll_timeout_ms = 250;
|
|
|
|
while (1) {
|
|
|
|
struct pollfd pfd;
|
|
|
|
int bytes_read;
|
|
|
|
int i, j;
|
2010-06-23 23:55:00 +05:30
|
|
|
char *data, *old;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-30 10:35:31 +05:30
|
|
|
// in the first loop G.width = G.height = 0: refresh
|
2010-06-21 05:47:29 +05:30
|
|
|
i = G.width;
|
|
|
|
j = G.height;
|
|
|
|
get_terminal_width_height(G.kbd_fd, &G.width, &G.height);
|
2010-06-30 10:35:31 +05:30
|
|
|
if (option_mask32 & FLAG(f)) {
|
2010-06-28 00:12:17 +05:30
|
|
|
int nx = G.remote.cursor_x - G.width + 1;
|
|
|
|
int ny = G.remote.cursor_y - G.height + 1;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-28 00:12:17 +05:30
|
|
|
if (G.remote.cursor_x < G.x) {
|
|
|
|
G.x = G.remote.cursor_x;
|
2010-10-29 15:16:52 +05:30
|
|
|
i = 0; // force refresh
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
if (nx > G.x) {
|
|
|
|
G.x = nx;
|
2010-10-29 15:16:52 +05:30
|
|
|
i = 0; // force refresh
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2010-06-28 00:12:17 +05:30
|
|
|
if (G.remote.cursor_y < G.y) {
|
|
|
|
G.y = G.remote.cursor_y;
|
2010-10-29 15:16:52 +05:30
|
|
|
i = 0; // force refresh
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
if (ny > G.y) {
|
|
|
|
G.y = ny;
|
2010-10-29 15:16:52 +05:30
|
|
|
i = 0; // force refresh
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan console data and redraw our tty where needed
|
2010-06-28 00:12:17 +05:30
|
|
|
old = G.data + G.current;
|
|
|
|
G.current = G.size - G.current;
|
|
|
|
data = G.data + G.current;
|
2010-06-23 23:55:00 +05:30
|
|
|
screen_read_close();
|
2010-06-21 05:47:29 +05:30
|
|
|
if (i != G.width || j != G.height) {
|
|
|
|
clrscr();
|
2010-06-23 23:55:00 +05:30
|
|
|
screen_dump();
|
2010-06-28 00:12:17 +05:30
|
|
|
} else {
|
|
|
|
// For each remote line
|
2010-06-30 10:35:31 +05:30
|
|
|
old += G.first_line_offset;
|
|
|
|
data += G.first_line_offset;
|
2010-06-28 00:12:17 +05:30
|
|
|
for (i = G.y; i < G.remote.lines; i++) {
|
|
|
|
char *first = NULL; // first char which needs updating
|
|
|
|
char *last = last; // last char which needs updating
|
|
|
|
unsigned iy = i - G.y;
|
|
|
|
|
|
|
|
if (iy >= G.height)
|
|
|
|
break;
|
2010-06-30 10:35:31 +05:30
|
|
|
for (j = 0; j < G.remote.cols; j++, NEXT(old), NEXT(data)) {
|
|
|
|
unsigned jx = j - G.x; // if will catch j >= G.x too
|
2010-06-21 05:47:29 +05:30
|
|
|
|
2010-06-28 00:12:17 +05:30
|
|
|
if (jx < G.width && DATA(data) != DATA(old)) {
|
|
|
|
last = data;
|
|
|
|
if (!first) {
|
|
|
|
first = data;
|
|
|
|
gotoxy(jx, iy);
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
2010-06-28 00:12:17 +05:30
|
|
|
if (first) {
|
|
|
|
// Rewrite updated data on the local screen
|
|
|
|
for (; first <= last; NEXT(first))
|
|
|
|
screen_char(first);
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
curmove();
|
|
|
|
|
|
|
|
// Wait for local user keypresses
|
2010-06-30 10:35:31 +05:30
|
|
|
fflush_all();
|
2010-06-21 05:47:29 +05:30
|
|
|
pfd.fd = G.kbd_fd;
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
bytes_read = 0;
|
|
|
|
switch (poll(&pfd, 1, poll_timeout_ms)) {
|
2010-06-23 23:55:00 +05:30
|
|
|
char *k;
|
2010-06-21 05:47:29 +05:30
|
|
|
case -1:
|
|
|
|
if (errno != EINTR)
|
2011-09-13 22:09:04 +05:30
|
|
|
goto abort;
|
2010-06-21 05:47:29 +05:30
|
|
|
break;
|
|
|
|
case 0:
|
2010-06-23 23:55:00 +05:30
|
|
|
if (++G.nokeys >= 4)
|
|
|
|
G.nokeys = G.escape_count = 0;
|
2010-06-21 05:47:29 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Read the keys pressed
|
2010-06-23 23:55:00 +05:30
|
|
|
k = keybuf + G.key_count;
|
2016-04-21 22:08:51 +05:30
|
|
|
bytes_read = read(G.kbd_fd, k, COMMON_BUFSIZE - G.key_count);
|
2010-06-21 05:47:29 +05:30
|
|
|
if (bytes_read < 0)
|
2011-09-13 22:09:04 +05:30
|
|
|
goto abort;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
|
|
|
// Do exit processing
|
2011-09-13 22:09:04 +05:30
|
|
|
if (!(option_mask32 & FLAG(Q))) {
|
|
|
|
for (i = 0; i < bytes_read; i++) {
|
|
|
|
if (k[i] != '\033')
|
|
|
|
G.escape_count = -1;
|
|
|
|
if (++G.escape_count >= 3)
|
|
|
|
cleanup(EXIT_SUCCESS);
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
poll_timeout_ms = 250;
|
2011-04-11 07:22:53 +05:30
|
|
|
if (option_mask32 & FLAG(v)) continue;
|
2010-06-21 05:47:29 +05:30
|
|
|
|
|
|
|
// Insert all keys pressed into the virtual console's input
|
|
|
|
// buffer. Don't do this if the virtual console is in scan
|
|
|
|
// code mode - giving ASCII characters to a program expecting
|
|
|
|
// scan codes will confuse it.
|
2011-04-11 07:22:53 +05:30
|
|
|
G.key_count += bytes_read;
|
|
|
|
if (G.escape_count == 0) {
|
2010-06-21 05:47:29 +05:30
|
|
|
int handle, result;
|
|
|
|
long kbd_mode;
|
|
|
|
|
|
|
|
handle = xopen(tty_name, O_WRONLY);
|
|
|
|
result = ioctl(handle, KDGKBMODE, &kbd_mode);
|
2010-07-01 10:48:41 +05:30
|
|
|
if (result >= 0) {
|
2010-06-30 10:35:31 +05:30
|
|
|
char *p = keybuf;
|
2010-07-01 10:48:41 +05:30
|
|
|
|
2011-09-13 22:09:04 +05:30
|
|
|
G.ioerror_count = 0;
|
2010-07-01 10:48:41 +05:30
|
|
|
if (kbd_mode != K_XLATE && kbd_mode != K_UNICODE) {
|
|
|
|
G.key_count = 0; // scan code mode
|
|
|
|
}
|
|
|
|
for (; G.key_count != 0; p++, G.key_count--) {
|
2010-06-30 10:35:31 +05:30
|
|
|
result = ioctl(handle, TIOCSTI, p);
|
2010-07-01 10:48:41 +05:30
|
|
|
if (result < 0) {
|
|
|
|
memmove(keybuf, p, G.key_count);
|
|
|
|
break;
|
|
|
|
}
|
2010-06-30 10:35:31 +05:30
|
|
|
// If there is an application on console which reacts
|
|
|
|
// to keypresses, we need to make our first sleep
|
|
|
|
// shorter to quickly redraw whatever it printed there.
|
|
|
|
poll_timeout_ms = 20;
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|
2011-09-13 22:09:04 +05:30
|
|
|
// We sometimes get spurious IO errors on the TTY
|
|
|
|
// as programs close and re-open it
|
|
|
|
else if (errno != EIO || ++G.ioerror_count > 4) {
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(handle);
|
|
|
|
goto abort;
|
|
|
|
}
|
2010-06-21 05:47:29 +05:30
|
|
|
// Close & re-open tty in case they have
|
|
|
|
// swapped virtual consoles
|
|
|
|
close(handle);
|
|
|
|
}
|
2010-06-30 10:35:31 +05:30
|
|
|
} /* while (1) */
|
2011-09-13 22:09:04 +05:30
|
|
|
abort:
|
|
|
|
cleanup(EXIT_FAILURE);
|
2010-06-21 05:47:29 +05:30
|
|
|
}
|