cmdline.c revision 315432
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: stable/11/contrib/libarchive/tar/cmdline.c 315432 2017-03-16 23:07:35Z mm $");
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	= "aBb: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 const struct bsdtar_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	{ "acls",                 0, OPTION_ACLS },
69	{ "auto-compress",        0, 'a' },
70	{ "b64encode",            0, OPTION_B64ENCODE },
71	{ "block-size",           1, 'b' },
72	{ "blocking-factor",	  1, 'b' },
73	{ "bunzip2",              0, 'j' },
74	{ "bzip",                 0, 'j' },
75	{ "bzip2",                0, 'j' },
76	{ "cd",                   1, 'C' },
77	{ "check-links",          0, OPTION_CHECK_LINKS },
78	{ "chroot",               0, OPTION_CHROOT },
79	{ "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS },
80	{ "compress",             0, 'Z' },
81	{ "confirmation",         0, 'w' },
82	{ "create",               0, 'c' },
83	{ "dereference",	  0, 'L' },
84	{ "directory",            1, 'C' },
85	{ "disable-copyfile",	  0, OPTION_NO_MAC_METADATA },
86	{ "exclude",              1, OPTION_EXCLUDE },
87	{ "exclude-from",         1, 'X' },
88	{ "extract",              0, 'x' },
89	{ "fast-read",            0, 'q' },
90	{ "fflags",               0, OPTION_FFLAGS },
91	{ "file",                 1, 'f' },
92	{ "files-from",           1, 'T' },
93	{ "format",               1, OPTION_FORMAT },
94	{ "gid",		  1, OPTION_GID },
95	{ "gname",		  1, OPTION_GNAME },
96	{ "grzip",                0, OPTION_GRZIP },
97	{ "gunzip",               0, 'z' },
98	{ "gzip",                 0, 'z' },
99	{ "help",                 0, OPTION_HELP },
100	{ "hfsCompression",       0, OPTION_HFS_COMPRESSION },
101	{ "ignore-zeros",         0, OPTION_IGNORE_ZEROS },
102	{ "include",              1, OPTION_INCLUDE },
103	{ "insecure",             0, 'P' },
104	{ "interactive",          0, 'w' },
105	{ "keep-newer-files",     0, OPTION_KEEP_NEWER_FILES },
106	{ "keep-old-files",       0, 'k' },
107	{ "list",                 0, 't' },
108	{ "lrzip",                0, OPTION_LRZIP },
109	{ "lz4",                  0, OPTION_LZ4 },
110	{ "lzip",                 0, OPTION_LZIP },
111	{ "lzma",                 0, OPTION_LZMA },
112	{ "lzop",                 0, OPTION_LZOP },
113	{ "mac-metadata",         0, OPTION_MAC_METADATA },
114	{ "modification-time",    0, 'm' },
115	{ "newer",		  1, OPTION_NEWER_CTIME },
116	{ "newer-ctime",	  1, OPTION_NEWER_CTIME },
117	{ "newer-ctime-than",	  1, OPTION_NEWER_CTIME_THAN },
118	{ "newer-mtime",	  1, OPTION_NEWER_MTIME },
119	{ "newer-mtime-than",	  1, OPTION_NEWER_MTIME_THAN },
120	{ "newer-than",		  1, OPTION_NEWER_CTIME_THAN },
121	{ "no-acls",              0, OPTION_NO_ACLS },
122	{ "no-fflags",            0, OPTION_NO_FFLAGS },
123	{ "no-mac-metadata",      0, OPTION_NO_MAC_METADATA },
124	{ "no-recursion",         0, 'n' },
125	{ "no-same-owner",	  0, OPTION_NO_SAME_OWNER },
126	{ "no-same-permissions",  0, OPTION_NO_SAME_PERMISSIONS },
127	{ "no-xattr",             0, OPTION_NO_XATTRS },
128	{ "no-xattrs",            0, OPTION_NO_XATTRS },
129	{ "nodump",               0, OPTION_NODUMP },
130	{ "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
131	{ "norecurse",            0, 'n' },
132	{ "null",		  0, OPTION_NULL },
133	{ "numeric-owner",	  0, OPTION_NUMERIC_OWNER },
134	{ "older",		  1, OPTION_OLDER_CTIME },
135	{ "older-ctime",	  1, OPTION_OLDER_CTIME },
136	{ "older-ctime-than",	  1, OPTION_OLDER_CTIME_THAN },
137	{ "older-mtime",	  1, OPTION_OLDER_MTIME },
138	{ "older-mtime-than",	  1, OPTION_OLDER_MTIME_THAN },
139	{ "older-than",		  1, OPTION_OLDER_CTIME_THAN },
140	{ "one-file-system",	  0, OPTION_ONE_FILE_SYSTEM },
141	{ "options",              1, OPTION_OPTIONS },
142	{ "passphrase",		  1, OPTION_PASSPHRASE },
143	{ "posix",		  0, OPTION_POSIX },
144	{ "preserve-permissions", 0, 'p' },
145	{ "read-full-blocks",	  0, 'B' },
146	{ "same-owner",	          0, OPTION_SAME_OWNER },
147	{ "same-permissions",     0, 'p' },
148	{ "strip-components",	  1, OPTION_STRIP_COMPONENTS },
149	{ "to-stdout",            0, 'O' },
150	{ "totals",		  0, OPTION_TOTALS },
151	{ "uid",		  1, OPTION_UID },
152	{ "uname",		  1, OPTION_UNAME },
153	{ "uncompress",           0, 'Z' },
154	{ "unlink",		  0, 'U' },
155	{ "unlink-first",	  0, 'U' },
156	{ "update",               0, 'u' },
157	{ "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
158	{ "uuencode",             0, OPTION_UUENCODE },
159	{ "verbose",              0, 'v' },
160	{ "version",              0, OPTION_VERSION },
161	{ "xattrs",               0, OPTION_XATTRS },
162	{ "xz",                   0, 'J' },
163	{ NULL, 0, 0 }
164};
165
166/*
167 * This getopt implementation has two key features that common
168 * getopt_long() implementations lack.  Apart from those, it's a
169 * straightforward option parser, considerably simplified by not
170 * needing to support the wealth of exotic getopt_long() features.  It
171 * has, of course, been shamelessly tailored for bsdtar.  (If you're
172 * looking for a generic getopt_long() implementation for your
173 * project, I recommend Gregory Pietsch's public domain getopt_long()
174 * implementation.)  The two additional features are:
175 *
176 * Old-style tar arguments: The original tar implementation treated
177 * the first argument word as a list of single-character option
178 * letters.  All arguments follow as separate words.  For example,
179 *    tar xbf 32 /dev/tape
180 * Here, the "xbf" is three option letters, "32" is the argument for
181 * "b" and "/dev/tape" is the argument for "f".  We support this usage
182 * if the first command-line argument does not begin with '-'.  We
183 * also allow regular short and long options to follow, e.g.,
184 *    tar xbf 32 /dev/tape -P --format=pax
185 *
186 * -W long options: There's an obscure GNU convention (only rarely
187 * supported even there) that allows "-W option=argument" as an
188 * alternative way to support long options.  This was supported in
189 * early bsdtar as a way to access long options on platforms that did
190 * not support getopt_long() and is preserved here for backwards
191 * compatibility.  (Of course, if I'd started with a custom
192 * command-line parser from the beginning, I would have had normal
193 * long option support on every platform so that hack wouldn't have
194 * been necessary.  Oh, well.  Some mistakes you just have to live
195 * with.)
196 *
197 * TODO: We should be able to use this to pull files and intermingled
198 * options (such as -C) from the command line in write mode.  That
199 * will require a little rethinking of the argument handling in
200 * bsdtar.c.
201 *
202 * TODO: If we want to support arbitrary command-line options from -T
203 * input (as GNU tar does), we may need to extend this to handle option
204 * words from sources other than argv/argc.  I'm not really sure if I
205 * like that feature of GNU tar, so it's certainly not a priority.
206 */
207
208int
209bsdtar_getopt(struct bsdtar *bsdtar)
210{
211	enum { state_start = 0, state_old_tar, state_next_word,
212	       state_short, state_long };
213
214	const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
215	const char *p, *long_prefix = "--";
216	size_t optlength;
217	int opt = '?';
218	int required = 0;
219
220	bsdtar->argument = NULL;
221
222	/* First time through, initialize everything. */
223	if (bsdtar->getopt_state == state_start) {
224		/* Skip program name. */
225		++bsdtar->argv;
226		--bsdtar->argc;
227		if (*bsdtar->argv == NULL)
228			return (-1);
229		/* Decide between "new style" and "old style" arguments. */
230		if (bsdtar->argv[0][0] == '-') {
231			bsdtar->getopt_state = state_next_word;
232		} else {
233			bsdtar->getopt_state = state_old_tar;
234			bsdtar->getopt_word = *bsdtar->argv++;
235			--bsdtar->argc;
236		}
237	}
238
239	/*
240	 * We're parsing old-style tar arguments
241	 */
242	if (bsdtar->getopt_state == state_old_tar) {
243		/* Get the next option character. */
244		opt = *bsdtar->getopt_word++;
245		if (opt == '\0') {
246			/* New-style args can follow old-style. */
247			bsdtar->getopt_state = state_next_word;
248		} else {
249			/* See if it takes an argument. */
250			p = strchr(short_options, opt);
251			if (p == NULL)
252				return ('?');
253			if (p[1] == ':') {
254				bsdtar->argument = *bsdtar->argv;
255				if (bsdtar->argument == NULL) {
256					lafe_warnc(0,
257					    "Option %c requires an argument",
258					    opt);
259					return ('?');
260				}
261				++bsdtar->argv;
262				--bsdtar->argc;
263			}
264		}
265	}
266
267	/*
268	 * We're ready to look at the next word in argv.
269	 */
270	if (bsdtar->getopt_state == state_next_word) {
271		/* No more arguments, so no more options. */
272		if (bsdtar->argv[0] == NULL)
273			return (-1);
274		/* Doesn't start with '-', so no more options. */
275		if (bsdtar->argv[0][0] != '-')
276			return (-1);
277		/* "--" marks end of options; consume it and return. */
278		if (strcmp(bsdtar->argv[0], "--") == 0) {
279			++bsdtar->argv;
280			--bsdtar->argc;
281			return (-1);
282		}
283		/* Get next word for parsing. */
284		bsdtar->getopt_word = *bsdtar->argv++;
285		--bsdtar->argc;
286		if (bsdtar->getopt_word[1] == '-') {
287			/* Set up long option parser. */
288			bsdtar->getopt_state = state_long;
289			bsdtar->getopt_word += 2; /* Skip leading '--' */
290		} else {
291			/* Set up short option parser. */
292			bsdtar->getopt_state = state_short;
293			++bsdtar->getopt_word;  /* Skip leading '-' */
294		}
295	}
296
297	/*
298	 * We're parsing a group of POSIX-style single-character options.
299	 */
300	if (bsdtar->getopt_state == state_short) {
301		/* Peel next option off of a group of short options. */
302		opt = *bsdtar->getopt_word++;
303		if (opt == '\0') {
304			/* End of this group; recurse to get next option. */
305			bsdtar->getopt_state = state_next_word;
306			return bsdtar_getopt(bsdtar);
307		}
308
309		/* Does this option take an argument? */
310		p = strchr(short_options, opt);
311		if (p == NULL)
312			return ('?');
313		if (p[1] == ':')
314			required = 1;
315
316		/* If it takes an argument, parse that. */
317		if (required) {
318			/* If arg is run-in, bsdtar->getopt_word already points to it. */
319			if (bsdtar->getopt_word[0] == '\0') {
320				/* Otherwise, pick up the next word. */
321				bsdtar->getopt_word = *bsdtar->argv;
322				if (bsdtar->getopt_word == NULL) {
323					lafe_warnc(0,
324					    "Option -%c requires an argument",
325					    opt);
326					return ('?');
327				}
328				++bsdtar->argv;
329				--bsdtar->argc;
330			}
331			if (opt == 'W') {
332				bsdtar->getopt_state = state_long;
333				long_prefix = "-W "; /* For clearer errors. */
334			} else {
335				bsdtar->getopt_state = state_next_word;
336				bsdtar->argument = bsdtar->getopt_word;
337			}
338		}
339	}
340
341	/* We're reading a long option, including -W long=arg convention. */
342	if (bsdtar->getopt_state == state_long) {
343		/* After this long option, we'll be starting a new word. */
344		bsdtar->getopt_state = state_next_word;
345
346		/* Option name ends at '=' if there is one. */
347		p = strchr(bsdtar->getopt_word, '=');
348		if (p != NULL) {
349			optlength = (size_t)(p - bsdtar->getopt_word);
350			bsdtar->argument = (char *)(uintptr_t)(p + 1);
351		} else {
352			optlength = strlen(bsdtar->getopt_word);
353		}
354
355		/* Search the table for an unambiguous match. */
356		for (popt = tar_longopts; popt->name != NULL; popt++) {
357			/* Short-circuit if first chars don't match. */
358			if (popt->name[0] != bsdtar->getopt_word[0])
359				continue;
360			/* If option is a prefix of name in table, record it.*/
361			if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
362				match2 = match; /* Record up to two matches. */
363				match = popt;
364				/* If it's an exact match, we're done. */
365				if (strlen(popt->name) == optlength) {
366					match2 = NULL; /* Forget the others. */
367					break;
368				}
369			}
370		}
371
372		/* Fail if there wasn't a unique match. */
373		if (match == NULL) {
374			lafe_warnc(0,
375			    "Option %s%s is not supported",
376			    long_prefix, bsdtar->getopt_word);
377			return ('?');
378		}
379		if (match2 != NULL) {
380			lafe_warnc(0,
381			    "Ambiguous option %s%s (matches --%s and --%s)",
382			    long_prefix, bsdtar->getopt_word, match->name, match2->name);
383			return ('?');
384		}
385
386		/* We've found a unique match; does it need an argument? */
387		if (match->required) {
388			/* Argument required: get next word if necessary. */
389			if (bsdtar->argument == NULL) {
390				bsdtar->argument = *bsdtar->argv;
391				if (bsdtar->argument == NULL) {
392					lafe_warnc(0,
393					    "Option %s%s requires an argument",
394					    long_prefix, match->name);
395					return ('?');
396				}
397				++bsdtar->argv;
398				--bsdtar->argc;
399			}
400		} else {
401			/* Argument forbidden: fail if there is one. */
402			if (bsdtar->argument != NULL) {
403				lafe_warnc(0,
404				    "Option %s%s does not allow an argument",
405				    long_prefix, match->name);
406				return ('?');
407			}
408		}
409		return (match->equivalent);
410	}
411
412	return (opt);
413}
414