getopt32: add new syntax of 'o:+' and 'o:*' for -o NUM and -o LIST

In many cases, this aqllows to drop use of opt_complementary.
Approximately -400 bytes:

function                                             old     new   delta
getopt32                                            1423    1502     +79
opt_string                                            17      18      +1
OPT_STR                                               24      25      +1
uniq_main                                            416     406     -10
timeout_main                                         279     269     -10
sulogin_main                                         270     260     -10
readprofile_main                                    1825    1815     -10
ps_main                                              543     533     -10
pidof_main                                           245     235     -10
pgrep_main                                           611     601     -10
od_main                                             2600    2590     -10
mkfs_minix_main                                     2684    2674     -10
mkfs_ext2_main                                      2603    2593     -10
microcom_main                                        712     702     -10
makemime_main                                        315     305     -10
ionice_main                                          282     272     -10
inetd_main                                          2074    2064     -10
ifplugd_main                                        1144    1134     -10
halt_main                                            353     343     -10
getopt_main                                          636     626     -10
fdisk_main                                          2854    2844     -10
env_main                                             206     196     -10
dmesg_main                                           319     309     -10
conspy_main                                         1214    1204     -10
awk_main                                             981     971     -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/22 up/down: 81/-220)         Total: -139 bytes
   text	   data	    bss	    dec	    hex	filename
 919373	    906	  14060	 934339	  e41c3	busybox_old
 918969	    906	  14060	 933935	  e402f	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2016-07-06 21:58:02 +02:00
parent 0844b5afe2
commit 237bedd499
58 changed files with 168 additions and 159 deletions

View File

