Back to index

plt-scheme  4.2.1
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, 1994
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    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software
00017    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #endif
00022 
00023 #include "getopt.h"
00024 
00025 #if !defined (__STDC__) || !__STDC__
00026 /* This is a separate conditional since some stdc systems
00027    reject `defined (const)'.  */
00028 #ifndef const
00029 #define const
00030 #endif
00031 #endif
00032 
00033 #include <stdio.h>
00034 
00035 /* Comment out all this code if we are using the GNU C Library, and are not
00036    actually compiling the library itself.  This code is part of the GNU C
00037    Library, but also included in many other GNU distributions.  Compiling
00038    and linking in this code is a waste when using the GNU C library
00039    (especially if it is a shared library).  Rather than having every GNU
00040    program understand `configure --with-gnu-libc' and omit the object files,
00041    it is simpler to just do this in the source for each such file.  */
00042 
00043 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
00044 
00045 
00046 /* This needs to come after some library #include
00047    to get __GNU_LIBRARY__ defined.  */
00048 #ifdef __GNU_LIBRARY__
00049 #include <stdlib.h>
00050 #else
00051 char *getenv ();
00052 #endif
00053 
00054 #ifndef       NULL
00055 #define NULL 0
00056 #endif
00057 
00058 int
00059 getopt_long (argc, argv, options, long_options, opt_index)
00060      int argc;
00061      char *const *argv;
00062      const char *options;
00063      const struct option *long_options;
00064      int *opt_index;
00065 {
00066   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00067 }
00068 
00069 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00070    If an option that starts with '-' (not '--') doesn't match a long option,
00071    but does match a short option, it is parsed as a short option
00072    instead.  */
00073 
00074 int
00075 getopt_long_only (argc, argv, options, long_options, opt_index)
00076      int argc;
00077      char *const *argv;
00078      const char *options;
00079      const struct option *long_options;
00080      int *opt_index;
00081 {
00082   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00083 }
00084 
00085 
00086 #endif /* _LIBC or not __GNU_LIBRARY__.  */
00087 
00088 #ifdef TEST
00089 
00090 #include <stdio.h>
00091 
00092 int
00093 main (argc, argv)
00094      int argc;
00095      char **argv;
00096 {
00097   int c;
00098   int digit_optind = 0;
00099 
00100   while (1)
00101     {
00102       int this_option_optind = optind ? optind : 1;
00103       int option_index = 0;
00104       static struct option long_options[] =
00105       {
00106        {"add", 1, 0, 0},
00107        {"append", 0, 0, 0},
00108        {"delete", 1, 0, 0},
00109        {"verbose", 0, 0, 0},
00110        {"create", 0, 0, 0},
00111        {"file", 1, 0, 0},
00112        {0, 0, 0, 0}
00113       };
00114 
00115       c = getopt_long (argc, argv, "abc:d:0123456789",
00116                      long_options, &option_index);
00117       if (c == EOF)
00118        break;
00119 
00120       switch (c)
00121        {
00122        case 0:
00123          printf ("option %s", long_options[option_index].name);
00124          if (optarg)
00125            printf (" with arg %s", optarg);
00126          printf ("\n");
00127          break;
00128 
00129        case '0':
00130        case '1':
00131        case '2':
00132        case '3':
00133        case '4':
00134        case '5':
00135        case '6':
00136        case '7':
00137        case '8':
00138        case '9':
00139          if (digit_optind != 0 && digit_optind != this_option_optind)
00140            printf ("digits occur in two different argv-elements.\n");
00141          digit_optind = this_option_optind;
00142          printf ("option %c\n", c);
00143          break;
00144 
00145        case 'a':
00146          printf ("option a\n");
00147          break;
00148 
00149        case 'b':
00150          printf ("option b\n");
00151          break;
00152 
00153        case 'c':
00154          printf ("option c with value `%s'\n", optarg);
00155          break;
00156 
00157        case 'd':
00158          printf ("option d with value `%s'\n", optarg);
00159          break;
00160 
00161        case '?':
00162          break;
00163 
00164        default:
00165          printf ("?? getopt returned character code 0%o ??\n", c);
00166        }
00167     }
00168 
00169   if (optind < argc)
00170     {
00171       printf ("non-option ARGV-elements: ");
00172       while (optind < argc)
00173        printf ("%s ", argv[optind++]);
00174       printf ("\n");
00175     }
00176 
00177   exit (0);
00178 }
00179 
00180 #endif /* TEST */