Blame cat/cmdline.c

Packit 08bd4c
/*-
Packit 08bd4c
 * Copyright (c) 2003-2008 Tim Kientzle
Packit 08bd4c
 * All rights reserved.
Packit 08bd4c
 *
Packit 08bd4c
 * Redistribution and use in source and binary forms, with or without
Packit 08bd4c
 * modification, are permitted provided that the following conditions
Packit 08bd4c
 * are met:
Packit 08bd4c
 * 1. Redistributions of source code must retain the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer.
Packit 08bd4c
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 08bd4c
 *    notice, this list of conditions and the following disclaimer in the
Packit 08bd4c
 *    documentation and/or other materials provided with the distribution.
Packit 08bd4c
 *
Packit 08bd4c
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit 08bd4c
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 08bd4c
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 08bd4c
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 08bd4c
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 08bd4c
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 08bd4c
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 08bd4c
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 08bd4c
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 08bd4c
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Command line parser for tar.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
#include "bsdcat_platform.h"
Packit 08bd4c
__FBSDID("$FreeBSD$");
Packit 08bd4c
Packit 08bd4c
#ifdef HAVE_ERRNO_H
Packit 08bd4c
#include <errno.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STDLIB_H
Packit 08bd4c
#include <stdlib.h>
Packit 08bd4c
#endif
Packit 08bd4c
#ifdef HAVE_STRING_H
Packit 08bd4c
#include <string.h>
Packit 08bd4c
#endif
Packit 08bd4c
Packit 08bd4c
#include "bsdcat.h"
Packit 08bd4c
#include "err.h"
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Short options for tar.  Please keep this sorted.
Packit 08bd4c
 */
Packit 08bd4c
static const char *short_options = "h";
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * Long options for tar.  Please keep this list sorted.
Packit 08bd4c
 *
Packit 08bd4c
 * The symbolic names for options that lack a short equivalent are
Packit 08bd4c
 * defined in bsdcat.h.  Also note that so far I've found no need
Packit 08bd4c
 * to support optional arguments to long options.  That would be
Packit 08bd4c
 * a small change to the code below.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
static const struct bsdcat_option {
Packit 08bd4c
	const char *name;
Packit 08bd4c
	int required;      /* 1 if this option requires an argument. */
Packit 08bd4c
	int equivalent;    /* Equivalent short option. */
Packit 08bd4c
} tar_longopts[] = {
Packit 08bd4c
	{ "help",                 0, 'h' },
Packit 08bd4c
	{ "version",              0, OPTION_VERSION },
Packit 08bd4c
	{ NULL, 0, 0 }
Packit 08bd4c
};
Packit 08bd4c
Packit 08bd4c
/*
Packit 08bd4c
 * This getopt implementation has two key features that common
Packit 08bd4c
 * getopt_long() implementations lack.  Apart from those, it's a
Packit 08bd4c
 * straightforward option parser, considerably simplified by not
Packit 08bd4c
 * needing to support the wealth of exotic getopt_long() features.  It
Packit 08bd4c
 * has, of course, been shamelessly tailored for bsdcat.  (If you're
Packit 08bd4c
 * looking for a generic getopt_long() implementation for your
Packit 08bd4c
 * project, I recommend Gregory Pietsch's public domain getopt_long()
Packit 08bd4c
 * implementation.)  The two additional features are:
Packit 08bd4c
 *
Packit 08bd4c
 * Old-style tar arguments: The original tar implementation treated
Packit 08bd4c
 * the first argument word as a list of single-character option
Packit 08bd4c
 * letters.  All arguments follow as separate words.  For example,
Packit 08bd4c
 *    tar xbf 32 /dev/tape
Packit 08bd4c
 * Here, the "xbf" is three option letters, "32" is the argument for
Packit 08bd4c
 * "b" and "/dev/tape" is the argument for "f".  We support this usage
Packit 08bd4c
 * if the first command-line argument does not begin with '-'.  We
Packit 08bd4c
 * also allow regular short and long options to follow, e.g.,
Packit 08bd4c
 *    tar xbf 32 /dev/tape -P --format=pax
Packit 08bd4c
 *
Packit 08bd4c
 * -W long options: There's an obscure GNU convention (only rarely
Packit 08bd4c
 * supported even there) that allows "-W option=argument" as an
Packit 08bd4c
 * alternative way to support long options.  This was supported in
Packit 08bd4c
 * early bsdcat as a way to access long options on platforms that did
Packit 08bd4c
 * not support getopt_long() and is preserved here for backwards
Packit 08bd4c
 * compatibility.  (Of course, if I'd started with a custom
Packit 08bd4c
 * command-line parser from the beginning, I would have had normal
Packit 08bd4c
 * long option support on every platform so that hack wouldn't have
Packit 08bd4c
 * been necessary.  Oh, well.  Some mistakes you just have to live
Packit 08bd4c
 * with.)
Packit 08bd4c
 *
Packit 08bd4c
 * TODO: We should be able to use this to pull files and intermingled
Packit 08bd4c
 * options (such as -C) from the command line in write mode.  That
Packit 08bd4c
 * will require a little rethinking of the argument handling in
Packit 08bd4c
 * bsdcat.c.
Packit 08bd4c
 *
Packit 08bd4c
 * TODO: If we want to support arbitrary command-line options from -T
Packit 08bd4c
 * input (as GNU tar does), we may need to extend this to handle option
Packit 08bd4c
 * words from sources other than argv/argc.  I'm not really sure if I
Packit 08bd4c
 * like that feature of GNU tar, so it's certainly not a priority.
Packit 08bd4c
 */
