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 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
|
/*
Copyright (c) 2000, 2003 Dmitry Butskoy
<buc@citadel.stu.neva.ru>
License: LGPL v2.1 or any later
See COPYING.LIB for the status of this software.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "clif.h"
#if 1 /* Bad idea, anyway... */
#define MAX_ARGC_NUMBER 256
typedef unsigned char _CLIF_index;
#else
#define MAX_ARGC_NUMBER (4096 / 5 + 1) /* POSIX ARG_MAX >= 4096 ... */
typedef unsigned short _CLIF_index;
#endif
/* This is needed for some print info functions.
This is ugly for thread-safe (is it really actual on program invoking?),
and for several CLIF_parse_cmdline invoking... But foo on this. Yeah...
*/
static struct {
int argc;
char **argv;
CLIF_option *option_list;
CLIF_argument *argument_list;
unsigned int parse_flags;
} curr = { 0, };
static void err_report (const char *format, ...) {
va_list ap;
if (curr.parse_flags & CLIF_SILENT)
return;
va_start (ap, format);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf (stderr, "\n");
return;
}
/* info generation stuff... */
#define SHORT_PLUS_MINUS "+/-"
#define LONG_PLUS_MINUS "++/--"
#define EXCL_DLM " | "
static char *show_short (const CLIF_option *optn) {
static char buf[80];
char *p = buf;
unsigned int flags = optn->flags | curr.parse_flags;
if (optn->function_plus) {
if (!optn->function) *p++ = '+';
else {
strcpy (p, SHORT_PLUS_MINUS);
p += sizeof (SHORT_PLUS_MINUS) - 1;
}
} else
*p++ = '-';
*p++ = optn->short_opt[0];
if (optn->arg_name) {
char *endp = buf + sizeof (buf) - sizeof (",...]");
const char *s;
if (!(flags & _CLIF_STRICT_JOIN_ARG)) *p++ = ' ';
if (flags & CLIF_OPTARG) *p++ = '[';
s = optn->arg_name;
while (*s && p < endp) *p++ = *s++;
if (flags & CLIF_SEVERAL) {
strcpy (p, ",...");
p += sizeof (",...") - 1; /* last '\0' ... */
}
if (flags & CLIF_OPTARG) *p++ = ']';
}
*p = '\0';
return buf;
}
static char *show_long (const CLIF_option *optn) {
static char buf[80];
char *p = buf;
char *endp;
const char *s;
unsigned int flags = optn->flags | curr.parse_flags;
if (!(flags & _CLIF_STRICT_KEYWORD)) {
if (!(flags & _CLIF_STRICT_ONEDASH)) {
if (optn->function_plus) {
if (!optn->function) { *p++ = '+'; *p++ = '+'; }
else {
strcpy (p, LONG_PLUS_MINUS);
p += sizeof (LONG_PLUS_MINUS) - 1;
}
} else { *p++ = '-'; *p++ = '-'; }
} else {
if (optn->function_plus) {
if (!optn->function) *p++ = '+';
else {
strcpy (p, SHORT_PLUS_MINUS);
p += sizeof (SHORT_PLUS_MINUS) - 1;
}
} else *p++ = '-';
}
}
s = optn->long_opt;
endp = buf + sizeof (buf) - sizeof (" [");
while (*s && p < endp) *p++ = *s++;
if (optn->arg_name) {
if (flags & _CLIF_STRICT_NOEQUAL) {
*p++ = ' ';
if (flags & CLIF_OPTARG) *p++ = '[';
} else {
if (flags & CLIF_OPTARG) *p++ = '[';
*p++ = '=';
}
s = optn->arg_name;
endp = buf + sizeof (buf) - sizeof (",...]");
while (*s && p < endp) *p++ = *s++;
if (flags & CLIF_SEVERAL) {
strcpy (p, ",...");
p += sizeof (",...") - 1; /* last '\0' ... */
}
if (flags & CLIF_OPTARG) *p++ = ']';
}
*p = '\0';
return buf;
}
static char *show_excl (const CLIF_option *option_list, int *cnt_p) {
static char buf[256];
const CLIF_option *optn;
char *p = buf;
char *endp = buf + sizeof (buf) - sizeof (EXCL_DLM);
int excl_cnt = 0;
*p = '\0';
if (cnt_p) *cnt_p = 0;
if (!option_list) return buf;
for (optn = option_list; optn->short_opt || optn->long_opt; optn++) {
char *s;
if (!(optn->flags & CLIF_EXCL)) continue;
if (optn->short_opt) s = show_short (optn);
else s = show_long (optn);
if (excl_cnt > 0) { /* i.e., second etc... */
strcpy (p, EXCL_DLM);
p += sizeof (EXCL_DLM) - 1;
}
while (*s && p < endp) *p++ = *s++;
excl_cnt++;
}
*p = '\0';
if (cnt_p) *cnt_p = excl_cnt;
return buf;
}
static int is_keyword (const CLIF_option *optn) {
unsigned int flags = optn->flags | curr.parse_flags;
return (flags & _CLIF_STRICT_KEYWORD) != 0;
}
static void err_bad_opt (const char *arg, char c, int n) {
char sym = (*arg == '+') ? '+' : '-';
if (c) err_report ("Bad option `%c%c' (argc %d)", sym, c, n);
else {
char *p = strchr (arg, '=');
const char *type = (*arg == sym) ? "option" : "keyword";
if (p)
err_report ("Bad %s `%s' (with arg `%s') (argc %d)",
type, arg, p + 1, n);
else
err_report ("Bad %s `%s' (argc %d)", type, arg, n);
}
}
static void err_bad_arg (const CLIF_option *optn, char c, int n) {
CLIF_option tmp = *optn;
char ss[80];
char *s;
tmp.arg_name = NULL;
if (c) {
s = show_short (&tmp); /* always without arg... */
strncpy (ss, s, sizeof (ss));
s = show_short (optn);
} else {
s = show_long (&tmp); /* always without arg... */
strncpy (ss, s, sizeof (ss));
s = show_long (optn);
}
err_report ("%s `%s' (argc %d) requires an argument: `%s'",
(c || !is_keyword (optn)) ? "Option" : "Keyword", ss, n, s);
}
static void err_bad_res (const CLIF_option *optn, char c,
const char *opt_arg, int n) {
CLIF_option tmp = *optn;
char *ss;
const char *type;
tmp.arg_name = NULL;
if (c) {
ss = show_short (&tmp);
type = "option";
} else {
ss = show_long (&tmp);
type = is_keyword (optn) ? "keyword" : "option";
}
if (optn->arg_name)
err_report ("Cannot handle `%s' %s with arg `%s' (argc %d)",
ss, type, opt_arg, n);
else
err_report ("Cannot handle `%s' %s (argc %d)", ss, type, n);
}
static void err_bad_excl (const CLIF_option *optn, char c, int n) {
CLIF_option tmp = *optn;
char *ss;
char *excl = show_excl (curr.option_list, 0);
/* Note: show_(short|long)() nested!!! */
tmp.arg_name = NULL;
if (c) ss = show_short (&tmp);
else ss = show_long (&tmp);
err_report ("%s `%s' (argc %d): Only one of:\n %s\n"
"may be specified.",
(c || !is_keyword (optn)) ? "Option" : "Keyword",
ss, n, excl);
}
static CLIF_option *find_long (char *arg, char **arg_p,
unsigned int match, unsigned int nomatch) {
CLIF_option *optn;
CLIF_option *abbrev = NULL;
char *abbrev_arg = NULL;
int abbrev_found = 0;
for (optn = curr.option_list;
optn->short_opt || optn->long_opt;
optn++
) {
char *a;
const char *o;
unsigned int flags;
if (!optn->long_opt) continue;
flags = curr.parse_flags | optn->flags;
if (flags & nomatch) continue;
if (match && !(flags & match)) continue; /* XXX: optimize it */
for (a = arg, o = optn->long_opt; *o && *a == *o; a++, o++) ;
if (*a == '\0' ||
(*a == '=' && optn->arg_name && !(flags & _CLIF_STRICT_NOEQUAL))
) { /* looks like end of option... */
if (!*o) { /* explicit match found */
if (*a == '=' && arg_p) *arg_p = a + 1;
return optn;
}
if ((flags & CLIF_ABBREV) &&
(a - arg >= CLIF_MIN_ABBREV)
) {
if (!abbrev_found) {
abbrev_found = 1;
abbrev = optn;
if (*a == '=') abbrev_arg = a + 1;
} else /* several possibility case... */
abbrev = NULL;
}
}
}
if (abbrev) { /* implicit match found */
if (abbrev_arg && arg_p) *arg_p = abbrev_arg;
return abbrev;
} else /* no match found */
return NULL;
}
static int check_sym (const CLIF_option *optn, char sym) {
if (sym == '+') {
if (!optn->function_plus) return -1;
}
else if (sym == '-') {
if (!optn->function && optn->function_plus)
return -1;
}
return 0;
}
static int call_function (CLIF_option *optn, char *opt_arg, char sym) {
int (*function) (CLIF_option *, char *);
function = (sym == '+') ? optn->function_plus : optn->function;
if (!function) return 0;
if (opt_arg && ((optn->flags | curr.parse_flags) & CLIF_SEVERAL)) {
char tmp[80];
char *t;
char *endt = tmp + sizeof (tmp);
while (*opt_arg) {
t = tmp;
while (t < endt && *opt_arg &&
*opt_arg != ' ' && *opt_arg != '\t' && *opt_arg != ','
) *t++ = *opt_arg++;
if (t >= endt) return -1;
*t = '\0';
if (function (optn, tmp) < 0) return -1;
while (*opt_arg == ' ' || *opt_arg == '\t' || *opt_arg == ',')
opt_arg++;
}
return 0;
}
return function (optn, opt_arg);
}
int CLIF_parse_cmdline (int argc, char *argv[],
CLIF_option *option_list,
CLIF_argument *argument_list,
unsigned int parse_flags) {
int i, j;
CLIF_option *optn;
CLIF_argument *argm;
int num_args = 0;
int num_argm = 0, strict_beg = 0, strict_end = 0;
_CLIF_index arg_n[MAX_ARGC_NUMBER];
unsigned int dirty_flags = 0;
int dirty_plus = 0;
int exclusive_cnt = 0;
int posix = getenv ("POSIXLY_CORRECT") != NULL ||
(parse_flags & CLIF_POSIX);
curr.argc = argc;
curr.argv = argv;
curr.option_list = option_list;
curr.argument_list = argument_list;
curr.parse_flags = parse_flags;
if (argc <= 1 && (parse_flags & CLIF_HELP_EMPTY)) {
CLIF_current_help ();
exit (0);
}
/* Scan argument_list for check and some info. */
if (argument_list) {
enum stages { STRICT_BEG, OPTIONAL, STRICT_END };
int stage = STRICT_BEG;
for (argm = argument_list; argm->name; argm++) {
if (argm->flags & CLIF_STRICT) {
if (stage == STRICT_BEG) strict_beg++;
else if (stage == OPTIONAL) {
stage = STRICT_END;
strict_end++;
}
else if (stage == STRICT_END)
strict_end++;
} else {
if (stage == STRICT_BEG) stage = OPTIONAL;
else if (stage == STRICT_END) {
err_report ("Incorrect argument list set in program "
"source: more than one optional area.");
return -1;
}
}
num_argm++;
}
}
/* Scan option_list for some info. */
if (option_list) {
dirty_flags = parse_flags;
for (optn = option_list;
optn->short_opt || optn->long_opt;
optn++
) {
dirty_flags |= optn->flags;
if (optn->function_plus) dirty_plus = 1;
}
}
if (dirty_flags & CLIF_EXCL)
exclusive_cnt = 1; /* only one is allowed... */
/* Go ! Store arguments, parse options. */
for (i = 1; i < argc; i++) {
char *arg = argv[i];
char *opt_arg = NULL;
char sym = '-';
if (!option_list)
goto handle_arg;
if (*arg == '+' && dirty_plus)
sym = '+';
if (*arg != sym) { /* argument or keyword */
if (dirty_flags & CLIF_MAY_KEYWORD) {
optn = find_long (arg, &opt_arg, CLIF_MAY_KEYWORD, 0);
if (optn) goto long_found;
}
if (num_args == 0 && (parse_flags & CLIF_FIRST_GROUP)) {
/* ugly... */
parse_flags &= ~CLIF_FIRST_GROUP;
dirty_flags &= ~CLIF_FIRST_GROUP; /* to be correct */
goto handle_short;
}
/* else it is an argument */
goto handle_arg;
}
else if (*++arg == sym) { /* `--' - long option */
arg++;
if (*arg == sym || /* `---' - let it be not option... */
(parse_flags & (_CLIF_STRICT_KEYWORD|_CLIF_STRICT_ONEDASH))
) {
arg -= 2;
goto handle_arg; /* not option anyway */
}
optn = find_long (arg, &opt_arg, 0,
_CLIF_STRICT_KEYWORD | _CLIF_STRICT_ONEDASH);
if (optn) goto long_found;
/* XXX: May be allow only for `--', not `++' too... */
if (!*arg && sym == '-') { /* `--' and no empty longoption */
option_list = NULL; /* POSIX way... */
continue;
}
/* XXX: or treat as an argument sometimes??? */
err_bad_opt (argv[i], 0, i);
return -1;
}
else { /* short option, or several short options... */
if (dirty_flags & CLIF_MAY_ONEDASH) {
optn = find_long (arg, &opt_arg, CLIF_MAY_ONEDASH, 0);
if (optn) goto long_found;
}
if (!*arg) { /* POSIX say: only "stdout specification"... */
arg--;
goto handle_arg;
}
goto handle_short;
}
long_found:
if (check_sym (optn, sym) < 0) { /* Oops... */
err_bad_opt (argv[i], 0, i);
return -1;
}
if (optn->flags & CLIF_EXCL) {
if (!exclusive_cnt) {
err_bad_excl (optn, 0, i);
return -1;
}
exclusive_cnt--;
}
if (optn->arg_name && !opt_arg) {
unsigned int flags = optn->flags | parse_flags;
if (++i >= argc ||
!(flags & CLIF_MAY_NOEQUAL)
) { /* missing opt arg */
i--;
if (!(flags & CLIF_OPTARG)) {
err_bad_arg (optn, 0, i);
return -1;
}
opt_arg = NULL;
} else
opt_arg = argv[i];
}
if (call_function (optn, opt_arg, sym) < 0) {
err_bad_res (optn, 0, opt_arg, i);
return -1;
}
if (optn->flags & CLIF_EXIT)
exit (0);
continue;
handle_arg:
if (argument_list) {
if (i < MAX_ARGC_NUMBER) /* XXX: ugly, better report */
arg_n[num_args++] = i;
} else {
err_report ("`%s' (argc %d): arguments are not allowed",
argv[i], i);
return -1;
}
/* POSIX say: No more options after args... */
if (posix) option_list = NULL; /* geniously... */
continue;
handle_short:
opt_arg = NULL;
do {
for (optn = option_list;
optn->short_opt || optn->long_opt;
optn++
) {
if (optn->short_opt && optn->short_opt[0] == *arg)
break;
}
if (!optn->short_opt ||
check_sym (optn, sym) < 0
) {
err_bad_opt (argv[i], *arg, i);
return -1;
}
if (optn->flags & CLIF_EXCL) {
if (!exclusive_cnt) {
err_bad_excl (optn, *arg, i);
return -1;
}
exclusive_cnt--;
}
if (optn->arg_name) {
unsigned int flags = parse_flags | optn->flags;
if (arg[1] == '\0') { /* a last one */
/* POSIX say: an option with arg cannot be grouped. */
if (posix && arg != argv[i] && arg[-1] != sym) {
err_bad_arg (optn, *arg, i); /* good way? */
return -1;
}
if (++i >= argc ||
(flags & _CLIF_STRICT_JOIN_ARG)
) {
i--;
if (!(flags & CLIF_OPTARG)) {
err_bad_arg (optn, *arg, i);
return -1;
}
opt_arg = NULL;
} else
opt_arg = argv[i];
}
else if ((arg == argv[i] || arg[-1] == sym) &&
(flags & CLIF_MAY_JOIN_ARG)
) {
opt_arg = ++arg;
}
else { /* inside a group... */
if (!(flags & CLIF_OPTARG) ||
(flags & CLIF_MAY_JOIN_ARG)
) {
err_bad_arg (optn, *arg, i);
return -1;
}
opt_arg = NULL;
}
}
if (call_function (optn, opt_arg, sym) < 0) {
err_bad_res (optn, optn->short_opt[0], opt_arg, i);
return -1;
}
if (optn->flags & CLIF_EXIT)
exit (0);
} while (!opt_arg && *++arg);
} /* for ( ... ) */
if ((parse_flags & CLIF_STRICT_EXCL) && exclusive_cnt != 0) {
err_report ("One of these must be specified:\n %s\n",
show_excl (option_list, 0));
return -1;
}
/* Now, after *ALL* options, handle arguments, if any. */
if (num_args < strict_beg + strict_end) {
/* Missing some needed arguments. */
if (num_args < strict_beg) argm = argument_list + num_args;
else
argm = argument_list +
((num_args - strict_beg) + (num_argm - strict_end));
if (num_args == strict_beg + strict_end - 1)
err_report ("Specify \"%s\" missing argument.", argm->name);
else
err_report ("Specify \"%s\" and other missing arguments.",
argm->name);
return -1;
}
if (num_args > 0) {
_CLIF_index argm_index[MAX_ARGC_NUMBER];
/* assing argm (by index) for each arg... */
for (i = 0, j = 0; i < strict_beg; i++, j++)
argm_index[i] = j;
for (i = num_args - strict_end, j = num_argm - strict_end;
i < num_args; i++, j++
) argm_index[i] = j;
for (i = strict_beg, j = strict_beg;
i < num_args - strict_end && j < num_argm - strict_end;
i++
) {
argm_index[i] = j;
if (!(argument_list[j].flags & CLIF_MORE))
j++;
}
if (i < num_args - strict_end) { /* there are extra args... */
err_report ("Extra arg `%s' (position %d, argc %d)",
argv[arg_n[i]], i + 1, arg_n[i]);
return -1;
}
if (j < num_argm - strict_end &&
!(argument_list[j].flags & CLIF_MORE) &&
/* ...i.e, there are some missing optional args... */
(argument_list[j].flags & CLIF_ACC_PREV)
) {
if (j == 0)
err_report ("Incorrect argument list set: first arg "
"cannot be `accompanied with previous'.");
else
err_report ("Arg \"%s\" must be specified because "
"\"%s\" `%s' is used.", argument_list[j].name,
argument_list[j - 1].name, argv[arg_n[i - 1]]);
return -1;
}
if (argm_index[--i] == j &&
/* above is true only after OPTIONAL area scan
and when `j' is stopped on CLIF_MORE */
++j < num_argm - strict_end
/* i.e: there is a *last* one (after CLIF_MORE)
in the OPTIONAL area */
) argm_index[i] = j; /* *last* is better than *more* */
/* ...and work now */
for (i = 0; i < num_args; i++) {
argm = argument_list + argm_index[i];
if (argm->function &&
argm->function (argm, argv[arg_n[i]], i) < 0
) {
err_report ("Cannot handle \"%s\" cmdline arg `%s' "
"on position %d (argc %d)",
argm->name, argv[arg_n[i]], i + 1, arg_n[i]);
return -1;
}
}
/* That`s all. */
}
return 0;
}
static void box_output (int start, int left, int width, const char *str,
const char *arg_name) {
char *p, *endp, *s;
int l;
char buf[1024];
char spacer[128]; /* assume it is enough */
if (left > sizeof (spacer) - 2) left = sizeof (spacer) - 2;
if (width > sizeof (buf) - 1) width = sizeof (buf) - 1;
spacer[0] = '\n';
memset (spacer + 1, ' ', left);
spacer[left + 1] = '\0';
l = left - start;
if (l > 0) {
memset (buf, ' ', l);
buf[l] = '\0';
fprintf (stderr, "%s", buf);
} else
fprintf (stderr, "%s", spacer);
endp = buf + width;
p = buf;
while (*str) {
while (*str && p < endp) {
if (*str == '%' && arg_name) {
if (str[1] == '%') {
*p++ = '%';
str += 2;
continue;
}
else if (str[1] == 's') {
const char *a = arg_name;
while (*a && p < endp) *p++ = *a++;
str += 2;
continue;
}
}
*p++ = *str++;
}
*p = '\0';
if (p < endp) break;
while (p > buf && *p != ' ' && *p != '\t') p--;
if (p <= buf) return; /* foo on you */
*p = '\0';
fprintf (stderr, "%s", buf);
fprintf (stderr, "%s", spacer);
p++;
for (s = buf; *p; *s++ = *p++) ;
*s = '\0';
p = s;
}
fprintf (stderr, "%s", buf);
return;
}
#define SHORT_LONG_DLM " "
#define OPT_START_DLM " "
#define OPT_FIELD_WIDTH 30
#define ARG_MARK_STRICT "+ "
#define ARG_MARK_GROUP0 " . "
#define ARG_MARK_GROUP " ' "
#define ARG_MARK_OPT " "
#define ARG_FIELD_WIDTH 20
#define SCREEN_WIDTH 80
void CLIF_print_options (const char *header,
const CLIF_option *option_list) {
const CLIF_option *optn;
char *excl;
int excl_cnt = 0;
/* Print a header string, if present... */
if (header) fprintf (stderr, "%s\n", header);
if (!option_list) return;
for (optn = option_list; optn->short_opt || optn->long_opt; optn++) {
int len;
/* generate and print an option usage */
if (optn->short_opt) {
if (optn->long_opt)
len = fprintf (stderr, OPT_START_DLM "%s"
SHORT_LONG_DLM "%s",
show_short (optn), show_long (optn));
else
len = fprintf (stderr, OPT_START_DLM "%s",
show_short (optn));
} else
len = fprintf (stderr, OPT_START_DLM "%s", show_long (optn));
/* print a help string, if present */
if (optn->help_string)
box_output (len, OPT_FIELD_WIDTH,
SCREEN_WIDTH - OPT_FIELD_WIDTH,
optn->help_string, optn->arg_name);
fprintf (stderr, "\n"); /* a last one */
}
excl = show_excl (option_list, &excl_cnt);
if (excl_cnt > 0) {
if (excl_cnt == 1) {
if ((curr.parse_flags & CLIF_STRICT_EXCL) &&
curr.option_list == option_list
) fprintf (stderr, "Anyway `%s' must be specified.\n", excl);
else /* simple ordinary option, because excl_cnt == 1 ... */;
} else
fprintf (stderr, "Only one of these may be specified:\n"
" %s\n", excl);
}
return;
}
void CLIF_print_arguments (const char *header,
const CLIF_argument *argument_list) {
const CLIF_argument *argm;
if (!argument_list) return;
/* Print a header string, if present... */
if (header) fprintf (stderr, "%s\n", header);
for (argm = argument_list; argm->name; argm++) {
int len;
if (argm->flags & CLIF_STRICT)
len = fprintf (stderr, ARG_MARK_STRICT "%s", argm->name);
else if (argm->flags & CLIF_MORE)
len = fprintf (stderr, ARG_MARK_OPT "%s ...", argm->name);
else if (argm->flags & CLIF_ACC_PREV)
len = fprintf (stderr, ARG_MARK_GROUP "%s", argm->name);
else if ((argm + 1)->name && ((argm + 1)->flags & CLIF_ACC_PREV))
len = fprintf (stderr, ARG_MARK_GROUP0 "%s", argm->name);
else
len = fprintf (stderr, ARG_MARK_OPT "%s", argm->name);
if (argm->help_string)
box_output (len, ARG_FIELD_WIDTH,
SCREEN_WIDTH - ARG_FIELD_WIDTH,
argm->help_string, argm->name);
fprintf (stderr, "\n");
}
return;
}
void CLIF_print_usage (const char *header, const char *progname,
const CLIF_option *option_list,
const CLIF_argument *argument_list) {
if (!progname && curr.argv)
progname = curr.argv[0];
if (!header) {
if (progname)
fprintf (stderr, "Usage: %s", progname);
else
fprintf (stderr, "Command line options:");
} else {
if (progname)
fprintf (stderr, "%s\n" OPT_START_DLM "%s", header, progname);
else
fprintf (stderr, "%s", header);
}
if (option_list) {
const CLIF_option *optn;
char m_buf[256], p_buf[256], mp_buf[256];
char *m = m_buf, *p = p_buf, *mp = mp_buf;
char *end_m = m_buf + sizeof (m_buf) - 1;
char *end_p = p_buf + sizeof (p_buf) - 1;
char *end_mp = mp_buf + sizeof (mp_buf) - 1;
char *excl;
int excl_cnt = 0;
/* first, show exclusive option list, if any... */
excl = show_excl (option_list, &excl_cnt);
if (excl_cnt > 0) {
if ((curr.parse_flags & CLIF_STRICT_EXCL) &&
curr.option_list == option_list
) {
if (excl_cnt == 1)
fprintf (stderr, " %s", excl);
else
fprintf (stderr, " { %s }", excl);
} else
fprintf (stderr, " [ %s ]", excl);
}
/* second, find short options without arguments... */
for (optn = option_list;
optn->short_opt || optn->long_opt;
optn++
) {
/* We don`t exclude CLIF_EXTRA hear:
simple one char don`t eat a lot of space...
*/
if (!optn->short_opt ||
optn->arg_name ||
(optn->flags & CLIF_EXCL)
) continue;
if (optn->function_plus) {
if (optn->function) {
if (mp < end_mp) *mp++ = optn->short_opt[0];
} else {
if (p < end_p) *p++ = optn->short_opt[0];
}
} else {
if (m < end_m) *m++ = optn->short_opt[0];
}
}
if (m > (char *) m_buf) {
*m = '\0';
fprintf (stderr, " [ -%s ]", m_buf);
}
if (p > (char *) p_buf) {
*p = '\0';
fprintf (stderr, " [ +%s ]", p_buf);
}
if (mp > (char *) mp_buf) {
*mp = '\0';
fprintf (stderr, " [ " SHORT_PLUS_MINUS "%s ]", mp_buf);
}
/* third, print all another... */
for (optn = option_list;
optn->short_opt || optn->long_opt;
optn++
) {
if (optn->flags & CLIF_EXTRA) continue;
if (optn->flags & CLIF_EXCL)
continue; /* already handled */
if (optn->short_opt) {
if (optn->arg_name)
fprintf (stderr, " [ %s ]", show_short (optn));
else
/* already handled */;
} else
fprintf (stderr, " [ %s ]", show_long (optn));
}
}
if (argument_list) {
const CLIF_argument *argm;
int deep = 0;
for (argm = argument_list; argm->name; argm++) {
if (argm->flags & CLIF_STRICT) {
if (deep > 0) {
fputc (' ', stderr);
while (deep--) fputc (']', stderr);
deep = 0;
}
fprintf (stderr, " %s", argm->name);
} else {
if (argm->flags & CLIF_MORE)
fprintf (stderr, " [ %s ...", argm->name);
else if (argm->flags & CLIF_ACC_PREV) {
fprintf (stderr, " %s", argm->name);
--deep; /* ugly, but easy */
} else
fprintf (stderr, " [ %s", argm->name);
deep++;
}
}
if (deep > 0) {
fputc (' ', stderr);
while (deep--) fputc (']', stderr);
}
}
fprintf (stderr, "\n");
}
int CLIF_current_help (void) {
if (!curr.argc) return -1; /* i.e., not inited... */
CLIF_print_usage ("Usage:", curr.argv[0], curr.option_list,
curr.argument_list);
if (curr.option_list)
CLIF_print_options ("Options:", curr.option_list);
if (curr.argument_list)
CLIF_print_arguments ("\nArguments:", curr.argument_list);
return 0;
}
/* Common useful option handlers. */
int CLIF_version_handler (CLIF_option *optn, char *arg) {
if (!optn->data) return -1;
fprintf (stderr, "%s\n", ((char *) optn->data));
return 0; /* be happy */
}
int CLIF_set_flag (CLIF_option *optn, char *arg) {
if (!optn->data) return -1;
*((int *) optn->data) = 1;
return 0;
}
int CLIF_unset_flag (CLIF_option *optn, char *arg) {
if (!optn->data) return -1;
*((int *) optn->data) = 0;
return 0;
}
static int set_string (char **data, char *arg) {
if (!data) return -1;
*data = arg;
return 0;
}
int CLIF_set_string (CLIF_option *optn, char *arg) {
return set_string (optn->data, arg);
}
int CLIF_arg_string (CLIF_argument *argm, char *arg, int index) {
return set_string (argm->data, arg);
}
static int set_int (int *data, char *arg) {
char *q;
if (!data) return -1;
*data = (int) strtol (arg, &q, 0);
return (q == arg) ? -1 : 0;
}
static int set_uint (unsigned int *data, char *arg) {
char *q;
if (!data) return -1;
*data = (unsigned int) strtoul (arg, &q, 0);
return (q == arg) ? -1 : 0;
}
static int set_double (double *data, char *arg) {
char *q;
if (!data) return -1;
*data = strtod (arg, &q);
return (q == arg) ? -1 : 0;
}
int CLIF_set_int (CLIF_option *optn, char *arg) {
return set_int (optn->data, arg);
}
int CLIF_set_uint (CLIF_option *optn, char *arg) {
return set_uint (optn->data, arg);
}
int CLIF_set_double (CLIF_option *optn, char *arg) {
return set_double (optn->data, arg);
}
int CLIF_arg_int (CLIF_argument *argm, char *arg, int index) {
return set_int (argm->data, arg);
}
int CLIF_arg_uint (CLIF_argument *argm, char *arg, int index) {
return set_uint (argm->data, arg);
}
int CLIF_arg_double (CLIF_argument *argm, char *arg, int index) {
return set_double (argm->data, arg);
}
int CLIF_call_func (CLIF_option *optn, char *arg) {
if (!optn->data) return -1;
if (optn->arg_name) {
int (*func) (char *) = optn->data;
return func (arg);
} else {
int (*func) (void) = optn->data;
return func ();
}
}
int CLIF_arg_func (CLIF_argument *argm, char *arg, int index) {
int (*func) (char *, int);
if (!argm->data) return -1;
func = (int (*) (char *, int)) argm->data;
return func (arg, index);
}
|