| Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1 | /* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */ | 
 | 2 | /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $       */ | 
 | 3 |  | 
 | 4 | /* | 
 | 5 |  * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | 
 | 6 |  * | 
 | 7 |  * Permission to use, copy, modify, and distribute this software for any | 
 | 8 |  * purpose with or without fee is hereby granted, provided that the above | 
 | 9 |  * copyright notice and this permission notice appear in all copies. | 
 | 10 |  * | 
 | 11 |  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 
 | 12 |  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 
 | 13 |  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | 
 | 14 |  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 
 | 15 |  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 
 | 16 |  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 
 | 17 |  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
 | 18 |  * | 
 | 19 |  * Sponsored in part by the Defense Advanced Research Projects | 
 | 20 |  * Agency (DARPA) and Air Force Research Laboratory, Air Force | 
 | 21 |  * Materiel Command, USAF, under agreement number F39502-99-1-0512. | 
 | 22 |  */ | 
 | 23 | /*- | 
 | 24 |  * Copyright (c) 2000 The NetBSD Foundation, Inc. | 
 | 25 |  * All rights reserved. | 
 | 26 |  * | 
 | 27 |  * This code is derived from software contributed to The NetBSD Foundation | 
 | 28 |  * by Dieter Baron and Thomas Klausner. | 
 | 29 |  * | 
 | 30 |  * Redistribution and use in source and binary forms, with or without | 
 | 31 |  * modification, are permitted provided that the following conditions | 
 | 32 |  * are met: | 
 | 33 |  * 1. Redistributions of source code must retain the above copyright | 
 | 34 |  *    notice, this list of conditions and the following disclaimer. | 
 | 35 |  * 2. Redistributions in binary form must reproduce the above copyright | 
 | 36 |  *    notice, this list of conditions and the following disclaimer in the | 
 | 37 |  *    documentation and/or other materials provided with the distribution. | 
 | 38 |  * | 
 | 39 |  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | 
 | 40 |  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | 
 | 41 |  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 
 | 42 |  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | 
 | 43 |  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 
 | 44 |  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 
 | 45 |  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 
 | 46 |  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 
 | 47 |  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 
 | 48 |  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | 
 | 49 |  * POSSIBILITY OF SUCH DAMAGE. | 
 | 50 |  */ | 
 | 51 |  | 
| Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 52 | #include <stdio.h> | 
 | 53 | #include <stdlib.h> | 
 | 54 | #include <string.h> | 
 | 55 | #include <sys/cdefs.h> | 
 | 56 |  | 
 | 57 | #include <log/getopt.h> | 
 | 58 |  | 
 | 59 | #define PRINT_ERROR ((context->opterr) && (*options != ':')) | 
 | 60 |  | 
 | 61 | #define FLAG_PERMUTE 0x01  // permute non-options to the end of argv | 
 | 62 | #define FLAG_ALLARGS 0x02  // treat non-options as args to option "-1" | 
 | 63 |  | 
 | 64 | // return values | 
 | 65 | #define BADCH (int)'?' | 
 | 66 | #define BADARG ((*options == ':') ? (int)':' : (int)'?') | 
 | 67 | #define INORDER (int)1 | 
 | 68 |  | 
 | 69 | #define D_PREFIX 0 | 
 | 70 | #define DD_PREFIX 1 | 
 | 71 | #define W_PREFIX 2 | 
 | 72 |  | 
 | 73 | // Compute the greatest common divisor of a and b. | 
 | 74 | static int gcd(int a, int b) { | 
 | 75 |     int c = a % b; | 
 | 76 |     while (c) { | 
 | 77 |         a = b; | 
 | 78 |         b = c; | 
 | 79 |         c = a % b; | 
 | 80 |     } | 
 | 81 |     return b; | 
 | 82 | } | 
 | 83 |  | 
 | 84 | // Exchange the block from nonopt_start to nonopt_end with the block from | 
 | 85 | // nonopt_end to opt_end (keeping the same order of arguments in each block). | 
 | 86 | // Returns optind - (nonopt_end - nonopt_start) for convenience. | 
 | 87 | static int permute_args(getopt_context* context, char* const* nargv) { | 
 | 88 |     // compute lengths of blocks and number and size of cycles | 
 | 89 |     int nnonopts = context->nonopt_end - context->nonopt_start; | 
 | 90 |     int nopts = context->optind - context->nonopt_end; | 
 | 91 |     int ncycle = gcd(nnonopts, nopts); | 
 | 92 |     int cyclelen = (context->optind - context->nonopt_start) / ncycle; | 
 | 93 |  | 
 | 94 |     for (int i = 0; i < ncycle; i++) { | 
 | 95 |         int cstart = context->nonopt_end + i; | 
 | 96 |         int pos = cstart; | 
 | 97 |         for (int j = 0; j < cyclelen; j++) { | 
 | 98 |             if (pos >= context->nonopt_end) { | 
 | 99 |                 pos -= nnonopts; | 
 | 100 |             } else { | 
 | 101 |                 pos += nopts; | 
 | 102 |             } | 
 | 103 |             char* swap = nargv[pos]; | 
 | 104 |             const_cast<char**>(nargv)[pos] = nargv[cstart]; | 
 | 105 |             const_cast<char**>(nargv)[cstart] = swap; | 
 | 106 |         } | 
 | 107 |     } | 
 | 108 |     return context->optind - (context->nonopt_end - context->nonopt_start); | 
 | 109 | } | 
 | 110 |  | 
 | 111 | // parse_long_options_r -- | 
 | 112 | //    Parse long options in argc/argv argument vector. | 
 | 113 | // Returns -1 if short_too is set and the option does not match long_options. | 
 | 114 | static int parse_long_options_r(char* const* nargv, const char* options, | 
 | 115 |                                 const struct option* long_options, int* idx, | 
 | 116 |                                 bool short_too, struct getopt_context* context) { | 
 | 117 |     const char* current_argv = context->place; | 
 | 118 |     const char* current_dash; | 
 | 119 |     switch (context->dash_prefix) { | 
 | 120 |         case D_PREFIX: | 
 | 121 |             current_dash = "-"; | 
 | 122 |             break; | 
 | 123 |         case DD_PREFIX: | 
 | 124 |             current_dash = "--"; | 
 | 125 |             break; | 
 | 126 |         case W_PREFIX: | 
 | 127 |             current_dash = "-W "; | 
 | 128 |             break; | 
 | 129 |         default: | 
 | 130 |             current_dash = ""; | 
 | 131 |             break; | 
 | 132 |     } | 
 | 133 |     context->optind++; | 
 | 134 |  | 
 | 135 |     const char* has_equal; | 
 | 136 |     size_t current_argv_len; | 
 | 137 |     if (!!(has_equal = strchr(current_argv, '='))) { | 
 | 138 |         // argument found (--option=arg) | 
 | 139 |         current_argv_len = has_equal - current_argv; | 
 | 140 |         has_equal++; | 
 | 141 |     } else { | 
 | 142 |         current_argv_len = strlen(current_argv); | 
 | 143 |     } | 
 | 144 |  | 
 | 145 |     int match = -1; | 
 | 146 |     bool exact_match = false; | 
 | 147 |     bool second_partial_match = false; | 
 | 148 |     for (int i = 0; long_options[i].name; i++) { | 
 | 149 |         // find matching long option | 
 | 150 |         if (strncmp(current_argv, long_options[i].name, current_argv_len)) { | 
 | 151 |             continue; | 
 | 152 |         } | 
 | 153 |  | 
 | 154 |         if (strlen(long_options[i].name) == current_argv_len) { | 
 | 155 |             // exact match | 
 | 156 |             match = i; | 
 | 157 |             exact_match = true; | 
 | 158 |             break; | 
 | 159 |         } | 
 | 160 |         // If this is a known short option, don't allow | 
 | 161 |         // a partial match of a single character. | 
 | 162 |         if (short_too && current_argv_len == 1) continue; | 
 | 163 |  | 
 | 164 |         if (match == -1) {  // first partial match | 
 | 165 |             match = i; | 
 | 166 |         } else if (long_options[i].has_arg != long_options[match].has_arg || | 
 | 167 |                    long_options[i].flag != long_options[match].flag || | 
 | 168 |                    long_options[i].val != long_options[match].val) { | 
 | 169 |             second_partial_match = true; | 
 | 170 |         } | 
 | 171 |     } | 
 | 172 |     if (!exact_match && second_partial_match) { | 
 | 173 |         // ambiguous abbreviation | 
 | 174 |         if (PRINT_ERROR) { | 
 | 175 |             fprintf(context->optstderr ?: stderr, | 
 | 176 |                     "option `%s%.*s' is ambiguous", current_dash, | 
 | 177 |                     (int)current_argv_len, current_argv); | 
 | 178 |         } | 
 | 179 |         context->optopt = 0; | 
 | 180 |         return BADCH; | 
 | 181 |     } | 
 | 182 |     if (match != -1) {  // option found | 
 | 183 |         if (long_options[match].has_arg == no_argument && has_equal) { | 
 | 184 |             if (PRINT_ERROR) { | 
 | 185 |                 fprintf(context->optstderr ?: stderr, | 
 | 186 |                         "option `%s%.*s' doesn't allow an argument", | 
 | 187 |                         current_dash, (int)current_argv_len, current_argv); | 
 | 188 |             } | 
 | 189 |             // XXX: GNU sets optopt to val regardless of flag | 
 | 190 |             context->optopt = | 
 | 191 |                 long_options[match].flag ? 0 : long_options[match].val; | 
 | 192 |             return BADCH; | 
 | 193 |         } | 
 | 194 |         if (long_options[match].has_arg == required_argument || | 
 | 195 |             long_options[match].has_arg == optional_argument) { | 
 | 196 |             if (has_equal) { | 
 | 197 |                 context->optarg = has_equal; | 
 | 198 |             } else if (long_options[match].has_arg == required_argument) { | 
 | 199 |                 // optional argument doesn't use next nargv | 
 | 200 |                 context->optarg = nargv[context->optind++]; | 
 | 201 |             } | 
 | 202 |         } | 
 | 203 |         if ((long_options[match].has_arg == required_argument) && | 
 | 204 |             !context->optarg) { | 
 | 205 |             // Missing argument; leading ':' indicates no error | 
 | 206 |             // should be generated. | 
 | 207 |             if (PRINT_ERROR) { | 
 | 208 |                 fprintf(context->optstderr ?: stderr, | 
 | 209 |                         "option `%s%s' requires an argument", current_dash, | 
 | 210 |                         current_argv); | 
 | 211 |             } | 
 | 212 |             // XXX: GNU sets optopt to val regardless of flag | 
 | 213 |             context->optopt = | 
 | 214 |                 long_options[match].flag ? 0 : long_options[match].val; | 
 | 215 |             context->optind--; | 
 | 216 |             return BADARG; | 
 | 217 |         } | 
 | 218 |     } else {  // unknown option | 
 | 219 |         if (short_too) { | 
 | 220 |             context->optind--; | 
 | 221 |             return -1; | 
 | 222 |         } | 
 | 223 |         if (PRINT_ERROR) { | 
 | 224 |             fprintf(context->optstderr ?: stderr, "unrecognized option `%s%s'", | 
 | 225 |                     current_dash, current_argv); | 
 | 226 |         } | 
 | 227 |         context->optopt = 0; | 
 | 228 |         return BADCH; | 
 | 229 |     } | 
 | 230 |     if (idx) *idx = match; | 
 | 231 |     if (long_options[match].flag) { | 
 | 232 |         *long_options[match].flag = long_options[match].val; | 
 | 233 |         return 0; | 
 | 234 |     } | 
 | 235 |     return long_options[match].val; | 
 | 236 | } | 
 | 237 |  | 
 | 238 | // getopt_long_r -- | 
 | 239 | //    Parse argc/argv argument vector. | 
 | 240 | int getopt_long_r(int nargc, char* const* nargv, const char* options, | 
 | 241 |                   const struct option* long_options, int* idx, | 
 | 242 |                   struct getopt_context* context) { | 
 | 243 |     if (!options) return -1; | 
 | 244 |  | 
 | 245 |     // XXX Some GNU programs (like cvs) set optind to 0 instead of | 
 | 246 |     // XXX using optreset.  Work around this braindamage. | 
 | 247 |     if (!context->optind) context->optind = context->optreset = 1; | 
 | 248 |  | 
 | 249 |     // Disable GNU extensions if options string begins with a '+'. | 
 | 250 |     int flags = FLAG_PERMUTE; | 
 | 251 |     if (*options == '-') { | 
 | 252 |         flags |= FLAG_ALLARGS; | 
 | 253 |     } else if (*options == '+') { | 
 | 254 |         flags &= ~FLAG_PERMUTE; | 
 | 255 |     } | 
 | 256 |     if (*options == '+' || *options == '-') options++; | 
 | 257 |  | 
 | 258 |     context->optarg = nullptr; | 
 | 259 |     if (context->optreset) context->nonopt_start = context->nonopt_end = -1; | 
 | 260 | start: | 
 | 261 |     if (context->optreset || !*context->place) {  // update scanning pointer | 
 | 262 |         context->optreset = 0; | 
 | 263 |         if (context->optind >= nargc) {  // end of argument vector | 
 | 264 |             context->place = EMSG; | 
 | 265 |             if (context->nonopt_end != -1) { | 
 | 266 |                 // do permutation, if we have to | 
 | 267 |                 context->optind = permute_args(context, nargv); | 
 | 268 |             } else if (context->nonopt_start != -1) { | 
 | 269 |                 // If we skipped non-options, set optind to the first of them. | 
 | 270 |                 context->optind = context->nonopt_start; | 
 | 271 |             } | 
 | 272 |             context->nonopt_start = context->nonopt_end = -1; | 
 | 273 |             return -1; | 
 | 274 |         } | 
 | 275 |         if (*(context->place = nargv[context->optind]) != '-' || | 
 | 276 |             context->place[1] == '\0') { | 
 | 277 |             context->place = EMSG;  // found non-option | 
 | 278 |             if (flags & FLAG_ALLARGS) { | 
 | 279 |                 // GNU extension: return non-option as argument to option 1 | 
 | 280 |                 context->optarg = nargv[context->optind++]; | 
 | 281 |                 return INORDER; | 
 | 282 |             } | 
 | 283 |             if (!(flags & FLAG_PERMUTE)) { | 
 | 284 |                 // If no permutation wanted, stop parsing at first non-option. | 
 | 285 |                 return -1; | 
 | 286 |             } | 
 | 287 |             // do permutation | 
 | 288 |             if (context->nonopt_start == -1) { | 
 | 289 |                 context->nonopt_start = context->optind; | 
 | 290 |             } else if (context->nonopt_end != -1) { | 
 | 291 |                 context->nonopt_start = permute_args(context, nargv); | 
 | 292 |                 context->nonopt_end = -1; | 
 | 293 |             } | 
 | 294 |             context->optind++; | 
 | 295 |             // process next argument | 
 | 296 |             goto start; | 
 | 297 |         } | 
 | 298 |         if (context->nonopt_start != -1 && context->nonopt_end == -1) { | 
 | 299 |             context->nonopt_end = context->optind; | 
 | 300 |         } | 
 | 301 |  | 
 | 302 |         // If we have "-" do nothing, if "--" we are done. | 
 | 303 |         if (context->place[1] != '\0' && *++(context->place) == '-' && | 
 | 304 |             context->place[1] == '\0') { | 
 | 305 |             context->optind++; | 
 | 306 |             context->place = EMSG; | 
 | 307 |             // We found an option (--), so if we skipped | 
 | 308 |             // non-options, we have to permute. | 
 | 309 |             if (context->nonopt_end != -1) { | 
 | 310 |                 context->optind = permute_args(context, nargv); | 
 | 311 |             } | 
 | 312 |             context->nonopt_start = context->nonopt_end = -1; | 
 | 313 |             return -1; | 
 | 314 |         } | 
 | 315 |     } | 
 | 316 |  | 
 | 317 |     int optchar; | 
 | 318 |     // Check long options if: | 
 | 319 |     //  1) we were passed some | 
 | 320 |     //  2) the arg is not just "-" | 
 | 321 |     //  3) either the arg starts with -- we are getopt_long_only() | 
 | 322 |     if (long_options && context->place != nargv[context->optind] && | 
 | 323 |         (*context->place == '-')) { | 
 | 324 |         bool short_too = false; | 
 | 325 |         context->dash_prefix = D_PREFIX; | 
 | 326 |         if (*context->place == '-') { | 
 | 327 |             context->place++;  // --foo long option | 
 | 328 |             context->dash_prefix = DD_PREFIX; | 
 | 329 |         } else if (*context->place != ':' && strchr(options, *context->place)) { | 
 | 330 |             short_too = true;  // could be short option too | 
 | 331 |         } | 
 | 332 |  | 
 | 333 |         optchar = parse_long_options_r(nargv, options, long_options, idx, | 
 | 334 |                                        short_too, context); | 
 | 335 |         if (optchar != -1) { | 
 | 336 |             context->place = EMSG; | 
 | 337 |             return optchar; | 
 | 338 |         } | 
 | 339 |     } | 
 | 340 |  | 
 | 341 |     const char* oli;  // option letter list index | 
 | 342 |     if ((optchar = (int)*(context->place)++) == (int)':' || | 
 | 343 |         (optchar == (int)'-' && *context->place != '\0') || | 
 | 344 |         !(oli = strchr(options, optchar))) { | 
 | 345 |         // If the user specified "-" and  '-' isn't listed in | 
 | 346 |         // options, return -1 (non-option) as per POSIX. | 
 | 347 |         // Otherwise, it is an unknown option character (or ':'). | 
 | 348 |         if (optchar == (int)'-' && *context->place == '\0') return -1; | 
 | 349 |         if (!*context->place) context->optind++; | 
 | 350 |         if (PRINT_ERROR) { | 
 | 351 |             fprintf(context->optstderr ?: stderr, "invalid option -- %c", | 
 | 352 |                     optchar); | 
 | 353 |         } | 
 | 354 |         context->optopt = optchar; | 
 | 355 |         return BADCH; | 
 | 356 |     } | 
 | 357 |  | 
 | 358 |     static const char recargchar[] = "option requires an argument -- %c"; | 
 | 359 |     if (long_options && optchar == 'W' && oli[1] == ';') { | 
 | 360 |         // -W long-option | 
 | 361 |         if (*context->place) {                      // no space | 
 | 362 |             ;                                       // NOTHING | 
 | 363 |         } else if (++(context->optind) >= nargc) {  // no arg | 
 | 364 |             context->place = EMSG; | 
 | 365 |             if (PRINT_ERROR) { | 
 | 366 |                 fprintf(context->optstderr ?: stderr, recargchar, optchar); | 
 | 367 |             } | 
 | 368 |             context->optopt = optchar; | 
 | 369 |             return BADARG; | 
 | 370 |         } else {  // white space | 
 | 371 |             context->place = nargv[context->optind]; | 
 | 372 |         } | 
 | 373 |         context->dash_prefix = W_PREFIX; | 
 | 374 |         optchar = parse_long_options_r(nargv, options, long_options, idx, false, | 
 | 375 |                                        context); | 
 | 376 |         context->place = EMSG; | 
 | 377 |         return optchar; | 
 | 378 |     } | 
 | 379 |     if (*++oli != ':') {  // doesn't take argument | 
 | 380 |         if (!*context->place) context->optind++; | 
 | 381 |     } else {  // takes (optional) argument | 
 | 382 |         context->optarg = nullptr; | 
 | 383 |         if (*context->place) {  // no white space | 
 | 384 |             context->optarg = context->place; | 
 | 385 |         } else if (oli[1] != ':') {              // arg not optional | 
 | 386 |             if (++(context->optind) >= nargc) {  // no arg | 
 | 387 |                 context->place = EMSG; | 
 | 388 |                 if (PRINT_ERROR) { | 
 | 389 |                     fprintf(context->optstderr ?: stderr, recargchar, optchar); | 
 | 390 |                 } | 
 | 391 |                 context->optopt = optchar; | 
 | 392 |                 return BADARG; | 
 | 393 |             } | 
 | 394 |             context->optarg = nargv[context->optind]; | 
 | 395 |         } | 
 | 396 |         context->place = EMSG; | 
 | 397 |         context->optind++; | 
 | 398 |     } | 
 | 399 |     // dump back option letter | 
 | 400 |     return optchar; | 
 | 401 | } |