Packit 08bd4c
Packit 08bd4c
int
Packit 08bd4c
bsdcat_getopt(struct bsdcat *bsdcat)
Packit 08bd4c
{
Packit 08bd4c
	enum { state_start = 0, state_old_tar, state_next_word,
Packit 08bd4c
	       state_short, state_long };
Packit 08bd4c
Packit 08bd4c
	const struct bsdcat_option *popt, *match = NULL, *match2 = NULL;
Packit 08bd4c
	const char *p, *long_prefix = "--";
Packit 08bd4c
	size_t optlength;
Packit 08bd4c
	int opt = '?';
Packit 08bd4c
	int required = 0;
Packit 08bd4c
Packit 08bd4c
	bsdcat->argument = NULL;
Packit 08bd4c
Packit 08bd4c
	/* First time through, initialize everything. */
Packit 08bd4c
	if (bsdcat->getopt_state == state_start) {
Packit 08bd4c
		/* Skip program name. */
Packit 08bd4c
		++bsdcat->argv;
Packit 08bd4c
		--bsdcat->argc;
Packit 08bd4c
		if (*bsdcat->argv == NULL)
Packit 08bd4c
			return (-1);
Packit 08bd4c
		/* Decide between "new style" and "old style" arguments. */
Packit 08bd4c
		bsdcat->getopt_state = state_next_word;
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * We're ready to look at the next word in argv.
Packit 08bd4c
	 */
Packit 08bd4c
	if (bsdcat->getopt_state == state_next_word) {
Packit 08bd4c
		/* No more arguments, so no more options. */
Packit 08bd4c
		if (bsdcat->argv[0] == NULL)
Packit 08bd4c
			return (-1);
Packit 08bd4c
		/* Doesn't start with '-', so no more options. */
Packit 08bd4c
		if (bsdcat->argv[0][0] != '-')
Packit 08bd4c
			return (-1);
Packit 08bd4c
		/* "--" marks end of options; consume it and return. */
Packit 08bd4c
		if (strcmp(bsdcat->argv[0], "--") == 0) {
Packit 08bd4c
			++bsdcat->argv;
Packit 08bd4c
			--bsdcat->argc;
Packit 08bd4c
			return (-1);
Packit 08bd4c
		}
Packit 08bd4c
		/* Get next word for parsing. */
Packit 08bd4c
		bsdcat->getopt_word = *bsdcat->argv++;
Packit 08bd4c
		--bsdcat->argc;
Packit 08bd4c
		if (bsdcat->getopt_word[1] == '-') {
Packit 08bd4c
			/* Set up long option parser. */
Packit 08bd4c
			bsdcat->getopt_state = state_long;
Packit 08bd4c
			bsdcat->getopt_word += 2; /* Skip leading '--' */
Packit 08bd4c
		} else {
Packit 08bd4c
			/* Set up short option parser. */
Packit 08bd4c
			bsdcat->getopt_state = state_short;
Packit 08bd4c
			++bsdcat->getopt_word;  /* Skip leading '-' */
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/*
Packit 08bd4c
	 * We're parsing a group of POSIX-style single-character options.
Packit 08bd4c
	 */
Packit 08bd4c
	if (bsdcat->getopt_state == state_short) {
Packit 08bd4c
		/* Peel next option off of a group of short options. */
Packit 08bd4c
		opt = *bsdcat->getopt_word++;
Packit 08bd4c
		if (opt == '\0') {
Packit 08bd4c
			/* End of this group; recurse to get next option. */
Packit 08bd4c
			bsdcat->getopt_state = state_next_word;
Packit 08bd4c
			return bsdcat_getopt(bsdcat);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* Does this option take an argument? */
Packit 08bd4c
		p = strchr(short_options, opt);
Packit 08bd4c
		if (p == NULL)
Packit 08bd4c
			return ('?');
Packit 08bd4c
		if (p[1] == ':')
Packit 08bd4c
			required = 1;
Packit 08bd4c
Packit 08bd4c
		/* If it takes an argument, parse that. */
Packit 08bd4c
		if (required) {
Packit 08bd4c
			/* If arg is run-in, bsdcat->getopt_word already points to it. */
Packit 08bd4c
			if (bsdcat->getopt_word[0] == '\0') {
Packit 08bd4c
				/* Otherwise, pick up the next word. */
Packit 08bd4c
				bsdcat->getopt_word = *bsdcat->argv;
Packit 08bd4c
				if (bsdcat->getopt_word == NULL) {
Packit 08bd4c
					lafe_warnc(0,
Packit 08bd4c
					    "Option -%c requires an argument",
Packit 08bd4c
					    opt);
Packit 08bd4c
					return ('?');
Packit 08bd4c
				}
Packit 08bd4c
				++bsdcat->argv;
Packit 08bd4c
				--bsdcat->argc;
Packit 08bd4c
			}
Packit 08bd4c
			if (opt == 'W') {
Packit 08bd4c
				bsdcat->getopt_state = state_long;
Packit 08bd4c
				long_prefix = "-W "; /* For clearer errors. */
Packit 08bd4c
			} else {
Packit 08bd4c
				bsdcat->getopt_state = state_next_word;
Packit 08bd4c
				bsdcat->argument = bsdcat->getopt_word;
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	/* We're reading a long option, including -W long=arg convention. */
Packit 08bd4c
	if (bsdcat->getopt_state == state_long) {
Packit 08bd4c
		/* After this long option, we'll be starting a new word. */
Packit 08bd4c
		bsdcat->getopt_state = state_next_word;
Packit 08bd4c
Packit 08bd4c
		/* Option name ends at '=' if there is one. */
Packit 08bd4c
		p = strchr(bsdcat->getopt_word, '=');
Packit 08bd4c
		if (p != NULL) {
Packit 08bd4c
			optlength = (size_t)(p - bsdcat->getopt_word);
Packit 08bd4c
			bsdcat->argument = (char *)(uintptr_t)(p + 1);
Packit 08bd4c
		} else {
Packit 08bd4c
			optlength = strlen(bsdcat->getopt_word);
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* Search the table for an unambiguous match. */
Packit 08bd4c
		for (popt = tar_longopts; popt->name != NULL; popt++) {
Packit 08bd4c
			/* Short-circuit if first chars don't match. */
Packit 08bd4c
			if (popt->name[0] != bsdcat->getopt_word[0])
Packit 08bd4c
				continue;
Packit 08bd4c
			/* If option is a prefix of name in table, record it.*/
Packit 08bd4c
			if (strncmp(bsdcat->getopt_word, popt->name, optlength) == 0) {
Packit 08bd4c
				match2 = match; /* Record up to two matches. */
Packit 08bd4c
				match = popt;
Packit 08bd4c
				/* If it's an exact match, we're done. */
Packit 08bd4c
				if (strlen(popt->name) == optlength) {
Packit 08bd4c
					match2 = NULL; /* Forget the others. */
Packit 08bd4c
					break;
Packit 08bd4c
				}
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* Fail if there wasn't a unique match. */
Packit 08bd4c
		if (match == NULL) {
Packit 08bd4c
			lafe_warnc(0,
Packit 08bd4c
			    "Option %s%s is not supported",
Packit 08bd4c
			    long_prefix, bsdcat->getopt_word);
Packit 08bd4c
			return ('?');
Packit 08bd4c
		}
Packit 08bd4c
		if (match2 != NULL) {
Packit 08bd4c
			lafe_warnc(0,
Packit 08bd4c
			    "Ambiguous option %s%s (matches --%s and --%s)",
Packit 08bd4c
			    long_prefix, bsdcat->getopt_word, match->name, match2->name);
Packit 08bd4c
			return ('?');
Packit 08bd4c
		}
Packit 08bd4c
Packit 08bd4c
		/* We've found a unique match; does it need an argument? */
Packit 08bd4c
		if (match->required) {
Packit 08bd4c
			/* Argument required: get next word if necessary. */
Packit 08bd4c
			if (bsdcat->argument == NULL) {
Packit 08bd4c
				bsdcat->argument = *bsdcat->argv;
Packit 08bd4c
				if (bsdcat->argument == NULL) {
Packit 08bd4c
					lafe_warnc(0,
Packit 08bd4c
					    "Option %s%s requires an argument",
Packit 08bd4c
					    long_prefix, match->name);
Packit 08bd4c
					return ('?');
Packit 08bd4c
				}
Packit 08bd4c
				++bsdcat->argv;
Packit 08bd4c
				--bsdcat->argc;
Packit 08bd4c
			}
Packit 08bd4c
		} else {
Packit 08bd4c
			/* Argument forbidden: fail if there is one. */
Packit 08bd4c
			if (bsdcat->argument != NULL) {
Packit 08bd4c
				lafe_warnc(0,
Packit 08bd4c
				    "Option %s%s does not allow an argument",
Packit 08bd4c
				    long_prefix, match->name);
Packit 08bd4c
				return ('?');
Packit 08bd4c
			}
Packit 08bd4c
		}
Packit 08bd4c
		return (match->equivalent);
Packit 08bd4c
	}
Packit 08bd4c
Packit 08bd4c
	return (opt);
Packit 08bd4c
}