/* vi: set sw=4 ts=4: */
/*
* universal getopt32 implementation for busybox
*
* Copyright (C) 2003-2005 Vladimir Oleynik
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#if ENABLE_LONG_OPTS
# include
#endif
#include "libbb.h"
//kbuild:lib-y += getopt32.o
/* Documentation
uint32_t
getopt32(char **argv, const char *applet_opts, ...)
The command line options are passed as the applet_opts string.
If one of the given options is found, a flag value is added to
the return value.
The flag value is determined by the position of the char in
applet_opts string. For example:
flags = getopt32(argv, "rnug");
"r" will set 1 (bit 0)
"n" will set 2 (bit 1)
"u" will set 4 (bit 2)
"g" will set 8 (bit 3)
and so on. You can also look at the return value as a bit
field and each option sets one bit.
On exit, global variable optind is set so that if you
will do argc -= optind; argv += optind; then
argc will be equal to number of remaining non-option
arguments, first one would be in argv[0], next in argv[1] and so on
(options and their parameters will be moved into argv[]
positions prior to argv[optind]).
"o:" If one of the options requires an argument, then add a ":"
after the char in applet_opts and provide a pointer to store
the argument. For example:
char *pointer_to_arg_for_a;
char *pointer_to_arg_for_b;
char *pointer_to_arg_for_c;
char *pointer_to_arg_for_d;
flags = getopt32(argv, "a:b:c:d:",
&pointer_to_arg_for_a, &pointer_to_arg_for_b,
&pointer_to_arg_for_c, &pointer_to_arg_for_d);
The type of the pointer may be controlled by "o::" or "o+" in
the external string opt_complementary (see below for more info).
"o::" If option can have an *optional* argument, then add a "::"
after its char in applet_opts and provide a pointer to store
the argument. Note that optional arguments _must_
immediately follow the option: -oparam, not -o param.
"o:+" This means that the parameter for this option is a nonnegative integer.
It will be processed with xatoi_positive() - allowed range
is 0..INT_MAX.
int param; // "unsigned param;" will also work
getopt32(argv, "p:+", ¶m);
"o:*" This means that the option can occur multiple times. Each occurrence
will be saved as a llist_t element instead of char*.
For example:
The grep applet can have one or more "-e pattern" arguments.
In this case you should use getopt32() as follows:
llist_t *patterns = NULL;
(this pointer must be initializated to NULL if the list is empty
as required by llist_add_to_end(llist_t **old_head, char *new_item).)
getopt32(argv, "e:*", &patterns);
$ grep -e user -e root /etc/passwd
root:x:0:0:root:/root:/bin/bash
user:x:500:500::/home/user:/bin/bash
"+" If the first character in the applet_opts string is a plus,
then option processing will stop as soon as a non-option is
encountered in the argv array. Useful for applets like env
which should not process arguments to subprograms:
env -i ls -d /
Here we want env to process just the '-i', not the '-d'.
"!" Report bad options, missing required options,
inconsistent options with all-ones return value (instead of abort).
"^" options string is "^optchars""\0""opt_complementary".
uint32_t
getopt32long(char **argv, const char *applet_opts, const char *logopts...)
This allows you to define long options:
static const char applet_longopts[] ALIGN1 =
//"name\0" has_arg val
"verbose\0" No_argument "v"
;
opt = getopt32long(argv, applet_opts, applet_longopts, ...);
The last element (val) typically is set to
matching short option from applet_opts. If there is no matching
char in applet_opts, then:
- return bit has next position after short options
- if has_arg is not "No_argument", use ptr for arg also
- opt_complementary affects it too
Note: a good applet will make long options configurable via the
config process and not a required feature. The current standard
is to name the config option CONFIG_FEATURE_