Blame cat/cmdline.c

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