Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

dtool/src/dtoolutil/gnu_getopt1.c

Go to the documentation of this file.
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 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
00048 
00049 
00050 /* This needs to come after some library #include
00051    to get __GNU_LIBRARY__ defined.  */
00052 #ifdef __GNU_LIBRARY__
00053 #include <stdlib.h>
00054 #else
00055 char *getenv ();
00056 #endif
00057 
00058 #ifndef NULL
00059 #define NULL 0
00060 #endif
00061 
00062 int
00063 getopt_long (argc, argv, options, long_options, opt_index)
00064      int argc;
00065      char *const *argv;
00066      const char *options;
00067      const struct option *long_options;
00068      int *opt_index;
00069 {
00070   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00071 }
00072 
00073 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00074    If an option that starts with '-' (not '--') doesn't match a long option,
00075    but does match a short option, it is parsed as a short option
00076    instead.  */
00077 
00078 int
00079 getopt_long_only (argc, argv, options, long_options, opt_index)
00080      int argc;
00081      char *const *argv;
00082      const char *options;
00083      const struct option *long_options;
00084      int *opt_index;
00085 {
00086   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00087 }
00088 
00089 
00090 #endif  /* _LIBC or not __GNU_LIBRARY__.  */
00091 
00092 #ifdef TEST
00093 
00094 #include <stdio.h>
00095 
00096 int
00097 main (argc, argv)
00098      int argc;
00099      char **argv;
00100 {
00101   int c;
00102   int digit_optind = 0;
00103 
00104   while (1)
00105     {
00106       int this_option_optind = optind ? optind : 1;
00107       int option_index = 0;
00108       static struct option long_options[] =
00109       {
00110         {"add", 1, 0, 0},
00111         {"append", 0, 0, 0},
00112         {"delete", 1, 0, 0},
00113         {"verbose", 0, 0, 0},
00114         {"create", 0, 0, 0},
00115         {"file", 1, 0, 0},
00116         {0, 0, 0, 0}
00117       };
00118 
00119       c = getopt_long (argc, argv, "abc:d:0123456789",
00120                        long_options, &option_index);
00121       if (c == EOF)
00122         break;
00123 
00124       switch (c)
00125         {
00126         case 0:
00127           printf ("option %s", long_options[option_index].name);
00128           if (optarg)
00129             printf (" with arg %s", optarg);
00130           printf ("\n");
00131           break;
00132 
00133         case '0':
00134         case '1':
00135         case '2':
00136         case '3':
00137         case '4':
00138         case '5':
00139         case '6':
00140         case '7':
00141         case '8':
00142         case '9':
00143           if (digit_optind != 0 && digit_optind != this_option_optind)
00144             printf ("digits occur in two different argv-elements.\n");
00145           digit_optind = this_option_optind;
00146           printf ("option %c\n", c);
00147           break;
00148 
00149         case 'a':
00150           printf ("option a\n");
00151           break;
00152 
00153         case 'b':
00154           printf ("option b\n");
00155           break;
00156 
00157         case 'c':
00158           printf ("option c with value `%s'\n", optarg);
00159           break;
00160 
00161         case 'd':
00162           printf ("option d with value `%s'\n", optarg);
00163           break;
00164 
00165         case '?':
00166           break;
00167 
00168         default:
00169           printf ("?? getopt returned character code 0%o ??\n", c);
00170         }
00171     }
00172 
00173   if (optind < argc)
00174     {
00175       printf ("non-option ARGV-elements: ");
00176       while (optind < argc)
00177         printf ("%s ", argv[optind++]);
00178       printf ("\n");
00179     }
00180 
00181   exit (0);
00182 }
00183 
00184 #endif /* TEST */
00185 
00186 #endif /* HAVE_GETOPT_LONG_ONLY */

Generated on Thu May 1 22:13:00 2003 for DTool by doxygen1.3