@@ -295,8 +295,8 @@ int arping_main(int argc UNUSED_PARAM, char **argv)
/* Dad also sets quit_on_reply.
* Advert also sets unsolicited.
*/
opt_complementary = "=1:Df:AU:c+";
opt = getopt32(argv, "DUAqfbc:w:I:s:",
opt_complementary = "=1:Df:AU";
opt = getopt32(argv, "DUAqfbc:+w:I:s:",
&count, &str_timeout, &device, &source);
if (opt & 0x80) /* -w: timeout */
timeout_us = xatou_range(str_timeout, 0, INT_MAX/2000000) * 1000000 + 500000;

View File

@@ -1130,11 +1130,11 @@ int ftpd_main(int argc UNUSED_PARAM, char **argv)
abs_timeout = 1 * 60 * 60;
verbose_S = 0;
G.timeout = 2 * 60;
opt_complementary = "t+:T+:vv:SS";
opt_complementary = "vv:SS";
#if BB_MMU
opts = getopt32(argv, "vS" IF_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose, &verbose_S);
opts = getopt32(argv, "vS" IF_FEATURE_FTP_WRITE("w") "t:+T:+", &G.timeout, &abs_timeout, &G.verbose, &verbose_S);
#else
opts = getopt32(argv, "l1AvS" IF_FEATURE_FTP_WRITE("w") "t:T:", &G.timeout, &abs_timeout, &G.verbose, &verbose_S);
opts = getopt32(argv, "l1AvS" IF_FEATURE_FTP_WRITE("w") "t:+T:+", &G.timeout, &abs_timeout, &G.verbose, &verbose_S);
if (opts & (OPT_l|OPT_1)) {
/* Our secret backdoor to ls */
/* TODO: pass --group-directories-first? would be nice, but ls doesn't do that yet */

View File

@@ -107,9 +107,9 @@ enum {
#endif
};
#if ENABLE_FEATURE_PIDFILE
# define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:Mk"
# define OPTION_STR "+ansfFi:r:It:+u:+d:+m:pqlx:Mk"
#else
# define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:M"
# define OPTION_STR "+ansfFi:r:It:+u:+d:+m:pqlx:M"
#endif
enum { // interface status
@@ -560,7 +560,6 @@ int ifplugd_main(int argc UNUSED_PARAM, char **argv)
INIT_G();
opt_complementary = "t+:u+:d+";
opts = getopt32(argv, OPTION_STR,
&G.iface, &G.script_name, &G.poll_time, &G.delay_up,
&G.delay_down, &G.api_mode, &G.extra_arg);

View File

@@ -1153,8 +1153,8 @@ int inetd_main(int argc UNUSED_PARAM, char **argv)
if (real_uid != 0) /* run by non-root user */
config_filename = NULL;
opt_complementary = "R+:q+"; /* -q N, -R N */
opt = getopt32(argv, "R:feq:", &max_concurrency, &global_queuelen);
/* -q N, -R N */
opt = getopt32(argv, "R:+feq:+", &max_concurrency, &global_queuelen);
argv += optind;
//argc -= optind;
if (argv[0])

View File

@@ -794,8 +794,8 @@ int nc_main(int argc UNUSED_PARAM, char **argv)
e_found:
// -g -G -t -r deleted, unimplemented -a deleted too
opt_complementary = "?2:vv:ll:w+"; /* max 2 params; -v and -l are counters; -w N */
getopt32(argv, "np:s:uvw:" IF_NC_SERVER("lk")
opt_complementary = "?2:vv:ll"; /* max 2 params; -v and -l are counters; -w N */
getopt32(argv, "np:s:uvw:+" IF_NC_SERVER("lk")
IF_NC_EXTRA("i:o:z"),
&str_p, &str_s, &o_wait
IF_NC_EXTRA(, &str_i, &str_o), &o_verbose IF_NC_SERVER(, &cnt_l));

View File

@@ -2197,11 +2197,11 @@ static NOINLINE void ntp_init(char **argv)
/* Parse options */
peers = NULL;
opt_complementary = "dd:p::wn" /* -d: counter; -p: list; -w implies -n */
opt_complementary = "dd:wn" /* -d: counter; -p: list; -w implies -n */
IF_FEATURE_NTPD_SERVER(":Il"); /* -I implies -l */
opts = getopt32(argv,
"nqNx" /* compat */
"wp:S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
"wp:*S:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */
IF_FEATURE_NTPD_SERVER("I:") /* compat */
"d" /* compat */
"46aAbgL", /* compat, ignored */

View File

@@ -341,7 +341,7 @@ static int common_ping_main(sa_family_t af, char **argv)
/* Full(er) version */
#define OPT_STRING ("qvc:s:t:w:W:I:np:4" IF_PING6("6"))
#define OPT_STRING ("qvc:+s:t:+w:+W:+I:np:4" IF_PING6("6"))
enum {
OPT_QUIET = 1 << 0,
OPT_VERBOSE = 1 << 1,
@@ -865,7 +865,7 @@ static int common_ping_main(int opt, char **argv)
INIT_G();
/* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */
opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+";
opt_complementary = "=1:q--v:v--q";
opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p);
if (opt & OPT_s)
datalen = xatou16(str_s); // -s

View File

@@ -232,9 +232,9 @@ int tcpudpsvd_main(int argc UNUSED_PARAM, char **argv)
tcp = (applet_name[0] == 't');
/* 3+ args, -i at most once, -p implies -h, -v is counter, -b N, -c N */
opt_complementary = "-3:i--i:ph:vv:b+:c+";
opt_complementary = "-3:i--i:ph:vv";
#ifdef SSLSVD
opts = getopt32(argv, "+c:C:i:x:u:l:Eb:hpt:vU:/:Z:K:",
opts = getopt32(argv, "+c:+C:i:x:u:l:Eb:+hpt:vU:/:Z:K:",
&cmax, &str_C, &instructs, &instructs, &user, &preset_local_hostname,
&backlog, &str_t, &ssluser, &root, &cert, &key, &verbose
);

View File

@@ -496,12 +496,12 @@ int telnetd_main(int argc UNUSED_PARAM, char **argv)
INIT_G();
/* -w NUM, and implies -F. -w and -i don't mix */
IF_FEATURE_TELNETD_INETD_WAIT(opt_complementary = "wF:w+:i--w:w--i";)
IF_FEATURE_TELNETD_INETD_WAIT(opt_complementary = "wF:i--w:w--i";)
/* Even if !STANDALONE, we accept (and ignore) -i, thus people
* don't need to guess whether it's ok to pass -i to us */
opt = getopt32(argv, "f:l:Ki"
IF_FEATURE_TELNETD_STANDALONE("p:b:F")
IF_FEATURE_TELNETD_INETD_WAIT("Sw:"),
IF_FEATURE_TELNETD_INETD_WAIT("Sw:+"),
&G.issuefile, &G.loginpath
IF_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr)
IF_FEATURE_TELNETD_INETD_WAIT(, &sec_linger)

View File

@@ -294,7 +294,7 @@
#define OPT_STRING \
"FIlnrdvxt:i:m:p:q:s:w:z:f:" \
IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:") \
IF_FEATURE_TRACEROUTE_SOURCE_ROUTE("g:*") \
"4" IF_TRACEROUTE6("6")
enum {
OPT_DONT_FRAGMNT = (1 << 0), /* F */
@@ -819,7 +819,7 @@ common_traceroute_main(int op, char **argv)
INIT_G();
/* minimum 1 arg */
opt_complementary = "-1:x-x" IF_FEATURE_TRACEROUTE_SOURCE_ROUTE(":g::");
opt_complementary = "-1:x-x";
op |= getopt32(argv, OPT_STRING
, &tos_str, &device, &max_ttl_str, &port_str, &nprobes_str
, &source, &waittime_str, &pausemsecs_str, &first_ttl_str

View File

@@ -944,9 +944,9 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
/* Parse command line */
/* O,x: list; -T,-t,-A take numeric param */
opt_complementary = "O::x::T+:t+:A+" IF_UDHCP_VERBOSE(":vv") ;
IF_UDHCP_VERBOSE(opt_complementary = "vv";)
IF_LONG_OPTS(applet_long_options = udhcpc6_longopts;)
opt = getopt32(argv, "i:np:qRr:s:T:t:SA:O:ox:f"
opt = getopt32(argv, "i:np:qRr:s:T:+t:+SA:+O:*ox:*f"
USE_FOR_MMU("b")
///IF_FEATURE_UDHCPC_ARPING("a")
IF_FEATURE_UDHCP_PORT("P:")

View File

@@ -1283,9 +1283,9 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
/* Parse command line */
/* O,x: list; -T,-t,-A take numeric param */
opt_complementary = "O::x::T+:t+:A+" IF_UDHCP_VERBOSE(":vv") ;
IF_UDHCP_VERBOSE(opt_complementary = "vv";)
IF_LONG_OPTS(applet_long_options = udhcpc_longopts;)
opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:t:SA:O:ox:fB"
opt = getopt32(argv, "CV:H:h:F:i:np:qRr:s:T:+t:+SA:+O:*ox:*fB"
USE_FOR_MMU("b")
IF_FEATURE_UDHCPC_ARPING("a::")
IF_FEATURE_UDHCP_PORT("P:")

View File

@@ -1268,9 +1268,8 @@ IF_DESKTOP( "no-parent\0" No_argument "\xf0")
applet_long_options = wget_longopts;
#endif
opt_complementary = "-1" /* at least one URL */
IF_FEATURE_WGET_TIMEOUT(":T+") /* -T NUM */
IF_FEATURE_WGET_LONG_OPTIONS(":\xff::"); /* --header is a list */
getopt32(argv, "csqO:P:Y:U:T:"
getopt32(argv, "csqO:P:Y:U:T:+"
/*ignored:*/ "t:"
/*ignored:*/ "n::"
/* wget has exactly four -n<letter> opts, all of which we can ignore:

View File

@@ -167,8 +167,8 @@ int whois_main(int argc UNUSED_PARAM, char **argv)
int port = 43;
const char *host = "whois.iana.org";
opt_complementary = "-1:p+";
getopt32(argv, "ih:p:", &host, &port);
opt_complementary = "-1";
getopt32(argv, "ih:p:+", &host, &port);
argv += optind;
do {