/*
* Short options for tar. Please keep this sorted.
*/
static const char *short_options
= "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
/*
* Long options for tar. Please keep this list sorted.
*
* The symbolic names for options that lack a short equivalent are
* defined in bsdtar.h. Also note that so far I've found no need
* to support optional arguments to long options. That would be
* a small change to the code below.
*/
/*
* This getopt implementation has two key features that common
* getopt_long() implementations lack. Apart from those, it's a
* straightforward option parser, considerably simplified by not
* needing to support the wealth of exotic getopt_long() features. It
* has, of course, been shamelessly tailored for bsdtar. (If you're
* looking for a generic getopt_long() implementation for your
* project, I recommend Gregory Pietsch's public domain getopt_long()
* implementation.) The two additional features are:
*
* Old-style tar arguments: The original tar implementation treated
* the first argument word as a list of single-character option
* letters. All arguments follow as separate words. For example,
* tar xbf 32 /dev/tape
* Here, the "xbf" is three option letters, "32" is the argument for
* "b" and "/dev/tape" is the argument for "f". We support this usage
* if the first command-line argument does not begin with '-'. We
* also allow regular short and long options to follow, e.g.,
* tar xbf 32 /dev/tape -P --format=pax
*
* -W long options: There's an obscure GNU convention (only rarely
* supported even there) that allows "-W option=argument" as an
* alternative way to support long options. This was supported in
* early bsdtar as a way to access long options on platforms that did
* not support getopt_long() and is preserved here for backwards
* compatibility. (Of course, if I'd started with a custom
* command-line parser from the beginning, I would have had normal
* long option support on every platform so that hack wouldn't have
* been necessary. Oh, well. Some mistakes you just have to live
* with.)
*
* TODO: We should be able to use this to pull files and intermingled
* options (such as -C) from the command line in write mode. That
* will require a little rethinking of the argument handling in
* bsdtar.c.
*
* TODO: If we want to support arbitrary command-line options from -T
* input (as GNU tar does), we may need to extend this to handle option
* words from sources other than argv/argc. I'm not really sure if I
* like that feature of GNU tar, so it's certainly not a priority.
*/
/* First time through, initialize everything. */
if (bsdtar->getopt_state == state_start) {
/* Skip program name. */
++bsdtar->argv;
--bsdtar->argc;
if (*bsdtar->argv == NULL)
return (-1);
/* Decide between "new style" and "old style" arguments. */
if (bsdtar->argv[0][0] == '-') {
bsdtar->getopt_state = state_next_word;
} else {
bsdtar->getopt_state = state_old_tar;
bsdtar->getopt_word = *bsdtar->argv++;
--bsdtar->argc;
}
}
/*
* We're parsing old-style tar arguments
*/
if (bsdtar->getopt_state == state_old_tar) {
/* Get the next option character. */
opt = *bsdtar->getopt_word++;
if (opt == '\0') {
/* New-style args can follow old-style. */
bsdtar->getopt_state = state_next_word;
} else {
/* See if it takes an argument. */
p = strchr(short_options, opt);
if (p == NULL)
return ('?');
if (p[1] == ':') {
bsdtar->argument = *bsdtar->argv;
if (bsdtar->argument == NULL) {
lafe_warnc(0,
"Option %c requires an argument",
opt);
return ('?');
}
++bsdtar->argv;
--bsdtar->argc;
}
}
}
/*
* We're ready to look at the next word in argv.
*/
if (bsdtar->getopt_state == state_next_word) {
/* No more arguments, so no more options. */
if (bsdtar->argv[0] == NULL)
return (-1);
/* Doesn't start with '-', so no more options. */
if (bsdtar->argv[0][0] != '-')
return (-1);
/* "--" marks end of options; consume it and return. */
if (strcmp(bsdtar->argv[0], "--") == 0) {
++bsdtar->argv;
--bsdtar->argc;
return (-1);
}
/* Get next word for parsing. */
bsdtar->getopt_word = *bsdtar->argv++;
--bsdtar->argc;
if (bsdtar->getopt_word[1] == '-') {
/* Set up long option parser. */
bsdtar->getopt_state = state_long;
bsdtar->getopt_word += 2; /* Skip leading '--' */
} else {
/* Set up short option parser. */
bsdtar->getopt_state = state_short;
++bsdtar->getopt_word; /* Skip leading '-' */
}
}
/*
* We're parsing a group of POSIX-style single-character options.
*/
if (bsdtar->getopt_state == state_short) {
/* Peel next option off of a group of short options. */
opt = *bsdtar->getopt_word++;
if (opt == '\0') {
/* End of this group; recurse to get next option. */
bsdtar->getopt_state = state_next_word;
goto again;
}
/* Does this option take an argument? */
p = strchr(short_options, opt);
if (p == NULL)
return ('?');
if (p[1] == ':')
required = 1;
/* If it takes an argument, parse that. */
if (required) {
/* If arg is run-in, bsdtar->getopt_word already points to it. */
if (bsdtar->getopt_word[0] == '\0') {
/* Otherwise, pick up the next word. */
bsdtar->getopt_word = *bsdtar->argv;
if (bsdtar->getopt_word == NULL) {
lafe_warnc(0,
"Option -%c requires an argument",
opt);
return ('?');
}
++bsdtar->argv;
--bsdtar->argc;
}
if (opt == 'W') {
bsdtar->getopt_state = state_long;
long_prefix = "-W "; /* For clearer errors. */
} else {
bsdtar->getopt_state = state_next_word;
bsdtar->argument = bsdtar->getopt_word;
}
}
}
/* We're reading a long option, including -W long=arg convention. */
if (bsdtar->getopt_state == state_long) {
/* After this long option, we'll be starting a new word. */
bsdtar->getopt_state = state_next_word;
/* Option name ends at '=' if there is one. */
p = strchr(bsdtar->getopt_word, '=');
if (p != NULL) {
optlength = (size_t)(p - bsdtar->getopt_word);
bsdtar->argument = (char *)(uintptr_t)(p + 1);
} else {
optlength = strlen(bsdtar->getopt_word);
}
/* Search the table for an unambiguous match. */
for (popt = tar_longopts; popt->name != NULL; popt++) {
/* Short-circuit if first chars don't match. */
if (popt->name[0] != bsdtar->getopt_word[0])
continue;
/* If option is a prefix of name in table, record it.*/
if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
match2 = match; /* Record up to two matches. */
match = popt;
/* If it's an exact match, we're done. */
if (strlen(popt->name) == optlength) {
match2 = NULL; /* Forget the others. */
break;
}
}
}
/* Fail if there wasn't a unique match. */
if (match == NULL) {
lafe_warnc(0,
"Option %s%s is not supported",
long_prefix, bsdtar->getopt_word);
return ('?');
}
if (match2 != NULL) {
lafe_warnc(0,
"Ambiguous option %s%s (matches --%s and --%s)",
long_prefix, bsdtar->getopt_word, match->name, match2->name);
return ('?');
}
/* We've found a unique match; does it need an argument? */
if (match->required) {
/* Argument required: get next word if necessary. */
if (bsdtar->argument == NULL) {
bsdtar->argument = *bsdtar->argv;
if (bsdtar->argument == NULL) {
lafe_warnc(0,
"Option %s%s requires an argument",
long_prefix, match->name);
return ('?');
}
++bsdtar->argv;
--bsdtar->argc;
}
} else {
/* Argument forbidden: fail if there is one. */
if (bsdtar->argument != NULL) {
lafe_warnc(0,
"Option %s%s does not allow an argument",
long_prefix, match->name);
return ('?');
}
}
return (match->equivalent);
}