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
|
/*-----------------------------------------------------------------------
File : cio_commandline.c
Author: Stephan Schulz
Contents
Functions for handling options and recognising non-option
arguments.
Copyright 1998-2017 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Created: Wed Sep 10 00:01:33 MET DST 1997
-----------------------------------------------------------------------*/
#include "cio_commandline.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: print_start_of_str()
//
// Print str up to the last blank character before the len's
// character or the first newline, whichever is first, followed by a
// newline. If there is no blank, break at
// character number len. Returns a pointer to the first character
// following the break, or NULL if the string was printed
// completely.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
static char* print_start_of_str(FILE* out, char* str, int len)
{
int i = 0;
char* search = str;
char* blank = NULL;
while(*search && i<len) /* Search last blank before
EOString/newline or len characters. */
{
if(*search == ' ')
{
blank = search;
}
else if(*search == '\n')
{
blank = search;
i=len;
break;
}
search++;
i++;
}
if(i<len) /* Print remaining string */
{
while(*str)
{
putc(*str, out);
str++;
}
putc('\n', out);
return 0;
}
else if(blank) /* Print string up to space */
{
while(str!=blank)
{
putc(*str, out);
str++;
}
putc('\n', out);
str++;
return *str?str:NULL;
}
else /* Now have to do a hard break! */
{
while(str!=search)
{
putc(*str, out);
str++;
}
putc('\n', out);
return str;
}
}
/*-----------------------------------------------------------------------
//
// Function: shift_array_left()
//
// Shift a 0-terminated array of char* elements left by one,
// dropping the first element. Return false if no element is
// present.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static bool shift_array_left(char* array[])
{
int i;
if(!array[0])
{
return false;
}
for(i=0; array[i+1]; i++)
{
array[i] = array[i+1];
}
array[i] = NULL;
return true;
}
/*-----------------------------------------------------------------------
//
// Function: find_long_opt()
//
// Find an option entry by long name. Return NULL if not found.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static Opt_p find_long_opt(char* option, OptCell options[])
{
int i;
unsigned int len;
option+=2; /* Jump -- */
len = 0;
while(option[len])
{
if(option[len]=='=')
{
break;
}
len++;
}
for(i=0; options[i].option_code; i++)
{
if((strncmp(options[i].longopt, option, len)==0) &&
(strlen(options[i].longopt)==len))
{
return &(options[i]);
}
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function: find_short_opt()
//
// Find an option entry by short name. Return NULL if not found.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static Opt_p find_short_opt(char option, OptCell options[])
{
int i;
for(i=0; options[i].option_code; i++)
{
if(option == options[i].shortopt)
{
return &(options[i]);
}
}
return NULL;
}
/*-----------------------------------------------------------------------
//
// Function: process_long_option()
//
// Process the long option that is found in state->argc[state->argi]:
// Find the option, check for argument, set *arg to an argument,
// update state.
//
// Global Variables: -
//
// Side Effects : Changes state, may cause error
//
/----------------------------------------------------------------------*/
static Opt_p process_long_option(CLState_p state, char** arg,
OptCell options[])
{
Opt_p handle;
char* eq_sign;
DStr_p err = DStrAlloc();
if(!(handle = find_long_opt(state->argv[state->argi], options)))
{
DStrAppendStr(err, "Unknown Option: ");
DStrAppendStr(err, state->argv[state->argi]);
DStrAppendStr(err," (Use -h for a list of valid options)");
Error(DStrView(err), USAGE_ERROR);
}
eq_sign = strchr(state->argv[state->argi], '=');
switch(handle->type)
{
case NoArg:
if(eq_sign)
{
DStrAppendStr(err, state->argv[state->argi]);
DStrAppendStr(err, " does not accept an argument!");
Error(DStrView(err), USAGE_ERROR);
}
*arg = NULL;
break;
case OptArg:
if(eq_sign)
{
*arg = eq_sign+1;
}
else
{
assert(handle->arg_default);
*arg = handle->arg_default;
}
break;
case ReqArg:
if(!eq_sign)
{
DStrAppendStr(err, state->argv[state->argi]);
DStrAppendStr(err, " requires an argument!");
Error(DStrView(err), USAGE_ERROR);
}
*arg = eq_sign+1;
break;
default:
assert(false);
break;
}
shift_array_left(&(state->argv[state->argi]));
state->argc--;
DStrFree(err);
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: process_short_option()
//
// Process the short option that is found in
// state->argc[state->argi][state->sc_opt_c]: Find the option, check
// for argument, set *arg to an argument, update state.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
static Opt_p process_short_option(CLState_p state, char** arg,
OptCell options[])
{
Opt_p handle;
DStr_p err = DStrAlloc();
char* optstr = state->argv[state->argi];
if(!(handle = find_short_opt(optstr[state->sc_opt_c], options)))
{
DStrAppendStr(err, "Unknown Option: -");
DStrAppendChar(err, optstr[state->sc_opt_c]);
DStrAppendStr(err, " (processing ");
DStrAppendStr(err, optstr);
DStrAppendStr(err, ")");
DStrAppendStr(err," (Use -h for a list of valid options)");
Error(DStrView(err), USAGE_ERROR);
}
*arg = NULL;
switch(handle->type)
{
case OptArg:
assert(handle->arg_default);
*arg = handle->arg_default;
/* Fall-through intentional! */
case NoArg:
state->sc_opt_c++;
if(!optstr[state->sc_opt_c])
{
state->sc_opt_c = 0;
shift_array_left(&(state->argv[state->argi]));
state->argc--;
}
break;
case ReqArg:
if(state->sc_opt_c!=1)
{
DStrAppendStr(err, optstr);
DStrAppendStr(err, ": POSIX forbids the aggregation of"
" options which take arguments (but you"
" probably only forgot the second hyphen for"
" a long GNU-style option)");
Error(DStrView(err), USAGE_ERROR);
}
if(optstr[state->sc_opt_c+1])
{
*arg=&optstr[state->sc_opt_c+1];
}
else
{
shift_array_left(&(state->argv[state->argi]));
state->argc--;
if(!state->argv[state->argi])
{
DStrAppendChar(err, '-');
DStrAppendChar(err, optstr[state->sc_opt_c]);
DStrAppendStr(err, " requires an argument");
Error(DStrView(err), USAGE_ERROR);
}
*arg = state->argv[state->argi];
}
state->sc_opt_c = 0;
shift_array_left(&(state->argv[state->argi]));
state->argc--;
break;
default:
assert(false);
break;
}
DStrFree(err);
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: append_option_desc()
//
// Append a description of the given option to the DStr.
//
// Global Variables: -
//
// Side Effects : Changes the string.
//
/----------------------------------------------------------------------*/
static void append_option_desc(DStr_p string, Opt_p option)
{
assert(option->shortopt || option->longopt);
if(option->shortopt)
{
DStrAppendChar(string, '-');
DStrAppendChar(string, option->shortopt);
}
if(option->shortopt && option->longopt)
{
DStrAppendStr(string, " or ");
}
if(option->longopt)
{
DStrAppendStr(string, "--");
DStrAppendStr(string, option->longopt);
}
}
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: CLStateAlloc()
//
// Allocate initialized Structure for the description of a
// (partially processed) command line.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
CLState_p CLStateAlloc(int argc, char* argv[])
{
int i;
CLState_p handle = CLStateCellAlloc();
handle->sc_opt_c = 0;
handle->argi = 0;
handle->argc = argc;
handle->argsize = argc+2; /* Allocate one entry extra for
default inserting of "-" */
handle->argv = SecureMalloc(handle->argsize * sizeof(char*));
for(i=0; i<argc; i++)
{
handle->argv[i] = argv[i];
}
handle->argv[argc] = NULL;
shift_array_left(handle->argv);
handle->argc--;
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: CLStateFree()
//
// Free a CLStateCell.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
void CLStateFree(CLState_p junk)
{
assert(junk);
FREE(junk->argv);
CLStateCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: CLStateInsertArg()
//
// Insert an additional argument at the end of state->argv, realloc
// for more memory if necessary. Return new state->argc value. arg
// is expected to be const, it is not copied!
//
// Global Variables: -
//
// Side Effects : Memory operations.
//
/----------------------------------------------------------------------*/
int CLStateInsertArg(CLState_p state, char* arg)
{
if(state->argsize == state->argc+2)
{
state->argsize++;
state->argv = SecureRealloc(state->argv, state->argsize *
sizeof(char*));
}
state->argv[state->argc] = arg;
state->argc++;
state->argv[state->argc] = NULL;
return state->argc;
}
/*-----------------------------------------------------------------------
//
// Function: CLStateGetOpt()
//
// Return a pointer to the next unprocessed option, set arg to point
// to the argument (if present) or the default (if present).
//
// Global Variables: -
//
// Side Effects : Updates state by removing options and arguments and
// adjusting counters accordingly.
//
/----------------------------------------------------------------------*/
Opt_p CLStateGetOpt(CLState_p state, char** arg, OptCell options[])
{
while(state->argv[state->argi])
{
if((state->argv[state->argi][0]=='-') &&
(state->argv[state->argi][1]!='\0'))
{
break;
}
state->argi++;
}
if(!state->argv[state->argi])
{
return NULL;
}
if(strcmp(state->argv[state->argi], "--")==0)
{
shift_array_left(&(state->argv[state->argi]));
state->argc--;
while(state->argv[state->argi])
{
state->argi++;
}
return NULL;
}
if(strncmp(state->argv[state->argi], "--", 2)==0)
{
return process_long_option(state, arg, options);
}
if(!state->sc_opt_c)
{
state->sc_opt_c = 1;
}
return process_short_option(state, arg, options);
}
/*-----------------------------------------------------------------------
//
// Function: CLStateGetFloatArg()
//
// Return the numerical value of the argument if it is a well-formed
// (double) float, print an error message otherwise.
//
// Global Variables: -
//
// Side Effects : May terminate program
//
/----------------------------------------------------------------------*/
double CLStateGetFloatArg(Opt_p option, char* arg)
{
double ret;
char* eoarg;
errno = 0;
ret = strtod(arg, &eoarg);
if(errno || *eoarg)
{
DStr_p err = DStrAlloc();
TmpErrno = errno;
append_option_desc(err, option);
DStrAppendStr(err, " expects float instead of '");
DStrAppendStr(err, arg);
DStrAppendChar(err, '\'');
if(TmpErrno)
{
SysError(DStrView(err), USAGE_ERROR);
}
else
{
Error(DStrView(err), USAGE_ERROR);
}
DStrFree(err);
}
return ret;
}
/*-----------------------------------------------------------------------
//
// Function: CLStateGetIntArg()
//
// Return the numerical value of the argument if it is a well-formed
// long, print an error message otherwise.
//
// Global Variables: -
//
// Side Effects : May terminate program
//
/----------------------------------------------------------------------*/
long CLStateGetIntArg(Opt_p option, char* arg)
{
long ret;
char* eoarg;
errno = 0;
ret = strtol(arg, &eoarg, 10);
if(errno || *eoarg)
{
DStr_p err = DStrAlloc();
TmpErrno = errno;
append_option_desc(err, option);
DStrAppendStr(err, " expects integer instead of '");
DStrAppendStr(err, arg);
DStrAppendChar(err, '\'');
if(TmpErrno)
{
SysError(DStrView(err), USAGE_ERROR);
}
else
{
Error(DStrView(err), USAGE_ERROR);
}
DStrFree(err); /* Symmetry */
}
return ret;
}
/*-----------------------------------------------------------------------
//
// Function: CLStateGetIntArgCheckRange()
//
// Return the numerical value of the argument if it is a well-formed
// long in the proper range, print an error message otherwise.
//
// Global Variables: -
//
// Side Effects : May terminate program
//
/----------------------------------------------------------------------*/
long CLStateGetIntArgCheckRange(Opt_p option, char* arg, long lower, long upper)
{
long ret = CLStateGetIntArg(option, arg);
if(ret<lower || ret > upper)
{
DStr_p err = DStrAlloc();
DStrAppendStr(err, "Option ");
append_option_desc(err, option);
DStrAppendStr(err, " expects integer argument from {");
DStrAppendInt(err, lower);
DStrAppendStr(err, "...");
DStrAppendInt(err, upper);
DStrAppendStr(err, "} but got ");
DStrAppendStr(err, arg);
Error(DStrView(err), USAGE_ERROR);
DStrFree(err); /* Symmetry */
}
return ret;
}
/*-----------------------------------------------------------------------
//
// Function: CLStateGetBoolArg()
//
// Return the boolean value of the argument if it is either 'true'
// or 'false' long, print an error message otherwise.
//
// Global Variables: -
//
// Side Effects : May terminate program
//
/----------------------------------------------------------------------*/
bool CLStateGetBoolArg(Opt_p option, char* arg)
{
DStr_p err;
if(strcmp(arg, "true")==0)
{
return true;
}
else if(strcmp(arg, "false")==0)
{
return false;
}
err = DStrAlloc();
append_option_desc(err, option);
DStrAppendStr(err, " expects 'true' or 'false' instead of '");
DStrAppendStr(err, arg);
DStrAppendChar(err, '\'');
Error(DStrView(err), USAGE_ERROR);
DStrFree(err); /* Symmetry */
return false; /* Just to stiffle warings */
}
/*-----------------------------------------------------------------------
//
// Function: PrintOption()
//
// Print the formatted description of an option (generated from the
// information in an OptCell() to the desired stream.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
void PrintOption(FILE* out, Opt_p option)
{
char* l_argdesc = "";
char* desc;
DStr_p optdesc = DStrAlloc();
switch(option->type)
{
case NoArg:
break;
case OptArg:
l_argdesc = "[=<arg>]";
break;
case ReqArg:
l_argdesc = "=<arg>";
break;
default:
assert(false);
break;
}
assert(option->longopt || option->shortopt);
if(option->shortopt)
{
fprintf(out, " -%c%s\n", option->shortopt,
option->type == ReqArg ? " <arg>" : "");
}
if(option->longopt)
{
fprintf(out, " --%s%s\n", option->longopt, l_argdesc);
}
DStrAppendStr(optdesc, option->desc);
if(option->type ==OptArg)
{
assert(option->longopt);
if(option->shortopt)
{
DStrAppendStr(optdesc,
" The short form or the long form without"
" the optional argument is equivalent to --");
}
else
{
DStrAppendStr(optdesc,
" The option without"
" the optional argument is equivalent to --");
}
DStrAppendStr(optdesc, option->longopt);
DStrAppendChar(optdesc, '=');
DStrAppendStr(optdesc, option->arg_default);
DStrAppendStr(optdesc, ".");
}
desc = DStrView(optdesc);
while(desc)
{
fprintf(out, " ");
desc = print_start_of_str(out, desc, FORMAT_WIDTH-4);
}
fprintf(out, "\n");
DStrFree(optdesc);
}
/*-----------------------------------------------------------------------
//
// Function: PrintOptions()
//
// Print the whole option array (terminated by an OptCell with type
// NoOption.
//
// Global Variables: -
//
// Side Effects : Output
//
/----------------------------------------------------------------------*/
void PrintOptions(FILE* out, OptCell option[], char* header)
{
int i;
if(header)
{
fprintf(out, "%s", header);
}
for(i=0; option[i].option_code; i++)
{
PrintOption(out, &option[i]);
}
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|