mirror of
https://github.com/sheumann/hush.git
synced 2025-03-04 01:30:00 +00:00
getopt_ulflags: formatting fixes
This commit is contained in:
parent
6248a734e2
commit
b02ef82c99
@ -13,7 +13,7 @@
|
|||||||
/* Documentation
|
/* Documentation
|
||||||
|
|
||||||
unsigned long
|
unsigned long
|
||||||
bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
|
||||||
|
|
||||||
The command line options must be declared in const char
|
The command line options must be declared in const char
|
||||||
*applet_opts as a string of chars, for example:
|
*applet_opts as a string of chars, for example:
|
||||||
@ -74,6 +74,7 @@ const struct option *bb_applet_long_options
|
|||||||
(see getopt(3))
|
(see getopt(3))
|
||||||
|
|
||||||
static const struct option applet_long_options[] = {
|
static const struct option applet_long_options[] = {
|
||||||
|
//name,has_arg,flag,val
|
||||||
{ "verbose", 0, 0, 'v' },
|
{ "verbose", 0, 0, 'v' },
|
||||||
{ 0, 0, 0, 0 }
|
{ 0, 0, 0, 0 }
|
||||||
};
|
};
|
||||||
@ -120,7 +121,7 @@ const char *bb_opt_complementally
|
|||||||
int w_counter = 0;
|
int w_counter = 0;
|
||||||
bb_opt_complementally = "ww";
|
bb_opt_complementally = "ww";
|
||||||
bb_getopt_ulflags(argc, argv, "w", &w_counter);
|
bb_getopt_ulflags(argc, argv, "w", &w_counter);
|
||||||
if(w_counter)
|
if (w_counter)
|
||||||
width = (w_counter == 1) ? 132 : INT_MAX;
|
width = (w_counter == 1) ? 132 : INT_MAX;
|
||||||
else
|
else
|
||||||
get_terminal_width(...&width...);
|
get_terminal_width(...&width...);
|
||||||
@ -136,11 +137,11 @@ const char *bb_opt_complementally
|
|||||||
int verbose_level = 0;
|
int verbose_level = 0;
|
||||||
bb_opt_complementally = "vv:b::b-c:c-b";
|
bb_opt_complementally = "vv:b::b-c:c-b";
|
||||||
f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level);
|
f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level);
|
||||||
if(f & 2) // -c after -b unsets -b flag
|
if (f & 2) // -c after -b unsets -b flag
|
||||||
while(my_b) { dosomething_with(my_b->data); my_b = my_b->link; }
|
while (my_b) { dosomething_with(my_b->data); my_b = my_b->link; }
|
||||||
if(my_b) // but llist is stored if -b is specified
|
if (my_b) // but llist is stored if -b is specified
|
||||||
free_llist(my_b);
|
free_llist(my_b);
|
||||||
if(verbose_level) bb_printf("verbose level is %d\n", verbose_level);
|
if (verbose_level) bb_printf("verbose level is %d\n", verbose_level);
|
||||||
|
|
||||||
Special characters:
|
Special characters:
|
||||||
|
|
||||||
@ -207,6 +208,9 @@ Special characters:
|
|||||||
if (flags & BB_GETOPT_ERROR)
|
if (flags & BB_GETOPT_ERROR)
|
||||||
bb_show_usage();
|
bb_show_usage();
|
||||||
|
|
||||||
|
"x--x" Variation of the above, it means that -x option should occur
|
||||||
|
at most once.
|
||||||
|
|
||||||
"?" A "?" as the first char in a bb_opt_complementally group means:
|
"?" A "?" as the first char in a bb_opt_complementally group means:
|
||||||
if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage
|
if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage
|
||||||
and exit instead. Next char after '?' can't be a digit.
|
and exit instead. Next char after '?' can't be a digit.
|
||||||
@ -255,7 +259,7 @@ Special characters:
|
|||||||
$ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng
|
$ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng
|
||||||
|
|
||||||
"X" A bb_opt_complementally group with just a single letter means
|
"X" A bb_opt_complementally group with just a single letter means
|
||||||
that this this option is required. If more than one such group exists,
|
that this option is required. If more than one such group exists,
|
||||||
at least one option is required to occur (not all of them).
|
at least one option is required to occur (not all of them).
|
||||||
For example from "start-stop-daemon" applet:
|
For example from "start-stop-daemon" applet:
|
||||||
|
|
||||||
@ -263,9 +267,6 @@ Special characters:
|
|||||||
bb_opt_complementally = "K:S:?K--S:S--K";
|
bb_opt_complementally = "K:S:?K--S:S--K";
|
||||||
flags = bb_getopt_ulflags(argc, argv, "KS...);
|
flags = bb_getopt_ulflags(argc, argv, "KS...);
|
||||||
|
|
||||||
|
|
||||||
"x--x" give error if double or more used -x option
|
|
||||||
|
|
||||||
Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as
|
Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as
|
||||||
"?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args;
|
"?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args;
|
||||||
if there is a '-2' option then unset '-3', '-X' and '-a'; if there is
|
if there is a '-2' option then unset '-3', '-X' and '-a'; if there is
|
||||||
@ -299,7 +300,7 @@ const struct option *bb_applet_long_options = bb_default_long_options;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
unsigned long
|
unsigned long
|
||||||
bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
|
||||||
{
|
{
|
||||||
unsigned long flags = 0;
|
unsigned long flags = 0;
|
||||||
unsigned long requires = 0;
|
unsigned long requires = 0;
|
||||||
@ -324,7 +325,7 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
#define FREE_FIRST_ARGV_IS_OPT 8
|
#define FREE_FIRST_ARGV_IS_OPT 8
|
||||||
int spec_flgs = 0;
|
int spec_flgs = 0;
|
||||||
|
|
||||||
va_start (p, applet_opts);
|
va_start(p, applet_opts);
|
||||||
|
|
||||||
c = 0;
|
c = 0;
|
||||||
on_off = complementally;
|
on_off = complementally;
|
||||||
@ -332,15 +333,15 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
|
|
||||||
/* skip GNU extension */
|
/* skip GNU extension */
|
||||||
s = (const unsigned char *)applet_opts;
|
s = (const unsigned char *)applet_opts;
|
||||||
if(*s == '+' || *s == '-')
|
if (*s == '+' || *s == '-')
|
||||||
s++;
|
s++;
|
||||||
for (; *s; s++) {
|
for (; *s; s++) {
|
||||||
if(c >= (int)(sizeof(flags)*8))
|
if (c >= (int)(sizeof(flags)*8))
|
||||||
break;
|
break;
|
||||||
on_off->opt = *s;
|
on_off->opt = *s;
|
||||||
on_off->switch_on = (1 << c);
|
on_off->switch_on = (1 << c);
|
||||||
if (s[1] == ':') {
|
if (s[1] == ':') {
|
||||||
on_off->optarg = va_arg (p, void **);
|
on_off->optarg = va_arg(p, void **);
|
||||||
do
|
do
|
||||||
s++;
|
s++;
|
||||||
while (s[1] == ':');
|
while (s[1] == ':');
|
||||||
@ -350,19 +351,19 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if ENABLE_GETOPT_LONG
|
#if ENABLE_GETOPT_LONG
|
||||||
for(l_o = bb_applet_long_options; l_o->name; l_o++) {
|
for (l_o = bb_applet_long_options; l_o->name; l_o++) {
|
||||||
if(l_o->flag)
|
if (l_o->flag)
|
||||||
continue;
|
continue;
|
||||||
for(on_off = complementally; on_off->opt != 0; on_off++)
|
for (on_off = complementally; on_off->opt != 0; on_off++)
|
||||||
if(on_off->opt == l_o->val)
|
if (on_off->opt == l_o->val)
|
||||||
break;
|
break;
|
||||||
if(on_off->opt == 0) {
|
if (on_off->opt == 0) {
|
||||||
if(c >= (int)(sizeof(flags)*8))
|
if (c >= (int)(sizeof(flags)*8))
|
||||||
break;
|
break;
|
||||||
on_off->opt = l_o->val;
|
on_off->opt = l_o->val;
|
||||||
on_off->switch_on = (1 << c);
|
on_off->switch_on = (1 << c);
|
||||||
if(l_o->has_arg != no_argument)
|
if (l_o->has_arg != no_argument)
|
||||||
on_off->optarg = va_arg (p, void **);
|
on_off->optarg = va_arg(p, void **);
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -374,8 +375,8 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
if (*s == ':')
|
if (*s == ':')
|
||||||
continue;
|
continue;
|
||||||
c = s[1];
|
c = s[1];
|
||||||
if(*s == '?') {
|
if (*s == '?') {
|
||||||
if(c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
spec_flgs |= SHOW_USAGE_IF_ERROR;
|
spec_flgs |= SHOW_USAGE_IF_ERROR;
|
||||||
} else {
|
} else {
|
||||||
max_arg = c - '0';
|
max_arg = c - '0';
|
||||||
@ -383,9 +384,9 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(*s == '-') {
|
if (*s == '-') {
|
||||||
if(c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
if(c == '-') {
|
if (c == '-') {
|
||||||
spec_flgs |= FIRST_ARGV_IS_OPT;
|
spec_flgs |= FIRST_ARGV_IS_OPT;
|
||||||
s++;
|
s++;
|
||||||
} else
|
} else
|
||||||
@ -399,99 +400,99 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
|
|||||||
for (on_off = complementally; on_off->opt; on_off++)
|
for (on_off = complementally; on_off->opt; on_off++)
|
||||||
if (on_off->opt == *s)
|
if (on_off->opt == *s)
|
||||||
break;
|
break;
|
||||||
if(c == ':' && s[2] == ':') {
|
if (c == ':' && s[2] == ':') {
|
||||||
on_off->list_flg++;
|
on_off->list_flg++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(c == ':' || c == '\0') {
|
if (c == ':' || c == '\0') {
|
||||||
requires |= on_off->switch_on;
|
requires |= on_off->switch_on;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(c == '-' && (s[2] == ':' || s[2] == '\0')) {
|
if (c == '-' && (s[2] == ':' || s[2] == '\0')) {
|
||||||
flags |= on_off->switch_on;
|
flags |= on_off->switch_on;
|
||||||
on_off->incongruously |= on_off->switch_on;
|
on_off->incongruously |= on_off->switch_on;
|
||||||
s++;
|
s++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(c == *s) {
|
if (c == *s) {
|
||||||
on_off->counter = va_arg (p, int *);
|
on_off->counter = va_arg(p, int *);
|
||||||
s++;
|
s++;
|
||||||
}
|
}
|
||||||
pair = on_off;
|
pair = on_off;
|
||||||
pair_switch = &(pair->switch_on);
|
pair_switch = &(pair->switch_on);
|
||||||
for(s++; *s && *s != ':'; s++) {
|
for (s++; *s && *s != ':'; s++) {
|
||||||
if(*s == '?') {
|
if (*s == '?') {
|
||||||
pair_switch = &(pair->requires);
|
pair_switch = &(pair->requires);
|
||||||
} else if (*s == '-') {
|
} else if (*s == '-') {
|
||||||
if(pair_switch == &(pair->switch_off))
|
if (pair_switch == &(pair->switch_off))
|
||||||
pair_switch = &(pair->incongruously);
|
pair_switch = &(pair->incongruously);
|
||||||
else
|
else
|
||||||
pair_switch = &(pair->switch_off);
|
pair_switch = &(pair->switch_off);
|
||||||
} else {
|
} else {
|
||||||
for (on_off = complementally; on_off->opt; on_off++)
|
for (on_off = complementally; on_off->opt; on_off++)
|
||||||
if (on_off->opt == *s) {
|
if (on_off->opt == *s) {
|
||||||
*pair_switch |= on_off->switch_on;
|
*pair_switch |= on_off->switch_on;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s--;
|
s--;
|
||||||
}
|
}
|
||||||
va_end (p);
|
va_end (p);
|
||||||
|
|
||||||
#if defined(CONFIG_AR) || defined(CONFIG_TAR)
|
#if ENABLE_AR || ENABLE_TAR
|
||||||
if((spec_flgs & FIRST_ARGV_IS_OPT)) {
|
if (spec_flgs & FIRST_ARGV_IS_OPT) {
|
||||||
if(argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
|
if (argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
|
||||||
argv[1] = xasprintf("-%s", argv[1]);
|
argv[1] = xasprintf("-%s", argv[1]);
|
||||||
if(ENABLE_FEATURE_CLEAN_UP)
|
if (ENABLE_FEATURE_CLEAN_UP)
|
||||||
spec_flgs |= FREE_FIRST_ARGV_IS_OPT;
|
spec_flgs |= FREE_FIRST_ARGV_IS_OPT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if ENABLE_GETOPT_LONG
|
#if ENABLE_GETOPT_LONG
|
||||||
while ((c = getopt_long (argc, argv, applet_opts,
|
while ((c = getopt_long(argc, argv, applet_opts,
|
||||||
bb_applet_long_options, NULL)) >= 0) {
|
bb_applet_long_options, NULL)) >= 0) {
|
||||||
#else
|
#else
|
||||||
while ((c = getopt (argc, argv, applet_opts)) >= 0) {
|
while ((c = getopt(argc, argv, applet_opts)) >= 0) {
|
||||||
#endif /* ENABLE_GETOPT_LONG */
|
#endif /* ENABLE_GETOPT_LONG */
|
||||||
#ifdef CONFIG_PS
|
#if ENABLE_PS
|
||||||
loop_arg_is_opt:
|
loop_arg_is_opt:
|
||||||
#endif
|
#endif
|
||||||
for (on_off = complementally; on_off->opt != c; on_off++) {
|
for (on_off = complementally; on_off->opt != c; on_off++) {
|
||||||
/* c==0 if long opt have non NULL flag */
|
/* c==0 if long opt have non NULL flag */
|
||||||
if(on_off->opt == 0 && c != 0)
|
if (on_off->opt == 0 && c != 0)
|
||||||
bb_show_usage ();
|
bb_show_usage();
|
||||||
}
|
}
|
||||||
if(flags & on_off->incongruously) {
|
if (flags & on_off->incongruously) {
|
||||||
if((spec_flgs & SHOW_USAGE_IF_ERROR))
|
if ((spec_flgs & SHOW_USAGE_IF_ERROR))
|
||||||
bb_show_usage ();
|
bb_show_usage();
|
||||||
flags |= BB_GETOPT_ERROR;
|
flags |= BB_GETOPT_ERROR;
|
||||||
}
|
}
|
||||||
trigger = on_off->switch_on & on_off->switch_off;
|
trigger = on_off->switch_on & on_off->switch_off;
|
||||||
flags &= ~(on_off->switch_off ^ trigger);
|
flags &= ~(on_off->switch_off ^ trigger);
|
||||||
flags |= on_off->switch_on ^ trigger;
|
flags |= on_off->switch_on ^ trigger;
|
||||||
flags ^= trigger;
|
flags ^= trigger;
|
||||||
if(on_off->counter)
|
if (on_off->counter)
|
||||||
(*(on_off->counter))++;
|
(*(on_off->counter))++;
|
||||||
if(on_off->list_flg) {
|
if (on_off->list_flg) {
|
||||||
llist_add_to((llist_t **)(on_off->optarg), optarg);
|
llist_add_to((llist_t **)(on_off->optarg), optarg);
|
||||||
} else if (on_off->optarg) {
|
} else if (on_off->optarg) {
|
||||||
*(char **)(on_off->optarg) = optarg;
|
*(char **)(on_off->optarg) = optarg;
|
||||||
}
|
}
|
||||||
#ifdef CONFIG_PS
|
#if ENABLE_PS
|
||||||
if(pargv != NULL)
|
if (pargv != NULL)
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_PS
|
#if ENABLE_PS
|
||||||
if((spec_flgs & ALL_ARGV_IS_OPTS)) {
|
if (spec_flgs & ALL_ARGV_IS_OPTS) {
|
||||||
/* process argv is option, for example "ps" applet */
|
/* process argv is option, for example "ps" applet */
|
||||||
if(pargv == NULL)
|
if (pargv == NULL)
|
||||||
pargv = argv + optind;
|
pargv = argv + optind;
|
||||||
while(*pargv) {
|
while (*pargv) {
|
||||||
c = **pargv;
|
c = **pargv;
|
||||||
if(c == '\0') {
|
if (c == '\0') {
|
||||||
pargv++;
|
pargv++;
|
||||||
} else {
|
} else {
|
||||||
(*pargv)++;
|
(*pargv)++;
|
||||||
@ -501,21 +502,20 @@ loop_arg_is_opt:
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if (defined(CONFIG_AR) || defined(CONFIG_TAR)) && \
|
#if (ENABLE_AR || ENABLE_TAR) && ENABLE_FEATURE_CLEAN_UP
|
||||||
defined(CONFIG_FEATURE_CLEAN_UP)
|
if (spec_flgs & FREE_FIRST_ARGV_IS_OPT)
|
||||||
if((spec_flgs & FREE_FIRST_ARGV_IS_OPT))
|
|
||||||
free(argv[1]);
|
free(argv[1]);
|
||||||
#endif
|
#endif
|
||||||
/* check depending requires for given options */
|
/* check depending requires for given options */
|
||||||
for (on_off = complementally; on_off->opt; on_off++) {
|
for (on_off = complementally; on_off->opt; on_off++) {
|
||||||
if(on_off->requires && (flags & on_off->switch_on) &&
|
if (on_off->requires && (flags & on_off->switch_on) &&
|
||||||
(flags & on_off->requires) == 0)
|
(flags & on_off->requires) == 0)
|
||||||
bb_show_usage ();
|
bb_show_usage();
|
||||||
}
|
}
|
||||||
if(requires && (flags & requires) == 0)
|
if (requires && (flags & requires) == 0)
|
||||||
bb_show_usage ();
|
bb_show_usage();
|
||||||
argc -= optind;
|
argc -= optind;
|
||||||
if(argc < min_arg || (max_arg >= 0 && argc > max_arg))
|
if (argc < min_arg || (max_arg >= 0 && argc > max_arg))
|
||||||
bb_show_usage ();
|
bb_show_usage();
|
||||||
return flags;
|
return flags;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user