| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#include <commonlib/bsd/gcd.h> |
| 58 |
#include <libpayload.h> |
| 59 |
#include <getopt.h> |
| 60 |
#define warnx(x...) printf(x) |
| 61 |
#include <stdlib.h> |
| 62 |
#include <string.h> |
| 63 |
#define REPLACE_GETOPT |
| 64 |
|
| 65 |
#ifdef REPLACE_GETOPT |
| 66 |
int opterr = 1; |
| 67 |
int optind = 1; |
| 68 |
int optopt = '?'; |
| 69 |
int optreset; |
| 70 |
char *optarg; |
| 71 |
|
| 72 |
int posixly_correct = 0; |
| 73 |
#endif |
| 74 |
|
| 75 |
#define PRINT_ERROR ((opterr) && (*options != ':')) |
| 76 |
|
| 77 |
#define FLAG_PERMUTE 0x01 |
| 78 |
#define FLAG_ALLARGS 0x02 |
| 79 |
#define FLAG_LONGONLY 0x04 |
| 80 |
|
| 81 |
|
| 82 |
#define BADCH (int)'?' |
| 83 |
#define BADARG ((*options == ':') ? (int)':' : (int)'?') |
| 84 |
#define INORDER (int)1 |
| 85 |
|
| 86 |
#define EMSG (char*)"" |
| 87 |
|
| 88 |
static int getopt_internal(int, char * const *, const char *, |
| 89 |
const struct option *, int *, int); |
| 90 |
static int parse_long_options(char * const *, const char *, |
| 91 |
const struct option *, int *, int); |
| 92 |
static void permute_args(int, int, int, char * const *); |
| 93 |
|
| 94 |
static char *place = EMSG; |
| 95 |
|
| 96 |
|
| 97 |
static int nonopt_start = -1; |
| 98 |
static int nonopt_end = -1; |
| 99 |
|
| 100 |
|
| 101 |
static const char recargchar[] = "option requires an argument -- %c"; |
| 102 |
static const char recargstring[] = "option requires an argument -- %s"; |
| 103 |
static const char ambig[] = "ambiguous option -- %.*s"; |
| 104 |
static const char noarg[] = "option doesn't take an argument -- %.*s"; |
| 105 |
static const char illoptchar[] = "unknown option -- %c"; |
| 106 |
static const char illoptstring[] = "unknown option -- %s"; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static void |
| 114 |
permute_args(int panonopt_start, int panonopt_end, int opt_end, |
| 115 |
char * const *nargv) |
| 116 |
{ |
| 117 |
int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; |
| 118 |
char *swap; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
nnonopts = panonopt_end - panonopt_start; |
| 124 |
nopts = opt_end - panonopt_end; |
| 125 |
ncycle = gcd(nnonopts, nopts); |
| 126 |
cyclelen = (opt_end - panonopt_start) / ncycle; |
| 127 |
|
| 128 |
for (i = 0; i < ncycle; i++) { |
| 129 |
cstart = panonopt_end+i; |
| 130 |
pos = cstart; |
| 131 |
for (j = 0; j < cyclelen; j++) { |
| 132 |
if (pos >= panonopt_end) |
| 133 |
pos -= nnonopts; |
| 134 |
else |
| 135 |
pos += nopts; |
| 136 |
swap = nargv[pos]; |
| 137 |
|
| 138 |
((char **) nargv)[pos] = nargv[cstart]; |
| 139 |
|
| 140 |
((char **)nargv)[cstart] = swap; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static int |
| 151 |
parse_long_options(char * const *nargv, const char *options, |
| 152 |
const struct option *long_options, int *idx, int short_too) |
| 153 |
{ |
| 154 |
char *current_argv, *has_equal; |
| 155 |
size_t current_argv_len; |
| 156 |
int i, match; |
| 157 |
|
| 158 |
current_argv = place; |
| 159 |
match = -1; |
| 160 |
|
| 161 |
optind++; |
| 162 |
|
| 163 |
if ((has_equal = strchr(current_argv, '=')) != NULL) { |
| 164 |
|
| 165 |
current_argv_len = has_equal - current_argv; |
| 166 |
has_equal++; |
| 167 |
} else |
| 168 |
current_argv_len = strlen(current_argv); |
| 169 |
|
| 170 |
for (i = 0; long_options[i].name; i++) { |
| 171 |
|
| 172 |
if (strncmp(current_argv, long_options[i].name, |
| 173 |
current_argv_len)) |
| 174 |
continue; |
| 175 |
|
| 176 |
if (strlen(long_options[i].name) == current_argv_len) { |
| 177 |
|
| 178 |
match = i; |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if (short_too && current_argv_len == 1) |
| 186 |
continue; |
| 187 |
|
| 188 |
if (match == -1) |
| 189 |
match = i; |
| 190 |
else { |
| 191 |
|
| 192 |
if (PRINT_ERROR) |
| 193 |
warnx(ambig, (int)current_argv_len, |
| 194 |
current_argv); |
| 195 |
optopt = 0; |
| 196 |
return (BADCH); |
| 197 |
} |
| 198 |
} |
| 199 |
if (match != -1) { |
| 200 |
if (long_options[match].has_arg == no_argument |
| 201 |
&& has_equal) { |
| 202 |
if (PRINT_ERROR) |
| 203 |
warnx(noarg, (int)current_argv_len, |
| 204 |
current_argv); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
if (long_options[match].flag == NULL) |
| 209 |
optopt = long_options[match].val; |
| 210 |
else |
| 211 |
optopt = 0; |
| 212 |
return (BADARG); |
| 213 |
} |
| 214 |
if (long_options[match].has_arg == required_argument || |
| 215 |
long_options[match].has_arg == optional_argument) { |
| 216 |
if (has_equal) |
| 217 |
optarg = has_equal; |
| 218 |
else if (long_options[match].has_arg == |
| 219 |
required_argument) { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
optarg = nargv[optind++]; |
| 224 |
} |
| 225 |
} |
| 226 |
if ((long_options[match].has_arg == required_argument) |
| 227 |
&& (optarg == NULL)) { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
if (PRINT_ERROR) |
| 233 |
warnx(recargstring, |
| 234 |
current_argv); |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
if (long_options[match].flag == NULL) |
| 239 |
optopt = long_options[match].val; |
| 240 |
else |
| 241 |
optopt = 0; |
| 242 |
--optind; |
| 243 |
return (BADARG); |
| 244 |
} |
| 245 |
} else { |
| 246 |
if (short_too) { |
| 247 |
--optind; |
| 248 |
return (-1); |
| 249 |
} |
| 250 |
if (PRINT_ERROR) |
| 251 |
warnx(illoptstring, current_argv); |
| 252 |
optopt = 0; |
| 253 |
return (BADCH); |
| 254 |
} |
| 255 |
if (idx) |
| 256 |
*idx = match; |
| 257 |
if (long_options[match].flag) { |
| 258 |
*long_options[match].flag = long_options[match].val; |
| 259 |
return (0); |
| 260 |
} else |
| 261 |
return (long_options[match].val); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
static int |
| 269 |
getopt_internal(int nargc, char * const *nargv, const char *options, |
| 270 |
const struct option *long_options, int *idx, int flags) |
| 271 |
{ |
| 272 |
char *oli; |
| 273 |
int optchar, short_too; |
| 274 |
|
| 275 |
if (options == NULL) |
| 276 |
return (-1); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
if (posixly_correct || *options == '+') |
| 283 |
flags &= ~FLAG_PERMUTE; |
| 284 |
else if (*options == '-') |
| 285 |
flags |= FLAG_ALLARGS; |
| 286 |
if (*options == '+' || *options == '-') |
| 287 |
options++; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
if (optind == 0) |
| 294 |
optind = optreset = 1; |
| 295 |
|
| 296 |
optarg = NULL; |
| 297 |
if (optreset) |
| 298 |
nonopt_start = nonopt_end = -1; |
| 299 |
start: |
| 300 |
if (optreset || !*place) { |
| 301 |
optreset = 0; |
| 302 |
if (optind >= nargc) { |
| 303 |
place = EMSG; |
| 304 |
if (nonopt_end != -1) { |
| 305 |
|
| 306 |
permute_args(nonopt_start, nonopt_end, |
| 307 |
optind, nargv); |
| 308 |
optind -= nonopt_end - nonopt_start; |
| 309 |
} |
| 310 |
else if (nonopt_start != -1) { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
optind = nonopt_start; |
| 316 |
} |
| 317 |
nonopt_start = nonopt_end = -1; |
| 318 |
return (-1); |
| 319 |
} |
| 320 |
if (*(place = nargv[optind]) != '-' || |
| 321 |
(place[1] == '\0' && strchr(options, '-') == NULL)) { |
| 322 |
place = EMSG; |
| 323 |
if (flags & FLAG_ALLARGS) { |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
optarg = nargv[optind++]; |
| 329 |
return (INORDER); |
| 330 |
} |
| 331 |
if (!(flags & FLAG_PERMUTE)) { |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
return (-1); |
| 337 |
} |
| 338 |
|
| 339 |
if (nonopt_start == -1) |
| 340 |
nonopt_start = optind; |
| 341 |
else if (nonopt_end != -1) { |
| 342 |
permute_args(nonopt_start, nonopt_end, |
| 343 |
optind, nargv); |
| 344 |
nonopt_start = optind - |
| 345 |
(nonopt_end - nonopt_start); |
| 346 |
nonopt_end = -1; |
| 347 |
} |
| 348 |
optind++; |
| 349 |
|
| 350 |
goto start; |
| 351 |
} |
| 352 |
if (nonopt_start != -1 && nonopt_end == -1) |
| 353 |
nonopt_end = optind; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { |
| 359 |
optind++; |
| 360 |
place = EMSG; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
if (nonopt_end != -1) { |
| 366 |
permute_args(nonopt_start, nonopt_end, |
| 367 |
optind, nargv); |
| 368 |
optind -= nonopt_end - nonopt_start; |
| 369 |
} |
| 370 |
nonopt_start = nonopt_end = -1; |
| 371 |
return (-1); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if (long_options != NULL && place != nargv[optind] && |
| 382 |
(*place == '-' || (flags & FLAG_LONGONLY))) { |
| 383 |
short_too = 0; |
| 384 |
if (*place == '-') |
| 385 |
place++; |
| 386 |
else if (*place != ':' && strchr(options, *place) != NULL) |
| 387 |
short_too = 1; |
| 388 |
|
| 389 |
optchar = parse_long_options(nargv, options, long_options, |
| 390 |
idx, short_too); |
| 391 |
if (optchar != -1) { |
| 392 |
place = EMSG; |
| 393 |
return (optchar); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if ((optchar = (int)*place++) == (int)':' || |
| 398 |
(optchar == (int)'-' && *place != '\0') || |
| 399 |
(oli = strchr(options, optchar)) == NULL) { |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if (optchar == (int)'-' && *place == '\0') |
| 406 |
return (-1); |
| 407 |
if (!*place) |
| 408 |
++optind; |
| 409 |
if (PRINT_ERROR) |
| 410 |
warnx(illoptchar, optchar); |
| 411 |
optopt = optchar; |
| 412 |
return (BADCH); |
| 413 |
} |
| 414 |
if (long_options != NULL && optchar == 'W' && oli[1] == ';') { |
| 415 |
|
| 416 |
if (*place) |
| 417 |
; |
| 418 |
else if (++optind >= nargc) { |
| 419 |
place = EMSG; |
| 420 |
if (PRINT_ERROR) |
| 421 |
warnx(recargchar, optchar); |
| 422 |
optopt = optchar; |
| 423 |
return (BADARG); |
| 424 |
} else |
| 425 |
place = nargv[optind]; |
| 426 |
optchar = parse_long_options(nargv, options, long_options, |
| 427 |
idx, 0); |
| 428 |
place = EMSG; |
| 429 |
return (optchar); |
| 430 |
} |
| 431 |
if (*++oli != ':') { |
| 432 |
if (!*place) |
| 433 |
++optind; |
| 434 |
} else { |
| 435 |
optarg = NULL; |
| 436 |
if (*place) |
| 437 |
optarg = place; |
| 438 |
else if (oli[1] != ':') { |
| 439 |
if (++optind >= nargc) { |
| 440 |
place = EMSG; |
| 441 |
if (PRINT_ERROR) |
| 442 |
warnx(recargchar, optchar); |
| 443 |
optopt = optchar; |
| 444 |
return (BADARG); |
| 445 |
} else |
| 446 |
optarg = nargv[optind]; |
| 447 |
} |
| 448 |
place = EMSG; |
| 449 |
++optind; |
| 450 |
} |
| 451 |
|
| 452 |
return (optchar); |
| 453 |
} |
| 454 |
|
| 455 |
#ifdef REPLACE_GETOPT |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
int |
| 463 |
getopt(int nargc, char * const *nargv, const char *options) |
| 464 |
{ |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); |
| 475 |
} |
| 476 |
#endif |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
int |
| 483 |
getopt_long(int nargc, char * const *nargv, const char *options, |
| 484 |
const struct option *long_options, int *idx) |
| 485 |
{ |
| 486 |
|
| 487 |
return (getopt_internal(nargc, nargv, options, long_options, idx, |
| 488 |
FLAG_PERMUTE)); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
int |
| 496 |
getopt_long_only(int nargc, char * const *nargv, const char *options, |
| 497 |
const struct option *long_options, int *idx) |
| 498 |
{ |
| 499 |
|
| 500 |
return (getopt_internal(nargc, nargv, options, long_options, idx, |
| 501 |
FLAG_PERMUTE|FLAG_LONGONLY)); |
| 502 |
} |