/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2003-2008 Tim Kientzle4* All rights reserved.5*/67/*8* Command line parser for bsdcat.9*/1011#include "bsdcat_platform.h"1213#ifdef HAVE_ERRNO_H14#include <errno.h>15#endif16#ifdef HAVE_STDLIB_H17#include <stdlib.h>18#endif19#ifdef HAVE_STRING_H20#include <string.h>21#endif2223#include "bsdcat.h"24#include "err.h"2526/*27* Short options for bsdcat. Please keep this sorted.28*/29static const char *short_options = "h";3031/*32* Long options for bsdcat. Please keep this list sorted.33*34* The symbolic names for options that lack a short equivalent are35* defined in bsdcat.h. Also note that so far I've found no need36* to support optional arguments to long options. That would be37* a small change to the code below.38*/3940static const struct bsdcat_option {41const char *name;42int required; /* 1 if this option requires an argument. */43int equivalent; /* Equivalent short option. */44} bsdcat_longopts[] = {45{ "help", 0, 'h' },46{ "version", 0, OPTION_VERSION },47{ NULL, 0, 0 }48};4950/*51* This getopt implementation has two key features that common52* getopt_long() implementations lack. Apart from those, it's a53* straightforward option parser, considerably simplified by not54* needing to support the wealth of exotic getopt_long() features. It55* has, of course, been shamelessly tailored for bsdcat. (If you're56* looking for a generic getopt_long() implementation for your57* project, I recommend Gregory Pietsch's public domain getopt_long()58* implementation.) The two additional features are:59*60* Old-style tar arguments: The original tar implementation treated61* the first argument word as a list of single-character option62* letters. All arguments follow as separate words. For example,63* tar xbf 32 /dev/tape64* Here, the "xbf" is three option letters, "32" is the argument for65* "b" and "/dev/tape" is the argument for "f". We support this usage66* if the first command-line argument does not begin with '-'. We67* also allow regular short and long options to follow, e.g.,68* tar xbf 32 /dev/tape -P --format=pax69*70* -W long options: There's an obscure GNU convention (only rarely71* supported even there) that allows "-W option=argument" as an72* alternative way to support long options. This was supported in73* early bsdtar as a way to access long options on platforms that did74* not support getopt_long() and is preserved here for backwards75* compatibility. (Of course, if I'd started with a custom76* command-line parser from the beginning, I would have had normal77* long option support on every platform so that hack wouldn't have78* been necessary. Oh, well. Some mistakes you just have to live79* with.)80*81* TODO: We should be able to use this to pull files and intermingled82* options (such as -C) from the command line in write mode. That83* will require a little rethinking of the argument handling in84* bsdcat.c.85*86* TODO: If we want to support arbitrary command-line options from -T87* input (as GNU tar does), we may need to extend this to handle option88* words from sources other than argv/argc. I'm not really sure if I89* like that feature of GNU tar, so it's certainly not a priority.90*/9192int93bsdcat_getopt(struct bsdcat *bsdcat)94{95enum { state_start = 0, state_old_tar, state_next_word,96state_short, state_long };9798const struct bsdcat_option *popt, *match, *match2;99const char *p, *long_prefix;100size_t optlength;101int opt;102int required;103104again:105match = NULL;106match2 = NULL;107long_prefix = "--";108opt = '?';109required = 0;110bsdcat->argument = NULL;111112/* First time through, initialize everything. */113if (bsdcat->getopt_state == state_start) {114/* Skip program name. */115++bsdcat->argv;116--bsdcat->argc;117if (*bsdcat->argv == NULL)118return (-1);119/* Decide between "new style" and "old style" arguments. */120bsdcat->getopt_state = state_next_word;121}122123/*124* We're ready to look at the next word in argv.125*/126if (bsdcat->getopt_state == state_next_word) {127/* No more arguments, so no more options. */128if (bsdcat->argv[0] == NULL)129return (-1);130/* Doesn't start with '-', so no more options. */131if (bsdcat->argv[0][0] != '-')132return (-1);133/* "--" marks end of options; consume it and return. */134if (strcmp(bsdcat->argv[0], "--") == 0) {135++bsdcat->argv;136--bsdcat->argc;137return (-1);138}139/* Get next word for parsing. */140bsdcat->getopt_word = *bsdcat->argv++;141--bsdcat->argc;142if (bsdcat->getopt_word[1] == '-') {143/* Set up long option parser. */144bsdcat->getopt_state = state_long;145bsdcat->getopt_word += 2; /* Skip leading '--' */146} else {147/* Set up short option parser. */148bsdcat->getopt_state = state_short;149++bsdcat->getopt_word; /* Skip leading '-' */150}151}152153/*154* We're parsing a group of POSIX-style single-character options.155*/156if (bsdcat->getopt_state == state_short) {157/* Peel next option off of a group of short options. */158opt = *bsdcat->getopt_word++;159if (opt == '\0') {160/* End of this group; recurse to get next option. */161bsdcat->getopt_state = state_next_word;162goto again;163}164165/* Does this option take an argument? */166p = strchr(short_options, opt);167if (p == NULL)168return ('?');169if (p[1] == ':')170required = 1;171172/* If it takes an argument, parse that. */173if (required) {174/* If arg is run-in, bsdcat->getopt_word already points to it. */175if (bsdcat->getopt_word[0] == '\0') {176/* Otherwise, pick up the next word. */177bsdcat->getopt_word = *bsdcat->argv;178if (bsdcat->getopt_word == NULL) {179lafe_warnc(0,180"Option -%c requires an argument",181opt);182return ('?');183}184++bsdcat->argv;185--bsdcat->argc;186}187if (opt == 'W') {188bsdcat->getopt_state = state_long;189long_prefix = "-W "; /* For clearer errors. */190} else {191bsdcat->getopt_state = state_next_word;192bsdcat->argument = bsdcat->getopt_word;193}194}195}196197/* We're reading a long option, including -W long=arg convention. */198if (bsdcat->getopt_state == state_long) {199/* After this long option, we'll be starting a new word. */200bsdcat->getopt_state = state_next_word;201202/* Option name ends at '=' if there is one. */203p = strchr(bsdcat->getopt_word, '=');204if (p != NULL) {205optlength = (size_t)(p - bsdcat->getopt_word);206bsdcat->argument = (char *)(uintptr_t)(p + 1);207} else {208optlength = strlen(bsdcat->getopt_word);209}210211/* Search the table for an unambiguous match. */212for (popt = bsdcat_longopts; popt->name != NULL; popt++) {213/* Short-circuit if first chars don't match. */214if (popt->name[0] != bsdcat->getopt_word[0])215continue;216/* If option is a prefix of name in table, record it.*/217if (strncmp(bsdcat->getopt_word, popt->name, optlength) == 0) {218match2 = match; /* Record up to two matches. */219match = popt;220/* If it's an exact match, we're done. */221if (strlen(popt->name) == optlength) {222match2 = NULL; /* Forget the others. */223break;224}225}226}227228/* Fail if there wasn't a unique match. */229if (match == NULL) {230lafe_warnc(0,231"Option %s%s is not supported",232long_prefix, bsdcat->getopt_word);233return ('?');234}235if (match2 != NULL) {236lafe_warnc(0,237"Ambiguous option %s%s (matches --%s and --%s)",238long_prefix, bsdcat->getopt_word, match->name, match2->name);239return ('?');240}241242/* We've found a unique match; does it need an argument? */243if (match->required) {244/* Argument required: get next word if necessary. */245if (bsdcat->argument == NULL) {246bsdcat->argument = *bsdcat->argv;247if (bsdcat->argument == NULL) {248lafe_warnc(0,249"Option %s%s requires an argument",250long_prefix, match->name);251return ('?');252}253++bsdcat->argv;254--bsdcat->argc;255}256} else {257/* Argument forbidden: fail if there is one. */258if (bsdcat->argument != NULL) {259lafe_warnc(0,260"Option %s%s does not allow an argument",261long_prefix, match->name);262return ('?');263}264}265return (match->equivalent);266}267268return (opt);269}270271272