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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
|
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 2011-2024 Free Software Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GDBM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
# include "autoconf.h"
# include "gdbm.h"
# include "gdbmapp.h"
# include "gdbmdefs.h"
# include <stdio.h>
# include <stdarg.h>
# include <errno.h>
# include <string.h>
# include <ctype.h>
# ifdef HAVE_GETOPT_H
# include <getopt.h>
# endif
static int argc;
static char **argv;
static struct gdbm_option *option_tab;
static size_t option_count;
static size_t option_max;
static char *short_options;
static size_t short_option_count;
static size_t short_option_max;
#ifdef HAVE_GETOPT_LONG
static struct option *long_options;
static size_t long_option_count;
static size_t long_option_max;
#endif
#define OPT_USAGE -2
struct gdbm_option parseopt_default_options[] = {
{ 0, NULL, NULL, "" },
{ 'h', "help", NULL, N_("give this help list") },
{ 'V', "version", NULL, N_("print program version") },
{ OPT_USAGE, "usage", NULL, N_("give a short usage message") },
{ 0 }
};
#define OPT_END(opt) \
((opt)->opt_short == 0 && (opt)->opt_long == 0 && (opt)->opt_descr == NULL)
#define IS_OPTION(opt) \
((opt)->opt_short || (opt)->opt_long)
#define IS_GROUP_HEADER(opt) \
(!IS_OPTION(opt) && (opt)->opt_descr)
#define IS_VALID_SHORT_OPTION(opt) \
((opt)->opt_short > 0 && (opt)->opt_short < 127 && \
isalnum ((opt)->opt_short))
#define IS_VALID_LONG_OPTION(opt) \
((opt)->opt_long != NULL)
static int
optcmp (const void *a, const void *b)
{
struct gdbm_option const *ap = (struct gdbm_option const *)a;
struct gdbm_option const *bp = (struct gdbm_option const *)b;
while (ap->opt_flags & PARSEOPT_ALIAS)
ap--;
while (bp->opt_flags & PARSEOPT_ALIAS)
bp--;
if (IS_VALID_SHORT_OPTION(ap) && IS_VALID_SHORT_OPTION(bp))
return ap->opt_short - bp->opt_short;
if (IS_VALID_LONG_OPTION(ap) && IS_VALID_LONG_OPTION(bp))
return strcmp (ap->opt_long, bp->opt_long);
if (IS_VALID_LONG_OPTION(ap))
return 1;
return -1;
}
static void
sort_options (int start, int count)
{
qsort (option_tab + start, count, sizeof (option_tab[0]), optcmp);
}
static size_t
sort_group (size_t start)
{
size_t i;
for (i = start; i < option_count && !IS_GROUP_HEADER (&option_tab[i]); i++)
;
sort_options (start, i - start);
return i + 1;
}
static void
sort_all_options (void)
{
size_t start;
/* Ensure sane start of options. This is necessary because optcmp backs up
until it finds an element with cleared PARSEOPT_ALIAS flag bit. */
option_tab[0].opt_flags &= PARSEOPT_ALIAS;
for (start = 0; start < option_count; )
{
if (IS_GROUP_HEADER (&option_tab[start]))
start = sort_group (start + 1);
else
start = sort_group (start);
}
}
static void
add_options (struct gdbm_option *options)
{
size_t optcnt = 0;
size_t argcnt = 0;
size_t count = 0;
struct gdbm_option *opt;
for (opt = options; !OPT_END(opt); opt++)
{
count++;
if (IS_OPTION(opt))
{
optcnt++;
if (opt->opt_arg)
argcnt++;
}
}
if (option_count + count + 1 > option_max)
{
option_max = option_count + count + 1;
option_tab = erealloc (option_tab,
sizeof (option_tab[0]) * option_max);
}
#ifdef HAVE_GETOPT_LONG
if (long_option_count + optcnt + 1 > long_option_max)
{
long_option_max = long_option_count + optcnt + 1;
long_options = erealloc (long_options,
sizeof (long_options[0]) * long_option_max);
}
#endif
if (short_option_count + optcnt + argcnt + 1 > short_option_max)
{
short_option_max = short_option_count + optcnt + argcnt + 1;
short_options = erealloc (short_options,
sizeof (short_options[0]) * short_option_max);
}
for (opt = options; !OPT_END(opt); opt++)
{
option_tab[option_count++] = *opt;
if (!IS_OPTION (opt))
continue;
if (IS_VALID_SHORT_OPTION (opt))
{
short_options[short_option_count++] = opt->opt_short;
if (opt->opt_arg)
short_options[short_option_count++] = ':';
}
#ifdef HAVE_GETOPT_LONG
if (IS_VALID_LONG_OPTION (opt))
{
long_options[long_option_count].name = opt->opt_long;
long_options[long_option_count].has_arg = opt->opt_arg != NULL;
long_options[long_option_count].flag = NULL;
long_options[long_option_count].val = opt->opt_short;
long_option_count++;
}
#endif
}
short_options[short_option_count] = 0;
#ifdef HAVE_GETOPT_LONG
memset (&long_options[long_option_count], 0,
sizeof long_options[long_option_count]);
#endif
}
void
parseopt_free (void)
{
free (option_tab);
option_tab = NULL;
free (short_options);
short_options = NULL;
short_option_count = short_option_max = 0;
#ifdef HAVE_GETOPT_LONG
free (long_options);
long_options = NULL;
long_option_count = long_option_max = 0;
#endif
}
int
parseopt_first (int pc, char **pv, struct gdbm_option *opts)
{
parseopt_free ();
add_options (opts);
add_options (parseopt_default_options);
opterr = 0;
argc = pc;
argv = pv;
return parseopt_next ();
}
static unsigned short_opt_col = 2;
static unsigned long_opt_col = 6;
static unsigned doc_opt_col = 2; /* FIXME: Not used: there are no doc
options in this implementation */
static unsigned header_col = 1;
static unsigned opt_doc_col = 29;
static unsigned usage_indent = 12;
static unsigned rmargin = 79;
static unsigned dup_args = 0;
static unsigned dup_args_note = 1;
enum usage_var_type
{
usage_var_column,
usage_var_bool
};
struct usage_var_def
{
char *name;
unsigned *valptr;
enum usage_var_type type;
};
static struct usage_var_def usage_var[] = {
{ "short-opt-col", &short_opt_col, usage_var_column },
{ "header-col", &header_col, usage_var_column },
{ "opt-doc-col", &opt_doc_col, usage_var_column },
{ "usage-indent", &usage_indent, usage_var_column },
{ "rmargin", &rmargin, usage_var_column },
{ "dup-args", &dup_args, usage_var_bool },
{ "dup-args-note", &dup_args_note, usage_var_bool },
{ "long-opt-col", &long_opt_col, usage_var_column },
{ "doc-opt-col", &doc_opt_col, usage_var_column },
{ NULL }
};
static void
set_usage_var (char const *text, char **end)
{
struct usage_var_def *p;
int boolval = 1;
char const *prog_name = parseopt_program_name ? parseopt_program_name : progname;
size_t len = strcspn (text, ",=");
char *endp;
if (len > 3 && memcmp (text, "no-", 3) == 0)
{
text += 3;
len -= 3;
boolval = 0;
}
for (p = usage_var; p->name; p++)
{
if (strlen (p->name) == len && memcmp (p->name, text, len) == 0)
break;
}
endp = (char*) text + len;
if (p)
{
if (p->type == usage_var_bool)
{
if (*endp == '=')
{
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr,
_("error in ARGP_HELP_FMT: improper usage of [no-]%s\n"),
p->name);
endp = strchr (text + len, ',');
}
else
*p->valptr = boolval;
}
else if (*endp == '=')
{
unsigned long val;
errno = 0;
val = strtoul (text + len + 1, &endp, 10);
if (errno || (*endp && *endp != ','))
{
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr,
_("error in ARGP_HELP_FMT: bad value for %s"),
p->name);
if (endp)
{
fprintf (stderr, _(" (near %s)"), endp);
}
fputc ('\n', stderr);
}
else if (val > UINT_MAX)
{
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr,
_("error in ARGP_HELP_FMT: %s value is out of range\n"),
p->name);
}
else
*p->valptr = val;
}
else
{
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr,
_("%s: ARGP_HELP_FMT parameter requires a value\n"),
p->name);
}
}
else
{
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr,
_("%s: Unknown ARGP_HELP_FMT parameter\n"),
text);
}
*end = endp;
}
static void
init_usage_vars (void)
{
char *fmt, *p;
fmt = getenv ("ARGP_HELP_FMT");
if (!fmt || !*fmt)
return;
while (1)
{
set_usage_var (fmt, &p);
if (*p == 0)
break;
else if (*p == ',')
p++;
else
{
char const *prog_name = parseopt_program_name ? parseopt_program_name : progname;
if (prog_name)
fprintf (stderr, "%s: ", prog_name);
fprintf (stderr, _("ARGP_HELP_FMT: missing delimiter near %s\n"),
p);
break;
}
fmt = p;
}
}
char *parseopt_program_name;
const char *program_bug_address = "<" PACKAGE_BUGREPORT ">";
void (*parseopt_help_hook) (FILE *stream);
static int argsused;
static int
print_arg (WORDWRAP_FILE wf, struct gdbm_option *opt, int delim)
{
if (opt->opt_arg)
{
argsused = 1;
return wordwrap_printf (wf, "%c%s", delim,
opt->opt_arg[0] ? gettext (opt->opt_arg) : "");
}
return 0;
}
size_t
print_option (WORDWRAP_FILE wf, size_t num)
{
struct gdbm_option *opt = option_tab + num;
size_t next, i;
int delim;
int w;
if (IS_GROUP_HEADER (opt))
{
wordwrap_set_left_margin (wf, header_col);
wordwrap_set_right_margin (wf, rmargin);
if (opt->opt_descr[0])
{
wordwrap_putc (wf, '\n');
wordwrap_puts (wf, gettext (opt->opt_descr));
wordwrap_putc (wf, '\n');
}
wordwrap_putc (wf, '\n');
return num + 1;
}
/* count aliases */
for (next = num + 1;
next < option_count && option_tab[next].opt_flags & PARSEOPT_ALIAS;
next++);
if (opt->opt_flags & PARSEOPT_HIDDEN)
return next;
wordwrap_set_left_margin (wf, short_opt_col);
w = 0;
for (i = num; i < next; i++)
{
if (IS_VALID_SHORT_OPTION (&option_tab[i]))
{
if (w)
wordwrap_write (wf, ", ", 2);
wordwrap_printf (wf, "-%c", option_tab[i].opt_short);
delim = ' ';
if (dup_args)
print_arg (wf, opt, delim);
w = 1;
}
}
#ifdef HAVE_GETOPT_LONG
for (i = num; i < next; i++)
{
if (IS_VALID_LONG_OPTION (&option_tab[i]))
{
if (w)
wordwrap_write (wf, ", ", 2);
wordwrap_set_left_margin (wf, long_opt_col);
w = 0;
break;
}
}
for (; i < next; i++)
{
if (IS_VALID_LONG_OPTION (&option_tab[i]))
{
if (w)
wordwrap_write (wf, ", ", 2);
wordwrap_printf (wf, "--%s", option_tab[i].opt_long);
delim = '=';
if (dup_args)
print_arg (wf, opt, delim);
w = 1;
}
}
#endif
if (!dup_args)
print_arg (wf, opt, delim);
wordwrap_set_left_margin (wf, opt_doc_col);
if (opt->opt_descr[0])
wordwrap_puts (wf, gettext (opt->opt_descr));
return next;
}
void
parseopt_print_help (void)
{
unsigned i;
WORDWRAP_FILE wf;
argsused = 0;
init_usage_vars ();
wf = wordwrap_fdopen (1);
wordwrap_printf (wf, "%s %s [%s]... %s\n", _("Usage:"),
parseopt_program_name ? parseopt_program_name : progname,
_("OPTION"),
(parseopt_program_args && parseopt_program_args[0])
? gettext (parseopt_program_args) : "");
wordwrap_set_right_margin (wf, rmargin);
if (parseopt_program_doc && parseopt_program_doc[0])
wordwrap_puts (wf, gettext (parseopt_program_doc));
wordwrap_para (wf);
sort_all_options ();
for (i = 0; i < option_count; )
{
i = print_option (wf, i);
}
wordwrap_para (wf);
#ifdef HAVE_GETOPT_LONG
if (argsused && dup_args_note)
{
wordwrap_set_left_margin (wf, 0);
wordwrap_set_right_margin (wf, rmargin);
wordwrap_puts (wf, _("Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options."));
wordwrap_para (wf);
}
#endif
if (parseopt_help_hook)
parseopt_help_hook (stdout);//FIXME
wordwrap_set_left_margin (wf, 0);
wordwrap_set_right_margin (wf, rmargin);
/* TRANSLATORS: The placeholder indicates the bug-reporting address
for this package. Please add _another line_ saying
"Report translation bugs to <...>\n" with the address for translation
bugs (typically your translation team's web or email address). */
wordwrap_printf (wf, _("Report bugs to %s.\n"), program_bug_address);
#ifdef PACKAGE_URL
wordwrap_printf (wf, _("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
#endif
}
static int
cmpidx_short (const void *a, const void *b)
{
unsigned const *ai = (unsigned const *)a;
unsigned const *bi = (unsigned const *)b;
return option_tab[*ai].opt_short - option_tab[*bi].opt_short;
}
#ifdef HAVE_GETOPT_LONG
static int
cmpidx_long (const void *a, const void *b)
{
unsigned const *ai = (unsigned const *)a;
unsigned const *bi = (unsigned const *)b;
struct gdbm_option const *ap = option_tab + *ai;
struct gdbm_option const *bp = option_tab + *bi;
return strcmp (ap->opt_long, bp->opt_long);
}
#endif
void
print_usage (void)
{
WORDWRAP_FILE wf;
unsigned i;
unsigned *idxbuf;
unsigned nidx;
init_usage_vars ();
idxbuf = ecalloc (option_count, sizeof (idxbuf[0]));
wf = wordwrap_fdopen (1);
wordwrap_set_right_margin (wf, rmargin);
wordwrap_printf (wf, "%s %s ", _("Usage:"),
parseopt_program_name ? parseopt_program_name : progname);
wordwrap_next_left_margin (wf, usage_indent);
/* Print a list of short options without arguments. */
for (i = nidx = 0; i < option_count; i++)
if (IS_VALID_SHORT_OPTION (&option_tab[i]) && !option_tab[i].opt_arg)
idxbuf[nidx++] = i;
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
wordwrap_puts (wf, "[-");
for (i = 0; i < nidx; i++)
{
wordwrap_putc (wf, option_tab[idxbuf[i]].opt_short);
}
wordwrap_putc (wf, ']');
}
/* Print a list of short options with arguments. */
for (i = nidx = 0; i < option_count; i++)
{
if (IS_VALID_SHORT_OPTION (&option_tab[i]) && option_tab[i].opt_arg)
idxbuf[nidx++] = i;
}
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_short);
for (i = 0; i < nidx; i++)
{
struct gdbm_option *opt = option_tab + idxbuf[i];
const char *arg = gettext (opt->opt_arg);
wordwrap_word_start (wf);
wordwrap_puts (wf, " [-");
wordwrap_putc (wf, opt->opt_short);
wordwrap_putc (wf, ' ');
wordwrap_puts (wf, arg);
wordwrap_putc (wf, ']');
wordwrap_word_end (wf);
}
}
#ifdef HAVE_GETOPT_LONG
/* Print a list of long options */
for (i = nidx = 0; i < option_count; i++)
{
if (IS_VALID_LONG_OPTION (&option_tab[i]))
idxbuf[nidx++] = i;
}
if (nidx)
{
qsort (idxbuf, nidx, sizeof (idxbuf[0]), cmpidx_long);
for (i = 0; i < nidx; i++)
{
struct gdbm_option *opt = option_tab + idxbuf[i];
const char *arg = opt->opt_arg ? gettext (opt->opt_arg) : NULL;
wordwrap_word_start (wf);
wordwrap_write (wf, " [--", 4);
wordwrap_puts (wf, opt->opt_long);
if (opt->opt_arg)
{
wordwrap_putc (wf, '=');
wordwrap_write (wf, arg, strlen (arg));
}
wordwrap_putc (wf, ']');
wordwrap_word_end (wf);
}
}
#endif
wordwrap_close (wf);
free (idxbuf);
}
const char version_etc_copyright[] =
/* Do *not* mark this string for translation. First %s is a copyright
symbol suitable for this locale, and second %s are the copyright
years. */
"Copyright %s %s Free Software Foundation, Inc";
const char license_text[] =
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.";
void
print_version_only (void)
{
printf ("%s (%s) %s\n",
parseopt_program_name ? parseopt_program_name : progname,
PACKAGE_NAME,
PACKAGE_VERSION);
/* TRANSLATORS: Translate "(C)" to the copyright symbol
(C-in-a-circle), if this symbol is available in the user's
locale. Otherwise, do not translate "(C)"; leave it as-is. */
printf (version_etc_copyright, _("(C)"), "2011-2024");
putchar ('\n');
puts (license_text);
putchar ('\n');
}
static int
handle_option (int c)
{
switch (c)
{
case 'h':
parseopt_print_help ();
exit (0);
case 'V':
print_version_only ();
exit (0);
case OPT_USAGE:
print_usage ();
exit (0);
default:
break;
}
return 0;
}
int
parseopt_next (void)
{
int rc;
do
{
#ifdef HAVE_GETOPT_LONG
rc = getopt_long (argc, argv, short_options, long_options, NULL);
#else
rc = getopt (argc, argv, short_options);
#endif
}
while (handle_option (rc));
return rc;
}
|