Back to index

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