Changeset 7709bf in git for Singular/getopt.c


Ignore:
Timestamp:
Sep 22, 1998, 4:15:04 PM (26 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
6594d42df45662b198c336ed0437cfba5e77a91a
Parents:
e0d91ca23156d80a43dde9ffd342ab947b5cc298
Message:
* hannes: cosmetic changes


git-svn-id: file:///usr/local/Singular/svn/trunk@2514 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/getopt.c

    re0d91c r7709bf  
    22*  Computer Algebra System SINGULAR     *
    33****************************************/
    4 /* $Id: getopt.c,v 1.2 1998-04-17 07:53:41 Singular Exp $ */
     4/* $Id: getopt.c,v 1.3 1998-09-22 14:15:03 Singular Exp $ */
    55
    66/* Getopt for GNU.
     
    1010
    1111   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94
    12         Free Software Foundation, Inc.
     12        Free Software Foundation, Inc.
    1313
    1414   This program is free software; you can redistribute it and/or modify it
     
    2727
    2828/* adapted for use with Singular by obachman@mathematik.uni-kl.de 4/98*/
    29 
    3029
    3130
     
    5958/* This needs to come after some library #include
    6059   to get __GNU_LIBRARY__ defined.  */
    61 #ifdef  __GNU_LIBRARY__
     60#ifdef  __GNU_LIBRARY__
    6261/* Don't include stdlib.h for non-GNU C libraries because some of them
    6362   contain conflicting prototypes for getopt.  */
    6463#include <stdlib.h>
    65 #endif  /* GNU C library.  */
     64#endif  /* GNU C library.  */
    6665
    6766/* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
     
    164163} ordering;
    165164
    166 
    167 #ifdef  __GNU_LIBRARY__
     165#ifdef  __GNU_LIBRARY__
    168166/* We want to avoid inclusion of string.h with non-GNU libraries
    169167   because there are many ways it can cause trouble.
     
    171169   in GCC.  */
    172170#include <string.h>
    173 #define my_index        strchr
    174 #define my_strlen       strlen
     171#define my_index        strchr
     172#define my_strlen       strlen
    175173#else
    176174
     
    207205    {
    208206      if (*str == chr)
    209         return (char *) str;
     207        return (char *) str;
    210208      str++;
    211209    }
     
    213211}
    214212
    215 #endif                          /* GNU C library.  */
    216 
     213#endif                          /* GNU C library.  */
    217214
    218215/* Handle permutation of arguments.  */
     
    277274}
    278275
    279 
    280276/* Scan elements of ARGV (whose length is ARGC) for option characters
    281277   given in OPTSTRING.
     
    361357
    362358      if (optstring[0] == '-')
    363         {
    364           ordering = RETURN_IN_ORDER;
    365           ++optstring;
    366         }
     359        {
     360          ordering = RETURN_IN_ORDER;
     361          ++optstring;
     362        }
    367363      else if (optstring[0] == '+')
    368         {
    369           ordering = REQUIRE_ORDER;
    370           ++optstring;
    371         }
     364        {
     365          ordering = REQUIRE_ORDER;
     366          ++optstring;
     367        }
    372368      else if (getenv ("POSIXLY_CORRECT") != NULL)
    373         ordering = REQUIRE_ORDER;
     369        ordering = REQUIRE_ORDER;
    374370      else
    375         ordering = PERMUTE;
     371        ordering = PERMUTE;
    376372    }
    377373
     
    379375    {
    380376      if (ordering == PERMUTE)
    381         {
    382           /* If we have just processed some options following some non-options,
    383              exchange them so that the options come first.  */
    384 
    385           if (first_nonopt != last_nonopt && last_nonopt != optind)
    386             exchange ((char **) argv);
    387           else if (last_nonopt != optind)
    388             first_nonopt = optind;
    389 
    390           /* Now skip any additional non-options
    391              and extend the range of non-options previously skipped.  */
    392 
    393           while (optind < argc
    394                 && (argv[optind][0] != '-' || argv[optind][1] == '\0')
     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')
    395391#ifdef GETOPT_COMPAT
    396                 && (longopts == NULL
    397                      || argv[optind][0] != '+' || argv[optind][1] == '\0')
    398 #endif                          /* GETOPT_COMPAT */
    399                 )
    400             optind++;
    401           last_nonopt = optind;
    402         }
     392                && (longopts == NULL
     393                     || argv[optind][0] != '+' || argv[optind][1] == '\0')
     394#endif                          /* GETOPT_COMPAT */
     395                )
     396            optind++;
     397          last_nonopt = optind;
     398        }
    403399
    404400      /* Special ARGV-element `--' means premature end of options.
    405         Skip it like a null option,
    406         then exchange with previous non-options as if it were an option,
    407         then skip everything else like a non-option.  */
     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.  */
    408404
    409405      if (optind != argc && !strcmp (argv[optind], "--"))
    410         {
    411           optind++;
    412 
    413           if (first_nonopt != last_nonopt && last_nonopt != optind)
    414             exchange ((char **) argv);
    415           else if (first_nonopt == last_nonopt)
    416             first_nonopt = optind;
    417           last_nonopt = argc;
    418 
    419           optind = argc;
    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        }
    421417
    422418      /* If we have done all the ARGV-elements, stop the scan
    423         and back over any non-options that we skipped and permuted.  */
     419        and back over any non-options that we skipped and permuted.  */
    424420
    425421      if (optind == argc)
    426         {
    427           /* Set the next-arg-index to point at the non-options
    428              that we previously skipped, so the caller will digest them.  */
    429           if (first_nonopt != last_nonopt)
    430             optind = first_nonopt;
    431           return EOF;
    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        }
    433429
    434430      /* If we have come to a non-option and did not permute it,
    435         either stop the scan or describe it to the caller and pass it by.  */
     431        either stop the scan or describe it to the caller and pass it by.  */
    436432
    437433      if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
    438434#ifdef GETOPT_COMPAT
    439           && (longopts == NULL
    440               || argv[optind][0] != '+' || argv[optind][1] == '\0')
    441 #endif                          /* GETOPT_COMPAT */
    442           )
    443         {
    444           if (ordering == REQUIRE_ORDER)
    445             return EOF;
    446           optarg = argv[optind++];
    447           return 1;
    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        }
    449445
    450446      /* We have found another option-ARGV-element.
    451         Start decoding its characters.  */
     447        Start decoding its characters.  */
    452448
    453449      nextchar = (argv[optind] + 1
    454                   + (longopts != NULL && argv[optind][1] == '-'));
     450                  + (longopts != NULL && argv[optind][1] == '-'));
    455451    }
    456452
    457453  if (longopts != NULL
    458454      && ((argv[optind][0] == '-'
    459            && (argv[optind][1] == '-' || long_only))
     455           && (argv[optind][1] == '-' || long_only))
    460456#ifdef GETOPT_COMPAT
    461           || argv[optind][0] == '+'
    462 #endif                          /* GETOPT_COMPAT */
    463           ))
     457          || argv[optind][0] == '+'
     458#endif                          /* GETOPT_COMPAT */
     459          ))
    464460    {
    465461      const struct option *p;
     
    471467
    472468      while (*s && *s != '=')
    473         s++;
     469        s++;
    474470
    475471      /* Test all options for either exact match or abbreviated matches.  */
    476472      for (p = longopts, option_index = 0; p->name;
    477            p++, option_index++)
    478         if (!strncmp (p->name, nextchar, s - nextchar))
    479           {
    480             if (s - nextchar == my_strlen (p->name))
    481               {
    482                 /* Exact match found.  */
    483                 pfound = p;
    484                 indfound = option_index;
    485                 exact = 1;
    486                 break;
    487               }
    488             else if (pfound == NULL)
    489               {
    490                 /* First nonexact match found.  */
    491                 pfound = p;
    492                 indfound = option_index;
    493               }
    494             else
    495               /* Second nonexact match found.  */
    496               ambig = 1;
    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          }
    498494
    499495      if (ambig && !exact)
    500         {
    501           if (opterr)
    502             fprintf (stderr, "%s: option `%s' is ambiguous\n",
    503                      argv[0], argv[optind]);
    504           nextchar += my_strlen (nextchar);
    505           optind++;
    506           return BAD_OPTION;
    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        }
    508504
    509505      if (pfound != NULL)
    510         {
    511           option_index = indfound;
    512           optind++;
    513           if (*s)
    514             {
    515               /* Don't test has_arg with >, because some C compilers don't
    516                 allow it to be used on enums.  */
    517               if (pfound->has_arg)
    518                 optarg = s + 1;
    519               else
    520                 {
    521                   if (opterr)
    522                     {
    523                       if (argv[optind - 1][1] == '-')
    524                         /* --option */
    525                         fprintf (stderr,
    526                                 "%s: option `--%s' doesn't allow an argument\n",
    527                                 argv[0], pfound->name);
    528                       else
    529                         /* +option or -option */
    530                         fprintf (stderr,
    531                              "%s: option `%c%s' doesn't allow an argument\n",
    532                              argv[0], argv[optind - 1][0], pfound->name);
    533                     }
    534                   nextchar += my_strlen (nextchar);
    535                   return BAD_OPTION;
    536                 }
    537             }
    538           else if (pfound->has_arg == 1)
    539             {
    540               if (optind < argc)
    541                 optarg = argv[optind++];
    542               else
    543                 {
    544                   if (opterr)
    545                     fprintf (stderr, "%s: option `%s' requires an argument\n",
    546                              argv[0], argv[optind - 1]);
    547                   nextchar += my_strlen (nextchar);
    548                   return optstring[0] == ':' ? ':' : BAD_OPTION;
    549                 }
    550             }
    551           nextchar += my_strlen (nextchar);
    552           if (longind != NULL)
    553             *longind = option_index;
    554           if (pfound->flag)
    555             {
    556               *(pfound->flag) = pfound->val;
    557               return 0;
    558             }
    559           return pfound->val;
    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        }
    561557      /* Can't find it as a long option.  If this is not getopt_long_only,
    562         or the option starts with '--' or is not a valid short
    563         option, then it's an error.
    564         Otherwise interpret it as a short option.  */
     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.  */
    565561      if (!long_only || argv[optind][1] == '-'
    566562#ifdef GETOPT_COMPAT
    567           || argv[optind][0] == '+'
    568 #endif                          /* GETOPT_COMPAT */
    569           || my_index (optstring, *nextchar) == NULL)
    570         {
    571           if (opterr)
    572             {
    573               if (argv[optind][1] == '-')
    574                 /* --option */
    575                 fprintf (stderr, "%s: unrecognized option `--%s'\n",
    576                         argv[0], nextchar);
    577               else
    578                 /* +option or -option */
    579                 fprintf (stderr, "%s: unrecognized option `%c%s'\n",
    580                         argv[0], argv[optind][0], nextchar);
    581             }
    582           nextchar = (char *) "";
    583           optind++;
    584           return BAD_OPTION;
    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        }
    586582    }
    587583
     
    598594    if (temp == NULL || c == ':')
    599595      {
    600         if (opterr)
    601           {
     596        if (opterr)
     597          {
    602598#if 0
    603             if (c < 040 || c >= 0177)
    604               fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
    605                        argv[0], c);
    606             else
    607               fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
     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);
    608604#else
    609             /* 1003.2 specifies the format of this message.  */
    610             fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
     605            /* 1003.2 specifies the format of this message.  */
     606            fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
    611607#endif
    612           }
    613         optopt = c;
    614         return BAD_OPTION;
     608          }
     609        optopt = c;
     610        return BAD_OPTION;
    615611      }
    616612    if (temp[1] == ':')
    617613      {
    618         if (temp[2] == ':')
    619           {
    620             /* This is an option that accepts an argument optionally.  */
    621             if (*nextchar != '\0')
    622               {
    623                 optarg = nextchar;
    624                 optind++;
    625               }
    626             else
    627               optarg = 0;
    628             nextchar = NULL;
    629           }
    630         else
    631           {
    632             /* This is an option that requires an argument.  */
    633             if (*nextchar != '\0')
    634               {
    635                 optarg = nextchar;
    636                 /* If we end this ARGV-element by taking the rest as an arg,
    637                    we must advance to the next element now.  */
    638                 optind++;
    639               }
    640             else if (optind == argc)
    641               {
    642                 if (opterr)
    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                  {
    644640#if 0
    645                     fprintf (stderr, "%s: option `-%c' requires an argument\n",
    646                              argv[0], c);
     641                    fprintf (stderr, "%s: option `-%c' requires an argument\n",
     642                             argv[0], c);
    647643#else
    648                     /* 1003.2 specifies the format of this message.  */
    649                     fprintf (stderr, "%s: option requires an argument -- %c\n",
    650                              argv[0], c);
     644                    /* 1003.2 specifies the format of this message.  */
     645                    fprintf (stderr, "%s: option requires an argument -- %c\n",
     646                             argv[0], c);
    651647#endif
    652                   }
    653                 optopt = c;
    654                 if (optstring[0] == ':')
    655                   c = ':';
    656                 else
    657                   c = BAD_OPTION;
    658               }
    659             else
    660               /* We already incremented `optind' once;
    661                 increment it again when taking next ARGV-elt as argument.  */
    662               optarg = argv[optind++];
    663             nextchar = NULL;
    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          }
    665661      }
    666662    return c;
     
    675671{
    676672  return _getopt_internal (argc, argv, optstring,
    677                            (const struct option *) 0,
    678                            (int *) 0,
    679                            0);
     673                           (const struct option *) 0,
     674                           (int *) 0,
     675                           0);
    680676}
    681677
     
    691687}
    692688
    693 #endif  /* _LIBC or not __GNU_LIBRARY__.  */
    694 
     689#endif  /* _LIBC or not __GNU_LIBRARY__.  */
    695690
    696691#ifdef TEST_GETOPT
     
    713708      c = getopt (argc, argv, "abc:d:0123456789");
    714709      if (c == EOF)
    715         break;
     710        break;
    716711
    717712      switch (c)
    718         {
    719         case '0':
    720         case '1':
    721         case '2':
    722         case '3':
    723         case '4':
    724         case '5':
    725         case '6':
    726         case '7':
    727         case '8':
    728         case '9':
    729           if (digit_optind != 0 && digit_optind != this_option_optind)
    730             printf ("digits occur in two different argv-elements.\n");
    731           digit_optind = this_option_optind;
    732           printf ("option %c\n", c);
    733           break;
    734 
    735         case 'a':
    736           printf ("option a\n");
    737           break;
    738 
    739         case 'b':
    740           printf ("option b\n");
    741           break;
    742 
    743         case 'c':
    744           printf ("option c with value `%s'\n", optarg);
    745           break;
    746 
    747         case BAD_OPTION:
    748           break;
    749 
    750         default:
    751           printf ("?? getopt returned character code 0%o ??\n", c);
    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        }
    753748    }
    754749
     
    757752      printf ("non-option ARGV-elements: ");
    758753      while (optind < argc)
    759         printf ("%s ", argv[optind++]);
     754        printf ("%s ", argv[optind++]);
    760755      printf ("\n");
    761756    }
Note: See TracChangeset for help on using the changeset viewer.