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