1/*-
2 * Copyright (c) 2003-2008 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26/*
27 * Command line parser for tar.
28 */
29
30#include "bsdtar_platform.h"
31__FBSDID("$FreeBSD$");
32
33#ifdef HAVE_ERRNO_H
34#include <errno.h>
35#endif
36#ifdef HAVE_STDLIB_H
37#include <stdlib.h>
38#endif
39#ifdef HAVE_STRING_H
40#include <string.h>
41#endif
42
43#include "bsdtar.h"
44#include "err.h"
45
46/*
47 * Short options for tar.  Please keep this sorted.
48 */
49static const char *short_options
50	= "Bb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
51
52/*
53 * Long options for tar.  Please keep this list sorted.
54 *
55 * The symbolic names for options that lack a short equivalent are
56 * defined in bsdtar.h.  Also note that so far I've found no need
57 * to support optional arguments to long options.  That would be
58 * a small change to the code below.
59 */
60
61static struct option {
62	const char *name;
63	int required;      /* 1 if this option requires an argument. */
64	int equivalent;    /* Equivalent short option. */
65} tar_longopts[] = {
66	{ "absolute-paths",       0, 'P' },
67	{ "append",               0, 'r' },
68	{ "block-size",           1, 'b' },
69	{ "bunzip2",              0, 'j' },
70	{ "bzip",                 0, 'j' },
71	{ "bzip2",                0, 'j' },
72	{ "cd",                   1, 'C' },
73	{ "check-links",          0, OPTION_CHECK_LINKS },
74	{ "chroot",               0, OPTION_CHROOT },
75	{ "compress",             0, 'Z' },
76	{ "confirmation",         0, 'w' },
77	{ "create",               0, 'c' },
78	{ "dereference",	  0, 'L' },
79	{ "directory",            1, 'C' },
80	{ "disable-copyfile",     0, OPTION_DISABLE_COPYFILE },
81	{ "exclude",              1, OPTION_EXCLUDE },
82	{ "exclude-from",         1, 'X' },
83	{ "extract",              0, 'x' },
84	{ "fast-read",            0, 'q' },
85	{ "file",                 1, 'f' },
86	{ "files-from",           1, 'T' },
87	{ "format",               1, OPTION_FORMAT },
88	{ "options",              1, OPTION_OPTIONS },
89	{ "gunzip",               0, 'z' },
90	{ "gzip",                 0, 'z' },
91	{ "help",                 0, OPTION_HELP },
92	{ "include",              1, OPTION_INCLUDE },
93	{ "interactive",          0, 'w' },
94	{ "insecure",             0, 'P' },
95	{ "keep-newer-files",     0, OPTION_KEEP_NEWER_FILES },
96	{ "keep-old-files",       0, 'k' },
97	{ "list",                 0, 't' },
98	{ "lzma",                 0, OPTION_LZMA },
99	{ "modification-time",    0, 'm' },
100	{ "newer",		  1, OPTION_NEWER_CTIME },
101	{ "newer-ctime",	  1, OPTION_NEWER_CTIME },
102	{ "newer-ctime-than",	  1, OPTION_NEWER_CTIME_THAN },
103	{ "newer-mtime",	  1, OPTION_NEWER_MTIME },
104	{ "newer-mtime-than",	  1, OPTION_NEWER_MTIME_THAN },
105	{ "newer-than",		  1, OPTION_NEWER_CTIME_THAN },
106	{ "nodump",               0, OPTION_NODUMP },
107	{ "norecurse",            0, 'n' },
108	{ "no-recursion",         0, 'n' },
109	{ "no-same-owner",	  0, OPTION_NO_SAME_OWNER },
110	{ "no-same-permissions",  0, OPTION_NO_SAME_PERMISSIONS },
111	{ "null",		  0, OPTION_NULL },
112	{ "numeric-owner",	  0, OPTION_NUMERIC_OWNER },
113	{ "one-file-system",	  0, OPTION_ONE_FILE_SYSTEM },
114	{ "posix",		  0, OPTION_POSIX },
115	{ "preserve-permissions", 0, 'p' },
116	{ "read-full-blocks",	  0, 'B' },
117	{ "same-owner",	          0, OPTION_SAME_OWNER },
118	{ "same-permissions",     0, 'p' },
119	{ "strip-components",	  1, OPTION_STRIP_COMPONENTS },
120	{ "to-stdout",            0, 'O' },
121	{ "totals",		  0, OPTION_TOTALS },
122	{ "uncompress",           0, 'Z' },
123	{ "unlink",		  0, 'U' },
124	{ "unlink-first",	  0, 'U' },
125	{ "update",               0, 'u' },
126	{ "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
127	{ "verbose",              0, 'v' },
128	{ "version",              0, OPTION_VERSION },
129	{ "xz",                   0, 'J' },
130	{ NULL, 0, 0 }
131};
132
133/*
134 * This getopt implementation has two key features that common
135 * getopt_long() implementations lack.  Apart from those, it's a
136 * straightforward option parser, considerably simplified by not
137 * needing to support the wealth of exotic getopt_long() features.  It
138 * has, of course, been shamelessly tailored for bsdtar.  (If you're
139 * looking for a generic getopt_long() implementation for your
140 * project, I recommend Gregory Pietsch's public domain getopt_long()
141 * implementation.)  The two additional features are:
142 *
143 * Old-style tar arguments: The original tar implementation treated
144 * the first argument word as a list of single-character option
145 * letters.  All arguments follow as separate words.  For example,
146 *    tar xbf 32 /dev/tape
147 * Here, the "xbf" is three option letters, "32" is the argument for
148 * "b" and "/dev/tape" is the argument for "f".  We support this usage
149 * if the first command-line argument does not begin with '-'.  We
150 * also allow regular short and long options to follow, e.g.,
151 *    tar xbf 32 /dev/tape -P --format=pax
152 *
153 * -W long options: There's an obscure GNU convention (only rarely
154 * supported even there) that allows "-W option=argument" as an
155 * alternative way to support long options.  This was supported in
156 * early bsdtar as a way to access long options on platforms that did
157 * not support getopt_long() and is preserved here for backwards
158 * compatibility.  (Of course, if I'd started with a custom
159 * command-line parser from the beginning, I would have had normal
160 * long option support on every platform so that hack wouldn't have
161 * been necessary.  Oh, well.  Some mistakes you just have to live
162 * with.)
163 *
164 * TODO: We should be able to use this to pull files and intermingled
165 * options (such as -C) from the command line in write mode.  That
166 * will require a little rethinking of the argument handling in
167 * bsdtar.c.
168 *
169 * TODO: If we want to support arbitrary command-line options from -T
170 * input (as GNU tar does), we may need to extend this to handle option
171 * words from sources other than argv/arc.  I'm not really sure if I
172 * like that feature of GNU tar, so it's certainly not a priority.
173 */
174
175int
176bsdtar_getopt(struct bsdtar *bsdtar)
177{
178	enum { state_start = 0, state_old_tar, state_next_word,
179	       state_short, state_long };
180	static int state = state_start;
181	static char *opt_word;
182
183	const struct option *popt, *match = NULL, *match2 = NULL;
184	const char *p, *long_prefix = "--";
185	size_t optlength;
186	int opt = '?';
187	int required = 0;
188
189	bsdtar->optarg = NULL;
190
191	/* First time through, initialize everything. */
192	if (state == state_start) {
193		/* Skip program name. */
194		++bsdtar->argv;
195		--bsdtar->argc;
196		if (*bsdtar->argv == NULL)
197			return (-1);
198		/* Decide between "new style" and "old style" arguments. */
199		if (bsdtar->argv[0][0] == '-') {
200			state = state_next_word;
201		} else {
202			state = state_old_tar;
203			opt_word = *bsdtar->argv++;
204			--bsdtar->argc;
205		}
206	}
207
208	/*
209	 * We're parsing old-style tar arguments
210	 */
211	if (state == state_old_tar) {
212		/* Get the next option character. */
213		opt = *opt_word++;
214		if (opt == '\0') {
215			/* New-style args can follow old-style. */
216			state = state_next_word;
217		} else {
218			/* See if it takes an argument. */
219			p = strchr(short_options, opt);
220			if (p == NULL)
221				return ('?');
222			if (p[1] == ':') {
223				bsdtar->optarg = *bsdtar->argv;
224				if (bsdtar->optarg == NULL) {
225					lafe_warnc(0,
226					    "Option %c requires an argument",
227					    opt);
228					return ('?');
229				}
230				++bsdtar->argv;
231				--bsdtar->argc;
232			}
233		}
234	}
235
236	/*
237	 * We're ready to look at the next word in argv.
238	 */
239	if (state == state_next_word) {
240		/* No more arguments, so no more options. */
241		if (bsdtar->argv[0] == NULL)
242			return (-1);
243		/* Doesn't start with '-', so no more options. */
244		if (bsdtar->argv[0][0] != '-')
245			return (-1);
246		/* "--" marks end of options; consume it and return. */
247		if (strcmp(bsdtar->argv[0], "--") == 0) {
248			++bsdtar->argv;
249			--bsdtar->argc;
250			return (-1);
251		}
252		/* Get next word for parsing. */
253		opt_word = *bsdtar->argv++;
254		--bsdtar->argc;
255		if (opt_word[1] == '-') {
256			/* Set up long option parser. */
257			state = state_long;
258			opt_word += 2; /* Skip leading '--' */
259		} else {
260			/* Set up short option parser. */
261			state = state_short;
262			++opt_word;  /* Skip leading '-' */
263		}
264	}
265
266	/*
267	 * We're parsing a group of POSIX-style single-character options.
268	 */
269	if (state == state_short) {
270		/* Peel next option off of a group of short options. */
271		opt = *opt_word++;
272		if (opt == '\0') {
273			/* End of this group; recurse to get next option. */
274			state = state_next_word;
275			return bsdtar_getopt(bsdtar);
276		}
277
278		/* Does this option take an argument? */
279		p = strchr(short_options, opt);
280		if (p == NULL)
281			return ('?');
282		if (p[1] == ':')
283			required = 1;
284
285		/* If it takes an argument, parse that. */
286		if (required) {
287			/* If arg is run-in, opt_word already points to it. */
288			if (opt_word[0] == '\0') {
289				/* Otherwise, pick up the next word. */
290				opt_word = *bsdtar->argv;
291				if (opt_word == NULL) {
292					lafe_warnc(0,
293					    "Option -%c requires an argument",
294					    opt);
295					return ('?');
296				}
297				++bsdtar->argv;
298				--bsdtar->argc;
299			}
300			if (opt == 'W') {
301				state = state_long;
302				long_prefix = "-W "; /* For clearer errors. */
303			} else {
304				state = state_next_word;
305				bsdtar->optarg = opt_word;
306			}
307		}
308	}
309
310	/* We're reading a long option, including -W long=arg convention. */
311	if (state == state_long) {
312		/* After this long option, we'll be starting a new word. */
313		state = state_next_word;
314
315		/* Option name ends at '=' if there is one. */
316		p = strchr(opt_word, '=');
317		if (p != NULL) {
318			optlength = (size_t)(p - opt_word);
319			bsdtar->optarg = (char *)(uintptr_t)(p + 1);
320		} else {
321			optlength = strlen(opt_word);
322		}
323
324		/* Search the table for an unambiguous match. */
325		for (popt = tar_longopts; popt->name != NULL; popt++) {
326			/* Short-circuit if first chars don't match. */
327			if (popt->name[0] != opt_word[0])
328				continue;
329			/* If option is a prefix of name in table, record it.*/
330			if (strncmp(opt_word, popt->name, optlength) == 0) {
331				match2 = match; /* Record up to two matches. */
332				match = popt;
333				/* If it's an exact match, we're done. */
334				if (strlen(popt->name) == optlength) {
335					match2 = NULL; /* Forget the others. */
336					break;
337				}
338			}
339		}
340
341		/* Fail if there wasn't a unique match. */
342		if (match == NULL) {
343			lafe_warnc(0,
344			    "Option %s%s is not supported",
345			    long_prefix, opt_word);
346			return ('?');
347		}
348		if (match2 != NULL) {
349			lafe_warnc(0,
350			    "Ambiguous option %s%s (matches --%s and --%s)",
351			    long_prefix, opt_word, match->name, match2->name);
352			return ('?');
353		}
354
355		/* We've found a unique match; does it need an argument? */
356		if (match->required) {
357			/* Argument required: get next word if necessary. */
358			if (bsdtar->optarg == NULL) {
359				bsdtar->optarg = *bsdtar->argv;
360				if (bsdtar->optarg == NULL) {
361					lafe_warnc(0,
362					    "Option %s%s requires an argument",
363					    long_prefix, match->name);
364					return ('?');
365				}
366				++bsdtar->argv;
367				--bsdtar->argc;
368			}
369		} else {
370			/* Argument forbidden: fail if there is one. */
371			if (bsdtar->optarg != NULL) {
372				lafe_warnc(0,
373				    "Option %s%s does not allow an argument",
374				    long_prefix, match->name);
375				return ('?');
376			}
377		}
378		return (match->equivalent);
379	}
380
381	return (opt);
382}
383