Ignore:
Timestamp:
Feb 26, 2006 11:17:28 PM (18 years ago)
Author:
bennylp
Message:

Fixed warnings in getopt

File:
1 edited

Legend:

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

    r184 r237  
    1818 */ 
    1919 
    20 #ifdef _MSC_VER 
    21 /* in VC this file will generate a lot of warning about old style function 
    22  * declarations. 
    23  */ 
    24 # pragma warning(push, 3) 
    25 #endif 
    26  
    2720/*  
    2821 * getopt entry points 
     
    3326 */ 
    3427 
    35 #ifdef HAVE_CONFIG_H 
    36 #include "config.h" 
    37 #endif 
    38  
     28#include "getopt.h" 
    3929#include <pj/string.h> 
    4030 
    41 #ifndef HAVE_GETOPT_LONG 
     31/* Internal only.  Users should not call this directly.  */ 
     32static 
     33int _getopt_internal (int argc, char *const *argv, 
     34                      const char *shortopts, 
     35                      const struct option *longopts, int *longind, 
     36                      int long_only); 
    4237 
    4338/* getopt_long and getopt_long_only entry points for GNU getopt. 
     
    6055   Boston, MA 02111-1307, USA.  */ 
    6156 
    62 #include "getopt.h" 
    63  
    64  
    65 #include <stdio.h> 
    6657 
    6758/* Comment out all this code if we are using the GNU C Library, and are not 
     
    7364   it is simpler to just do this in the source for each such file.  */ 
    7465 
    75 #define GETOPT_INTERFACE_VERSION 2 
    76 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 
    77 #include <gnu-versions.h> 
    78 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 
    79 #define ELIDE_CODE 
    80 #endif 
    81 #endif 
    82  
    83 #ifndef ELIDE_CODE 
    84  
    85  
    86 /* This needs to come after some library #include 
    87    to get __GNU_LIBRARY__ defined.  */ 
    88 #ifdef __GNU_LIBRARY__ 
    89 #include <stdlib.h> 
    90 #endif 
    91  
    92 #ifndef NULL 
    93 #define NULL 0 
    94 #endif 
     66#   define GETOPT_INTERFACE_VERSION 2 
     67 
    9568 
    9669int 
     
    10679   instead.  */ 
    10780  
    108 int 
    109 getopt_long_only (argc, argv, options, long_options, opt_index) 
    110      int argc; 
    111      char *const *argv; 
    112      const char *options; 
    113      const struct option *long_options; 
    114      int *opt_index; 
    115 { 
    116   return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 
    117 } 
    118  
    11981int 
    12082getopt (int argc, char * const * argv, const char * optstring) 
     
    12688} 
    12789 
    128 #endif  /* Not ELIDE_CODE.  */ 
    129  
    130  
    131 #ifndef _NO_PROTO 
    132 #define _NO_PROTO 
    133 #endif 
    134  
    135  
    136 //#include <strings.h> 
    137  
    138 /* Comment out all this code if we are using the GNU C Library, and are not 
    139    actually compiling the library itself.  This code is part of the GNU C 
    140    Library, but also included in many other GNU distributions.  Compiling 
    141    and linking in this code is a waste when using the GNU C library 
    142    (especially if it is a shared library).  Rather than having every GNU 
    143    program understand `configure --with-gnu-libc' and omit the object files, 
    144    it is simpler to just do this in the source for each such file.  */ 
    145  
    146 #define GETOPT_INTERFACE_VERSION 2 
    147 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2 
    148 #include <gnu-versions.h> 
    149 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION 
    150 #define ELIDE_CODE 
    151 #endif 
    152 #endif 
    153  
    154 #ifndef ELIDE_CODE 
    155  
    156  
    157 /* This needs to come after some library #include 
    158    to get __GNU_LIBRARY__ defined.  */ 
    159 #ifdef  __GNU_LIBRARY__ 
    160 /* Don't include stdlib.h for non-GNU C libraries because some of them 
    161    contain conflicting prototypes for getopt.  */ 
    162 #include <stdlib.h> 
    163 #include <unistd.h> 
    164 #endif  /* GNU C library.  */ 
    165  
    166 #ifdef VMS 
    167 #include <unixlib.h> 
    168 #if HAVE_STRING_H - 0 
    169 #include <string.h> 
    170 #endif 
    171 #endif 
    172  
    173 #if defined (WIN32) && !defined (__CYGWIN32__) 
    174 /* It's not Unix, really.  See?  Capital letters.  */ 
    175 #include <windows.h> 
    176 #define getpid() GetCurrentProcessId() 
    177 #endif 
    178  
    179 #ifndef _ 
    180 /* This is for other GNU distributions with internationalized messages. 
    181    When compiling libc, the _ macro is predefined.  */ 
    182 #ifdef HAVE_LIBINTL_H 
    183 # include <libintl.h> 
    184 # define _(msgid)       gettext (msgid) 
    185 #else 
    186 # define _(msgid)       (msgid) 
    187 #endif 
    188 #endif 
     90 
     91#define _(msgid)        (msgid) 
    18992 
    19093/* This version of `getopt' appears to the caller like standard Unix `getopt' 
     
    201104   GNU application programs can use a third alternative mode in which 
    202105   they can distinguish the relative order of options and other arguments.  */ 
    203  
    204 #include "getopt.h" 
    205106 
    206107/* For communication from `getopt' to the caller. 
     
    241142 
    242143static char *nextchar; 
    243  
    244 /* Callers store zero here to inhibit the error message 
    245    for unrecognized options.  */ 
    246  
    247 int opterr = 1; 
    248144 
    249145/* Set to an option character which was unrecognized. 
     
    289185/* Value of POSIXLY_CORRECT environment variable.  */ 
    290186static char *posixly_correct; 
    291  
    292 #ifdef  __GNU_LIBRARY__ 
    293 /* We want to avoid inclusion of string.h with non-GNU libraries 
    294    because there are many ways it can cause trouble. 
    295    On some systems, it contains special magic macros that don't work 
    296    in GCC.  */ 
    297 #include <string.h> 
    298 #define my_index        pj_ansi_strchr 
    299 #else 
    300187 
    301188static char * 
     
    311198} 
    312199 
    313 /* If using GCC, we can safely declare strlen this way. 
    314    If not using GCC, it is ok not to declare it.  */ 
    315 #ifdef __GNUC__ 
    316 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. 
    317    That was relevant to code that was here before.  */ 
    318 #if !defined (__STDC__) || !__STDC__ 
    319 /* gcc with -traditional declares the built-in strlen to return int, 
    320    and has done so at least since version 2.4.5. -- rms.  */ 
    321 extern int strlen (const char *); 
    322 #endif /* not __STDC__ */ 
    323 #endif /* __GNUC__ */ 
    324  
    325 #endif /* not __GNU_LIBRARY__ */ 
    326200 
    327201/* Handle permutation of arguments.  */ 
     
    334208static int last_nonopt; 
    335209 
    336 #ifdef _LIBC 
    337 /* Bash 2.0 gives us an environment variable containing flags 
    338    indicating ARGV elements that should not be considered arguments.  */ 
    339  
    340 /* Defined in getopt_init.c  */ 
    341 extern char *__getopt_nonoption_flags; 
    342  
    343 static int nonoption_flags_max_len; 
    344 static int nonoption_flags_len; 
    345  
    346 static int original_argc; 
    347 static char *const *original_argv; 
    348  
    349 extern pid_t __libc_pid; 
    350  
    351 /* Make sure the environment variable bash 2.0 puts in the environment 
    352    is valid for the getopt call we must make sure that the ARGV passed 
    353    to getopt is that one passed to the process.  */ 
    354 static void 
    355 __attribute__ ((unused)) 
    356 store_args_and_env (int argc, char *const *argv) 
    357 { 
    358   /* XXX This is no good solution.  We should rather copy the args so 
    359      that we can compare them later.  But we must not use malloc(3).  */ 
    360   original_argc = argc; 
    361   original_argv = argv; 
    362 } 
    363 text_set_element (__libc_subinit, store_args_and_env); 
    364  
    365 # define SWAP_FLAGS(ch1, ch2) \ 
    366   if (nonoption_flags_len > 0)                                                \ 
    367     {                                                                         \ 
    368       char __tmp = __getopt_nonoption_flags[ch1];                             \ 
    369       __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \ 
    370       __getopt_nonoption_flags[ch2] = __tmp;                                  \ 
    371     } 
    372 #else   /* !_LIBC */ 
    373 # define SWAP_FLAGS(ch1, ch2) 
    374 #endif  /* _LIBC */ 
     210#   define SWAP_FLAGS(ch1, ch2) 
    375211 
    376212/* Exchange two adjacent subsequences of ARGV. 
     
    383219   the new indices of the non-options in ARGV after they are moved.  */ 
    384220 
    385 #if defined (__STDC__) && __STDC__ 
    386 static void exchange (char **); 
    387 #endif 
    388  
    389221static void 
    390 exchange (argv) 
    391      char **argv; 
     222exchange (char **argv) 
    392223{ 
    393224  int bottom = first_nonopt; 
     
    400231     It leaves the longer segment in the right place overall, 
    401232     but it consists of two parts that need to be swapped next.  */ 
    402  
    403 #ifdef _LIBC 
    404   /* First make sure the handling of the `__getopt_nonoption_flags' 
    405      string can work normally.  Our top argument must be in the range 
    406      of the string.  */ 
    407   if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 
    408     { 
    409       /* We must extend the array.  The user plays games with us and 
    410          presents new arguments.  */ 
    411       char *new_str = malloc (top + 1); 
    412       if (new_str == NULL) 
    413         nonoption_flags_len = nonoption_flags_max_len = 0; 
    414       else 
    415         { 
    416           memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len); 
    417           memset (&new_str[nonoption_flags_max_len], '\0', 
    418                   top + 1 - nonoption_flags_max_len); 
    419           nonoption_flags_max_len = top + 1; 
    420           __getopt_nonoption_flags = new_str; 
    421         } 
    422     } 
    423 #endif 
    424233 
    425234  while (top > middle && middle > bottom) 
     
    469278/* Initialize the internal data when the first call is made.  */ 
    470279 
    471 #if defined (__STDC__) && __STDC__ 
    472 static const char *_getopt_initialize (int, char *const *, const char *); 
    473 #endif 
    474 static const char * 
    475 _getopt_initialize (argc, argv, optstring) 
    476      int argc; 
    477      char *const *argv; 
    478      const char *optstring; 
     280static const char *_getopt_initialize (int argc, char *const *argv,  
     281                                       const char *optstring) 
    479282{ 
     283    PJ_UNUSED_ARG(argc); 
     284    PJ_UNUSED_ARG(argv); 
     285 
    480286  /* Start processing options with ARGV-element 1 (since ARGV-element 0 
    481287     is the program name); the sequence of previously skipped 
     
    506312    ordering = PERMUTE; 
    507313 
    508 #ifdef _LIBC 
    509   if (posixly_correct == NULL 
    510       && argc == original_argc && argv == original_argv) 
    511     { 
    512       if (nonoption_flags_max_len == 0) 
    513         { 
    514           if (__getopt_nonoption_flags == NULL 
    515               || __getopt_nonoption_flags[0] == '\0') 
    516             nonoption_flags_max_len = -1; 
    517           else 
    518             { 
    519               const char *orig_str = __getopt_nonoption_flags; 
    520               int len = nonoption_flags_max_len = strlen (orig_str); 
    521               if (nonoption_flags_max_len < argc) 
    522                 nonoption_flags_max_len = argc; 
    523               __getopt_nonoption_flags = 
    524                 (char *) malloc (nonoption_flags_max_len); 
    525               if (__getopt_nonoption_flags == NULL) 
    526                 nonoption_flags_max_len = -1; 
    527               else 
    528                 { 
    529                   memcpy (__getopt_nonoption_flags, orig_str, len); 
    530                   memset (&__getopt_nonoption_flags[len], '\0', 
    531                           nonoption_flags_max_len - len); 
    532                 } 
    533             } 
    534         } 
    535       nonoption_flags_len = nonoption_flags_max_len; 
    536     } 
    537   else 
    538     nonoption_flags_len = 0; 
    539 #endif 
    540  
    541314  return optstring; 
    542315} 
     
    598371   long-named options.  */ 
    599372 
    600 int 
    601 _getopt_internal (argc, argv, optstring, longopts, longind, long_only) 
    602      int argc; 
    603      char *const *argv; 
    604      const char *optstring; 
    605      const struct option *longopts; 
    606      int *longind; 
    607      int long_only; 
     373static int 
     374_getopt_internal (int argc, char *const *argv, const char *optstring,  
     375                  const struct option *longopts, int *longind,  
     376                  int long_only) 
    608377{ 
    609378  optarg = NULL; 
     
    621390     from the shell indicating it is not an option.  The later information 
    622391     is only used when the used in the GNU libc.  */ 
    623 #ifdef _LIBC 
    624 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'        \ 
    625                      || (optind < nonoption_flags_len                         \ 
    626                          && __getopt_nonoption_flags[optind] == '1')) 
    627 #else 
    628392#define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 
    629 #endif 
    630393 
    631394  if (nextchar == NULL || *nextchar == '\0') 
     
    763526      if (ambig && !exact) 
    764527        { 
    765           if (opterr) 
    766             fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 
    767                      argv[0], argv[optind]); 
    768528          nextchar += strlen (nextchar); 
    769529          optind++; 
     
    784544              else 
    785545                { 
    786                   if (opterr) 
    787                    { 
    788                    if (argv[optind - 1][1] == '-') 
    789                     /* --option */ 
    790                     fprintf (stderr, 
    791                      _("%s: option `--%s' doesn't allow an argument\n"), 
    792                      argv[0], pfound->name); 
    793                    else 
    794                       {  
    795                     /* +option or -option */ 
    796                     fprintf (stderr, 
    797                      _("%s: option `%c%s' doesn't allow an argument\n"), 
    798                      argv[0], argv[optind - 1][0], pfound->name); 
    799                       } 
    800                     } 
    801546                  nextchar += strlen (nextchar); 
    802547 
     
    811556              else 
    812557                { 
    813                   if (opterr) 
    814                     fprintf (stderr, 
    815                            _("%s: option `%s' requires an argument\n"), 
    816                            argv[0], argv[optind - 1]); 
    817558                  nextchar += strlen (nextchar); 
    818559                  optopt = pfound->val; 
     
    838579          || my_index (optstring, *nextchar) == NULL) 
    839580        { 
    840           if (opterr) 
    841             { 
    842               if (argv[optind][1] == '-') 
    843                 /* --option */ 
    844                 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 
    845                          argv[0], nextchar); 
    846               else 
    847                 /* +option or -option */ 
    848                 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 
    849                          argv[0], argv[optind][0], nextchar); 
    850             } 
    851581          nextchar = (char *) ""; 
    852582          optind++; 
     
    868598    if (temp == NULL || c == ':') 
    869599      { 
    870         if (opterr) 
    871           { 
    872             if (posixly_correct) 
    873               /* 1003.2 specifies the format of this message.  */ 
    874               fprintf (stderr, _("%s: illegal option -- %c\n"), 
    875                        argv[0], c); 
    876             else 
    877               fprintf (stderr, _("%s: invalid option -- %c\n"), 
    878                        argv[0], c); 
    879           } 
    880600        optopt = c; 
    881601        return '?'; 
     
    902622        else if (optind == argc) 
    903623          { 
    904             if (opterr) 
    905               { 
    906                 /* 1003.2 specifies the format of this message.  */ 
    907                 fprintf (stderr, _("%s: option requires an argument -- %c\n"), 
    908                          argv[0], c); 
    909               } 
    910624            optopt = c; 
    911625            if (optstring[0] == ':') 
     
    951665        if (ambig && !exact) 
    952666          { 
    953             if (opterr) 
    954               fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 
    955                        argv[0], argv[optind]); 
    956667            nextchar += strlen (nextchar); 
    957668            optind++; 
     
    969680                else 
    970681                  { 
    971                     if (opterr) 
    972                       fprintf (stderr, _("\ 
    973 %s: option `-W %s' doesn't allow an argument\n"), 
    974                                argv[0], pfound->name); 
    975  
    976682                    nextchar += strlen (nextchar); 
    977683                    return '?'; 
     
    984690                else 
    985691                  { 
    986                     if (opterr) 
    987                       fprintf (stderr, 
    988                                _("%s: option `%s' requires an argument\n"), 
    989                                argv[0], argv[optind - 1]); 
    990692                    nextchar += strlen (nextchar); 
    991693                    return optstring[0] == ':' ? ':' : '?'; 
     
    1031733            else if (optind == argc) 
    1032734              { 
    1033                 if (opterr) 
    1034                   { 
    1035                     /* 1003.2 specifies the format of this message.  */ 
    1036                     fprintf (stderr, 
    1037                            _("%s: option requires an argument -- %c\n"), 
    1038                            argv[0], c); 
    1039                   } 
    1040735                optopt = c; 
    1041736                if (optstring[0] == ':') 
     
    1055750} 
    1056751 
    1057 #endif  /* Not ELIDE_CODE.  */ 
    1058  
    1059 #endif 
    1060  
    1061 #ifdef _MSC_VER 
    1062 # pragma warning(pop) 
    1063 #endif 
    1064  
Note: See TracChangeset for help on using the changeset viewer.