Ignore:
Timestamp:
Mar 6, 2006 3:14:59 PM (18 years ago)
Author:
bennylp
Message:

Renamed getopt() to pj_getopt(), and fixed misc Linux warnings

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip/src/pjsua-lib/getopt.c

    r238 r300  
    1919 
    2020/*  
    21  * getopt entry points 
     21 * pj_getopt entry points 
    2222 * 
    2323 * modified by Mike Borella <mike_borella@mw.3com.com> 
     
    3333int _getopt_internal (int argc, char *const *argv, 
    3434                      const char *shortopts, 
    35                       const struct option *longopts, int *longind, 
     35                      const struct pj_getopt_option *longopts, int *longind, 
    3636                      int long_only); 
    3737 
    38 /* getopt_long and getopt_long_only entry points for GNU getopt. 
     38/* pj_getopt_long and pj_getopt_long_only entry points for GNU pj_getopt. 
    3939   Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc. 
    4040   This file is part of the GNU C Library. 
     
    6868 
    6969int 
    70 getopt_long (int argc, char *const *argv, const char *options,  
    71              const struct option *long_options, int *opt_index) 
     70pj_getopt_long (int argc, char *const *argv, const char *options,  
     71             const struct pj_getopt_option *long_options, int *opt_index) 
    7272{ 
    7373  return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 
    7474} 
    7575 
    76 /* Like getopt_long, but '-' as well as '--' can indicate a long option. 
     76/* Like pj_getopt_long, but '-' as well as '--' can indicate a long option. 
    7777   If an option that starts with '-' (not '--') doesn't match a long option, 
    7878   but does match a short option, it is parsed as a short option 
     
    8080  
    8181int 
    82 getopt (int argc, char * const * argv, const char * optstring) 
     82pj_getopt (int argc, char * const * argv, const char * optstring) 
    8383{ 
    8484  return _getopt_internal (argc, argv, optstring, 
    85                            (const struct option *) 0, 
     85                           (const struct pj_getopt_option *) 0, 
    8686                           (int *) 0, 
    8787                           0); 
     
    9191#define _(msgid)        (msgid) 
    9292 
    93 /* This version of `getopt' appears to the caller like standard Unix `getopt' 
     93/* This version of `pj_getopt' appears to the caller like standard Unix `pj_getopt' 
    9494   but it behaves differently for the user, since it allows the user 
    9595   to intersperse the options with the other arguments. 
    9696 
    97    As `getopt' works, it permutes the elements of ARGV so that, 
     97   As `pj_getopt' works, it permutes the elements of ARGV so that, 
    9898   when it is done, all the options precede everything else.  Thus 
    9999   all application programs are extended to handle flexible argument order. 
     
    105105   they can distinguish the relative order of options and other arguments.  */ 
    106106 
    107 /* For communication from `getopt' to the caller. 
    108    When `getopt' finds an option that takes an argument, 
     107/* For communication from `pj_getopt' to the caller. 
     108   When `pj_getopt' finds an option that takes an argument, 
    109109   the argument value is returned here. 
    110110   Also, when `ordering' is RETURN_IN_ORDER, 
    111111   each non-option ARGV-element is returned here.  */ 
    112112 
    113 char *optarg = NULL; 
     113char *pj_optarg = NULL; 
    114114 
    115115/* Index in ARGV of the next element to be scanned. 
    116116   This is used for communication to and from the caller 
    117    and for communication between successive calls to `getopt'. 
    118  
    119    On entry to `getopt', zero means this is the first call; initialize. 
    120  
    121    When `getopt' returns -1, this is the index of the first of the 
     117   and for communication between successive calls to `pj_getopt'. 
     118 
     119   On entry to `pj_getopt', zero means this is the first call; initialize. 
     120 
     121   When `pj_getopt' returns -1, this is the index of the first of the 
    122122   non-option elements that the caller should itself scan. 
    123123 
    124    Otherwise, `optind' communicates from one call to the next 
     124   Otherwise, `pj_optind' communicates from one call to the next 
    125125   how much of ARGV has been scanned so far.  */ 
    126126 
    127127/* 1003.2 says this must be 1 before any call.  */ 
    128 int optind = 1; 
    129  
    130 /* Formerly, initialization of getopt depended on optind==0, which 
    131    causes problems with re-calling getopt as programs generally don't 
     128int pj_optind = 1; 
     129 
     130/* Formerly, initialization of pj_getopt depended on pj_optind==0, which 
     131   causes problems with re-calling pj_getopt as programs generally don't 
    132132   know that. */ 
    133133 
     
    145145/* Set to an option character which was unrecognized. 
    146146   This must be initialized on some systems to avoid linking in the 
    147    system's own getopt implementation.  */ 
    148  
    149 int optopt = '?'; 
     147   system's own pj_getopt implementation.  */ 
     148 
     149int pj_optopt = '?'; 
    150150 
    151151/* Describe how to deal with options that follow non-option ARGV-elements. 
     
    176176   The special argument `--' forces an end of option-scanning regardless 
    177177   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only 
    178    `--' can cause `getopt' to return -1 with `optind' != ARGC.  */ 
     178   `--' can cause `pj_getopt' to return -1 with `pj_optind' != ARGC.  */ 
    179179 
    180180static enum 
     
    213213   One subsequence is elements [first_nonopt,last_nonopt) 
    214214   which contains all the non-options that have been skipped so far. 
    215    The other is elements [last_nonopt,optind), which contains all 
     215   The other is elements [last_nonopt,pj_optind), which contains all 
    216216   the options processed since those non-options were skipped. 
    217217 
     
    224224  int bottom = first_nonopt; 
    225225  int middle = last_nonopt; 
    226   int top = optind; 
     226  int top = pj_optind; 
    227227  char *tem; 
    228228 
     
    272272  /* Update records for the slots the non-options now occupy.  */ 
    273273 
    274   first_nonopt += (optind - last_nonopt); 
    275   last_nonopt = optind; 
     274  first_nonopt += (pj_optind - last_nonopt); 
     275  last_nonopt = pj_optind; 
    276276} 
    277277 
     
    288288     non-option ARGV-elements is empty.  */ 
    289289 
    290   first_nonopt = last_nonopt = optind; 
     290  first_nonopt = last_nonopt = pj_optind; 
    291291 
    292292  nextchar = NULL; 
     
    320320   If an element of ARGV starts with '-', and is not exactly "-" or "--", 
    321321   then it is an option element.  The characters of this element 
    322    (aside from the initial '-') are option characters.  If `getopt' 
     322   (aside from the initial '-') are option characters.  If `pj_getopt' 
    323323   is called repeatedly, it returns successively each of the option characters 
    324324   from each of the option elements. 
    325325 
    326    If `getopt' finds another option character, it returns that character, 
    327    updating `optind' and `nextchar' so that the next call to `getopt' can 
     326   If `pj_getopt' finds another option character, it returns that character, 
     327   updating `pj_optind' and `nextchar' so that the next call to `pj_getopt' can 
    328328   resume the scan with the following option character or ARGV-element. 
    329329 
    330    If there are no more option characters, `getopt' returns -1. 
    331    Then `optind' is the index in ARGV of the first ARGV-element 
     330   If there are no more option characters, `pj_getopt' returns -1. 
     331   Then `pj_optind' is the index in ARGV of the first ARGV-element 
    332332   that is not an option.  (The ARGV-elements have been permuted 
    333333   so that those that are not options now come last.) 
     
    335335   OPTSTRING is a string containing the legitimate option characters. 
    336336   If an option character is seen that is not listed in OPTSTRING, 
    337    return '?' after printing an error message.  If you set `opterr' to 
     337   return '?' after printing an error message.  If you set `pj_opterr' to 
    338338   zero, the error message is suppressed but we still return '?'. 
    339339 
    340340   If a char in OPTSTRING is followed by a colon, that means it wants an arg, 
    341341   so the following text in the same ARGV-element, or the text of the following 
    342    ARGV-element, is returned in `optarg'.  Two colons mean an option that 
     342   ARGV-element, is returned in `pj_optarg'.  Two colons mean an option that 
    343343   wants an optional arg; if there is text in the current ARGV-element, 
    344    it is returned in `optarg', otherwise `optarg' is set to zero. 
     344   it is returned in `pj_optarg', otherwise `pj_optarg' is set to zero. 
    345345 
    346346   If OPTSTRING starts with `-' or `+', it requests different methods of 
     
    353353   argument, it follows the option name in the same ARGV-element, separated 
    354354   from the option name by a `=', or else the in next ARGV-element. 
    355    When `getopt' finds a long-named option, it returns 0 if that option's 
     355   When `pj_getopt' finds a long-named option, it returns 0 if that option's 
    356356   `flag' field is nonzero, the value of the option's `val' field 
    357357   if the `flag' field is zero. 
     
    361361   with other systems. 
    362362 
    363    LONGOPTS is a vector of `struct option' terminated by an 
     363   LONGOPTS is a vector of `struct pj_getopt_option' terminated by an 
    364364   element containing a name which is zero. 
    365365 
     
    373373static int 
    374374_getopt_internal (int argc, char *const *argv, const char *optstring,  
    375                   const struct option *longopts, int *longind,  
     375                  const struct pj_getopt_option *longopts, int *longind,  
    376376                  int long_only) 
    377377{ 
    378   optarg = NULL; 
    379  
    380   if (optind == 0 || !__getopt_initialized) 
     378  pj_optarg = NULL; 
     379 
     380  if (pj_optind == 0 || !__getopt_initialized) 
    381381    { 
    382       if (optind == 0) 
    383         optind = 1;     /* Don't scan ARGV[0], the program name.  */ 
     382      if (pj_optind == 0) 
     383        pj_optind = 1;  /* Don't scan ARGV[0], the program name.  */ 
    384384      optstring = _getopt_initialize (argc, argv, optstring); 
    385385      __getopt_initialized = 1; 
    386386    } 
    387387 
    388   /* Test whether ARGV[optind] points to a non-option argument. 
     388  /* Test whether ARGV[pj_optind] points to a non-option argument. 
    389389     Either it does not have option syntax, or there is an environment flag 
    390390     from the shell indicating it is not an option.  The later information 
    391391     is only used when the used in the GNU libc.  */ 
    392 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 
     392#define NONOPTION_P (argv[pj_optind][0] != '-' || argv[pj_optind][1] == '\0') 
    393393 
    394394  if (nextchar == NULL || *nextchar == '\0') 
     
    398398      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been 
    399399         moved back by the user (who may also have changed the arguments).  */ 
    400       if (last_nonopt > optind) 
    401         last_nonopt = optind; 
    402       if (first_nonopt > optind) 
    403         first_nonopt = optind; 
     400      if (last_nonopt > pj_optind) 
     401        last_nonopt = pj_optind; 
     402      if (first_nonopt > pj_optind) 
     403        first_nonopt = pj_optind; 
    404404 
    405405      if (ordering == PERMUTE) 
     
    408408             exchange them so that the options come first.  */ 
    409409 
    410           if (first_nonopt != last_nonopt && last_nonopt != optind) 
     410          if (first_nonopt != last_nonopt && last_nonopt != pj_optind) 
    411411            exchange ((char **) argv); 
    412           else if (last_nonopt != optind) 
    413             first_nonopt = optind; 
     412          else if (last_nonopt != pj_optind) 
     413            first_nonopt = pj_optind; 
    414414 
    415415          /* Skip any additional non-options 
    416416             and extend the range of non-options previously skipped.  */ 
    417417 
    418           while (optind < argc && NONOPTION_P) 
    419             optind++; 
    420           last_nonopt = optind; 
     418          while (pj_optind < argc && NONOPTION_P) 
     419            pj_optind++; 
     420          last_nonopt = pj_optind; 
    421421        } 
    422422 
     
    426426         then skip everything else like a non-option.  */ 
    427427 
    428       if (optind != argc && !pj_ansi_strcmp(argv[optind], "--")) 
    429         { 
    430           optind++; 
    431  
    432           if (first_nonopt != last_nonopt && last_nonopt != optind) 
     428      if (pj_optind != argc && !pj_ansi_strcmp(argv[pj_optind], "--")) 
     429        { 
     430          pj_optind++; 
     431 
     432          if (first_nonopt != last_nonopt && last_nonopt != pj_optind) 
    433433            exchange ((char **) argv); 
    434434          else if (first_nonopt == last_nonopt) 
    435             first_nonopt = optind; 
     435            first_nonopt = pj_optind; 
    436436          last_nonopt = argc; 
    437437 
    438           optind = argc; 
     438          pj_optind = argc; 
    439439        } 
    440440 
     
    442442         and back over any non-options that we skipped and permuted.  */ 
    443443 
    444       if (optind == argc) 
     444      if (pj_optind == argc) 
    445445        { 
    446446          /* Set the next-arg-index to point at the non-options 
    447447             that we previously skipped, so the caller will digest them.  */ 
    448448          if (first_nonopt != last_nonopt) 
    449             optind = first_nonopt; 
     449            pj_optind = first_nonopt; 
    450450          return -1; 
    451451        } 
     
    458458          if (ordering == REQUIRE_ORDER) 
    459459            return -1; 
    460           optarg = argv[optind++]; 
     460          pj_optarg = argv[pj_optind++]; 
    461461          return 1; 
    462462        } 
     
    465465         Skip the initial punctuation.  */ 
    466466 
    467       nextchar = (argv[optind] + 1 
    468                   + (longopts != NULL && argv[optind][1] == '-')); 
     467      nextchar = (argv[pj_optind] + 1 
     468                  + (longopts != NULL && argv[pj_optind][1] == '-')); 
    469469    } 
    470470 
     
    485485 
    486486  if (longopts != NULL 
    487       && (argv[optind][1] == '-' 
    488           || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) 
     487      && (argv[pj_optind][1] == '-' 
     488          || (long_only && (argv[pj_optind][2] || !my_index (optstring, argv[pj_optind][1]))))) 
    489489    { 
    490490      char *nameend; 
    491       const struct option *p; 
    492       const struct option *pfound = NULL; 
     491      const struct pj_getopt_option *p; 
     492      const struct pj_getopt_option *pfound = NULL; 
    493493      int exact = 0; 
    494494      int ambig = 0; 
     
    527527        { 
    528528          nextchar += strlen (nextchar); 
    529           optind++; 
    530           optopt = 0; 
     529          pj_optind++; 
     530          pj_optopt = 0; 
    531531          return '?'; 
    532532        } 
     
    535535        { 
    536536          option_index = indfound; 
    537           optind++; 
     537          pj_optind++; 
    538538          if (*nameend) 
    539539            { 
     
    541541                 allow it to be used on enums.  */ 
    542542              if (pfound->has_arg) 
    543                 optarg = nameend + 1; 
     543                pj_optarg = nameend + 1; 
    544544              else 
    545545                { 
    546546                  nextchar += strlen (nextchar); 
    547547 
    548                   optopt = pfound->val; 
     548                  pj_optopt = pfound->val; 
    549549                  return '?'; 
    550550                } 
     
    552552          else if (pfound->has_arg == 1) 
    553553            { 
    554               if (optind < argc) 
    555                 optarg = argv[optind++]; 
     554              if (pj_optind < argc) 
     555                pj_optarg = argv[pj_optind++]; 
    556556              else 
    557557                { 
    558558                  nextchar += strlen (nextchar); 
    559                   optopt = pfound->val; 
     559                  pj_optopt = pfound->val; 
    560560                  return optstring[0] == ':' ? ':' : '?'; 
    561561                } 
     
    572572        } 
    573573 
    574       /* Can't find it as a long option.  If this is not getopt_long_only, 
     574      /* Can't find it as a long option.  If this is not pj_getopt_long_only, 
    575575         or the option starts with '--' or is not a valid short 
    576576         option, then it's an error. 
    577577         Otherwise interpret it as a short option.  */ 
    578       if (!long_only || argv[optind][1] == '-' 
     578      if (!long_only || argv[pj_optind][1] == '-' 
    579579          || my_index (optstring, *nextchar) == NULL) 
    580580        { 
    581581          nextchar = (char *) ""; 
    582           optind++; 
    583           optopt = 0; 
     582          pj_optind++; 
     583          pj_optopt = 0; 
    584584          return '?'; 
    585585        } 
     
    592592    char *temp = my_index (optstring, c); 
    593593 
    594     /* Increment `optind' when we start to process its last character.  */ 
     594    /* Increment `pj_optind' when we start to process its last character.  */ 
    595595    if (*nextchar == '\0') 
    596       ++optind; 
     596      ++pj_optind; 
    597597 
    598598    if (temp == NULL || c == ':') 
    599599      { 
    600         optopt = c; 
     600        pj_optopt = c; 
    601601        return '?'; 
    602602      } 
     
    605605      { 
    606606        char *nameend; 
    607         const struct option *p; 
    608         const struct option *pfound = NULL; 
     607        const struct pj_getopt_option *p; 
     608        const struct pj_getopt_option *pfound = NULL; 
    609609        int exact = 0; 
    610610        int ambig = 0; 
     
    615615        if (*nextchar != '\0') 
    616616          { 
    617             optarg = nextchar; 
     617            pj_optarg = nextchar; 
    618618            /* If we end this ARGV-element by taking the rest as an arg, 
    619619               we must advance to the next element now.  */ 
    620             optind++; 
     620            pj_optind++; 
    621621          } 
    622         else if (optind == argc) 
     622        else if (pj_optind == argc) 
    623623          { 
    624             optopt = c; 
     624            pj_optopt = c; 
    625625            if (optstring[0] == ':') 
    626626              c = ':'; 
     
    630630          } 
    631631        else 
    632           /* We already incremented `optind' once; 
     632          /* We already incremented `pj_optind' once; 
    633633             increment it again when taking next ARGV-elt as argument.  */ 
    634           optarg = argv[optind++]; 
    635  
    636         /* optarg is now the argument, see if it's in the 
     634          pj_optarg = argv[pj_optind++]; 
     635 
     636        /* pj_optarg is now the argument, see if it's in the 
    637637           table of longopts.  */ 
    638638 
    639         for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) 
     639        for (nextchar = nameend = pj_optarg; *nameend && *nameend != '='; nameend++) 
    640640          /* Do nothing.  */ ; 
    641641 
     
    666666          { 
    667667            nextchar += strlen (nextchar); 
    668             optind++; 
     668            pj_optind++; 
    669669            return '?'; 
    670670          } 
     
    677677                   allow it to be used on enums.  */ 
    678678                if (pfound->has_arg) 
    679                   optarg = nameend + 1; 
     679                  pj_optarg = nameend + 1; 
    680680                else 
    681681                  { 
     
    686686            else if (pfound->has_arg == 1) 
    687687              { 
    688                 if (optind < argc) 
    689                   optarg = argv[optind++]; 
     688                if (pj_optind < argc) 
     689                  pj_optarg = argv[pj_optind++]; 
    690690                else 
    691691                  { 
     
    714714            if (*nextchar != '\0') 
    715715              { 
    716                 optarg = nextchar; 
    717                 optind++; 
     716                pj_optarg = nextchar; 
     717                pj_optind++; 
    718718              } 
    719719            else 
    720               optarg = NULL; 
     720              pj_optarg = NULL; 
    721721            nextchar = NULL; 
    722722          } 
     
    726726            if (*nextchar != '\0') 
    727727              { 
    728                 optarg = nextchar; 
     728                pj_optarg = nextchar; 
    729729                /* If we end this ARGV-element by taking the rest as an arg, 
    730730                   we must advance to the next element now.  */ 
    731                 optind++; 
     731                pj_optind++; 
    732732              } 
    733             else if (optind == argc) 
     733            else if (pj_optind == argc) 
    734734              { 
    735                 optopt = c; 
     735                pj_optopt = c; 
    736736                if (optstring[0] == ':') 
    737737                  c = ':'; 
     
    740740              } 
    741741            else 
    742               /* We already incremented `optind' once; 
     742              /* We already incremented `pj_optind' once; 
    743743                 increment it again when taking next ARGV-elt as argument.  */ 
    744               optarg = argv[optind++]; 
     744              pj_optarg = argv[pj_optind++]; 
    745745            nextchar = NULL; 
    746746          } 
Note: See TracChangeset for help on using the changeset viewer.