1/* $NetBSD: getarg.c,v 1.2 2011/04/14 18:12:08 elric Exp $ */ 2 3/* 4 * Copyright (c) 1997 - 2002 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36#include <config.h> 37 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <krb5/roken.h> 42#include <krb5/getarg.h> 43 44#define ISFLAG(X) ((X).type == arg_flag || (X).type == arg_negative_flag) 45 46static size_t 47print_arg (char *string, 48 size_t len, 49 int mdoc, 50 int longp, 51 struct getargs *arg, 52 char *(i18n)(const char *)) 53{ 54 const char *s; 55 56 *string = '\0'; 57 58 if (ISFLAG(*arg) || (!longp && arg->type == arg_counter)) 59 return 0; 60 61 if(mdoc){ 62 if(longp) 63 strlcat(string, "= Ns", len); 64 strlcat(string, " Ar ", len); 65 } else { 66 if (longp) 67 strlcat (string, "=", len); 68 else 69 strlcat (string, " ", len); 70 } 71 72 if (arg->arg_help) 73 s = (*i18n)(arg->arg_help); 74 else if (arg->type == arg_integer || arg->type == arg_counter) 75 s = "integer"; 76 else if (arg->type == arg_string) 77 s = "string"; 78 else if (arg->type == arg_strings) 79 s = "strings"; 80 else if (arg->type == arg_double) 81 s = "float"; 82 else 83 s = "<undefined>"; 84 85 strlcat(string, s, len); 86 return 1 + strlen(s); 87} 88 89static void 90mandoc_template(struct getargs *args, 91 size_t num_args, 92 const char *progname, 93 const char *extra_string, 94 char *(i18n)(const char *)) 95{ 96 size_t i; 97 char timestr[64], cmd[64]; 98 char buf[128]; 99 const char *p; 100 time_t t; 101 102 printf(".\\\" Things to fix:\n"); 103 printf(".\\\" * correct section, and operating system\n"); 104 printf(".\\\" * remove Op from mandatory flags\n"); 105 printf(".\\\" * use better macros for arguments (like .Pa for files)\n"); 106 printf(".\\\"\n"); 107 t = time(NULL); 108 strftime(timestr, sizeof(timestr), "%B %e, %Y", localtime(&t)); 109 printf(".Dd %s\n", timestr); 110 p = strrchr(progname, '/'); 111 if(p) p++; else p = progname; 112 strlcpy(cmd, p, sizeof(cmd)); 113 strupr(cmd); 114 115 printf(".Dt %s SECTION\n", cmd); 116 printf(".Os OPERATING_SYSTEM\n"); 117 printf(".Sh NAME\n"); 118 printf(".Nm %s\n", p); 119 printf(".Nd\n"); 120 printf("in search of a description\n"); 121 printf(".Sh SYNOPSIS\n"); 122 printf(".Nm\n"); 123 for(i = 0; i < num_args; i++){ 124 /* we seem to hit a limit on number of arguments if doing 125 short and long flags with arguments -- split on two lines */ 126 if(ISFLAG(args[i]) || 127 args[i].short_name == 0 || args[i].long_name == NULL) { 128 printf(".Op "); 129 130 if(args[i].short_name) { 131 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n); 132 printf("Fl %c%s", args[i].short_name, buf); 133 if(args[i].long_name) 134 printf(" | "); 135 } 136 if(args[i].long_name) { 137 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n); 138 printf("Fl -%s%s%s", 139 args[i].type == arg_negative_flag ? "no-" : "", 140 args[i].long_name, buf); 141 } 142 printf("\n"); 143 } else { 144 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n); 145 printf(".Oo Fl %c%s \\*(Ba Xo\n", args[i].short_name, buf); 146 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n); 147 printf(".Fl -%s%s\n.Xc\n.Oc\n", args[i].long_name, buf); 148 } 149 /* 150 if(args[i].type == arg_strings) 151 fprintf (stderr, "..."); 152 */ 153 } 154 if (extra_string && *extra_string) 155 printf (".Ar %s\n", extra_string); 156 printf(".Sh DESCRIPTION\n"); 157 printf("Supported options:\n"); 158 printf(".Bl -tag -width Ds\n"); 159 for(i = 0; i < num_args; i++){ 160 printf(".It Xo\n"); 161 if(args[i].short_name){ 162 printf(".Fl %c", args[i].short_name); 163 print_arg(buf, sizeof(buf), 1, 0, args + i, i18n); 164 printf("%s", buf); 165 if(args[i].long_name) 166 printf(" ,"); 167 printf("\n"); 168 } 169 if(args[i].long_name){ 170 printf(".Fl -%s%s", 171 args[i].type == arg_negative_flag ? "no-" : "", 172 args[i].long_name); 173 print_arg(buf, sizeof(buf), 1, 1, args + i, i18n); 174 printf("%s\n", buf); 175 } 176 printf(".Xc\n"); 177 if(args[i].help) 178 printf("%s\n", args[i].help); 179 /* 180 if(args[i].type == arg_strings) 181 fprintf (stderr, "..."); 182 */ 183 } 184 printf(".El\n"); 185 printf(".\\\".Sh ENVIRONMENT\n"); 186 printf(".\\\".Sh FILES\n"); 187 printf(".\\\".Sh EXAMPLES\n"); 188 printf(".\\\".Sh DIAGNOSTICS\n"); 189 printf(".\\\".Sh SEE ALSO\n"); 190 printf(".\\\".Sh STANDARDS\n"); 191 printf(".\\\".Sh HISTORY\n"); 192 printf(".\\\".Sh AUTHORS\n"); 193 printf(".\\\".Sh BUGS\n"); 194} 195 196static int 197check_column(FILE *f, int col, int len, int columns) 198{ 199 if(col + len > columns) { 200 fprintf(f, "\n"); 201 col = fprintf(f, " "); 202 } 203 return col; 204} 205 206static char * 207builtin_i18n(const char *str) 208{ 209 return rk_UNCONST(str); 210} 211 212ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 213arg_printusage (struct getargs *args, 214 size_t num_args, 215 const char *progname, 216 const char *extra_string) 217{ 218 arg_printusage_i18n(args, num_args, "Usage", 219 progname, extra_string, builtin_i18n); 220} 221 222ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 223arg_printusage_i18n (struct getargs *args, 224 size_t num_args, 225 const char *usage, 226 const char *progname, 227 const char *extra_string, 228 char *(*i18n)(const char *)) 229{ 230 size_t i, max_len = 0; 231 char buf[128]; 232 int col = 0, columns; 233 234 if (progname == NULL) 235 progname = getprogname(); 236 237 if (i18n == NULL) 238 i18n = builtin_i18n; 239 240 if(getenv("GETARGMANDOC")){ 241 mandoc_template(args, num_args, progname, extra_string, i18n); 242 return; 243 } 244 if(get_window_size(2, NULL, &columns) == -1) 245 columns = 80; 246 col = 0; 247 col += fprintf (stderr, "%s: %s", usage, progname); 248 buf[0] = '\0'; 249 for (i = 0; i < num_args; ++i) { 250 if(args[i].short_name && ISFLAG(args[i])) { 251 char s[2]; 252 if(buf[0] == '\0') 253 strlcpy(buf, "[-", sizeof(buf)); 254 s[0] = args[i].short_name; 255 s[1] = '\0'; 256 strlcat(buf, s, sizeof(buf)); 257 } 258 } 259 if(buf[0] != '\0') { 260 strlcat(buf, "]", sizeof(buf)); 261 col = check_column(stderr, col, strlen(buf) + 1, columns); 262 col += fprintf(stderr, " %s", buf); 263 } 264 265 for (i = 0; i < num_args; ++i) { 266 size_t len = 0; 267 268 if (args[i].long_name) { 269 buf[0] = '\0'; 270 strlcat(buf, "[--", sizeof(buf)); 271 len += 2; 272 if(args[i].type == arg_negative_flag) { 273 strlcat(buf, "no-", sizeof(buf)); 274 len += 3; 275 } 276 strlcat(buf, args[i].long_name, sizeof(buf)); 277 len += strlen(args[i].long_name); 278 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf), 279 0, 1, &args[i], i18n); 280 strlcat(buf, "]", sizeof(buf)); 281 if(args[i].type == arg_strings) 282 strlcat(buf, "...", sizeof(buf)); 283 col = check_column(stderr, col, strlen(buf) + 1, columns); 284 col += fprintf(stderr, " %s", buf); 285 } 286 if (args[i].short_name && !ISFLAG(args[i])) { 287 snprintf(buf, sizeof(buf), "[-%c", args[i].short_name); 288 len += 2; 289 len += print_arg(buf + strlen(buf), sizeof(buf) - strlen(buf), 290 0, 0, &args[i], i18n); 291 strlcat(buf, "]", sizeof(buf)); 292 if(args[i].type == arg_strings) 293 strlcat(buf, "...", sizeof(buf)); 294 col = check_column(stderr, col, strlen(buf) + 1, columns); 295 col += fprintf(stderr, " %s", buf); 296 } 297 if (args[i].long_name && args[i].short_name) 298 len += 2; /* ", " */ 299 max_len = max(max_len, len); 300 } 301 if (extra_string) { 302 check_column(stderr, col, strlen(extra_string) + 1, columns); 303 fprintf (stderr, " %s\n", extra_string); 304 } else 305 fprintf (stderr, "\n"); 306 for (i = 0; i < num_args; ++i) { 307 if (args[i].help) { 308 size_t count = 0; 309 310 if (args[i].short_name) { 311 count += fprintf (stderr, "-%c", args[i].short_name); 312 print_arg (buf, sizeof(buf), 0, 0, &args[i], i18n); 313 count += fprintf(stderr, "%s", buf); 314 } 315 if (args[i].short_name && args[i].long_name) 316 count += fprintf (stderr, ", "); 317 if (args[i].long_name) { 318 count += fprintf (stderr, "--"); 319 if (args[i].type == arg_negative_flag) 320 count += fprintf (stderr, "no-"); 321 count += fprintf (stderr, "%s", args[i].long_name); 322 print_arg (buf, sizeof(buf), 0, 1, &args[i], i18n); 323 count += fprintf(stderr, "%s", buf); 324 } 325 while(count++ <= max_len) 326 putc (' ', stderr); 327 fprintf (stderr, "%s\n", (*i18n)(args[i].help)); 328 } 329 } 330} 331 332static int 333add_string(getarg_strings *s, char *value) 334{ 335 char **strings; 336 337 strings = realloc(s->strings, (s->num_strings + 1) * sizeof(*s->strings)); 338 if (strings == NULL) { 339 free(s->strings); 340 s->strings = NULL; 341 s->num_strings = 0; 342 return ENOMEM; 343 } 344 s->strings = strings; 345 s->strings[s->num_strings] = value; 346 s->num_strings++; 347 return 0; 348} 349 350static int 351arg_match_long(struct getargs *args, size_t num_args, 352 char *argv, int argc, char **rargv, int *goptind) 353{ 354 int i; 355 char *goptarg = NULL; 356 int negate = 0; 357 int partial_match = 0; 358 struct getargs *partial = NULL; 359 struct getargs *current = NULL; 360 int argv_len; 361 char *p; 362 int p_len; 363 364 argv_len = strlen(argv); 365 p = strchr (argv, '='); 366 if (p != NULL) 367 argv_len = p - argv; 368 369 for (i = 0; i < num_args; ++i) { 370 if(args[i].long_name) { 371 int len = strlen(args[i].long_name); 372 p = argv; 373 p_len = argv_len; 374 negate = 0; 375 376 for (;;) { 377 if (strncmp (args[i].long_name, p, p_len) == 0) { 378 if(p_len == len) 379 current = &args[i]; 380 else { 381 ++partial_match; 382 partial = &args[i]; 383 } 384 goptarg = p + p_len; 385 } else if (ISFLAG(args[i]) && strncmp (p, "no-", 3) == 0) { 386 negate = !negate; 387 p += 3; 388 p_len -= 3; 389 continue; 390 } 391 break; 392 } 393 if (current) 394 break; 395 } 396 } 397 if (current == NULL) { 398 if (partial_match == 1) 399 current = partial; 400 else 401 return ARG_ERR_NO_MATCH; 402 } 403 404 if(*goptarg == '\0' 405 && !ISFLAG(*current) 406 && current->type != arg_collect 407 && current->type != arg_counter) 408 return ARG_ERR_NO_MATCH; 409 switch(current->type){ 410 case arg_integer: 411 { 412 int tmp; 413 if(sscanf(goptarg + 1, "%d", &tmp) != 1) 414 return ARG_ERR_BAD_ARG; 415 *(int*)current->value = tmp; 416 return 0; 417 } 418 case arg_string: 419 { 420 *(char**)current->value = goptarg + 1; 421 return 0; 422 } 423 case arg_strings: 424 { 425 return add_string((getarg_strings*)current->value, goptarg + 1); 426 } 427 case arg_flag: 428 case arg_negative_flag: 429 { 430 int *flag = current->value; 431 if(*goptarg == '\0' || 432 strcmp(goptarg + 1, "yes") == 0 || 433 strcmp(goptarg + 1, "true") == 0){ 434 *flag = !negate; 435 return 0; 436 } else if (*goptarg && strcmp(goptarg + 1, "maybe") == 0) { 437 *flag = rk_random() & 1; 438 } else { 439 *flag = negate; 440 return 0; 441 } 442 return ARG_ERR_BAD_ARG; 443 } 444 case arg_counter : 445 { 446 int val; 447 448 if (*goptarg == '\0') 449 val = 1; 450 else if(sscanf(goptarg + 1, "%d", &val) != 1) 451 return ARG_ERR_BAD_ARG; 452 *(int *)current->value += val; 453 return 0; 454 } 455 case arg_double: 456 { 457 double tmp; 458 if(sscanf(goptarg + 1, "%lf", &tmp) != 1) 459 return ARG_ERR_BAD_ARG; 460 *(double*)current->value = tmp; 461 return 0; 462 } 463 case arg_collect:{ 464 struct getarg_collect_info *c = current->value; 465 int o = argv - rargv[*goptind]; 466 return (*c->func)(FALSE, argc, rargv, goptind, &o, c->data); 467 } 468 469 default: 470 abort (); 471 UNREACHABLE(return 0); 472 } 473} 474 475static int 476arg_match_short (struct getargs *args, size_t num_args, 477 char *argv, int argc, char **rargv, int *goptind) 478{ 479 int j, k; 480 481 for(j = 1; j > 0 && j < strlen(rargv[*goptind]); j++) { 482 for(k = 0; k < num_args; k++) { 483 char *goptarg; 484 485 if(args[k].short_name == 0) 486 continue; 487 if(argv[j] == args[k].short_name) { 488 if(args[k].type == arg_flag) { 489 *(int*)args[k].value = 1; 490 break; 491 } 492 if(args[k].type == arg_negative_flag) { 493 *(int*)args[k].value = 0; 494 break; 495 } 496 if(args[k].type == arg_counter) { 497 ++*(int *)args[k].value; 498 break; 499 } 500 if(args[k].type == arg_collect) { 501 struct getarg_collect_info *c = args[k].value; 502 503 if((*c->func)(TRUE, argc, rargv, goptind, &j, c->data)) 504 return ARG_ERR_BAD_ARG; 505 break; 506 } 507 508 if(argv[j + 1]) 509 goptarg = &argv[j + 1]; 510 else { 511 ++*goptind; 512 goptarg = rargv[*goptind]; 513 } 514 if(goptarg == NULL) { 515 --*goptind; 516 return ARG_ERR_NO_ARG; 517 } 518 if(args[k].type == arg_integer) { 519 int tmp; 520 if(sscanf(goptarg, "%d", &tmp) != 1) 521 return ARG_ERR_BAD_ARG; 522 *(int*)args[k].value = tmp; 523 return 0; 524 } else if(args[k].type == arg_string) { 525 *(char**)args[k].value = goptarg; 526 return 0; 527 } else if(args[k].type == arg_strings) { 528 return add_string((getarg_strings*)args[k].value, goptarg); 529 } else if(args[k].type == arg_double) { 530 double tmp; 531 if(sscanf(goptarg, "%lf", &tmp) != 1) 532 return ARG_ERR_BAD_ARG; 533 *(double*)args[k].value = tmp; 534 return 0; 535 } 536 return ARG_ERR_BAD_ARG; 537 } 538 } 539 if (k == num_args) 540 return ARG_ERR_NO_MATCH; 541 } 542 return 0; 543} 544 545ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 546getarg(struct getargs *args, size_t num_args, 547 int argc, char **argv, int *goptind) 548{ 549 int i; 550 int ret = 0; 551 552 rk_random_init(); 553 (*goptind)++; 554 for(i = *goptind; i < argc; i++) { 555 if(argv[i][0] != '-') 556 break; 557 if(argv[i][1] == '-'){ 558 if(argv[i][2] == 0){ 559 i++; 560 break; 561 } 562 ret = arg_match_long (args, num_args, argv[i] + 2, 563 argc, argv, &i); 564 } else { 565 ret = arg_match_short (args, num_args, argv[i], 566 argc, argv, &i); 567 } 568 if(ret) 569 break; 570 } 571 *goptind = i; 572 return ret; 573} 574 575ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL 576free_getarg_strings (getarg_strings *s) 577{ 578 free (s->strings); 579} 580 581#if TEST 582int foo_flag = 2; 583int flag1 = 0; 584int flag2 = 0; 585int bar_int; 586char *baz_string; 587 588struct getargs args[] = { 589 { NULL, '1', arg_flag, &flag1, "one", NULL }, 590 { NULL, '2', arg_flag, &flag2, "two", NULL }, 591 { "foo", 'f', arg_negative_flag, &foo_flag, "foo", NULL }, 592 { "bar", 'b', arg_integer, &bar_int, "bar", "seconds"}, 593 { "baz", 'x', arg_string, &baz_string, "baz", "name" }, 594}; 595 596int main(int argc, char **argv) 597{ 598 int goptind = 0; 599 while(getarg(args, 5, argc, argv, &goptind)) 600 printf("Bad arg: %s\n", argv[goptind]); 601 printf("flag1 = %d\n", flag1); 602 printf("flag2 = %d\n", flag2); 603 printf("foo_flag = %d\n", foo_flag); 604 printf("bar_int = %d\n", bar_int); 605 printf("baz_flag = %s\n", baz_string); 606 arg_printusage (args, 5, argv[0], "nothing here"); 607} 608#endif 609