diff options
author | Zack Weinberg <zackw@panix.com> | 2017-03-25 11:24:24 -0400 |
---|---|---|
committer | Zack Weinberg <zackw@panix.com> | 2017-04-07 07:53:03 -0400 |
commit | 10a33cf8b403e3c031c5dd10a06b4a2a6489e48c (patch) | |
tree | dd0fe2c58f8a61cb33ee60fffd287f609745ac7a /posix | |
parent | 0f3be8721a86299600eae2d266934f661bf7c990 (diff) | |
download | glibc-10a33cf8b403e3c031c5dd10a06b4a2a6489e48c.zip glibc-10a33cf8b403e3c031c5dd10a06b4a2a6489e48c.tar.gz glibc-10a33cf8b403e3c031c5dd10a06b4a2a6489e48c.tar.bz2 |
getopt: eliminate __need_getopt by splitting up getopt.h.
__need_getopt is misnamed; what it really means is "we want only the
getopt features specified in POSIX, not the GNU extensions". Because
this code is shared with gnulib, it winds up being cleanest to split
getopt.h into *four* headers. getopt_core.h and getopt_ext.h will
be shared with gnulib, getopt_posix.h will be just for glibc, and
each project will have its own copy of getopt.h.
* posix/bits/getopt_core.h, posix/bits/getopt_ext.h:
New files, intended to be shared with gnulib.
* posix/bits/getopt_posix.h:
New file, not intended to be shared with gnulib.
* posix/getopt.h: Now just includes features.h,
bits/getopt_core.h, and bits/getopt_ext.h. Will
no longer be shared with gnulib.
* include/bits/getopt_core.h, include/bits/getopt_ext.h
* include/bits/getopt_posix.h: New wrappers.
* posix/Makefile: Install new headers.
* posix/unistd.h, libio/stdio.h:
Include bits/getopt_posix.h instead of getopt.h.
Diffstat (limited to 'posix')
-rw-r--r-- | posix/Makefile | 3 | ||||
-rw-r--r-- | posix/bits/getopt_core.h | 95 | ||||
-rw-r--r-- | posix/bits/getopt_ext.h | 76 | ||||
-rw-r--r-- | posix/bits/getopt_posix.h | 50 | ||||
-rw-r--r-- | posix/getopt.h | 182 | ||||
-rw-r--r-- | posix/unistd.h | 3 |
6 files changed, 228 insertions, 181 deletions
diff --git a/posix/Makefile b/posix/Makefile index 8e29eea..a6586ea 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -23,7 +23,8 @@ subdir := posix include ../Makeconfig headers := sys/utsname.h sys/times.h sys/wait.h sys/types.h unistd.h \ - glob.h regex.h wordexp.h fnmatch.h getopt.h \ + glob.h regex.h wordexp.h fnmatch.h \ + getopt.h bits/getopt_core.h bits/getopt_ext.h bits/getopt_posix.h \ bits/types.h bits/typesizes.h bits/pthreadtypes.h \ bits/posix1_lim.h bits/posix2_lim.h bits/posix_opt.h \ bits/local_lim.h tar.h bits/utsname.h bits/confname.h \ diff --git a/posix/bits/getopt_core.h b/posix/bits/getopt_core.h new file mode 100644 index 0000000..1e4dc25 --- /dev/null +++ b/posix/bits/getopt_core.h @@ -0,0 +1,95 @@ +/* Declarations for getopt (basic, portable features only). + Copyright (C) 1989-2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _GETOPT_CORE_H +#define _GETOPT_CORE_H 1 + +/* This header should not be used directly; include getopt.h or + unistd.h instead. Unlike most bits headers, it does not have + a protective #error, because the guard macro for getopt.h in + gnulib is not fixed. */ + +__BEGIN_DECLS + +/* For communication from 'getopt' to the caller. + When 'getopt' finds an option that takes an argument, + the argument value is returned here. + Also, when 'ordering' is RETURN_IN_ORDER, + each non-option ARGV-element is returned here. */ + +extern char *optarg; + +/* Index in ARGV of the next element to be scanned. + This is used for communication to and from the caller + and for communication between successive calls to 'getopt'. + + On entry to 'getopt', zero means this is the first call; initialize. + + When 'getopt' returns -1, this is the index of the first of the + non-option elements that the caller should itself scan. + + Otherwise, 'optind' communicates from one call to the next + how much of ARGV has been scanned so far. */ + +extern int optind; + +/* Callers store zero here to inhibit the error message 'getopt' prints + for unrecognized options. */ + +extern int opterr; + +/* Set to an option character which was unrecognized. */ + +extern int optopt; + +/* Get definitions and prototypes for functions to process the + arguments in ARGV (ARGC of them, minus the program name) for + options given in OPTS. + + Return the option character from OPTS just read. Return -1 when + there are no more options. For unrecognized options, or options + missing arguments, 'optopt' is set to the option letter, and '?' is + returned. + + The OPTS string is a list of characters which are recognized option + letters, optionally followed by colons, specifying that that letter + takes an argument, to be placed in 'optarg'. + + If a letter in OPTS is followed by two colons, its argument is + optional. This behavior is specific to the GNU 'getopt'. + + The argument '--' causes premature termination of argument + scanning, explicitly telling 'getopt' that there are no more + options. + + If OPTS begins with '-', then non-option arguments are treated as + arguments to the option '\1'. This behavior is specific to the GNU + 'getopt'. If OPTS begins with '+', or POSIXLY_CORRECT is set in + the environment, then do not permute arguments. + + For standards compliance, the 'argv' argument has the type + char *const *, but this is inaccurate; if argument permutation is + enabled, the argv array (not the strings it points to) must be + writable. */ + +extern int getopt (int ___argc, char *const *___argv, const char *__shortopts) + __THROW __nonnull ((2, 3)); + +__END_DECLS + +#endif /* getopt_core.h */ diff --git a/posix/bits/getopt_ext.h b/posix/bits/getopt_ext.h new file mode 100644 index 0000000..2382f88 --- /dev/null +++ b/posix/bits/getopt_ext.h @@ -0,0 +1,76 @@ +/* Declarations for getopt (GNU extensions). + Copyright (C) 1989-2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _GETOPT_EXT_H +#define _GETOPT_EXT_H 1 + +/* This header should not be used directly; include getopt.h instead. + Unlike most bits headers, it does not have a protective #error, + because the guard macro for getopt.h in gnulib is not fixed. */ + +__BEGIN_DECLS + +/* Describe the long-named options requested by the application. + The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector + of 'struct option' terminated by an element containing a name which is + zero. + + The field 'has_arg' is: + no_argument (or 0) if the option does not take an argument, + required_argument (or 1) if the option requires an argument, + optional_argument (or 2) if the option takes an optional argument. + + If the field 'flag' is not NULL, it points to a variable that is set + to the value given in the field 'val' when the option is found, but + left unchanged if the option is not found. + + To have a long-named option do something other than set an 'int' to + a compiled-in constant, such as set a value from 'optarg', set the + option's 'flag' field to zero and its 'val' field to a nonzero + value (the equivalent single-letter option character, if there is + one). For long options that have a zero 'flag' field, 'getopt' + returns the contents of the 'val' field. */ + +struct option +{ + const char *name; + /* has_arg can't be an enum because some compilers complain about + type mismatches in all the code that assumes it is an int. */ + int has_arg; + int *flag; + int val; +}; + +/* Names for the values of the 'has_arg' field of 'struct option'. */ + +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 + +extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv, + const char *__shortopts, + const struct option *__longopts, int *__longind) + __THROW __nonnull ((2, 3)); +extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv, + const char *__shortopts, + const struct option *__longopts, int *__longind) + __THROW __nonnull ((2, 3)); + +__END_DECLS + +#endif /* getopt_ext.h */ diff --git a/posix/bits/getopt_posix.h b/posix/bits/getopt_posix.h new file mode 100644 index 0000000..9436bb9 --- /dev/null +++ b/posix/bits/getopt_posix.h @@ -0,0 +1,50 @@ +/* Declarations for getopt (POSIX compatibility shim). + Copyright (C) 1989-2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _GETOPT_POSIX_H +#define _GETOPT_POSIX_H 1 + +#if !defined _UNISTD_H && !defined _STDIO_H +#error "Never include getopt_posix.h directly; use unistd.h instead." +#endif + +#include <bits/getopt_core.h> + +__BEGIN_DECLS + +#if defined __USE_POSIX2 && !defined __USE_POSIX_IMPLICITLY \ + && !defined __USE_GNU && !defined _GETOPT_H +/* GNU getopt has more functionality than POSIX getopt. When we are + explicitly conforming to POSIX and not GNU, and getopt.h (which is + not part of POSIX) has not been included, the extra functionality + is disabled. */ +# ifdef __REDIRECT +extern int __REDIRECT_NTH (getopt, (int ___argc, char *const *___argv, + const char *__shortopts), + __posix_getopt); +# else +extern int __posix_getopt (int ___argc, char *const *___argv, + const char *__shortopts) + __THROW __nonnull ((2, 3)); +# define getopt __posix_getopt +# endif +#endif + +__END_DECLS + +#endif /* getopt_posix.h */ diff --git a/posix/getopt.h b/posix/getopt.h index fa6aa6b..2b63865 100644 --- a/posix/getopt.h +++ b/posix/getopt.h @@ -17,168 +17,9 @@ <http://www.gnu.org/licenses/>. */ #ifndef _GETOPT_H +#define _GETOPT_H 1 -#ifndef __need_getopt -# define _GETOPT_H 1 -#endif - -/* If __GNU_LIBRARY__ is not already defined, either we are being used - standalone, or this is the first header included in the source file. - If we are being used with glibc, we need to include <features.h>, but - that does not exist if we are standalone. So: if __GNU_LIBRARY__ is - not defined, include <ctype.h>, which will pull in <features.h> for us - if it's from glibc. (Why ctype.h? It's guaranteed to exist and it - doesn't flood the namespace with stuff the way some other headers do.) */ -#if !defined __GNU_LIBRARY__ -# include <ctype.h> -#endif - -#ifndef __GNUC_PREREQ -# define __GNUC_PREREQ(maj, min) (0) -#endif - -#ifndef __THROW -# if defined __cplusplus && __GNUC_PREREQ (2,8) -# define __THROW throw () -# else -# define __THROW -# endif -#endif - -#ifndef __nonnull -# if __GNUC_PREREQ (3, 3) -# define __nonnull(params) __attribute__ ((__nonnull__ params)) -# else -# define __nonnull(params) -# endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* For communication from 'getopt' to the caller. - When 'getopt' finds an option that takes an argument, - the argument value is returned here. - Also, when 'ordering' is RETURN_IN_ORDER, - each non-option ARGV-element is returned here. */ - -extern char *optarg; - -/* Index in ARGV of the next element to be scanned. - This is used for communication to and from the caller - and for communication between successive calls to 'getopt'. - - On entry to 'getopt', zero means this is the first call; initialize. - - When 'getopt' returns -1, this is the index of the first of the - non-option elements that the caller should itself scan. - - Otherwise, 'optind' communicates from one call to the next - how much of ARGV has been scanned so far. */ - -extern int optind; - -/* Callers store zero here to inhibit the error message 'getopt' prints - for unrecognized options. */ - -extern int opterr; - -/* Set to an option character which was unrecognized. */ - -extern int optopt; - -#ifndef __need_getopt -/* Describe the long-named options requested by the application. - The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector - of 'struct option' terminated by an element containing a name which is - zero. - - The field 'has_arg' is: - no_argument (or 0) if the option does not take an argument, - required_argument (or 1) if the option requires an argument, - optional_argument (or 2) if the option takes an optional argument. - - If the field 'flag' is not NULL, it points to a variable that is set - to the value given in the field 'val' when the option is found, but - left unchanged if the option is not found. - - To have a long-named option do something other than set an 'int' to - a compiled-in constant, such as set a value from 'optarg', set the - option's 'flag' field to zero and its 'val' field to a nonzero - value (the equivalent single-letter option character, if there is - one). For long options that have a zero 'flag' field, 'getopt' - returns the contents of the 'val' field. */ - -struct option -{ - const char *name; - /* has_arg can't be an enum because some compilers complain about - type mismatches in all the code that assumes it is an int. */ - int has_arg; - int *flag; - int val; -}; - -/* Names for the values of the 'has_arg' field of 'struct option'. */ - -# define no_argument 0 -# define required_argument 1 -# define optional_argument 2 -#endif /* need getopt */ - - -/* Get definitions and prototypes for functions to process the - arguments in ARGV (ARGC of them, minus the program name) for - options given in OPTS. - - Return the option character from OPTS just read. Return -1 when - there are no more options. For unrecognized options, or options - missing arguments, 'optopt' is set to the option letter, and '?' is - returned. - - The OPTS string is a list of characters which are recognized option - letters, optionally followed by colons, specifying that that letter - takes an argument, to be placed in 'optarg'. - - If a letter in OPTS is followed by two colons, its argument is - optional. This behavior is specific to the GNU 'getopt'. - - The argument '--' causes premature termination of argument - scanning, explicitly telling 'getopt' that there are no more - options. - - If OPTS begins with '-', then non-option arguments are treated as - arguments to the option '\1'. This behavior is specific to the GNU - 'getopt'. If OPTS begins with '+', or POSIXLY_CORRECT is set in - the environment, then do not permute arguments. - - For standards compliance, the 'argv' argument has the type - char *const *, but this is inaccurate; if argument permutation is - enabled, the argv array (not the strings it points to) must be - writable. */ - -extern int getopt (int ___argc, char *const *___argv, const char *__shortopts) - __THROW __nonnull ((2, 3)); - -#if defined __need_getopt && defined __USE_POSIX2 \ - && !defined __USE_POSIX_IMPLICITLY && !defined __USE_GNU -/* The GNU getopt has more functionality than the standard version. The - additional functionality can be disable at runtime. This redirection - helps to also do this at runtime. */ -# ifdef __REDIRECT - extern int __REDIRECT_NTH (getopt, (int ___argc, char *const *___argv, - const char *__shortopts), - __posix_getopt); -# else -extern int __posix_getopt (int ___argc, char *const *___argv, - const char *__shortopts) - __THROW __nonnull ((2, 3)); -# define getopt __posix_getopt -# endif -#endif - -#ifndef __need_getopt +#include <features.h> /* The type of the 'argv' argument to getopt_long and getopt_long_only is properly 'char **', since both functions may write to the array @@ -189,22 +30,7 @@ extern int __posix_getopt (int ___argc, char *const *___argv, # define __getopt_argv_const const #endif -extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv, - const char *__shortopts, - const struct option *__longopts, int *__longind) - __THROW __nonnull ((2, 3)); -extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv, - const char *__shortopts, - const struct option *__longopts, int *__longind) - __THROW __nonnull ((2, 3)); - -#endif - -#ifdef __cplusplus -} -#endif - -/* Make sure we later can get all the definitions and declarations. */ -#undef __need_getopt +#include <bits/getopt_core.h> +#include <bits/getopt_ext.h> #endif /* getopt.h */ diff --git a/posix/unistd.h b/posix/unistd.h index fa2492e..32b0f48 100644 --- a/posix/unistd.h +++ b/posix/unistd.h @@ -869,8 +869,7 @@ extern int setlogin (const char *__name) __THROW __nonnull ((1)); /* Get definitions and prototypes for functions to process the arguments in ARGV (ARGC of them, minus the program name) for options given in OPTS. */ -# define __need_getopt -# include <getopt.h> +# include <bits/getopt_posix.h> #endif |