00001 /* Declarations for getopt. 00002 Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 2000 00003 Free Software Foundation, Inc. 00004 00005 NOTE: The canonical source of this file is maintained with the GNU C Library. 00006 Bugs can be reported to bug-glibc@gnu.org. 00007 00008 This program is free software; you can redistribute it and/or modify it 00009 under the terms of the GNU General Public License as published by the 00010 Free Software Foundation; either version 2, or (at your option) any 00011 later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00021 USA. */ 00022 00023 #ifndef _GETOPT_H 00024 #define _GETOPT_H 1 00025 00026 #include <config.h> 00027 00028 #if HAVE_UNISTD_H 00029 /* Declares getopt, if present */ 00030 #include <unistd.h> 00031 #endif 00032 00033 #ifdef __cplusplus 00034 extern "C" { 00035 #endif 00036 00037 /* We're building this with a C++ compiler, essentially. Such 00038 compilers are not required to define __STDC__, but the path we 00039 should follow, below, is indeed that marked by __STDC__. We don't 00040 want to force a definition of __STDC__ (though that works), because 00041 (a) that feels bad, and (b) some compilers perfectly reasonable 00042 complain bitterly about it. So define THIS_IS__STDC__, and replace 00043 occurrences of __STDC__ throughout with that. 00044 00045 That means that all of the occurrences of THIS_IS__STDC__ in this 00046 file and in getopt_long.c are redundant, but I'm leaving them here 00047 in case it becomes necessary to do cleverer things with it than 00048 simply define it to be 1, and also as a sort of warped documentation. */ 00049 #define THIS_IS__STDC__ 1 00050 00051 #if !HAVE_DECL_GETOPT 00052 /* For communication from `getopt' to the caller. 00053 When `getopt' finds an option that takes an argument, 00054 the argument value is returned here. 00055 Also, when `ordering' is RETURN_IN_ORDER, 00056 each non-option ARGV-element is returned here. */ 00057 00058 extern char *optarg; 00059 00060 /* Index in ARGV of the next element to be scanned. 00061 This is used for communication to and from the caller 00062 and for communication between successive calls to `getopt'. 00063 00064 On entry to `getopt', zero means this is the first call; initialize. 00065 00066 When `getopt' returns -1, this is the index of the first of the 00067 non-option elements that the caller should itself scan. 00068 00069 Otherwise, `optind' communicates from one call to the next 00070 how much of ARGV has been scanned so far. */ 00071 00072 extern int optind; 00073 00074 /* Callers store zero here to inhibit the error message `getopt' prints 00075 for unrecognized options. */ 00076 00077 extern int opterr; 00078 00079 /* Set to an option character which was unrecognized. */ 00080 00081 extern int optopt; 00082 00083 #endif /* ifndef HAVE_DECL_GETOPT */ 00084 00085 #if !HAVE_DECL_GETOPT_LONG 00086 /* Describe the long-named options requested by the application. 00087 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector 00088 of `struct option' terminated by an element containing a name which is 00089 zero. 00090 00091 The field `has_arg' is: 00092 no_argument (or 0) if the option does not take an argument, 00093 required_argument (or 1) if the option requires an argument, 00094 optional_argument (or 2) if the option takes an optional argument. 00095 00096 If the field `flag' is not NULL, it points to a variable that is set 00097 to the value given in the field `val' when the option is found, but 00098 left unchanged if the option is not found. 00099 00100 To have a long-named option do something other than set an `int' to 00101 a compiled-in constant, such as set a value from `optarg', set the 00102 option's `flag' field to zero and its `val' field to a nonzero 00103 value (the equivalent single-letter option character, if there is 00104 one). For long options that have a zero `flag' field, `getopt' 00105 returns the contents of the `val' field. */ 00106 00107 struct option 00108 { 00109 #if defined (THIS_IS__STDC__) && THIS_IS__STDC__ 00110 const char *name; 00111 #else 00112 char *name; 00113 #endif 00114 /* has_arg can't be an enum because some compilers complain about 00115 type mismatches in all the code that assumes it is an int. */ 00116 int has_arg; 00117 int *flag; 00118 int val; 00119 }; 00120 00121 /* Names for the values of the `has_arg' field of `struct option'. */ 00122 00123 #define no_argument 0 00124 #define required_argument 1 00125 #define optional_argument 2 00126 00127 #endif /* #if !HAVE_DECL_GETOPT_LONG */ 00128 00129 #if defined (THIS_IS__STDC__) && THIS_IS__STDC__ 00130 /* HAVE_DECL_* is a three-state macro: undefined, 0 or 1. If it is 00131 undefined, we haven't run the autoconf check so provide the 00132 declaration without arguments. If it is 0, we checked and failed 00133 to find the declaration so provide a fully prototyped one. If it 00134 is 1, we found it so don't provide any declaration at all. */ 00135 #if defined (__GNU_LIBRARY__) || (defined (HAVE_DECL_GETOPT) && !HAVE_DECL_GETOPT) 00136 /* Many other libraries have conflicting prototypes for getopt, with 00137 differences in the consts, in stdlib.h. To avoid compilation 00138 errors, only prototype getopt for the GNU C library. */ 00139 extern int getopt (int argc, char *const *argv, const char *shortopts); 00140 #else /* not __GNU_LIBRARY__ */ 00141 # if !defined (HAVE_DECL_GETOPT) 00142 extern int getopt (); 00143 # endif 00144 #endif /* __GNU_LIBRARY__ */ 00145 #if !HAVE_DECL_GETOPT_LONG 00146 extern int getopt_long (int argc, char *const *argv, const char *shortopts, 00147 const struct option *longopts, int *longind); 00148 extern int getopt_long_only (int argc, char *const *argv, 00149 const char *shortopts, 00150 const struct option *longopts, int *longind); 00151 00152 /* Internal only. Users should not call this directly. */ 00153 extern int _getopt_internal (int argc, char *const *argv, 00154 const char *shortopts, 00155 const struct option *longopts, int *longind, 00156 int long_only); 00157 #endif /* HAVE_DECL_GETOPT_LONG */ 00158 #else /* not THIS_IS__STDC__ */ 00159 #if !HAVE_DECL_GETOPT 00160 extern int getopt (); 00161 #endif /* HAVE_DECL_GETOPT */ 00162 #if !HAVE_DECL_GETOPT_LONG 00163 extern int getopt_long (); 00164 extern int getopt_long_only (); 00165 00166 extern int _getopt_internal (); 00167 #endif /* HAVE_DECL_GETOPT_LONG */ 00168 #endif /* THIS_IS__STDC__ */ 00169 00170 00171 #ifdef __cplusplus 00172 } 00173 #endif 00174 00175 #endif /* getopt.h */