2 * Copyright (c) 2003-2008 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 * Command line parser for tar.
30 #include "bsdtar_platform.h"
31 __FBSDID("$FreeBSD$");
47 * Short options for tar. Please keep this sorted.
49 static const char *short_options
50 = "aBb:C:cf:HhI:JjkLlmnOoPpqrSs:T:tUuvW:wX:xyZz";
53 * Long options for tar. Please keep this list sorted.
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.
61 static const struct bsdtar_option {
63 int required; /* 1 if this option requires an argument. */
64 int equivalent; /* Equivalent short option. */
66 { "absolute-paths", 0, 'P' },
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' },
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' },
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 { "exclude-vcs", 0, OPTION_EXCLUDE_VCS },
89 { "extract", 0, 'x' },
90 { "fast-read", 0, 'q' },
91 { "fflags", 0, OPTION_FFLAGS },
93 { "files-from", 1, 'T' },
94 { "format", 1, OPTION_FORMAT },
95 { "gid", 1, OPTION_GID },
96 { "gname", 1, OPTION_GNAME },
97 { "grzip", 0, OPTION_GRZIP },
100 { "help", 0, OPTION_HELP },
101 { "hfsCompression", 0, OPTION_HFS_COMPRESSION },
102 { "ignore-zeros", 0, OPTION_IGNORE_ZEROS },
103 { "include", 1, OPTION_INCLUDE },
104 { "insecure", 0, 'P' },
105 { "interactive", 0, 'w' },
106 { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
107 { "keep-old-files", 0, 'k' },
109 { "lrzip", 0, OPTION_LRZIP },
110 { "lz4", 0, OPTION_LZ4 },
111 { "lzip", 0, OPTION_LZIP },
112 { "lzma", 0, OPTION_LZMA },
113 { "lzop", 0, OPTION_LZOP },
114 { "mac-metadata", 0, OPTION_MAC_METADATA },
115 { "modification-time", 0, 'm' },
116 { "newer", 1, OPTION_NEWER_CTIME },
117 { "newer-ctime", 1, OPTION_NEWER_CTIME },
118 { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
119 { "newer-mtime", 1, OPTION_NEWER_MTIME },
120 { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
121 { "newer-than", 1, OPTION_NEWER_CTIME_THAN },
122 { "no-acls", 0, OPTION_NO_ACLS },
123 { "no-fflags", 0, OPTION_NO_FFLAGS },
124 { "no-mac-metadata", 0, OPTION_NO_MAC_METADATA },
125 { "no-read-sparse", 0, OPTION_NO_READ_SPARSE },
126 { "no-recursion", 0, 'n' },
127 { "no-safe-writes", 0, OPTION_NO_SAFE_WRITES },
128 { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
129 { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
130 { "no-xattr", 0, OPTION_NO_XATTRS },
131 { "no-xattrs", 0, OPTION_NO_XATTRS },
132 { "nodump", 0, OPTION_NODUMP },
133 { "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
134 { "norecurse", 0, 'n' },
135 { "null", 0, OPTION_NULL },
136 { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
137 { "older", 1, OPTION_OLDER_CTIME },
138 { "older-ctime", 1, OPTION_OLDER_CTIME },
139 { "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN },
140 { "older-mtime", 1, OPTION_OLDER_MTIME },
141 { "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN },
142 { "older-than", 1, OPTION_OLDER_CTIME_THAN },
143 { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
144 { "options", 1, OPTION_OPTIONS },
145 { "passphrase", 1, OPTION_PASSPHRASE },
146 { "posix", 0, OPTION_POSIX },
147 { "preserve-permissions", 0, 'p' },
148 { "read-full-blocks", 0, 'B' },
149 { "read-sparse", 0, OPTION_READ_SPARSE },
150 { "safe-writes", 0, OPTION_SAFE_WRITES },
151 { "same-owner", 0, OPTION_SAME_OWNER },
152 { "same-permissions", 0, 'p' },
153 { "strip-components", 1, OPTION_STRIP_COMPONENTS },
154 { "to-stdout", 0, 'O' },
155 { "totals", 0, OPTION_TOTALS },
156 { "uid", 1, OPTION_UID },
157 { "uname", 1, OPTION_UNAME },
158 { "uncompress", 0, 'Z' },
159 { "unlink", 0, 'U' },
160 { "unlink-first", 0, 'U' },
161 { "update", 0, 'u' },
162 { "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
163 { "uuencode", 0, OPTION_UUENCODE },
164 { "verbose", 0, 'v' },
165 { "version", 0, OPTION_VERSION },
166 { "xattrs", 0, OPTION_XATTRS },
168 { "zstd", 0, OPTION_ZSTD },
173 * This getopt implementation has two key features that common
174 * getopt_long() implementations lack. Apart from those, it's a
175 * straightforward option parser, considerably simplified by not
176 * needing to support the wealth of exotic getopt_long() features. It
177 * has, of course, been shamelessly tailored for bsdtar. (If you're
178 * looking for a generic getopt_long() implementation for your
179 * project, I recommend Gregory Pietsch's public domain getopt_long()
180 * implementation.) The two additional features are:
182 * Old-style tar arguments: The original tar implementation treated
183 * the first argument word as a list of single-character option
184 * letters. All arguments follow as separate words. For example,
185 * tar xbf 32 /dev/tape
186 * Here, the "xbf" is three option letters, "32" is the argument for
187 * "b" and "/dev/tape" is the argument for "f". We support this usage
188 * if the first command-line argument does not begin with '-'. We
189 * also allow regular short and long options to follow, e.g.,
190 * tar xbf 32 /dev/tape -P --format=pax
192 * -W long options: There's an obscure GNU convention (only rarely
193 * supported even there) that allows "-W option=argument" as an
194 * alternative way to support long options. This was supported in
195 * early bsdtar as a way to access long options on platforms that did
196 * not support getopt_long() and is preserved here for backwards
197 * compatibility. (Of course, if I'd started with a custom
198 * command-line parser from the beginning, I would have had normal
199 * long option support on every platform so that hack wouldn't have
200 * been necessary. Oh, well. Some mistakes you just have to live
203 * TODO: We should be able to use this to pull files and intermingled
204 * options (such as -C) from the command line in write mode. That
205 * will require a little rethinking of the argument handling in
208 * TODO: If we want to support arbitrary command-line options from -T
209 * input (as GNU tar does), we may need to extend this to handle option
210 * words from sources other than argv/argc. I'm not really sure if I
211 * like that feature of GNU tar, so it's certainly not a priority.
215 bsdtar_getopt(struct bsdtar *bsdtar)
217 enum { state_start = 0, state_old_tar, state_next_word,
218 state_short, state_long };
220 const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
221 const char *p, *long_prefix = "--";
226 bsdtar->argument = NULL;
228 /* First time through, initialize everything. */
229 if (bsdtar->getopt_state == state_start) {
230 /* Skip program name. */
233 if (*bsdtar->argv == NULL)
235 /* Decide between "new style" and "old style" arguments. */
236 if (bsdtar->argv[0][0] == '-') {
237 bsdtar->getopt_state = state_next_word;
239 bsdtar->getopt_state = state_old_tar;
240 bsdtar->getopt_word = *bsdtar->argv++;
246 * We're parsing old-style tar arguments
248 if (bsdtar->getopt_state == state_old_tar) {
249 /* Get the next option character. */
250 opt = *bsdtar->getopt_word++;
252 /* New-style args can follow old-style. */
253 bsdtar->getopt_state = state_next_word;
255 /* See if it takes an argument. */
256 p = strchr(short_options, opt);
260 bsdtar->argument = *bsdtar->argv;
261 if (bsdtar->argument == NULL) {
263 "Option %c requires an argument",
274 * We're ready to look at the next word in argv.
276 if (bsdtar->getopt_state == state_next_word) {
277 /* No more arguments, so no more options. */
278 if (bsdtar->argv[0] == NULL)
280 /* Doesn't start with '-', so no more options. */
281 if (bsdtar->argv[0][0] != '-')
283 /* "--" marks end of options; consume it and return. */
284 if (strcmp(bsdtar->argv[0], "--") == 0) {
289 /* Get next word for parsing. */
290 bsdtar->getopt_word = *bsdtar->argv++;
292 if (bsdtar->getopt_word[1] == '-') {
293 /* Set up long option parser. */
294 bsdtar->getopt_state = state_long;
295 bsdtar->getopt_word += 2; /* Skip leading '--' */
297 /* Set up short option parser. */
298 bsdtar->getopt_state = state_short;
299 ++bsdtar->getopt_word; /* Skip leading '-' */
304 * We're parsing a group of POSIX-style single-character options.
306 if (bsdtar->getopt_state == state_short) {
307 /* Peel next option off of a group of short options. */
308 opt = *bsdtar->getopt_word++;
310 /* End of this group; recurse to get next option. */
311 bsdtar->getopt_state = state_next_word;
312 return bsdtar_getopt(bsdtar);
315 /* Does this option take an argument? */
316 p = strchr(short_options, opt);
322 /* If it takes an argument, parse that. */
324 /* If arg is run-in, bsdtar->getopt_word already points to it. */
325 if (bsdtar->getopt_word[0] == '\0') {
326 /* Otherwise, pick up the next word. */
327 bsdtar->getopt_word = *bsdtar->argv;
328 if (bsdtar->getopt_word == NULL) {
330 "Option -%c requires an argument",
338 bsdtar->getopt_state = state_long;
339 long_prefix = "-W "; /* For clearer errors. */
341 bsdtar->getopt_state = state_next_word;
342 bsdtar->argument = bsdtar->getopt_word;
347 /* We're reading a long option, including -W long=arg convention. */
348 if (bsdtar->getopt_state == state_long) {
349 /* After this long option, we'll be starting a new word. */
350 bsdtar->getopt_state = state_next_word;
352 /* Option name ends at '=' if there is one. */
353 p = strchr(bsdtar->getopt_word, '=');
355 optlength = (size_t)(p - bsdtar->getopt_word);
356 bsdtar->argument = (char *)(uintptr_t)(p + 1);
358 optlength = strlen(bsdtar->getopt_word);
361 /* Search the table for an unambiguous match. */
362 for (popt = tar_longopts; popt->name != NULL; popt++) {
363 /* Short-circuit if first chars don't match. */
364 if (popt->name[0] != bsdtar->getopt_word[0])
366 /* If option is a prefix of name in table, record it.*/
367 if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
368 match2 = match; /* Record up to two matches. */
370 /* If it's an exact match, we're done. */
371 if (strlen(popt->name) == optlength) {
372 match2 = NULL; /* Forget the others. */
378 /* Fail if there wasn't a unique match. */
381 "Option %s%s is not supported",
382 long_prefix, bsdtar->getopt_word);
385 if (match2 != NULL) {
387 "Ambiguous option %s%s (matches --%s and --%s)",
388 long_prefix, bsdtar->getopt_word, match->name, match2->name);
392 /* We've found a unique match; does it need an argument? */
393 if (match->required) {
394 /* Argument required: get next word if necessary. */
395 if (bsdtar->argument == NULL) {
396 bsdtar->argument = *bsdtar->argv;
397 if (bsdtar->argument == NULL) {
399 "Option %s%s requires an argument",
400 long_prefix, match->name);
407 /* Argument forbidden: fail if there is one. */
408 if (bsdtar->argument != NULL) {
410 "Option %s%s does not allow an argument",
411 long_prefix, match->name);
415 return (match->equivalent);