Changeset 7709bf in git
- Timestamp:
- Sep 22, 1998, 4:15:04 PM (25 years ago)
- Branches:
- (u'spielwiese', '0d6b7fcd9813a1ca1ed4220cfa2b104b97a0a003')
- Children:
- 6594d42df45662b198c336ed0437cfba5e77a91a
- Parents:
- e0d91ca23156d80a43dde9ffd342ab947b5cc298
- Location:
- Singular
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/find_exec.c
re0d91c r7709bf 13 13 #include "mod2.h" 14 14 15 #if ! defined(MSDOS) && ! defined(__MWERKS__) 15 #if ! defined(MSDOS) && ! defined(__MWERKS__) 16 16 17 17 #include <unistd.h> /* always defiend */ -
Singular/getopt.c
re0d91c r7709bf 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: getopt.c,v 1. 2 1998-04-17 07:53:41Singular Exp $ */4 /* $Id: getopt.c,v 1.3 1998-09-22 14:15:03 Singular Exp $ */ 5 5 6 6 /* Getopt for GNU. … … 10 10 11 11 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94 12 12 Free Software Foundation, Inc. 13 13 14 14 This program is free software; you can redistribute it and/or modify it … … 27 27 28 28 /* adapted for use with Singular by obachman@mathematik.uni-kl.de 4/98*/ 29 30 29 31 30 … … 59 58 /* This needs to come after some library #include 60 59 to get __GNU_LIBRARY__ defined. */ 61 #ifdef 60 #ifdef __GNU_LIBRARY__ 62 61 /* Don't include stdlib.h for non-GNU C libraries because some of them 63 62 contain conflicting prototypes for getopt. */ 64 63 #include <stdlib.h> 65 #endif 64 #endif /* GNU C library. */ 66 65 67 66 /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a … … 164 163 } ordering; 165 164 166 167 #ifdef __GNU_LIBRARY__ 165 #ifdef __GNU_LIBRARY__ 168 166 /* We want to avoid inclusion of string.h with non-GNU libraries 169 167 because there are many ways it can cause trouble. … … 171 169 in GCC. */ 172 170 #include <string.h> 173 #define my_indexstrchr174 #define my_strlenstrlen171 #define my_index strchr 172 #define my_strlen strlen 175 173 #else 176 174 … … 207 205 { 208 206 if (*str == chr) 209 207 return (char *) str; 210 208 str++; 211 209 } … … 213 211 } 214 212 215 #endif /* GNU C library. */ 216 213 #endif /* GNU C library. */ 217 214 218 215 /* Handle permutation of arguments. */ … … 277 274 } 278 275 279 280 276 /* Scan elements of ARGV (whose length is ARGC) for option characters 281 277 given in OPTSTRING. … … 361 357 362 358 if (optstring[0] == '-') 363 364 365 366 359 { 360 ordering = RETURN_IN_ORDER; 361 ++optstring; 362 } 367 363 else if (optstring[0] == '+') 368 369 370 371 364 { 365 ordering = REQUIRE_ORDER; 366 ++optstring; 367 } 372 368 else if (getenv ("POSIXLY_CORRECT") != NULL) 373 369 ordering = REQUIRE_ORDER; 374 370 else 375 371 ordering = PERMUTE; 376 372 } 377 373 … … 379 375 { 380 376 if (ordering == PERMUTE) 381 382 383 384 385 386 387 388 389 390 391 392 393 394 377 { 378 /* If we have just processed some options following some non-options, 379 exchange them so that the options come first. */ 380 381 if (first_nonopt != last_nonopt && last_nonopt != optind) 382 exchange ((char **) argv); 383 else if (last_nonopt != optind) 384 first_nonopt = optind; 385 386 /* Now skip any additional non-options 387 and extend the range of non-options previously skipped. */ 388 389 while (optind < argc 390 && (argv[optind][0] != '-' || argv[optind][1] == '\0') 395 391 #ifdef GETOPT_COMPAT 396 397 398 #endif 399 400 401 402 392 && (longopts == NULL 393 || argv[optind][0] != '+' || argv[optind][1] == '\0') 394 #endif /* GETOPT_COMPAT */ 395 ) 396 optind++; 397 last_nonopt = optind; 398 } 403 399 404 400 /* Special ARGV-element `--' means premature end of options. 405 406 407 401 Skip it like a null option, 402 then exchange with previous non-options as if it were an option, 403 then skip everything else like a non-option. */ 408 404 409 405 if (optind != argc && !strcmp (argv[optind], "--")) 410 411 412 413 414 415 416 417 418 419 420 406 { 407 optind++; 408 409 if (first_nonopt != last_nonopt && last_nonopt != optind) 410 exchange ((char **) argv); 411 else if (first_nonopt == last_nonopt) 412 first_nonopt = optind; 413 last_nonopt = argc; 414 415 optind = argc; 416 } 421 417 422 418 /* If we have done all the ARGV-elements, stop the scan 423 419 and back over any non-options that we skipped and permuted. */ 424 420 425 421 if (optind == argc) 426 427 428 429 430 431 432 422 { 423 /* Set the next-arg-index to point at the non-options 424 that we previously skipped, so the caller will digest them. */ 425 if (first_nonopt != last_nonopt) 426 optind = first_nonopt; 427 return EOF; 428 } 433 429 434 430 /* If we have come to a non-option and did not permute it, 435 431 either stop the scan or describe it to the caller and pass it by. */ 436 432 437 433 if ((argv[optind][0] != '-' || argv[optind][1] == '\0') 438 434 #ifdef GETOPT_COMPAT 439 440 441 #endif 442 443 444 445 446 447 448 435 && (longopts == NULL 436 || argv[optind][0] != '+' || argv[optind][1] == '\0') 437 #endif /* GETOPT_COMPAT */ 438 ) 439 { 440 if (ordering == REQUIRE_ORDER) 441 return EOF; 442 optarg = argv[optind++]; 443 return 1; 444 } 449 445 450 446 /* We have found another option-ARGV-element. 451 447 Start decoding its characters. */ 452 448 453 449 nextchar = (argv[optind] + 1 454 450 + (longopts != NULL && argv[optind][1] == '-')); 455 451 } 456 452 457 453 if (longopts != NULL 458 454 && ((argv[optind][0] == '-' 459 455 && (argv[optind][1] == '-' || long_only)) 460 456 #ifdef GETOPT_COMPAT 461 462 #endif 463 457 || argv[optind][0] == '+' 458 #endif /* GETOPT_COMPAT */ 459 )) 464 460 { 465 461 const struct option *p; … … 471 467 472 468 while (*s && *s != '=') 473 469 s++; 474 470 475 471 /* Test all options for either exact match or abbreviated matches. */ 476 472 for (p = longopts, option_index = 0; p->name; 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 473 p++, option_index++) 474 if (!strncmp (p->name, nextchar, s - nextchar)) 475 { 476 if (s - nextchar == my_strlen (p->name)) 477 { 478 /* Exact match found. */ 479 pfound = p; 480 indfound = option_index; 481 exact = 1; 482 break; 483 } 484 else if (pfound == NULL) 485 { 486 /* First nonexact match found. */ 487 pfound = p; 488 indfound = option_index; 489 } 490 else 491 /* Second nonexact match found. */ 492 ambig = 1; 493 } 498 494 499 495 if (ambig && !exact) 500 501 502 503 504 505 506 507 496 { 497 if (opterr) 498 fprintf (stderr, "%s: option `%s' is ambiguous\n", 499 argv[0], argv[optind]); 500 nextchar += my_strlen (nextchar); 501 optind++; 502 return BAD_OPTION; 503 } 508 504 509 505 if (pfound != NULL) 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 506 { 507 option_index = indfound; 508 optind++; 509 if (*s) 510 { 511 /* Don't test has_arg with >, because some C compilers don't 512 allow it to be used on enums. */ 513 if (pfound->has_arg) 514 optarg = s + 1; 515 else 516 { 517 if (opterr) 518 { 519 if (argv[optind - 1][1] == '-') 520 /* --option */ 521 fprintf (stderr, 522 "%s: option `--%s' doesn't allow an argument\n", 523 argv[0], pfound->name); 524 else 525 /* +option or -option */ 526 fprintf (stderr, 527 "%s: option `%c%s' doesn't allow an argument\n", 528 argv[0], argv[optind - 1][0], pfound->name); 529 } 530 nextchar += my_strlen (nextchar); 531 return BAD_OPTION; 532 } 533 } 534 else if (pfound->has_arg == 1) 535 { 536 if (optind < argc) 537 optarg = argv[optind++]; 538 else 539 { 540 if (opterr) 541 fprintf (stderr, "%s: option `%s' requires an argument\n", 542 argv[0], argv[optind - 1]); 543 nextchar += my_strlen (nextchar); 544 return optstring[0] == ':' ? ':' : BAD_OPTION; 545 } 546 } 547 nextchar += my_strlen (nextchar); 548 if (longind != NULL) 549 *longind = option_index; 550 if (pfound->flag) 551 { 552 *(pfound->flag) = pfound->val; 553 return 0; 554 } 555 return pfound->val; 556 } 561 557 /* Can't find it as a long option. If this is not getopt_long_only, 562 563 564 558 or the option starts with '--' or is not a valid short 559 option, then it's an error. 560 Otherwise interpret it as a short option. */ 565 561 if (!long_only || argv[optind][1] == '-' 566 562 #ifdef GETOPT_COMPAT 567 568 #endif 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 563 || argv[optind][0] == '+' 564 #endif /* GETOPT_COMPAT */ 565 || my_index (optstring, *nextchar) == NULL) 566 { 567 if (opterr) 568 { 569 if (argv[optind][1] == '-') 570 /* --option */ 571 fprintf (stderr, "%s: unrecognized option `--%s'\n", 572 argv[0], nextchar); 573 else 574 /* +option or -option */ 575 fprintf (stderr, "%s: unrecognized option `%c%s'\n", 576 argv[0], argv[optind][0], nextchar); 577 } 578 nextchar = (char *) ""; 579 optind++; 580 return BAD_OPTION; 581 } 586 582 } 587 583 … … 598 594 if (temp == NULL || c == ':') 599 595 { 600 601 596 if (opterr) 597 { 602 598 #if 0 603 604 605 606 607 599 if (c < 040 || c >= 0177) 600 fprintf (stderr, "%s: unrecognized option, character code 0%o\n", 601 argv[0], c); 602 else 603 fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c); 608 604 #else 609 610 605 /* 1003.2 specifies the format of this message. */ 606 fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); 611 607 #endif 612 613 614 608 } 609 optopt = c; 610 return BAD_OPTION; 615 611 } 616 612 if (temp[1] == ':') 617 613 { 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 614 if (temp[2] == ':') 615 { 616 /* This is an option that accepts an argument optionally. */ 617 if (*nextchar != '\0') 618 { 619 optarg = nextchar; 620 optind++; 621 } 622 else 623 optarg = 0; 624 nextchar = NULL; 625 } 626 else 627 { 628 /* This is an option that requires an argument. */ 629 if (*nextchar != '\0') 630 { 631 optarg = nextchar; 632 /* If we end this ARGV-element by taking the rest as an arg, 633 we must advance to the next element now. */ 634 optind++; 635 } 636 else if (optind == argc) 637 { 638 if (opterr) 639 { 644 640 #if 0 645 646 641 fprintf (stderr, "%s: option `-%c' requires an argument\n", 642 argv[0], c); 647 643 #else 648 649 650 644 /* 1003.2 specifies the format of this message. */ 645 fprintf (stderr, "%s: option requires an argument -- %c\n", 646 argv[0], c); 651 647 #endif 652 653 654 655 656 657 658 659 660 661 662 663 664 648 } 649 optopt = c; 650 if (optstring[0] == ':') 651 c = ':'; 652 else 653 c = BAD_OPTION; 654 } 655 else 656 /* We already incremented `optind' once; 657 increment it again when taking next ARGV-elt as argument. */ 658 optarg = argv[optind++]; 659 nextchar = NULL; 660 } 665 661 } 666 662 return c; … … 675 671 { 676 672 return _getopt_internal (argc, argv, optstring, 677 678 679 673 (const struct option *) 0, 674 (int *) 0, 675 0); 680 676 } 681 677 … … 691 687 } 692 688 693 #endif /* _LIBC or not __GNU_LIBRARY__. */ 694 689 #endif /* _LIBC or not __GNU_LIBRARY__. */ 695 690 696 691 #ifdef TEST_GETOPT … … 713 708 c = getopt (argc, argv, "abc:d:0123456789"); 714 709 if (c == EOF) 715 710 break; 716 711 717 712 switch (c) 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 713 { 714 case '0': 715 case '1': 716 case '2': 717 case '3': 718 case '4': 719 case '5': 720 case '6': 721 case '7': 722 case '8': 723 case '9': 724 if (digit_optind != 0 && digit_optind != this_option_optind) 725 printf ("digits occur in two different argv-elements.\n"); 726 digit_optind = this_option_optind; 727 printf ("option %c\n", c); 728 break; 729 730 case 'a': 731 printf ("option a\n"); 732 break; 733 734 case 'b': 735 printf ("option b\n"); 736 break; 737 738 case 'c': 739 printf ("option c with value `%s'\n", optarg); 740 break; 741 742 case BAD_OPTION: 743 break; 744 745 default: 746 printf ("?? getopt returned character code 0%o ??\n", c); 747 } 753 748 } 754 749 … … 757 752 printf ("non-option ARGV-elements: "); 758 753 while (optind < argc) 759 754 printf ("%s ", argv[optind++]); 760 755 printf ("\n"); 761 756 }
Note: See TracChangeset
for help on using the changeset viewer.