getopt1.c

00001 /* getopt_long and getopt_long_only entry points for GNU getopt.
00002    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
00003      Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU Lesser General Public License as published by
00008    the Free Software Foundation; either version 2.1, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public License along
00017    with this program; if not, write to the Free Software Foundation,
00018    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00019 
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023 
00024 #ifdef _LIBC
00025 # include <getopt.h>
00026 #else
00027 # include "getopt.h"
00028 #endif
00029 #include "getopt_int.h"
00030 
00031 #include <stdio.h>
00032 
00033 /* This needs to come after some library #include
00034    to get __GNU_LIBRARY__ defined.  */
00035 #ifdef __GNU_LIBRARY__
00036 #include <stdlib.h>
00037 #endif
00038 
00039 #ifndef NULL
00040 #define NULL 0
00041 #endif
00042 
00043 #ifndef __getopt_argv_const
00044 # define __getopt_argv_const const
00045 #endif
00046 
00047 int
00048 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
00049              const struct option *long_options, int *opt_index)
00050 {
00051   return _getopt_internal (argc, (char **) argv, options, long_options,
00052                            opt_index, 0, 0);
00053 }
00054 
00055 int
00056 _getopt_long_r (int argc, char **argv, const char *options,
00057                 const struct option *long_options, int *opt_index,
00058                 struct _getopt_data *d)
00059 {
00060   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00061                              0, 0, d);
00062 }
00063 
00064 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00065    If an option that starts with '-' (not '--') doesn't match a long option,
00066    but does match a short option, it is parsed as a short option
00067    instead.  */
00068 
00069 int
00070 getopt_long_only (int argc, char *__getopt_argv_const *argv,
00071                   const char *options,
00072                   const struct option *long_options, int *opt_index)
00073 {
00074   return _getopt_internal (argc, (char **) argv, options, long_options,
00075                            opt_index, 1, 0);
00076 }
00077 
00078 int
00079 _getopt_long_only_r (int argc, char **argv, const char *options,
00080                      const struct option *long_options, int *opt_index,
00081                      struct _getopt_data *d)
00082 {
00083   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00084                              1, 0, d);
00085 }
00086 
00087 
00088 #ifdef TEST
00089 
00090 #include <stdio.h>
00091 
00092 int
00093 main (int argc, char **argv)
00094 {
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 == -1)
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 */

Generated on Wed Jul 12 17:53:19 2006 for WvStreams by  doxygen 1.4.7