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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
All rights reserved.
This software was developed in the APRIL Robotics Lab under the
direction of Edwin Olson, ebolson@umich.edu. This software may be
available under alternative licensing terms; contact the address above.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the Regents of The University of Michigan.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "zhash.h"
#include "zarray.h"
#include "getopt.h"
#include "common/math_util.h"
#define GOO_BOOL_TYPE 1
#define GOO_STRING_TYPE 2
typedef struct getopt_option getopt_option_t;
struct getopt_option
{
char *sname;
char *lname;
char *svalue;
char *help;
int type;
int spacer;
int was_specified;
};
struct getopt
{
zhash_t *lopts;
zhash_t *sopts;
zarray_t *extraargs;
zarray_t *options;
};
getopt_t *getopt_create()
{
getopt_t *gopt = (getopt_t*) calloc(1, sizeof(getopt_t));
gopt->lopts = zhash_create(sizeof(char*), sizeof(getopt_option_t*), zhash_str_hash, zhash_str_equals);
gopt->sopts = zhash_create(sizeof(char*), sizeof(getopt_option_t*), zhash_str_hash, zhash_str_equals);
gopt->options = zarray_create(sizeof(getopt_option_t*));
gopt->extraargs = zarray_create(sizeof(char*));
return gopt;
}
void getopt_option_destroy(getopt_option_t *goo)
{
free(goo->sname);
free(goo->lname);
free(goo->svalue);
free(goo->help);
memset(goo, 0, sizeof(getopt_option_t));
free(goo);
}
void getopt_option_destroy_void(void *goo)
{
getopt_option_destroy((getopt_option_t *)goo);
}
void getopt_destroy(getopt_t *gopt)
{
// free the extra arguments and container
zarray_vmap(gopt->extraargs, free);
zarray_destroy(gopt->extraargs);
// deep free of the getopt_option structs. Also frees key/values, so
// after this loop, hash tables will no longer work
zarray_vmap(gopt->options, getopt_option_destroy_void);
zarray_destroy(gopt->options);
// free tables
zhash_destroy(gopt->lopts);
zhash_destroy(gopt->sopts);
memset(gopt, 0, sizeof(getopt_t));
free(gopt);
}
static void getopt_modify_string(char **str, char *newvalue)
{
char *old = *str;
*str = newvalue;
if (old != NULL)
free(old);
}
static char *get_arg_assignment(char *arg)
{
// not an arg starting with "--"?
if (!str_starts_with(arg, "--")) {
return NULL;
}
int eq_index = str_indexof(arg, "=");
// no assignment?
if (eq_index == -1) {
return NULL;
}
// no quotes allowed before '=' in "--key=value" option specification.
// quotes can be used in value string, or by extra arguments
for (int i = 0; i < eq_index; i++) {
if (arg[i] == '\'' || arg[i] == '"') {
return NULL;
}
}
return &arg[eq_index];
}
// returns 1 if no error
int getopt_parse(getopt_t *gopt, int argc, char *argv[], int showErrors)
{
int okay = 1;
zarray_t *toks = zarray_create(sizeof(char*));
// take the input stream and chop it up into tokens
for (int i = 1; i < argc; i++) {
char *arg = strdup(argv[i]);
char *eq = get_arg_assignment(arg);
// no equal sign? Push the whole thing.
if (eq == NULL) {
zarray_add(toks, &arg);
} else {
// there was an equal sign. Push the part
// before and after the equal sign
char *val = strdup(&eq[1]);
eq[0] = 0;
zarray_add(toks, &arg);
// if the part after the equal sign is
// enclosed by quotation marks, strip them.
if (val[0]=='\"') {
size_t last = strlen(val) - 1;
if (val[last]=='\"')
val[last] = 0;
char *valclean = strdup(&val[1]);
zarray_add(toks, &valclean);
free(val);
} else {
zarray_add(toks, &val);
}
}
}
// now loop over the elements and evaluate the arguments
int i = 0;
char *tok = NULL;
while (i < zarray_size(toks)) {
// rather than free statement throughout this while loop
if (tok != NULL)
free(tok);
zarray_get(toks, i, &tok);
if (!strncmp(tok,"--", 2)) {
char *optname = &tok[2];
getopt_option_t *goo = NULL;
zhash_get(gopt->lopts, &optname, &goo);
if (goo == NULL) {
okay = 0;
if (showErrors)
printf("Unknown option --%s\n", optname);
i++;
continue;
}
goo->was_specified = 1;
if (goo->type == GOO_BOOL_TYPE) {
if ((i+1) < zarray_size(toks)) {
char *val = NULL;
zarray_get(toks, i+1, &val);
if (!strcmp(val,"true")) {
i+=2;
getopt_modify_string(&goo->svalue, val);
continue;
}
if (!strcmp(val,"false")) {
i+=2;
getopt_modify_string(&goo->svalue, val);
continue;
}
}
getopt_modify_string(&goo->svalue, strdup("true"));
i++;
continue;
}
if (goo->type == GOO_STRING_TYPE) {
// TODO: check whether next argument is an option, denoting missing argument
if ((i+1) < zarray_size(toks)) {
char *val = NULL;
zarray_get(toks, i+1, &val);
i+=2;
getopt_modify_string(&goo->svalue, val);
continue;
}
okay = 0;
if (showErrors)
printf("Option %s requires a string argument.\n",optname);
}
}
if (!strncmp(tok,"-",1) && strncmp(tok,"--",2)) {
size_t len = strlen(tok);
size_t pos;
for (pos = 1; pos < len; pos++) {
char sopt[2];
sopt[0] = tok[pos];
sopt[1] = 0;
char *sopt_ptr = (char*) &sopt;
getopt_option_t *goo = NULL;
zhash_get(gopt->sopts, &sopt_ptr, &goo);
if (goo==NULL) {
// is the argument a numerical literal that happens to be negative?
if (pos==1 && isdigit(tok[pos])) {
zarray_add(gopt->extraargs, &tok);
tok = NULL;
break;
} else {
okay = 0;
if (showErrors)
printf("Unknown option -%c\n", tok[pos]);
i++;
continue;
}
}
goo->was_specified = 1;
if (goo->type == GOO_BOOL_TYPE) {
getopt_modify_string(&goo->svalue, strdup("true"));
continue;
}
if (goo->type == GOO_STRING_TYPE) {
if ((i+1) < zarray_size(toks)) {
char *val = NULL;
zarray_get(toks, i+1, &val);
// TODO: allow negative numerical values for short-name options ?
if (val[0]=='-')
{
okay = 0;
if (showErrors)
printf("Ran out of arguments for option block %s\n", tok);
}
i++;
getopt_modify_string(&goo->svalue, val);
continue;
}
okay = 0;
if (showErrors)
printf("Option -%c requires a string argument.\n", tok[pos]);
}
}
i++;
continue;
}
// it's not an option-- it's an argument.
zarray_add(gopt->extraargs, &tok);
tok = NULL;
i++;
}
if (tok != NULL)
free(tok);
zarray_destroy(toks);
return okay;
}
void getopt_add_spacer(getopt_t *gopt, const char *s)
{
getopt_option_t *goo = (getopt_option_t*) calloc(1, sizeof(getopt_option_t));
goo->spacer = 1;
goo->help = strdup(s);
zarray_add(gopt->options, &goo);
}
void getopt_add_bool(getopt_t *gopt, char sopt, const char *lname, int def, const char *help)
{
char sname[2];
sname[0] = sopt;
sname[1] = 0;
char *sname_ptr = (char*) &sname;
if (strlen(lname) < 1) { // must have long name
fprintf (stderr, "getopt_add_bool(): must supply option name\n");
exit (EXIT_FAILURE);
}
if (sopt == '-') { // short name cannot be '-' (no way to reference)
fprintf (stderr, "getopt_add_bool(): invalid option character: '%c'\n", sopt);
exit (EXIT_FAILURE);
}
if (zhash_contains(gopt->lopts, &lname)) {
fprintf (stderr, "getopt_add_bool(): duplicate option name: --%s\n", lname);
exit (EXIT_FAILURE);
}
if (sopt != '\0' && zhash_contains(gopt->sopts, &sname_ptr)) {
fprintf (stderr, "getopt_add_bool(): duplicate option: -%s ('%s')\n", sname, lname);
exit (EXIT_FAILURE);
}
getopt_option_t *goo = (getopt_option_t*) calloc(1, sizeof(getopt_option_t));
goo->sname=strdup(sname);
goo->lname=strdup(lname);
goo->svalue=strdup(def ? "true" : "false");
goo->type=GOO_BOOL_TYPE;
goo->help=strdup(help);
zhash_put(gopt->lopts, &goo->lname, &goo, NULL, NULL);
zhash_put(gopt->sopts, &goo->sname, &goo, NULL, NULL);
zarray_add(gopt->options, &goo);
}
void getopt_add_int(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
{
getopt_add_string(gopt, sopt, lname, def, help);
}
void
getopt_add_double (getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
{
getopt_add_string (gopt, sopt, lname, def, help);
}
void getopt_add_string(getopt_t *gopt, char sopt, const char *lname, const char *def, const char *help)
{
char sname[2];
sname[0] = sopt;
sname[1] = 0;
char *sname_ptr = (char*) &sname;
if (strlen(lname) < 1) { // must have long name
fprintf (stderr, "getopt_add_string(): must supply option name\n");
exit (EXIT_FAILURE);
}
if (sopt == '-') { // short name cannot be '-' (no way to reference)
fprintf (stderr, "getopt_add_string(): invalid option character: '%c'\n", sopt);
exit (EXIT_FAILURE);
}
if (zhash_contains(gopt->lopts, &lname)) {
fprintf (stderr, "getopt_add_string(): duplicate option name: --%s\n", lname);
exit (EXIT_FAILURE);
}
if (sopt != '\0' && zhash_contains(gopt->sopts, &sname_ptr)) {
fprintf (stderr, "getopt_add_string(): duplicate option: -%s ('%s')\n", sname, lname);
exit (EXIT_FAILURE);
}
getopt_option_t *goo = (getopt_option_t*) calloc(1, sizeof(getopt_option_t));
goo->sname=strdup(sname);
goo->lname=strdup(lname);
goo->svalue=strdup(def);
goo->type=GOO_STRING_TYPE;
goo->help=strdup(help);
zhash_put(gopt->lopts, &goo->lname, &goo, NULL, NULL);
zhash_put(gopt->sopts, &goo->sname, &goo, NULL, NULL);
zarray_add(gopt->options, &goo);
}
const char *getopt_get_string(getopt_t *gopt, const char *lname)
{
getopt_option_t *goo = NULL;
zhash_get(gopt->lopts, &lname, &goo);
// could return null, but this would be the only
// method that doesn't assert on a missing key
assert (goo != NULL);
return goo->svalue;
}
int getopt_get_int(getopt_t *getopt, const char *lname)
{
const char *v = getopt_get_string(getopt, lname);
assert(v != NULL);
errno = 0;
char *endptr = (char *) v;
long val = strtol(v, &endptr, 10);
if (errno != 0) {
fprintf (stderr, "--%s argument: strtol failed: %s\n", lname, strerror(errno));
exit (EXIT_FAILURE);
}
if (endptr == v) {
fprintf (stderr, "--%s argument cannot be parsed as an int\n", lname);
exit (EXIT_FAILURE);
}
return (int) val;
}
int getopt_get_bool(getopt_t *getopt, const char *lname)
{
const char *v = getopt_get_string(getopt, lname);
assert (v!=NULL);
int val = !strcmp(v, "true");
return val;
}
double getopt_get_double (getopt_t *getopt, const char *lname)
{
const char *v = getopt_get_string (getopt, lname);
assert (v!=NULL);
errno = 0;
char *endptr = (char *) v;
double d = strtod (v, &endptr);
if (errno != 0) {
fprintf (stderr, "--%s argument: strtod failed: %s\n", lname, strerror(errno));
exit (EXIT_FAILURE);
}
if (endptr == v) {
fprintf (stderr, "--%s argument cannot be parsed as a double\n", lname);
exit (EXIT_FAILURE);
}
return d;
}
int getopt_was_specified(getopt_t *getopt, const char *lname)
{
getopt_option_t *goo = NULL;
zhash_get(getopt->lopts, &lname, &goo);
if (goo == NULL)
return 0;
return goo->was_specified;
}
const zarray_t *getopt_get_extra_args(getopt_t *gopt)
{
return gopt->extraargs;
}
void getopt_do_usage(getopt_t * gopt)
{
char * usage = getopt_get_usage(gopt);
printf("%s", usage);
free(usage);
}
char * getopt_get_usage(getopt_t *gopt)
{
string_buffer_t * sb = string_buffer_create();
int leftmargin=2;
int longwidth=12;
int valuewidth=10;
for (int i = 0; i < zarray_size(gopt->options); i++) {
getopt_option_t *goo = NULL;
zarray_get(gopt->options, i, &goo);
if (goo->spacer)
continue;
longwidth = imax(longwidth, (int) strlen(goo->lname));
if (goo->type == GOO_STRING_TYPE)
valuewidth = imax(valuewidth, (int) strlen(goo->svalue));
}
for (int i = 0; i < zarray_size(gopt->options); i++) {
getopt_option_t *goo = NULL;
zarray_get(gopt->options, i, &goo);
if (goo->spacer)
{
if (goo->help==NULL || strlen(goo->help)==0)
string_buffer_appendf(sb,"\n");
else
string_buffer_appendf(sb,"\n%*s%s\n\n", leftmargin, "", goo->help);
continue;
}
string_buffer_appendf(sb,"%*s", leftmargin, "");
if (goo->sname[0]==0)
string_buffer_appendf(sb," ");
else
string_buffer_appendf(sb,"-%c | ", goo->sname[0]);
string_buffer_appendf(sb,"--%*s ", -longwidth, goo->lname);
string_buffer_appendf(sb," [ %s ]", goo->svalue); // XXX: displays current value rather than default value
string_buffer_appendf(sb,"%*s", (int) (valuewidth-strlen(goo->svalue)), "");
string_buffer_appendf(sb," %s ", goo->help);
string_buffer_appendf(sb,"\n");
}
char * usage = string_buffer_to_string(sb);
string_buffer_destroy(sb);
return usage;
}
|