xref: /freebsd/contrib/libarchive/unzip/cmdline.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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 bsdunzip.
28  */
29 
30 #include "bsdunzip_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 "bsdunzip.h"
44 #include "err.h"
45 
46 /*
47  * Short options for bsdunzip.  Please keep this sorted.
48  */
49 static const char *short_options
50 	= "aCcd:fI:jLlnO:opP:qtuvx:yZ:";
51 
52 /*
53  * Long options for bsdunzip.  Please keep this list sorted.
54  *
55  * The symbolic names for options that lack a short equivalent are
56  * defined in bsdunzip.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 bsdunzip_option {
62 	const char *name;
63 	int required;      /* 1 if this option requires an argument. */
64 	int equivalent;    /* Equivalent short option. */
65 } bsdunzip_longopts[] = {
66 	{ "version", 0, OPTION_VERSION },
67 	{ NULL, 0, 0 }
68 };
69 
70 /*
71  * This getopt implementation has two key features that common
72  * getopt_long() implementations lack.  Apart from those, it's a
73  * straightforward option parser, considerably simplified by not
74  * needing to support the wealth of exotic getopt_long() features.  It
75  * has, of course, been shamelessly tailored for bsdunzip.  (If you're
76  * looking for a generic getopt_long() implementation for your
77  * project, I recommend Gregory Pietsch's public domain getopt_long()
78  * implementation.)  The two additional features are:
79  */
80 
81 int
82 bsdunzip_getopt(struct bsdunzip *bsdunzip)
83 {
84 	enum { state_start = 0, state_next_word, state_short, state_long };
85 
86 	const struct bsdunzip_option *popt, *match = NULL, *match2 = NULL;
87 	const char *p, *long_prefix = "--";
88 	size_t optlength;
89 	int opt = OPTION_NONE;
90 	int required = 0;
91 
92 	bsdunzip->argument = NULL;
93 
94 	/* First time through, initialize everything. */
95 	if (bsdunzip->getopt_state == state_start) {
96 		/* Skip program name. */
97 		++bsdunzip->argv;
98 		--bsdunzip->argc;
99 		if (*bsdunzip->argv == NULL)
100 			return (-1);
101 		bsdunzip->getopt_state = state_next_word;
102 	}
103 
104 	/*
105 	 * We're ready to look at the next word in argv.
106 	 */
107 	if (bsdunzip->getopt_state == state_next_word) {
108 		/* No more arguments, so no more options. */
109 		if (bsdunzip->argv[0] == NULL)
110 			return (-1);
111 		/* Doesn't start with '-', so no more options. */
112 		if (bsdunzip->argv[0][0] != '-')
113 			return (-1);
114 		/* "--" marks end of options; consume it and return. */
115 		if (strcmp(bsdunzip->argv[0], "--") == 0) {
116 			++bsdunzip->argv;
117 			--bsdunzip->argc;
118 			return (-1);
119 		}
120 		/* Get next word for parsing. */
121 		bsdunzip->getopt_word = *bsdunzip->argv++;
122 		--bsdunzip->argc;
123 		bsdunzip_optind++;
124 		if (bsdunzip->getopt_word[1] == '-') {
125 			/* Set up long option parser. */
126 			bsdunzip->getopt_state = state_long;
127 			bsdunzip->getopt_word += 2; /* Skip leading '--' */
128 		} else {
129 			/* Set up short option parser. */
130 			bsdunzip->getopt_state = state_short;
131 			++bsdunzip->getopt_word;  /* Skip leading '-' */
132 		}
133 	}
134 
135 	/*
136 	 * We're parsing a group of POSIX-style single-character options.
137 	 */
138 	if (bsdunzip->getopt_state == state_short) {
139 		/* Peel next option off of a group of short options. */
140 		opt = *bsdunzip->getopt_word++;
141 		if (opt == '\0') {
142 			/* End of this group; recurse to get next option. */
143 			bsdunzip->getopt_state = state_next_word;
144 			return bsdunzip_getopt(bsdunzip);
145 		}
146 
147 		/* Does this option take an argument? */
148 		p = strchr(short_options, opt);
149 		if (p == NULL)
150 			return ('?');
151 		if (p[1] == ':')
152 			required = 1;
153 
154 		/* If it takes an argument, parse that. */
155 		if (required) {
156 			/* If arg is run-in, bsdunzip->getopt_word already points to it. */
157 			if (bsdunzip->getopt_word[0] == '\0') {
158 				/* Otherwise, pick up the next word. */
159 				bsdunzip->getopt_word = *bsdunzip->argv;
160 				if (bsdunzip->getopt_word == NULL) {
161 					lafe_warnc(0,
162 					    "Option -%c requires an argument",
163 					    opt);
164 					return ('?');
165 				}
166 				++bsdunzip->argv;
167 				--bsdunzip->argc;
168 				bsdunzip_optind++;
169 			}
170 			bsdunzip->getopt_state = state_next_word;
171 			bsdunzip->argument = bsdunzip->getopt_word;
172 		}
173 	}
174 
175 	/* We're reading a long option */
176 	if (bsdunzip->getopt_state == state_long) {
177 		/* After this long option, we'll be starting a new word. */
178 		bsdunzip->getopt_state = state_next_word;
179 
180 		/* Option name ends at '=' if there is one. */
181 		p = strchr(bsdunzip->getopt_word, '=');
182 		if (p != NULL) {
183 			optlength = (size_t)(p - bsdunzip->getopt_word);
184 			bsdunzip->argument = (char *)(uintptr_t)(p + 1);
185 		} else {
186 			optlength = strlen(bsdunzip->getopt_word);
187 		}
188 
189 		/* Search the table for an unambiguous match. */
190 		for (popt = bsdunzip_longopts; popt->name != NULL; popt++) {
191 			/* Short-circuit if first chars don't match. */
192 			if (popt->name[0] != bsdunzip->getopt_word[0])
193 				continue;
194 			/* If option is a prefix of name in table, record it.*/
195 			if (strncmp(bsdunzip->getopt_word, popt->name, optlength) == 0) {
196 				match2 = match; /* Record up to two matches. */
197 				match = popt;
198 				/* If it's an exact match, we're done. */
199 				if (strlen(popt->name) == optlength) {
200 					match2 = NULL; /* Forget the others. */
201 					break;
202 				}
203 			}
204 		}
205 
206 		/* Fail if there wasn't a unique match. */
207 		if (match == NULL) {
208 			lafe_warnc(0,
209 			    "Option %s%s is not supported",
210 			    long_prefix, bsdunzip->getopt_word);
211 			return ('?');
212 		}
213 		if (match2 != NULL) {
214 			lafe_warnc(0,
215 			    "Ambiguous option %s%s (matches --%s and --%s)",
216 			    long_prefix, bsdunzip->getopt_word, match->name, match2->name);
217 			return ('?');
218 		}
219 
220 		/* We've found a unique match; does it need an argument? */
221 		if (match->required) {
222 			/* Argument required: get next word if necessary. */
223 			if (bsdunzip->argument == NULL) {
224 				bsdunzip->argument = *bsdunzip->argv;
225 				if (bsdunzip->argument == NULL) {
226 					lafe_warnc(0,
227 					    "Option %s%s requires an argument",
228 					    long_prefix, match->name);
229 					return ('?');
230 				}
231 				++bsdunzip->argv;
232 				--bsdunzip->argc;
233 				bsdunzip_optind++;
234 			}
235 		} else {
236 			/* Argument forbidden: fail if there is one. */
237 			if (bsdunzip->argument != NULL) {
238 				lafe_warnc(0,
239 				    "Option %s%s does not allow an argument",
240 				    long_prefix, match->name);
241 				return ('?');
242 			}
243 		}
244 		return (match->equivalent);
245 	}
246 
247 	return (opt);
248 }
249