Panda3D

gnu_getopt1.c

00001 /* getopt_long and getopt_long_only entry points for GNU getopt.
00002    Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
00003         Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify it
00006    under the terms of the GNU General Public License as published by the
00007    Free Software Foundation; either version 2, or (at your option) any
00008    later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.  */
00014 
00015 
00016 #include "dtoolbase.h"
00017 
00018 #ifndef HAVE_GETOPT_LONG_ONLY
00019 
00020 #ifdef WIN32_VC
00021 /* This file seems particularly egregious with this particular warning,
00022    but it's not clear why.  Disable. */
00023 /* C4028: formal parameter N different from declaration */
00024 #pragma warning (disable : 4028)
00025 #endif
00026 
00027 #include "gnu_getopt.h"
00028 
00029 #ifndef __STDC__
00030 /* This is a separate conditional since some stdc systems
00031    reject `defined (const)'.  */
00032 #ifndef const
00033 #define const
00034 #endif
00035 #endif
00036 
00037 #include <stdio.h>
00038 
00039 /* Comment out all this code if we are using the GNU C Library, and are not
00040    actually compiling the library itself.  This code is part of the GNU C
00041    Library, but also included in many other GNU distributions.  Compiling
00042    and linking in this code is a waste when using the GNU C library
00043    (especially if it is a shared library).  Rather than having every GNU
00044    program understand `configure --with-gnu-libc' and omit the object files,
00045    it is simpler to just do this in the source for each such file.  */
00046 
00047 /* Actually, we may need to compile this anyway, even on a gnu platform. */
00048 #if 1 /* defined (_LIBC) || !defined (__GNU_LIBRARY__) */
00049 
00050 
00051 /* This needs to come after some library #include
00052    to get __GNU_LIBRARY__ defined.  */
00053 #ifdef __GNU_LIBRARY__
00054 #include <stdlib.h>
00055 #else
00056 char *getenv ();
00057 #endif
00058 
00059 #ifndef NULL
00060 #define NULL 0
00061 #endif
00062 
00063 int
00064 getopt_long (int argc, 
00065              char *const *argv, 
00066              const char *options, 
00067              const struct option *long_options, 
00068              int *opt_index) {
00069   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00070 }
00071 
00072 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00073    If an option that starts with '-' (not '--') doesn't match a long option,
00074    but does match a short option, it is parsed as a short option
00075    instead.  */
00076 
00077 int
00078 getopt_long_only (int argc, 
00079                   char *const *argv, 
00080                   const char *options, 
00081                   const struct option *long_options, 
00082                   int *opt_index) {
00083   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00084 }
00085 
00086 
00087 #endif  /* _LIBC or not __GNU_LIBRARY__.  */
00088 
00089 #ifdef TEST
00090 
00091 #include <stdio.h>
00092 
00093 int
00094 main (int argc, char **argv) {
00095   int c;
00096   int digit_optind = 0;
00097 
00098   while (1)
00099     {
00100       int this_option_optind = optind ? optind : 1;
00101       int option_index = 0;
00102       static struct option long_options[] =
00103       {
00104         {"add", 1, 0, 0},
00105         {"append", 0, 0, 0},
00106         {"delete", 1, 0, 0},
00107         {"verbose", 0, 0, 0},
00108         {"create", 0, 0, 0},
00109         {"file", 1, 0, 0},
00110         {0, 0, 0, 0}
00111       };
00112 
00113       c = getopt_long (argc, argv, "abc:d:0123456789",
00114                        long_options, &option_index);
00115       if (c == EOF)
00116         break;
00117 
00118       switch (c)
00119         {
00120         case 0:
00121           printf ("option %s", long_options[option_index].name);
00122           if (optarg)
00123             printf (" with arg %s", optarg);
00124           printf ("\n");
00125           break;
00126 
00127         case '0':
00128         case '1':
00129         case '2':
00130         case '3':
00131         case '4':
00132         case '5':
00133         case '6':
00134         case '7':
00135         case '8':
00136         case '9':
00137           if (digit_optind != 0 && digit_optind != this_option_optind)
00138             printf ("digits occur in two different argv-elements.\n");
00139           digit_optind = this_option_optind;
00140           printf ("option %c\n", c);
00141           break;
00142 
00143         case 'a':
00144           printf ("option a\n");
00145           break;
00146 
00147         case 'b':
00148           printf ("option b\n");
00149           break;
00150 
00151         case 'c':
00152           printf ("option c with value `%s'\n", optarg);
00153           break;
00154 
00155         case 'd':
00156           printf ("option d with value `%s'\n", optarg);
00157           break;
00158 
00159         case '?':
00160           break;
00161 
00162         default:
00163           printf ("?? getopt returned character code 0%o ??\n", c);
00164         }
00165     }
00166 
00167   if (optind < argc)
00168     {
00169       printf ("non-option ARGV-elements: ");
00170       while (optind < argc)
00171         printf ("%s ", argv[optind++]);
00172       printf ("\n");
00173     }
00174 
00175   exit (0);
00176 }
00177 
00178 #endif /* TEST */
00179 
00180 #endif /* HAVE_GETOPT_LONG_ONLY */
 All Classes Functions Variables Enumerations