1*6b384f39SPeter Avalos /*-
2*6b384f39SPeter Avalos * Copyright (c) 2003-2008 Tim Kientzle
3*6b384f39SPeter Avalos * All rights reserved.
4*6b384f39SPeter Avalos *
5*6b384f39SPeter Avalos * Redistribution and use in source and binary forms, with or without
6*6b384f39SPeter Avalos * modification, are permitted provided that the following conditions
7*6b384f39SPeter Avalos * are met:
8*6b384f39SPeter Avalos * 1. Redistributions of source code must retain the above copyright
9*6b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer.
10*6b384f39SPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
11*6b384f39SPeter Avalos * notice, this list of conditions and the following disclaimer in the
12*6b384f39SPeter Avalos * documentation and/or other materials provided with the distribution.
13*6b384f39SPeter Avalos *
14*6b384f39SPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15*6b384f39SPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*6b384f39SPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*6b384f39SPeter Avalos * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18*6b384f39SPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*6b384f39SPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*6b384f39SPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*6b384f39SPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*6b384f39SPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*6b384f39SPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*6b384f39SPeter Avalos */
25*6b384f39SPeter Avalos
26*6b384f39SPeter Avalos /*
27*6b384f39SPeter Avalos * Command line parser for tar.
28*6b384f39SPeter Avalos */
29*6b384f39SPeter Avalos
30*6b384f39SPeter Avalos #include "bsdcat_platform.h"
31*6b384f39SPeter Avalos __FBSDID("$FreeBSD$");
32*6b384f39SPeter Avalos
33*6b384f39SPeter Avalos #ifdef HAVE_ERRNO_H
34*6b384f39SPeter Avalos #include <errno.h>
35*6b384f39SPeter Avalos #endif
36*6b384f39SPeter Avalos #ifdef HAVE_STDLIB_H
37*6b384f39SPeter Avalos #include <stdlib.h>
38*6b384f39SPeter Avalos #endif
39*6b384f39SPeter Avalos #ifdef HAVE_STRING_H
40*6b384f39SPeter Avalos #include <string.h>
41*6b384f39SPeter Avalos #endif
42*6b384f39SPeter Avalos
43*6b384f39SPeter Avalos #include "bsdcat.h"
44*6b384f39SPeter Avalos #include "err.h"
45*6b384f39SPeter Avalos
46*6b384f39SPeter Avalos /*
47*6b384f39SPeter Avalos * Short options for tar. Please keep this sorted.
48*6b384f39SPeter Avalos */
49*6b384f39SPeter Avalos static const char *short_options = "h";
50*6b384f39SPeter Avalos
51*6b384f39SPeter Avalos /*
52*6b384f39SPeter Avalos * Long options for tar. Please keep this list sorted.
53*6b384f39SPeter Avalos *
54*6b384f39SPeter Avalos * The symbolic names for options that lack a short equivalent are
55*6b384f39SPeter Avalos * defined in bsdcat.h. Also note that so far I've found no need
56*6b384f39SPeter Avalos * to support optional arguments to long options. That would be
57*6b384f39SPeter Avalos * a small change to the code below.
58*6b384f39SPeter Avalos */
59*6b384f39SPeter Avalos
60*6b384f39SPeter Avalos static const struct bsdcat_option {
61*6b384f39SPeter Avalos const char *name;
62*6b384f39SPeter Avalos int required; /* 1 if this option requires an argument. */
63*6b384f39SPeter Avalos int equivalent; /* Equivalent short option. */
64*6b384f39SPeter Avalos } tar_longopts[] = {
65*6b384f39SPeter Avalos { "help", 0, 'h' },
66*6b384f39SPeter Avalos { "version", 0, OPTION_VERSION },
67*6b384f39SPeter Avalos { NULL, 0, 0 }
68*6b384f39SPeter Avalos };
69*6b384f39SPeter Avalos
70*6b384f39SPeter Avalos /*
71*6b384f39SPeter Avalos * This getopt implementation has two key features that common
72*6b384f39SPeter Avalos * getopt_long() implementations lack. Apart from those, it's a
73*6b384f39SPeter Avalos * straightforward option parser, considerably simplified by not
74*6b384f39SPeter Avalos * needing to support the wealth of exotic getopt_long() features. It
75*6b384f39SPeter Avalos * has, of course, been shamelessly tailored for bsdcat. (If you're
76*6b384f39SPeter Avalos * looking for a generic getopt_long() implementation for your
77*6b384f39SPeter Avalos * project, I recommend Gregory Pietsch's public domain getopt_long()
78*6b384f39SPeter Avalos * implementation.) The two additional features are:
79*6b384f39SPeter Avalos *
80*6b384f39SPeter Avalos * Old-style tar arguments: The original tar implementation treated
81*6b384f39SPeter Avalos * the first argument word as a list of single-character option
82*6b384f39SPeter Avalos * letters. All arguments follow as separate words. For example,
83*6b384f39SPeter Avalos * tar xbf 32 /dev/tape
84*6b384f39SPeter Avalos * Here, the "xbf" is three option letters, "32" is the argument for
85*6b384f39SPeter Avalos * "b" and "/dev/tape" is the argument for "f". We support this usage
86*6b384f39SPeter Avalos * if the first command-line argument does not begin with '-'. We
87*6b384f39SPeter Avalos * also allow regular short and long options to follow, e.g.,
88*6b384f39SPeter Avalos * tar xbf 32 /dev/tape -P --format=pax
89*6b384f39SPeter Avalos *
90*6b384f39SPeter Avalos * -W long options: There's an obscure GNU convention (only rarely
91*6b384f39SPeter Avalos * supported even there) that allows "-W option=argument" as an
92*6b384f39SPeter Avalos * alternative way to support long options. This was supported in
93*6b384f39SPeter Avalos * early bsdcat as a way to access long options on platforms that did
94*6b384f39SPeter Avalos * not support getopt_long() and is preserved here for backwards
95*6b384f39SPeter Avalos * compatibility. (Of course, if I'd started with a custom
96*6b384f39SPeter Avalos * command-line parser from the beginning, I would have had normal
97*6b384f39SPeter Avalos * long option support on every platform so that hack wouldn't have
98*6b384f39SPeter Avalos * been necessary. Oh, well. Some mistakes you just have to live
99*6b384f39SPeter Avalos * with.)
100*6b384f39SPeter Avalos *
101*6b384f39SPeter Avalos * TODO: We should be able to use this to pull files and intermingled
102*6b384f39SPeter Avalos * options (such as -C) from the command line in write mode. That
103*6b384f39SPeter Avalos * will require a little rethinking of the argument handling in
104*6b384f39SPeter Avalos * bsdcat.c.
105*6b384f39SPeter Avalos *
106*6b384f39SPeter Avalos * TODO: If we want to support arbitrary command-line options from -T
107*6b384f39SPeter Avalos * input (as GNU tar does), we may need to extend this to handle option
108*6b384f39SPeter Avalos * words from sources other than argv/argc. I'm not really sure if I
109*6b384f39SPeter Avalos * like that feature of GNU tar, so it's certainly not a priority.
110*6b384f39SPeter Avalos */
111*6b384f39SPeter Avalos
112*6b384f39SPeter Avalos int
bsdcat_getopt(struct bsdcat * bsdcat)113*6b384f39SPeter Avalos bsdcat_getopt(struct bsdcat *bsdcat)
114*6b384f39SPeter Avalos {
115*6b384f39SPeter Avalos enum { state_start = 0, state_old_tar, state_next_word,
116*6b384f39SPeter Avalos state_short, state_long };
117*6b384f39SPeter Avalos
118*6b384f39SPeter Avalos const struct bsdcat_option *popt, *match = NULL, *match2 = NULL;
119*6b384f39SPeter Avalos const char *p, *long_prefix = "--";
120*6b384f39SPeter Avalos size_t optlength;
121*6b384f39SPeter Avalos int opt = '?';
122*6b384f39SPeter Avalos int required = 0;
123*6b384f39SPeter Avalos
124*6b384f39SPeter Avalos bsdcat->argument = NULL;
125*6b384f39SPeter Avalos
126*6b384f39SPeter Avalos /* First time through, initialize everything. */
127*6b384f39SPeter Avalos if (bsdcat->getopt_state == state_start) {
128*6b384f39SPeter Avalos /* Skip program name. */
129*6b384f39SPeter Avalos ++bsdcat->argv;
130*6b384f39SPeter Avalos --bsdcat->argc;
131*6b384f39SPeter Avalos if (*bsdcat->argv == NULL)
132*6b384f39SPeter Avalos return (-1);
133*6b384f39SPeter Avalos /* Decide between "new style" and "old style" arguments. */
134*6b384f39SPeter Avalos bsdcat->getopt_state = state_next_word;
135*6b384f39SPeter Avalos }
136*6b384f39SPeter Avalos
137*6b384f39SPeter Avalos /*
138*6b384f39SPeter Avalos * We're ready to look at the next word in argv.
139*6b384f39SPeter Avalos */
140*6b384f39SPeter Avalos if (bsdcat->getopt_state == state_next_word) {
141*6b384f39SPeter Avalos /* No more arguments, so no more options. */
142*6b384f39SPeter Avalos if (bsdcat->argv[0] == NULL)
143*6b384f39SPeter Avalos return (-1);
144*6b384f39SPeter Avalos /* Doesn't start with '-', so no more options. */
145*6b384f39SPeter Avalos if (bsdcat->argv[0][0] != '-')
146*6b384f39SPeter Avalos return (-1);
147*6b384f39SPeter Avalos /* "--" marks end of options; consume it and return. */
148*6b384f39SPeter Avalos if (strcmp(bsdcat->argv[0], "--") == 0) {
149*6b384f39SPeter Avalos ++bsdcat->argv;
150*6b384f39SPeter Avalos --bsdcat->argc;
151*6b384f39SPeter Avalos return (-1);
152*6b384f39SPeter Avalos }
153*6b384f39SPeter Avalos /* Get next word for parsing. */
154*6b384f39SPeter Avalos bsdcat->getopt_word = *bsdcat->argv++;
155*6b384f39SPeter Avalos --bsdcat->argc;
156*6b384f39SPeter Avalos if (bsdcat->getopt_word[1] == '-') {
157*6b384f39SPeter Avalos /* Set up long option parser. */
158*6b384f39SPeter Avalos bsdcat->getopt_state = state_long;
159*6b384f39SPeter Avalos bsdcat->getopt_word += 2; /* Skip leading '--' */
160*6b384f39SPeter Avalos } else {
161*6b384f39SPeter Avalos /* Set up short option parser. */
162*6b384f39SPeter Avalos bsdcat->getopt_state = state_short;
163*6b384f39SPeter Avalos ++bsdcat->getopt_word; /* Skip leading '-' */
164*6b384f39SPeter Avalos }
165*6b384f39SPeter Avalos }
166*6b384f39SPeter Avalos
167*6b384f39SPeter Avalos /*
168*6b384f39SPeter Avalos * We're parsing a group of POSIX-style single-character options.
169*6b384f39SPeter Avalos */
170*6b384f39SPeter Avalos if (bsdcat->getopt_state == state_short) {
171*6b384f39SPeter Avalos /* Peel next option off of a group of short options. */
172*6b384f39SPeter Avalos opt = *bsdcat->getopt_word++;
173*6b384f39SPeter Avalos if (opt == '\0') {
174*6b384f39SPeter Avalos /* End of this group; recurse to get next option. */
175*6b384f39SPeter Avalos bsdcat->getopt_state = state_next_word;
176*6b384f39SPeter Avalos return bsdcat_getopt(bsdcat);
177*6b384f39SPeter Avalos }
178*6b384f39SPeter Avalos
179*6b384f39SPeter Avalos /* Does this option take an argument? */
180*6b384f39SPeter Avalos p = strchr(short_options, opt);
181*6b384f39SPeter Avalos if (p == NULL)
182*6b384f39SPeter Avalos return ('?');
183*6b384f39SPeter Avalos if (p[1] == ':')
184*6b384f39SPeter Avalos required = 1;
185*6b384f39SPeter Avalos
186*6b384f39SPeter Avalos /* If it takes an argument, parse that. */
187*6b384f39SPeter Avalos if (required) {
188*6b384f39SPeter Avalos /* If arg is run-in, bsdcat->getopt_word already points to it. */
189*6b384f39SPeter Avalos if (bsdcat->getopt_word[0] == '\0') {
190*6b384f39SPeter Avalos /* Otherwise, pick up the next word. */
191*6b384f39SPeter Avalos bsdcat->getopt_word = *bsdcat->argv;
192*6b384f39SPeter Avalos if (bsdcat->getopt_word == NULL) {
193*6b384f39SPeter Avalos lafe_warnc(0,
194*6b384f39SPeter Avalos "Option -%c requires an argument",
195*6b384f39SPeter Avalos opt);
196*6b384f39SPeter Avalos return ('?');
197*6b384f39SPeter Avalos }
198*6b384f39SPeter Avalos ++bsdcat->argv;
199*6b384f39SPeter Avalos --bsdcat->argc;
200*6b384f39SPeter Avalos }
201*6b384f39SPeter Avalos if (opt == 'W') {
202*6b384f39SPeter Avalos bsdcat->getopt_state = state_long;
203*6b384f39SPeter Avalos long_prefix = "-W "; /* For clearer errors. */
204*6b384f39SPeter Avalos } else {
205*6b384f39SPeter Avalos bsdcat->getopt_state = state_next_word;
206*6b384f39SPeter Avalos bsdcat->argument = bsdcat->getopt_word;
207*6b384f39SPeter Avalos }
208*6b384f39SPeter Avalos }
209*6b384f39SPeter Avalos }
210*6b384f39SPeter Avalos
211*6b384f39SPeter Avalos /* We're reading a long option, including -W long=arg convention. */
212*6b384f39SPeter Avalos if (bsdcat->getopt_state == state_long) {
213*6b384f39SPeter Avalos /* After this long option, we'll be starting a new word. */
214*6b384f39SPeter Avalos bsdcat->getopt_state = state_next_word;
215*6b384f39SPeter Avalos
216*6b384f39SPeter Avalos /* Option name ends at '=' if there is one. */
217*6b384f39SPeter Avalos p = strchr(bsdcat->getopt_word, '=');
218*6b384f39SPeter Avalos if (p != NULL) {
219*6b384f39SPeter Avalos optlength = (size_t)(p - bsdcat->getopt_word);
220*6b384f39SPeter Avalos bsdcat->argument = (char *)(uintptr_t)(p + 1);
221*6b384f39SPeter Avalos } else {
222*6b384f39SPeter Avalos optlength = strlen(bsdcat->getopt_word);
223*6b384f39SPeter Avalos }
224*6b384f39SPeter Avalos
225*6b384f39SPeter Avalos /* Search the table for an unambiguous match. */
226*6b384f39SPeter Avalos for (popt = tar_longopts; popt->name != NULL; popt++) {
227*6b384f39SPeter Avalos /* Short-circuit if first chars don't match. */
228*6b384f39SPeter Avalos if (popt->name[0] != bsdcat->getopt_word[0])
229*6b384f39SPeter Avalos continue;
230*6b384f39SPeter Avalos /* If option is a prefix of name in table, record it.*/
231*6b384f39SPeter Avalos if (strncmp(bsdcat->getopt_word, popt->name, optlength) == 0) {
232*6b384f39SPeter Avalos match2 = match; /* Record up to two matches. */
233*6b384f39SPeter Avalos match = popt;
234*6b384f39SPeter Avalos /* If it's an exact match, we're done. */
235*6b384f39SPeter Avalos if (strlen(popt->name) == optlength) {
236*6b384f39SPeter Avalos match2 = NULL; /* Forget the others. */
237*6b384f39SPeter Avalos break;
238*6b384f39SPeter Avalos }
239*6b384f39SPeter Avalos }
240*6b384f39SPeter Avalos }
241*6b384f39SPeter Avalos
242*6b384f39SPeter Avalos /* Fail if there wasn't a unique match. */
243*6b384f39SPeter Avalos if (match == NULL) {
244*6b384f39SPeter Avalos lafe_warnc(0,
245*6b384f39SPeter Avalos "Option %s%s is not supported",
246*6b384f39SPeter Avalos long_prefix, bsdcat->getopt_word);
247*6b384f39SPeter Avalos return ('?');
248*6b384f39SPeter Avalos }
249*6b384f39SPeter Avalos if (match2 != NULL) {
250*6b384f39SPeter Avalos lafe_warnc(0,
251*6b384f39SPeter Avalos "Ambiguous option %s%s (matches --%s and --%s)",
252*6b384f39SPeter Avalos long_prefix, bsdcat->getopt_word, match->name, match2->name);
253*6b384f39SPeter Avalos return ('?');
254*6b384f39SPeter Avalos }
255*6b384f39SPeter Avalos
256*6b384f39SPeter Avalos /* We've found a unique match; does it need an argument? */
257*6b384f39SPeter Avalos if (match->required) {
258*6b384f39SPeter Avalos /* Argument required: get next word if necessary. */
259*6b384f39SPeter Avalos if (bsdcat->argument == NULL) {
260*6b384f39SPeter Avalos bsdcat->argument = *bsdcat->argv;
261*6b384f39SPeter Avalos if (bsdcat->argument == NULL) {
262*6b384f39SPeter Avalos lafe_warnc(0,
263*6b384f39SPeter Avalos "Option %s%s requires an argument",
264*6b384f39SPeter Avalos long_prefix, match->name);
265*6b384f39SPeter Avalos return ('?');
266*6b384f39SPeter Avalos }
267*6b384f39SPeter Avalos ++bsdcat->argv;
268*6b384f39SPeter Avalos --bsdcat->argc;
269*6b384f39SPeter Avalos }
270*6b384f39SPeter Avalos } else {
271*6b384f39SPeter Avalos /* Argument forbidden: fail if there is one. */
272*6b384f39SPeter Avalos if (bsdcat->argument != NULL) {
273*6b384f39SPeter Avalos lafe_warnc(0,
274*6b384f39SPeter Avalos "Option %s%s does not allow an argument",
275*6b384f39SPeter Avalos long_prefix, match->name);
276*6b384f39SPeter Avalos return ('?');
277*6b384f39SPeter Avalos }
278*6b384f39SPeter Avalos }
279*6b384f39SPeter Avalos return (match->equivalent);
280*6b384f39SPeter Avalos }
281*6b384f39SPeter Avalos
282*6b384f39SPeter Avalos return (opt);
283*6b384f39SPeter Avalos }
284