conspy: code shrink

function                                             old     new   delta
conspy_main                                         1446    1444      -2
screen_read_close                                    114     108      -6
screen_char                                          299     293      -6

Signed-off-by: Pascal Bellard <pascal.bellard@ads-lu.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Pascal Bellard 2010-06-28 15:50:22 +02:00 committed by Denys Vlasenko
parent 7d1201c5d7
commit ff37799dfe
3 changed files with 76 additions and 84 deletions

View File

@ -163,16 +163,6 @@ INSERT
#define blkid_full_usage "\n\n" \ #define blkid_full_usage "\n\n" \
"Print UUIDs of all filesystems" "Print UUIDs of all filesystems"
#define bootchartd_trivial_usage \
"start [PROG ARGS]|stop|init"
#define bootchartd_full_usage "\n\n" \
"Create /var/log/bootchart.tgz with boot chart data\n" \
"\nOptions:" \
"\nstart: start background logging; with PROG, run PROG, then kill logging with USR1" \
"\nstop: send USR1 to all bootchartd processes" \
"\ninit: start background logging; stop when getty/xdm is seen (for init scripts)" \
"\nUnder PID 1: as init, then exec $bootchart_init, /init, /sbin/init" \
#define brctl_trivial_usage \ #define brctl_trivial_usage \
"COMMAND [BRIDGE [INTERFACE]]" "COMMAND [BRIDGE [INTERFACE]]"
#define brctl_full_usage "\n\n" \ #define brctl_full_usage "\n\n" \

View File

