From c72b43c2f07e5fae288fff9e220b1f88e2889a72 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 13 Jul 2013 23:49:45 +0200 Subject: Commonalize typical [b,]k,m suffix struct function old new delta bkm_suffixes - 32 +32 static.km_suffixes 24 - -24 suffixes 32 - -32 static.bkm 32 - -32 head_tail_suffixes 32 - -32 ------------------------------------------------------------------------------ (add/remove: 2/6 grow/shrink: 0/0 up/down: 72/-160) Total: -88 bytes Signed-off-by: Denys Vlasenko --- TODO | 5 ----- 1 file changed, 5 deletions(-) (limited to 'TODO') diff --git a/TODO b/TODO index 44364690f..b66a1c1cb 100644 --- a/TODO +++ b/TODO @@ -132,11 +132,6 @@ stty / catv stty's visible() function and catv's guts are identical. Merge them into an appropriate libbb function. --- -struct suffix_mult - Several duplicate users of: grep -r "1024\*1024" * -B2 -A1 - Merge to a single size_suffixes[] in libbb. - Users: head tail od_bloaty hexdump and (partially as it wouldn't hurt) svlogd ---- tail ./busybox tail -f foo.c~ TODO should not print fmt=header_fmt for subsequent date >> TODO; i.e. only -- cgit v1.2.3-55-g6feb From c19be75d57ff42dee54b53e21b3eb4723b8cf243 Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Thu, 25 Jul 2013 04:39:04 +0200 Subject: networking: code shrink function old new delta in_ether - 124 +124 hexchar2int 42 - -42 ifconfig_main 1237 1106 -131 ether_input 141 - -141 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 0/1 up/down: 124/-314) Total: -190 bytes Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- TODO | 2 -- include/libbb.h | 1 + libbb/in_ether.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ networking/ifconfig.c | 43 ------------------------------------- networking/interface.c | 55 +---------------------------------------------- 5 files changed, 60 insertions(+), 99 deletions(-) create mode 100644 libbb/in_ether.c (limited to 'TODO') diff --git a/TODO b/TODO index b66a1c1cb..8d0850cd7 100644 --- a/TODO +++ b/TODO @@ -228,8 +228,6 @@ Minor stuff: --- See grep -r strtod Alot of duplication that wants cleanup. ---- - in_ether duplicated in network/{interface,ifconfig}.c --- unify progress_meter. wget, flash_eraseall, pipe_progress, fbsplash, setfiles. --- diff --git a/include/libbb.h b/include/libbb.h index f22c1252b..83e9b5fb9 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1140,6 +1140,7 @@ struct hwtype { }; extern smallint interface_opt_a; int display_interfaces(char *ifname) FAST_FUNC; +int in_ether(const char *bufp, struct sockaddr *sap) FAST_FUNC; #if ENABLE_FEATURE_HWIB int in_ib(const char *bufp, struct sockaddr *sap) FAST_FUNC; #else diff --git a/libbb/in_ether.c b/libbb/in_ether.c new file mode 100644 index 000000000..dadadbafe --- /dev/null +++ b/libbb/in_ether.c @@ -0,0 +1,58 @@ +/* vi: set sw=4 ts=4: */ +/* + * Utility routines. + */ + +//kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o +//kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o + +#include "libbb.h" +#include +#include + +/* Convert Ethernet address from "XX[:]XX[:]XX[:]XX[:]XX[:]XX" to sockaddr. + * Return nonzero on error. + */ +int FAST_FUNC in_ether(const char *bufp, struct sockaddr *sap) +{ + char *ptr; + int i, j; + unsigned char val; + unsigned char c; + + sap->sa_family = ARPHRD_ETHER; + ptr = (char *) sap->sa_data; + + i = ETH_ALEN; + goto first; + do { + /* We might get a semicolon here */ + if (*bufp == ':') + bufp++; + first: + j = val = 0; + do { + c = *bufp; + if (((unsigned char)(c - '0')) <= 9) { + c -= '0'; + } else if ((unsigned char)((c|0x20) - 'a') <= 5) { + c = (unsigned char)((c|0x20) - 'a') + 10; + } else { + if (j && (c == ':' || c == '\0')) + /* One-digit byte: __:X:__ */ + break; + return -1; + } + ++bufp; + val <<= 4; + val += c; + j ^= 1; + } while (j); + + *ptr++ = val; + + } while (--i); + + /* Error if we aren't at end of string */ + return *bufp; +} diff --git a/networking/ifconfig.c b/networking/ifconfig.c index 782374b35..999305aff 100644 --- a/networking/ifconfig.c +++ b/networking/ifconfig.c @@ -265,49 +265,6 @@ static const struct options OptArray[] = { { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING) } }; -#if ENABLE_FEATURE_IFCONFIG_HW -/* Input an Ethernet address and convert to binary. */ -static int in_ether(const char *bufp, struct sockaddr *sap) -{ - char *ptr; - int i, j; - unsigned char val; - unsigned char c; - - sap->sa_family = ARPHRD_ETHER; - ptr = (char *) sap->sa_data; - - i = 0; - do { - j = val = 0; - - /* We might get a semicolon here - not required. */ - if (i && (*bufp == ':')) { - bufp++; - } - - do { - c = *bufp; - if (((unsigned char)(c - '0')) <= 9) { - c -= '0'; - } else if ((unsigned char)((c|0x20) - 'a') <= 5) { - c = (unsigned char)((c|0x20) - 'a') + 10; - } else if (j && (c == ':' || c == 0)) { - break; - } else { - return -1; - } - ++bufp; - val <<= 4; - val += c; - } while (++j < 2); - *ptr++ = val; - } while (++i < ETH_ALEN); - - return *bufp; /* Error if we don't end at end of string. */ -} -#endif - int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ifconfig_main(int argc UNUSED_PARAM, char **argv) { diff --git a/networking/interface.c b/networking/interface.c index 9ae8b3f03..3dc5b3640 100644 --- a/networking/interface.c +++ b/networking/interface.c @@ -722,68 +722,15 @@ static char* FAST_FUNC ether_print(unsigned char *ptr) return buff; } -static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap); - static const struct hwtype ether_hwtype = { .name = "ether", .title = "Ethernet", .type = ARPHRD_ETHER, .alen = ETH_ALEN, .print = ether_print, - .input = ether_input + .input = in_ether }; -static unsigned hexchar2int(char c) -{ - if (isdigit(c)) - return c - '0'; - c &= ~0x20; /* a -> A */ - if ((unsigned)(c - 'A') <= 5) - return c - ('A' - 10); - return ~0U; -} - -/* Input an Ethernet address and convert to binary. */ -static int FAST_FUNC ether_input(const char *bufp, struct sockaddr *sap) -{ - unsigned char *ptr; - char c; - int i; - unsigned val; - - sap->sa_family = ether_hwtype.type; - ptr = (unsigned char*) sap->sa_data; - - i = 0; - while ((*bufp != '\0') && (i < ETH_ALEN)) { - val = hexchar2int(*bufp++) * 0x10; - if (val > 0xff) { - errno = EINVAL; - return -1; - } - c = *bufp; - if (c == ':' || c == 0) - val >>= 4; - else { - val |= hexchar2int(c); - if (val > 0xff) { - errno = EINVAL; - return -1; - } - } - if (c != 0) - bufp++; - *ptr++ = (unsigned char) val; - i++; - - /* We might get a semicolon here - not required. */ - if (*bufp == ':') { - bufp++; - } - } - return 0; -} - static const struct hwtype ppp_hwtype = { .name = "ppp", .title = "Point-to-Point Protocol", -- cgit v1.2.3-55-g6feb From 79c618c41193eaaa092cb977f06fc112155ba92b Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Tue, 30 Jul 2013 06:29:42 +0200 Subject: Refactor catv. Move visible() from stty to libbb. Fixes the following TODO: stty's visible() function and catv's guts are identical. Merge them into an appropriate libbb function. Also makes catv behave exactly like coreutils' cat -v e.g. it'll print 'M-^I' instead of 'M- '. function old new delta visible - 70 +70 do_display 431 379 -52 catv_main 306 250 -56 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/2 up/down: 70/-108) Total: -38 bytes Signed-off-by: Bartosz Golaszewski Signed-off-by: Denys Vlasenko --- TODO | 4 ---- coreutils/catv.c | 38 ++++++++++++++++---------------------- coreutils/stty.c | 39 +++++++-------------------------------- include/libbb.h | 4 ++++ libbb/printable.c | 24 ++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 58 deletions(-) (limited to 'TODO') diff --git a/TODO b/TODO index 8d0850cd7..d2a085ede 100644 --- a/TODO +++ b/TODO @@ -128,10 +128,6 @@ patch And while we're at it, a new patch filename quoting format is apparently coming soon: http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2 --- -stty / catv - stty's visible() function and catv's guts are identical. Merge them into - an appropriate libbb function. ---- tail ./busybox tail -f foo.c~ TODO should not print fmt=header_fmt for subsequent date >> TODO; i.e. only diff --git a/coreutils/catv.c b/coreutils/catv.c index 214b4311a..18b18104e 100644 --- a/coreutils/catv.c +++ b/coreutils/catv.c @@ -25,14 +25,20 @@ int catv_main(int argc UNUSED_PARAM, char **argv) { int retval = EXIT_SUCCESS; int fd; - unsigned flags; + unsigned opts; + int flags = 0; - flags = getopt32(argv, "etv"); + opts = getopt32(argv, "etv"); #define CATV_OPT_e (1<<0) #define CATV_OPT_t (1<<1) #define CATV_OPT_v (1<<2) - flags ^= CATV_OPT_v; argv += optind; + if (opts & (CATV_OPT_e | CATV_OPT_t)) + opts &= ~CATV_OPT_v; + if (opts & CATV_OPT_e) + flags |= VISIBLE_ENDLINE; + if (opts & CATV_OPT_t) + flags |= VISIBLE_SHOW_TABS; /* Read from stdin if there's nothing else to do. */ if (!argv[0]) @@ -50,29 +56,17 @@ int catv_main(int argc UNUSED_PARAM, char **argv) res = read(fd, read_buf, COMMON_BUFSIZE); if (res < 0) retval = EXIT_FAILURE; - if (res < 1) + if (res <= 0) break; for (i = 0; i < res; i++) { unsigned char c = read_buf[i]; - - if (c > 126 && (flags & CATV_OPT_v)) { - if (c == 127) { - printf("^?"); - continue; - } - printf("M-"); - c -= 128; - } - if (c < 32) { - if (c == 10) { - if (flags & CATV_OPT_e) - bb_putchar('$'); - } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { - printf("^%c", c+'@'); - continue; - } + if (opts & CATV_OPT_v) { + putchar(c); + } else { + char buf[sizeof("M-^c")]; + visible(c, buf, flags); + fputs(buf, stdout); } - bb_putchar(c); } } if (ENABLE_FEATURE_CLEAN_UP && fd) diff --git a/coreutils/stty.c b/coreutils/stty.c index d1e74f437..378a848e7 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -781,36 +781,6 @@ struct globals { G.max_col = 80; \ } while (0) - -/* Return a string that is the printable representation of character CH */ -/* Adapted from 'cat' by Torbjorn Granlund */ -static const char *visible(unsigned ch) -{ - char *bpout = G.buf; - - if (ch == _POSIX_VDISABLE) - return ""; - - if (ch >= 128) { - ch -= 128; - *bpout++ = 'M'; - *bpout++ = '-'; - } - - if (ch < 32) { - *bpout++ = '^'; - *bpout++ = ch + 64; - } else if (ch < 127) { - *bpout++ = ch; - } else { - *bpout++ = '^'; - *bpout++ = '?'; - } - - *bpout = '\0'; - return G.buf; -} - static void set_speed_or_die(enum speed_setting type, const char *arg, struct termios *mode) { @@ -1038,6 +1008,7 @@ static void do_display(const struct termios *mode, int all) #endif for (i = 0; i != CIDX_min; ++i) { + char ch; /* If swtch is the same as susp, don't print both */ #if VSWTCH == VSUSP if (i == CIDX_swtch) @@ -1051,8 +1022,12 @@ static void do_display(const struct termios *mode, int all) continue; } #endif - wrapf("%s = %s;", nth_string(control_name, i), - visible(mode->c_cc[control_info[i].offset])); + ch = mode->c_cc[control_info[i].offset]; + if (ch == _POSIX_VDISABLE) + strcpy(G.buf, ""); + else + visible(ch, G.buf, 0); + wrapf("%s = %s;", nth_string(control_name, i), G.buf); } #if VEOF == VMIN if ((mode->c_lflag & ICANON) == 0) diff --git a/include/libbb.h b/include/libbb.h index 83e9b5fb9..0c3734cdb 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -678,6 +678,10 @@ const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str); * else it is printed as-is (except for ch = 0x9b) */ enum { PRINTABLE_META = 0x100 }; void fputc_printable(int ch, FILE *file) FAST_FUNC; +/* Return a string that is the printable representation of character ch. + * Buffer must hold at least four characters. */ +enum { VISIBLE_SHOW_TABS = 1, VISIBLE_ENDLINE = 2 }; +void visible(unsigned ch, char *buf, int flags) FAST_FUNC; /* dmalloc will redefine these to it's own implementation. It is safe * to have the prototypes here unconditionally. */ diff --git a/libbb/printable.c b/libbb/printable.c index f6ada4904..9a423431e 100644 --- a/libbb/printable.c +++ b/libbb/printable.c @@ -32,3 +32,27 @@ void FAST_FUNC fputc_printable(int ch, FILE *file) } fputc(ch, file); } + +void FAST_FUNC visible(unsigned ch, char *buf, int flags) +{ + if (ch == '\t' && !(flags & VISIBLE_SHOW_TABS)) { + goto raw; + } + if (ch == '\n') { + if (flags & VISIBLE_ENDLINE) + *buf++ = '$'; + } else { + if (ch >= 128) { + ch -= 128; + *buf++ = 'M'; + *buf++ = '-'; + } + if (ch < 32 || ch == 127) { + *buf++ = '^'; + ch ^= 0x40; + } + } + raw: + *buf++ = ch; + *buf = '\0'; +} -- cgit v1.2.3-55-g6feb