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,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 General Public License as published by
00008    the Free Software Foundation; either version 2, 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 General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License along
00017    with this program; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 int
00044 getopt_long (int argc, char *const *argv, const char *options,
00045             const struct option *long_options, int *opt_index)
00046 {
00047   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00048 }
00049 
00050 int
00051 _getopt_long_r (int argc, char *const *argv, const char *options,
00052               const struct option *long_options, int *opt_index,
00053               struct _getopt_data *d)
00054 {
00055   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00056                           0, d);
00057 }
00058 
00059 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00060    If an option that starts with '-' (not '--') doesn't match a long option,
00061    but does match a short option, it is parsed as a short option
00062    instead.  */
00063 
00064 int
00065 getopt_long_only (int argc, char *const *argv, const char *options,
00066                 const struct option *long_options, int *opt_index)
00067 {
00068   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00069 }
00070 
00071 int
00072 _getopt_long_only_r (int argc, char *const *argv, const char *options,
00073                    const struct option *long_options, int *opt_index,
00074                    struct _getopt_data *d)
00075 {
00076   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00077                           1, d);
00078 }
00079 
00080 
00081 #ifdef TEST
00082 
00083 #include <stdio.h>
00084 
00085 int
00086 main (int argc, char **argv)
00087 {
00088   int c;
00089   int digit_optind = 0;
00090 
00091   while (1)
00092     {
00093       int this_option_optind = optind ? optind : 1;
00094       int option_index = 0;
00095       static struct option long_options[] =
00096       {
00097        {"add", 1, 0, 0},
00098        {"append", 0, 0, 0},
00099        {"delete", 1, 0, 0},
00100        {"verbose", 0, 0, 0},
00101        {"create", 0, 0, 0},
00102        {"file", 1, 0, 0},
00103        {0, 0, 0, 0}
00104       };
00105 
00106       c = getopt_long (argc, argv, "abc:d:0123456789",
00107                      long_options, &option_index);
00108       if (c == -1)
00109        break;
00110 
00111       switch (c)
00112        {
00113        case 0:
00114          printf ("option %s", long_options[option_index].name);
00115          if (optarg)
00116            printf (" with arg %s", optarg);
00117          printf ("\n");
00118          break;
00119 
00120        case '0':
00121        case '1':
00122        case '2':
00123        case '3':
00124        case '4':
00125        case '5':
00126        case '6':
00127        case '7':
00128        case '8':
00129        case '9':
00130          if (digit_optind != 0 && digit_optind != this_option_optind)
00131            printf ("digits occur in two different argv-elements.\n");
00132          digit_optind = this_option_optind;
00133          printf ("option %c\n", c);
00134          break;
00135 
00136        case 'a':
00137          printf ("option a\n");
00138          break;
00139 
00140        case 'b':
00141          printf ("option b\n");
00142          break;
00143 
00144        case 'c':
00145          printf ("option c with value `%s'\n", optarg);
00146          break;
00147 
00148        case 'd':
00149          printf ("option d with value `%s'\n", optarg);
00150          break;
00151 
00152        case '?':
00153          break;
00154 
00155        default:
00156          printf ("?? getopt returned character code 0%o ??\n", c);
00157        }
00158     }
00159 
00160   if (optind < argc)
00161     {
00162       printf ("non-option ARGV-elements: ");
00163       while (optind < argc)
00164        printf ("%s ", argv[optind++]);
00165       printf ("\n");
00166     }
00167 
00168   exit (0);
00169 }
00170 
00171 #endif /* TEST */