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