xref: /dragonfly/contrib/libarchive/tar/cmdline.c (revision e0cfcc6f)
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  */
49 static 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 
61 static 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 	{ "zstd",                 0, OPTION_ZSTD },
164 	{ NULL, 0, 0 }
165 };
166 
167 /*
168  * This getopt implementation has two key features that common
169  * getopt_long() implementations lack.  Apart from those, it's a
170  * straightforward option parser, considerably simplified by not
171  * needing to support the wealth of exotic getopt_long() features.  It
172  * has, of course, been shamelessly tailored for bsdtar.  (If you're
173  * looking for a generic getopt_long() implementation for your
174  * project, I recommend Gregory Pietsch's public domain getopt_long()
175  * implementation.)  The two additional features are:
176  *
177  * Old-style tar arguments: The original tar implementation treated
178  * the first argument word as a list of single-character option
179  * letters.  All arguments follow as separate words.  For example,
180  *    tar xbf 32 /dev/tape
181  * Here, the "xbf" is three option letters, "32" is the argument for
182  * "b" and "/dev/tape" is the argument for "f".  We support this usage
183  * if the first command-line argument does not begin with '-'.  We
184  * also allow regular short and long options to follow, e.g.,
185  *    tar xbf 32 /dev/tape -P --format=pax
186  *
187  * -W long options: There's an obscure GNU convention (only rarely
188  * supported even there) that allows "-W option=argument" as an
189  * alternative way to support long options.  This was supported in
190  * early bsdtar as a way to access long options on platforms that did
191  * not support getopt_long() and is preserved here for backwards
192  * compatibility.  (Of course, if I'd started with a custom
193  * command-line parser from the beginning, I would have had normal
194  * long option support on every platform so that hack wouldn't have
195  * been necessary.  Oh, well.  Some mistakes you just have to live
196  * with.)
197  *
198  * TODO: We should be able to use this to pull files and intermingled
199  * options (such as -C) from the command line in write mode.  That
200  * will require a little rethinking of the argument handling in
201  * bsdtar.c.
202  *
203  * TODO: If we want to support arbitrary command-line options from -T
204  * input (as GNU tar does), we may need to extend this to handle option
205  * words from sources other than argv/argc.  I'm not really sure if I
206  * like that feature of GNU tar, so it's certainly not a priority.
207  */
208 
209 int
210 bsdtar_getopt(struct bsdtar *bsdtar)
211 {
212 	enum { state_start = 0, state_old_tar, state_next_word,
213 	       state_short, state_long };
214 
215 	const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
216 	const char *p, *long_prefix = "--";
217 	size_t optlength;
218 	int opt = '?';
219 	int required = 0;
220 
221 	bsdtar->argument = NULL;
222 
223 	/* First time through, initialize everything. */
224 	if (bsdtar->getopt_state == state_start) {
225 		/* Skip program name. */
226 		++bsdtar->argv;
227 		--bsdtar->argc;
228 		if (*bsdtar->argv == NULL)
229 			return (-1);
230 		/* Decide between "new style" and "old style" arguments. */
231 		if (bsdtar->argv[0][0] == '-') {
232 			bsdtar->getopt_state = state_next_word;
233 		} else {
234 			bsdtar->getopt_state = state_old_tar;
235 			bsdtar->getopt_word = *bsdtar->argv++;
236 			--bsdtar->argc;
237 		}
238 	}
239 
240 	/*
241 	 * We're parsing old-style tar arguments
242 	 */
243 	if (bsdtar->getopt_state == state_old_tar) {
244 		/* Get the next option character. */
245 		opt = *bsdtar->getopt_word++;
246 		if (opt == '\0') {
247 			/* New-style args can follow old-style. */
248 			bsdtar->getopt_state = state_next_word;
249 		} else {
250 			/* See if it takes an argument. */
251 			p = strchr(short_options, opt);
252 			if (p == NULL)
253 				return ('?');
254 			if (p[1] == ':') {
255 				bsdtar->argument = *bsdtar->argv;
256 				if (bsdtar->argument == NULL) {
257 					lafe_warnc(0,
258 					    "Option %c requires an argument",
259 					    opt);
260 					return ('?');
261 				}
262 				++bsdtar->argv;
263 				--bsdtar->argc;
264 			}
265 		}
266 	}
267 
268 	/*
269 	 * We're ready to look at the next word in argv.
270 	 */
271 	if (bsdtar->getopt_state == state_next_word) {
272 		/* No more arguments, so no more options. */
273 		if (bsdtar->argv[0] == NULL)
274 			return (-1);
275 		/* Doesn't start with '-', so no more options. */
276 		if (bsdtar->argv[0][0] != '-')
277 			return (-1);
278 		/* "--" marks end of options; consume it and return. */
279 		if (strcmp(bsdtar->argv[0], "--") == 0) {
280 			++bsdtar->argv;
281 			--bsdtar->argc;
282 			return (-1);
283 		}
284 		/* Get next word for parsing. */
285 		bsdtar->getopt_word = *bsdtar->argv++;
286 		--bsdtar->argc;
287 		if (bsdtar->getopt_word[1] == '-') {
288 			/* Set up long option parser. */
289 			bsdtar->getopt_state = state_long;
290 			bsdtar->getopt_word += 2; /* Skip leading '--' */
291 		} else {
292 			/* Set up short option parser. */
293 			bsdtar->getopt_state = state_short;
294 			++bsdtar->getopt_word;  /* Skip leading '-' */
295 		}
296 	}
297 
298 	/*
299 	 * We're parsing a group of POSIX-style single-character options.
300 	 */
301 	if (bsdtar->getopt_state == state_short) {
302 		/* Peel next option off of a group of short options. */
303 		opt = *bsdtar->getopt_word++;
304 		if (opt == '\0') {
305 			/* End of this group; recurse to get next option. */
306 			bsdtar->getopt_state = state_next_word;
307 			return bsdtar_getopt(bsdtar);
308 		}
309 
310 		/* Does this option take an argument? */
311 		p = strchr(short_options, opt);
312 		if (p == NULL)
313 			return ('?');
314 		if (p[1] == ':')
315 			required = 1;
316 
317 		/* If it takes an argument, parse that. */
318 		if (required) {
319 			/* If arg is run-in, bsdtar->getopt_word already points to it. */
320 			if (bsdtar->getopt_word[0] == '\0') {
321 				/* Otherwise, pick up the next word. */
322 				bsdtar->getopt_word = *bsdtar->argv;
323 				if (bsdtar->getopt_word == NULL) {
324 					lafe_warnc(0,
325 					    "Option -%c requires an argument",
326 					    opt);
327 					return ('?');
328 				}
329 				++bsdtar->argv;
330 				--bsdtar->argc;
331 			}
332 			if (opt == 'W') {
333 				bsdtar->getopt_state = state_long;
334 				long_prefix = "-W "; /* For clearer errors. */
335 			} else {
336 				bsdtar->getopt_state = state_next_word;
337 				bsdtar->argument = bsdtar->getopt_word;
338 			}
339 		}
340 	}
341 
342 	/* We're reading a long option, including -W long=arg convention. */
343 	if (bsdtar->getopt_state == state_long) {
344 		/* After this long option, we'll be starting a new word. */
345 		bsdtar->getopt_state = state_next_word;
346 
347 		/* Option name ends at '=' if there is one. */
348 		p = strchr(bsdtar->getopt_word, '=');
349 		if (p != NULL) {
350 			optlength = (size_t)(p - bsdtar->getopt_word);
351 			bsdtar->argument = (char *)(uintptr_t)(p + 1);
352 		} else {
353 			optlength = strlen(bsdtar->getopt_word);
354 		}
355 
356 		/* Search the table for an unambiguous match. */
357 		for (popt = tar_longopts; popt->name != NULL; popt++) {
358 			/* Short-circuit if first chars don't match. */
359 			if (popt->name[0] != bsdtar->getopt_word[0])
360 				continue;
361 			/* If option is a prefix of name in table, record it.*/
362 			if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
363 				match2 = match; /* Record up to two matches. */
364 				match = popt;
365 				/* If it's an exact match, we're done. */
366 				if (strlen(popt->name) == optlength) {
367 					match2 = NULL; /* Forget the others. */
368 					break;
369 				}
370 			}
371 		}
372 
373 		/* Fail if there wasn't a unique match. */
374 		if (match == NULL) {
375 			lafe_warnc(0,
376 			    "Option %s%s is not supported",
377 			    long_prefix, bsdtar->getopt_word);
378 			return ('?');
379 		}
380 		if (match2 != NULL) {
381 			lafe_warnc(0,
382 			    "Ambiguous option %s%s (matches --%s and --%s)",
383 			    long_prefix, bsdtar->getopt_word, match->name, match2->name);
384 			return ('?');
385 		}
386 
387 		/* We've found a unique match; does it need an argument? */
388 		if (match->required) {
389 			/* Argument required: get next word if necessary. */
390 			if (bsdtar->argument == NULL) {
391 				bsdtar->argument = *bsdtar->argv;
392 				if (bsdtar->argument == NULL) {
393 					lafe_warnc(0,
394 					    "Option %s%s requires an argument",
395 					    long_prefix, match->name);
396 					return ('?');
397 				}
398 				++bsdtar->argv;
399 				--bsdtar->argc;
400 			}
401 		} else {
402 			/* Argument forbidden: fail if there is one. */
403 			if (bsdtar->argument != NULL) {
404 				lafe_warnc(0,
405 				    "Option %s%s does not allow an argument",
406 				    long_prefix, match->name);
407 				return ('?');
408 			}
409 		}
410 		return (match->equivalent);
411 	}
412 
413 	return (opt);
414 }
415