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