]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/tar/cmdline.c
MFC r304075,r304989:
[FreeBSD/stable/10.git] / contrib / libarchive / tar / cmdline.c
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         { "auto-compress",        0, 'a' },
69         { "b64encode",            0, OPTION_B64ENCODE },
70         { "block-size",           1, 'b' },
71         { "blocking-factor",      1, 'b' },
72         { "bunzip2",              0, 'j' },
73         { "bzip",                 0, 'j' },
74         { "bzip2",                0, 'j' },
75         { "cd",                   1, 'C' },
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' },
81         { "create",               0, 'c' },
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' },
89         { "file",                 1, 'f' },
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 },
95         { "gunzip",               0, 'z' },
96         { "gzip",                 0, 'z' },
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' },
105         { "list",                 0, 't' },
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 },
154         { "xz",                   0, 'J' },
155         { NULL, 0, 0 }
156 };
157
158 /*
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:
167  *
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
177  *
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
187  * with.)
188  *
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
192  * bsdtar.c.
193  *
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.
198  */
199
200 int
201 bsdtar_getopt(struct bsdtar *bsdtar)
202 {
203         enum { state_start = 0, state_old_tar, state_next_word,
204                state_short, state_long };
205
206         const struct bsdtar_option *popt, *match = NULL, *match2 = NULL;
207         const char *p, *long_prefix = "--";
208         size_t optlength;
209         int opt = '?';
210         int required = 0;
211
212         bsdtar->argument = NULL;
213
214         /* First time through, initialize everything. */
215         if (bsdtar->getopt_state == state_start) {
216                 /* Skip program name. */
217                 ++bsdtar->argv;
218                 --bsdtar->argc;
219                 if (*bsdtar->argv == NULL)
220                         return (-1);
221                 /* Decide between "new style" and "old style" arguments. */
222                 if (bsdtar->argv[0][0] == '-') {
223                         bsdtar->getopt_state = state_next_word;
224                 } else {
225                         bsdtar->getopt_state = state_old_tar;
226                         bsdtar->getopt_word = *bsdtar->argv++;
227                         --bsdtar->argc;
228                 }
229         }
230
231         /*
232          * We're parsing old-style tar arguments
233          */
234         if (bsdtar->getopt_state == state_old_tar) {
235                 /* Get the next option character. */
236                 opt = *bsdtar->getopt_word++;
237                 if (opt == '\0') {
238                         /* New-style args can follow old-style. */
239                         bsdtar->getopt_state = state_next_word;
240                 } else {
241                         /* See if it takes an argument. */
242                         p = strchr(short_options, opt);
243                         if (p == NULL)
244                                 return ('?');
245                         if (p[1] == ':') {
246                                 bsdtar->argument = *bsdtar->argv;
247                                 if (bsdtar->argument == NULL) {
248                                         lafe_warnc(0,
249                                             "Option %c requires an argument",
250                                             opt);
251                                         return ('?');
252                                 }
253                                 ++bsdtar->argv;
254                                 --bsdtar->argc;
255                         }
256                 }
257         }
258
259         /*
260          * We're ready to look at the next word in argv.
261          */
262         if (bsdtar->getopt_state == state_next_word) {
263                 /* No more arguments, so no more options. */
264                 if (bsdtar->argv[0] == NULL)
265                         return (-1);
266                 /* Doesn't start with '-', so no more options. */
267                 if (bsdtar->argv[0][0] != '-')
268                         return (-1);
269                 /* "--" marks end of options; consume it and return. */
270                 if (strcmp(bsdtar->argv[0], "--") == 0) {
271                         ++bsdtar->argv;
272                         --bsdtar->argc;
273                         return (-1);
274                 }
275                 /* Get next word for parsing. */
276                 bsdtar->getopt_word = *bsdtar->argv++;
277                 --bsdtar->argc;
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 '--' */
282                 } else {
283                         /* Set up short option parser. */
284                         bsdtar->getopt_state = state_short;
285                         ++bsdtar->getopt_word;  /* Skip leading '-' */
286                 }
287         }
288
289         /*
290          * We're parsing a group of POSIX-style single-character options.
291          */
292         if (bsdtar->getopt_state == state_short) {
293                 /* Peel next option off of a group of short options. */
294                 opt = *bsdtar->getopt_word++;
295                 if (opt == '\0') {
296                         /* End of this group; recurse to get next option. */
297                         bsdtar->getopt_state = state_next_word;
298                         return bsdtar_getopt(bsdtar);
299                 }
300
301                 /* Does this option take an argument? */
302                 p = strchr(short_options, opt);
303                 if (p == NULL)
304                         return ('?');
305                 if (p[1] == ':')
306                         required = 1;
307
308                 /* If it takes an argument, parse that. */
309                 if (required) {
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) {
315                                         lafe_warnc(0,
316                                             "Option -%c requires an argument",
317                                             opt);
318                                         return ('?');
319                                 }
320                                 ++bsdtar->argv;
321                                 --bsdtar->argc;
322                         }
323                         if (opt == 'W') {
324                                 bsdtar->getopt_state = state_long;
325                                 long_prefix = "-W "; /* For clearer errors. */
326                         } else {
327                                 bsdtar->getopt_state = state_next_word;
328                                 bsdtar->argument = bsdtar->getopt_word;
329                         }
330                 }
331         }
332
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;
337
338                 /* Option name ends at '=' if there is one. */
339                 p = strchr(bsdtar->getopt_word, '=');
340                 if (p != NULL) {
341                         optlength = (size_t)(p - bsdtar->getopt_word);
342                         bsdtar->argument = (char *)(uintptr_t)(p + 1);
343                 } else {
344                         optlength = strlen(bsdtar->getopt_word);
345                 }
346
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])
351                                 continue;
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. */
355                                 match = popt;
356                                 /* If it's an exact match, we're done. */
357                                 if (strlen(popt->name) == optlength) {
358                                         match2 = NULL; /* Forget the others. */
359                                         break;
360                                 }
361                         }
362                 }
363
364                 /* Fail if there wasn't a unique match. */
365                 if (match == NULL) {
366                         lafe_warnc(0,
367                             "Option %s%s is not supported",
368                             long_prefix, bsdtar->getopt_word);
369                         return ('?');
370                 }
371                 if (match2 != NULL) {
372                         lafe_warnc(0,
373                             "Ambiguous option %s%s (matches --%s and --%s)",
374                             long_prefix, bsdtar->getopt_word, match->name, match2->name);
375                         return ('?');
376                 }
377
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) {
384                                         lafe_warnc(0,
385                                             "Option %s%s requires an argument",
386                                             long_prefix, match->name);
387                                         return ('?');
388                                 }
389                                 ++bsdtar->argv;
390                                 --bsdtar->argc;
391                         }
392                 } else {
393                         /* Argument forbidden: fail if there is one. */
394                         if (bsdtar->argument != NULL) {
395                                 lafe_warnc(0,
396                                     "Option %s%s does not allow an argument",
397                                     long_prefix, match->name);
398                                 return ('?');
399                         }
400                 }
401                 return (match->equivalent);
402         }
403
404         return (opt);
405 }