@ -156,7 +156,7 @@ static int dump_procs(FILE *fp, int look_for_login_process)
continue; continue;
p++; p++;
strchrnul(p, ')')[0] = '\0'; strchrnul(p, ')')[0] = '\0';
/* If is gdm, kdm or a getty? */ /* Is it gdm, kdm or a getty? */
if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x') && p[1] == 'd' && p[2] == 'm') if (((p[0] == 'g' || p[0] == 'k' || p[0] == 'x') && p[1] == 'd' && p[2] == 'm')
|| strstr(p, "getty") || strstr(p, "getty")
) { ) {
@ -258,13 +258,12 @@ static void finalize(char *tempdir, const char *prog)
fprintf(header_fp, "profile.process = %s\n", prog); fprintf(header_fp, "profile.process = %s\n", prog);
fputs("version = "BC_VERSION_STR"\n", header_fp); fputs("version = "BC_VERSION_STR"\n", header_fp);
if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) { if (ENABLE_FEATURE_BOOTCHARTD_BLOATED_HEADER) {
char *hostname; char *hostname;
char *kcmdline; char *kcmdline;
time_t t; time_t t;
struct tm tm_time; struct tm tm_time;
/* x2 for possible localized data */ /* x2 for possible localized weekday/month names */
char date_buf[sizeof("Mon Jun 21 05:29:03 CEST 2010") * 2]; char date_buf[sizeof("Mon Jun 21 05:29:03 CEST 2010") * 2];
struct utsname unamebuf; struct utsname unamebuf;
@ -313,16 +312,16 @@ static void finalize(char *tempdir, const char *prog)
*/ */
} }
/* Usage: //usage:#define bootchartd_trivial_usage
* bootchartd start [PROG ARGS]: start logging in background, USR1 stops it. //usage: "start [PROG ARGS]|stop|init"
* With PROG, runs PROG, then kills background logging. //usage:#define bootchartd_full_usage "\n\n"
* bootchartd stop: same as "killall -USR1 bootchartd" //usage: "Create /var/log/bootchart.tgz with boot chart data\n"
* bootchartd init: start logging in background //usage: "\nOptions:"
* Stop when getty/gdm is seen (if AUTO_STOP_LOGGER = yes). //usage: "\nstart: start background logging; with PROG, run PROG, then kill logging with USR1"
* Meant to be used from init scripts. //usage: "\nstop: send USR1 to all bootchartd processes"
* bootchartd (pid==1): as init, but then execs $bootchart_init, /init, /sbin/init //usage: "\ninit: start background logging; stop when getty/xdm is seen (for init scripts)"
* Meant to be used as kernel's init process. //usage: "\nUnder PID 1: as init, then exec $bootchart_init, /init, /sbin/init"
*/
int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int bootchartd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int bootchartd_main(int argc UNUSED_PARAM, char **argv) int bootchartd_main(int argc UNUSED_PARAM, char **argv)
{ {
@ -358,7 +357,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
cmd = CMD_PID1; cmd = CMD_PID1;
} }
/* Here we are in START or INIT state */ /* Here we are in START, INIT or CMD_PID1 state */
/* Read config file: */ /* Read config file: */
sample_period_us = 200 * 1000; sample_period_us = 200 * 1000;
@ -422,7 +421,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
execl(bootchart_init, bootchart_init, NULL); execl(bootchart_init, bootchart_init, NULL);
execl("/init", "init", NULL); execl("/init", "init", NULL);
execl("/sbin/init", "init", NULL); execl("/sbin/init", "init", NULL);
bb_perror_msg_and_die("can't exec '%s'", "/sbin/init"); bb_perror_msg_and_die("can't execute '%s'", "/sbin/init");
} }
if (cmd == CMD_START && argv[2]) { /* "start PROG ARGS" */ if (cmd == CMD_START && argv[2]) { /* "start PROG ARGS" */
@ -432,7 +431,7 @@ int bootchartd_main(int argc UNUSED_PARAM, char **argv)
if (pid == 0) { /* child */ if (pid == 0) { /* child */
argv += 2; argv += 2;
execvp(argv[0], argv); execvp(argv[0], argv);
bb_perror_msg_and_die("can't exec '%s'", argv[0]); bb_perror_msg_and_die("can't execute '%s'", argv[0]);
} }
/* parent */ /* parent */
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);

View File

@ -56,31 +56,33 @@ struct globals {
int size; int size;
int x, y; int x, y;
int kbd_fd; int kbd_fd;
unsigned width; int vcsa_fd;
unsigned height;
unsigned col;
unsigned line;
int ioerror_count; int ioerror_count;
int key_count; int key_count;
int escape_count; int escape_count;
int nokeys; int nokeys;
int current; int current;
int vcsa_fd; // cached local tty parameters
uint16_t last_attr; unsigned width;
uint8_t last_bold; unsigned height;
uint8_t last_blink; unsigned col;
uint8_t last_fg; unsigned line;
uint8_t last_bg;
char attrbuf[sizeof("\033[0;1;5;30;40m")];
smallint curoff; smallint curoff;
uint8_t last_attr;
uint8_t force_attr_change;
char attrbuf[sizeof("\033[0;1;5;30;40m")];
// remote console
struct screen_info remote; struct screen_info remote;
// saved local tty terminfo
struct termios term_orig; struct termios term_orig;
}; };
#define G (*ptr_to_globals) #define G (*ptr_to_globals)
#define INIT_G() do { \ #define INIT_G() do { \
SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
strcpy((char*)&G.last_attr, "\xff\xff\xff\xff\xff\xff" "\033["); \ G.attrbuf[0] = '\033'; \
G.attrbuf[1] = '['; \
G.force_attr_change = 0xff; \
} while (0) } while (0)
enum { enum {
@ -100,7 +102,6 @@ static void screen_read_close(void)
char *data = G.data + G.current; char *data = G.data + G.current;
xread(G.vcsa_fd, data, G.size); xread(G.vcsa_fd, data, G.size);
G.last_attr = 0;
for (i = 0; i < G.remote.lines; i++) { for (i = 0; i < G.remote.lines; i++) {
for (j = 0; j < G.remote.cols; j++, NEXT(data)) { for (j = 0; j < G.remote.cols; j++, NEXT(data)) {
unsigned x = j - G.x; // if will catch j < G.x too unsigned x = j - G.x; // if will catch j < G.x too
@ -117,9 +118,12 @@ static void screen_read_close(void)
static void screen_char(char *data) static void screen_char(char *data)
{ {
uint8_t attr = ATTR(data); if (!BW) {
uint8_t attr = ATTR(data);
//uint8_t attr = ATTR(data) >> 1; // for framebuffer console
uint8_t attr_diff = (G.last_attr ^ attr) | G.force_attr_change;
if (!BW && G.last_attr != attr) { if (attr_diff) {
// Attribute layout for VGA compatible text videobuffer: // Attribute layout for VGA compatible text videobuffer:
// blinking text // blinking text
// |red bkgd // |red bkgd
@ -143,51 +147,49 @@ static void screen_char(char *data)
// green text // green text
// blue text // blue text
// text 8th bit // text 8th bit
// converting RGB color bit triad to BGR: // converting RGB color bit triad to BGR:
static const char color[8] = "04261537"; static const char color[8] = "04261537";
char *ptr; const uint8_t fg_mask = 0x07, bold_mask = 0x08;
uint8_t fg, bold, bg, blink; const uint8_t bg_mask = 0x70, blink_mask = 0x80;
char *ptr;
G.last_attr = attr; ptr = G.attrbuf + 2; /* skip "ESC [" */
//attr >>= 1; // for framebuffer console // (G.last_attr & ~attr) has 1 only where
ptr = G.attrbuf + sizeof("\033[")-1; // G.last_attr has 1 but attr has 0.
fg = (attr & 0x07); // Here we check whether we have transition
bold = (attr & 0x08); // bold->non-bold or blink->non-blink:
bg = (attr & 0x70); if ((G.last_attr & ~attr) & (bold_mask | blink_mask)) {
blink = (attr & 0x80); *ptr++ = '0'; // "reset all attrs"
if (G.last_bold > bold || G.last_blink > blink) { *ptr++ = ';';
G.last_bold = G.last_blink = 0; // must set fg & bg, maybe need to set bold or blink:
G.last_bg = 0xff; attr_diff = attr | ~(bold_mask | blink_mask);
*ptr++ = '0'; }
*ptr++ = ';'; G.force_attr_change = 0;
} G.last_attr = attr;
if (G.last_bold != bold) { if (attr_diff & bold_mask) {
G.last_bold = bold; *ptr++ = '1';
*ptr++ = '1'; *ptr++ = ';';
*ptr++ = ';'; }
} if (attr_diff & blink_mask) {
if (G.last_blink != blink) { *ptr++ = '5';
G.last_blink = blink; *ptr++ = ';';
*ptr++ = '5'; }
*ptr++ = ';'; if (attr_diff & fg_mask) {
} *ptr++ = '3';
if (G.last_fg != fg) { *ptr++ = color[attr & fg_mask];
G.last_fg = fg; *ptr++ = ';';
*ptr++ = '3'; }
*ptr++ = color[fg]; if (attr_diff & bg_mask) {
*ptr++ = ';'; *ptr++ = '4';
} *ptr++ = color[(attr & bg_mask) >> 4];
if (G.last_bg != bg) { *ptr++ = ';';
G.last_bg = bg; }
*ptr++ = '4'; if (ptr != G.attrbuf + 2) {
*ptr++ = color[bg >> 4]; ptr[-1] = 'm';
*ptr++ = ';'; *ptr = '\0';
} fputs(G.attrbuf, stdout);
if (ptr != G.attrbuf + sizeof("\033[")-1) { }
ptr[-1] = 'm';
*ptr = '\0';
fputs(G.attrbuf, stdout);
} }
} }
putchar(CHAR(data)); putchar(CHAR(data));
@ -402,6 +404,7 @@ int conspy_main(int argc UNUSED_PARAM, char **argv)
termbuf.c_cc[VMIN] = 1; termbuf.c_cc[VMIN] = 1;
termbuf.c_cc[VTIME] = 0; termbuf.c_cc[VTIME] = 0;
tcsetattr(G.kbd_fd, TCSANOW, &termbuf); tcsetattr(G.kbd_fd, TCSANOW, &termbuf);
poll_timeout_ms = 250; poll_timeout_ms = 250;
while (1) { while (1) {
struct pollfd pfd; struct pollfd pfd;