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 { "auto-compress", 0, 'a' },
69 { "b64encode", 0, OPTION_B64ENCODE },
70 { "block-size", 1, 'b' },
71 { "blocking-factor", 1, 'b' },
72 { "bunzip2", 0, 'j' },
76 { "check-links", 0, OPTION_CHECK_LINKS },
77 { "chroot", 0, OPTION_CHROOT },
78 { "clear-nochange-fflags", 0, OPTION_CLEAR_NOCHANGE_FFLAGS },
79 { "compress", 0, 'Z' },
80 { "confirmation", 0, 'w' },
82 { "dereference", 0, 'L' },
83 { "directory", 1, 'C' },
84 { "disable-copyfile", 0, OPTION_DISABLE_COPYFILE },
85 { "exclude", 1, OPTION_EXCLUDE },
86 { "exclude-from", 1, 'X' },
87 { "extract", 0, 'x' },
88 { "fast-read", 0, 'q' },
90 { "files-from", 1, 'T' },
91 { "format", 1, OPTION_FORMAT },
92 { "gid", 1, OPTION_GID },
93 { "gname", 1, OPTION_GNAME },
94 { "grzip", 0, OPTION_GRZIP },
97 { "help", 0, OPTION_HELP },
98 { "hfsCompression", 0, OPTION_HFS_COMPRESSION },
99 { "ignore-zeros", 0, OPTION_IGNORE_ZEROS },
100 { "include", 1, OPTION_INCLUDE },
101 { "insecure", 0, 'P' },
102 { "interactive", 0, 'w' },
103 { "keep-newer-files", 0, OPTION_KEEP_NEWER_FILES },
104 { "keep-old-files", 0, 'k' },
106 { "lrzip", 0, OPTION_LRZIP },
107 { "lz4", 0, OPTION_LZ4 },
108 { "lzip", 0, OPTION_LZIP },
109 { "lzma", 0, OPTION_LZMA },
110 { "lzop", 0, OPTION_LZOP },
111 { "modification-time", 0, 'm' },
112 { "newer", 1, OPTION_NEWER_CTIME },
113 { "newer-ctime", 1, OPTION_NEWER_CTIME },
114 { "newer-ctime-than", 1, OPTION_NEWER_CTIME_THAN },
115 { "newer-mtime", 1, OPTION_NEWER_MTIME },
116 { "newer-mtime-than", 1, OPTION_NEWER_MTIME_THAN },
117 { "newer-than", 1, OPTION_NEWER_CTIME_THAN },
118 { "no-recursion", 0, 'n' },
119 { "no-same-owner", 0, OPTION_NO_SAME_OWNER },
120 { "no-same-permissions", 0, OPTION_NO_SAME_PERMISSIONS },
121 { "no-xattr", 0, OPTION_NO_XATTR },
122 { "nodump", 0, OPTION_NODUMP },
123 { "nopreserveHFSCompression",0, OPTION_NOPRESERVE_HFS_COMPRESSION },
124 { "norecurse", 0, 'n' },
125 { "null", 0, OPTION_NULL },
126 { "numeric-owner", 0, OPTION_NUMERIC_OWNER },
127 { "older", 1, OPTION_OLDER_CTIME },
128 { "older-ctime", 1, OPTION_OLDER_CTIME },
129 { "older-ctime-than", 1, OPTION_OLDER_CTIME_THAN },
130 { "older-mtime", 1, OPTION_OLDER_MTIME },
131 { "older-mtime-than", 1, OPTION_OLDER_MTIME_THAN },
132 { "older-than", 1, OPTION_OLDER_CTIME_THAN },
133 { "one-file-system", 0, OPTION_ONE_FILE_SYSTEM },
134 { "options", 1, OPTION_OPTIONS },
135 { "passphrase", 1, OPTION_PASSPHRASE },
136 { "posix", 0, OPTION_POSIX },
137 { "preserve-permissions", 0, 'p' },
138 { "read-full-blocks", 0, 'B' },
139 { "same-owner", 0, OPTION_SAME_OWNER },
140 { "same-permissions", 0, 'p' },
141 { "strip-components", 1, OPTION_STRIP_COMPONENTS },
142 { "to-stdout", 0, 'O' },
143 { "totals", 0, OPTION_TOTALS },
144 { "uid", 1, OPTION_UID },
145 { "uname", 1, OPTION_UNAME },
146 { "uncompress", 0, 'Z' },
147 { "unlink", 0, 'U' },
148 { "unlink-first", 0, 'U' },
149 { "update", 0, 'u' },
150 { "use-compress-program", 1, OPTION_USE_COMPRESS_PROGRAM },
151 { "uuencode", 0, OPTION_UUENCODE },
152 { "verbose", 0, 'v' },
153 { "version", 0, OPTION_VERSION },
159 * This getopt implementation has two key features that common
160 * getopt_long() implementations lack. Apart from those, it's a
161 * straightforward option parser, considerably simplified by not
162 * needing to support the wealth of exotic getopt_long() features. It
163 * has, of course, been shamelessly tailored for bsdtar. (If you're
164 * looking for a generic getopt_long() implementation for your
165 * project, I recommend Gregory Pietsch's public domain getopt_long()
166 * implementation.) The two additional features are:
168 * Old-style tar arguments: The original tar implementation treated
169 * the first argument word as a list of single-character option
170 * letters. All arguments follow as separate words. For example,
171 * tar xbf 32 /dev/tape
172 * Here, the "xbf" is three option letters, "32" is the argument for
173 * "b" and "/dev/tape" is the argument for "f". We support this usage
174 * if the first command-line argument does not begin with '-'. We
175 * also allow regular short and long options to follow, e.g.,
176 * tar xbf 32 /dev/tape -P --format=pax
178 * -W long options: There's an obscure GNU convention (only rarely
179 * supported even there) that allows "-W option=argument" as an
180 * alternative way to support long options. This was supported in
181 * early bsdtar as a way to access long options on platforms that did
182 * not support getopt_long() and is preserved here for backwards
183 * compatibility. (Of course, if I'd started with a custom
184 * command-line parser from the beginning, I would have had normal
185 * long option support on every platform so that hack wouldn't have
186 * been necessary. Oh, well. Some mistakes you just have to live
189 * TODO: We should be able to use this to pull files and intermingled
190 * options (such as -C) from the command line in write mode. That
191 * will require a little rethinking of the argument handling in
194 * TODO: If we want to support arbitrary command-line options from -T
195 * input (as GNU tar does), we may need to extend this to handle option
196 * words from sources other than argv/argc. I'm not really sure if I
197 * like that feature of GNU tar, so it's certainly not a priority.
201 bsdtar_getopt(struct bsdtar *bsdtar)
203 enum { state_start = 0, state_old_tar, state_next_word,
204 state_short, state_long };
206 const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
207 const char *p, *long_prefix = "--";
212 bsdtar->argument = NULL;
214 /* First time through, initialize everything. */
215 if (bsdtar->getopt_state == state_start) {
216 /* Skip program name. */
219 if (*bsdtar->argv == NULL)
221 /* Decide between "new style" and "old style" arguments. */
222 if (bsdtar->argv[0][0] == '-') {
223 bsdtar->getopt_state = state_next_word;
225 bsdtar->getopt_state = state_old_tar;
226 bsdtar->getopt_word = *bsdtar->argv++;
232 * We're parsing old-style tar arguments
234 if (bsdtar->getopt_state == state_old_tar) {
235 /* Get the next option character. */
236 opt = *bsdtar->getopt_word++;
238 /* New-style args can follow old-style. */
239 bsdtar->getopt_state = state_next_word;
241 /* See if it takes an argument. */
242 p = strchr(short_options, opt);
246 bsdtar->argument = *bsdtar->argv;
247 if (bsdtar->argument == NULL) {
249 "Option %c requires an argument",
260 * We're ready to look at the next word in argv.
262 if (bsdtar->getopt_state == state_next_word) {
263 /* No more arguments, so no more options. */
264 if (bsdtar->argv[0] == NULL)
266 /* Doesn't start with '-', so no more options. */
267 if (bsdtar->argv[0][0] != '-')
269 /* "--" marks end of options; consume it and return. */
270 if (strcmp(bsdtar->argv[0], "--") == 0) {
275 /* Get next word for parsing. */
276 bsdtar->getopt_word = *bsdtar->argv++;
278 if (bsdtar->getopt_word[1] == '-') {
279 /* Set up long option parser. */
280 bsdtar->getopt_state = state_long;
281 bsdtar->getopt_word += 2; /* Skip leading '--' */
283 /* Set up short option parser. */
284 bsdtar->getopt_state = state_short;
285 ++bsdtar->getopt_word; /* Skip leading '-' */
290 * We're parsing a group of POSIX-style single-character options.
292 if (bsdtar->getopt_state == state_short) {
293 /* Peel next option off of a group of short options. */
294 opt = *bsdtar->getopt_word++;
296 /* End of this group; recurse to get next option. */
297 bsdtar->getopt_state = state_next_word;
298 return bsdtar_getopt(bsdtar);
301 /* Does this option take an argument? */
302 p = strchr(short_options, opt);
308 /* If it takes an argument, parse that. */
310 /* If arg is run-in, bsdtar->getopt_word already points to it. */
311 if (bsdtar->getopt_word[0] == '\0') {
312 /* Otherwise, pick up the next word. */
313 bsdtar->getopt_word = *bsdtar->argv;
314 if (bsdtar->getopt_word == NULL) {
316 "Option -%c requires an argument",
324 bsdtar->getopt_state = state_long;
325 long_prefix = "-W "; /* For clearer errors. */
327 bsdtar->getopt_state = state_next_word;
328 bsdtar->argument = bsdtar->getopt_word;
333 /* We're reading a long option, including -W long=arg convention. */
334 if (bsdtar->getopt_state == state_long) {
335 /* After this long option, we'll be starting a new word. */
336 bsdtar->getopt_state = state_next_word;
338 /* Option name ends at '=' if there is one. */
339 p = strchr(bsdtar->getopt_word, '=');
341 optlength = (size_t)(p - bsdtar->getopt_word);
342 bsdtar->argument = (char *)(uintptr_t)(p + 1);
344 optlength = strlen(bsdtar->getopt_word);
347 /* Search the table for an unambiguous match. */
348 for (popt = tar_longopts; popt->name != NULL; popt++) {
349 /* Short-circuit if first chars don't match. */
350 if (popt->name[0] != bsdtar->getopt_word[0])
352 /* If option is a prefix of name in table, record it.*/
353 if (strncmp(bsdtar->getopt_word, popt->name, optlength) == 0) {
354 match2 = match; /* Record up to two matches. */
356 /* If it's an exact match, we're done. */
357 if (strlen(popt->name) == optlength) {
358 match2 = NULL; /* Forget the others. */
364 /* Fail if there wasn't a unique match. */
367 "Option %s%s is not supported",
368 long_prefix, bsdtar->getopt_word);
371 if (match2 != NULL) {
373 "Ambiguous option %s%s (matches --%s and --%s)",
374 long_prefix, bsdtar->getopt_word, match->name, match2->name);
378 /* We've found a unique match; does it need an argument? */
379 if (match->required) {
380 /* Argument required: get next word if necessary. */
381 if (bsdtar->argument == NULL) {
382 bsdtar->argument = *bsdtar->argv;
383 if (bsdtar->argument == NULL) {
385 "Option %s%s requires an argument",
386 long_prefix, match->name);
393 /* Argument forbidden: fail if there is one. */
394 if (bsdtar->argument != NULL) {
396 "Option %s%s does not allow an argument",
397 long_prefix, match->name);
401 return (match->equivalent);