summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorwl <wl>2005-06-23 11:33:00 +0000
committerwl <wl>2005-06-23 11:33:00 +0000
commit6ca33b817111c819a5ce390026a892e964a073ce (patch)
tree4b1497eb6636b50e846714456b4489eb8af82a52 /src/include
parent0ef9b05c12ad681d7d81a4a894a596b64f5ef721 (diff)
downloadgroff-6ca33b817111c819a5ce390026a892e964a073ce.tar.gz
Another round in fixing getopt problems. Hopefully the last one!
This time we use the getopt stuff from gnulib and define a prefix unconditionally so that there are no collisions with any other getopt implementations. This is a slight waste of space in case we already use a GNU getopt implementation, but on the other hand it really simplifies life. * src/include/getopt.h: Use gnulib version. * src/include/getopt_int.h: Use gnulib version. * src/include/groff-getopt.h: Removed. Obsolete now. * src/include/lib.h: Regarding getopt, don't handle any platform specially; just include getopt.h. (__GETOPT_PREFIX): Define (before getopt.h). * src/include/Makefile.sub (HDRS): Updated. * src/libs/libgroff/getopt1.c, src/libs/libgroff/getopt.c: Use gnulib version. * src/libs/libgroff/Makefile.sub (EXTRA_CFLAGS): Set __GETOPT_PREFIX. * src/utils/pfbtops/pfbtops.c, src/utils/xtotroff/xtotroff.c (__GETOPT_PREFIX): Define (before getopt.h).
Diffstat (limited to 'src/include')
-rw-r--r--src/include/Makefile.sub1
-rw-r--r--src/include/getopt.h92
-rw-r--r--src/include/getopt_int.h39
-rw-r--r--src/include/groff-getopt.h71
-rw-r--r--src/include/lib.h14
5 files changed, 92 insertions, 125 deletions
diff --git a/src/include/Makefile.sub b/src/include/Makefile.sub
index a4dfb459..58e1ac98 100644
--- a/src/include/Makefile.sub
+++ b/src/include/Makefile.sub
@@ -13,7 +13,6 @@ HDRS=\
getopt.h \
getopt_int.h \
gettext.h \
- groff-getopt.h \
htmlhint.h \
index.h \
lib.h \
diff --git a/src/include/getopt.h b/src/include/getopt.h
index b7a026c5..a4e78cb0 100644
--- a/src/include/getopt.h
+++ b/src/include/getopt.h
@@ -3,20 +3,19 @@
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.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
- The GNU C Library is distributed in the hope that it will be useful,
+ This program 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.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU 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, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifndef _GETOPT_H
@@ -24,6 +23,64 @@
# define _GETOPT_H 1
#endif
+/* Standalone applications should #define __GETOPT_PREFIX to an
+ identifier that prefixes the external functions and variables
+ defined in this header. When this happens, include the
+ headers that might declare getopt so that they will not cause
+ confusion if included after this file. Then systematically rename
+ identifiers so that they do not collide with the system functions
+ and variables. Renaming avoids problems with some compilers and
+ linkers. */
+#if defined __GETOPT_PREFIX && !defined __need_getopt
+# include <stdlib.h>
+# include <stdio.h>
+# if HAVE_UNISTD_H
+# include <unistd.h>
+# endif
+# undef __need_getopt
+# undef getopt
+# undef getopt_long
+# undef getopt_long_only
+# undef optarg
+# undef opterr
+# undef optind
+# undef optopt
+# define __GETOPT_CONCAT(x, y) x ## y
+# define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
+# define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
+# define getopt __GETOPT_ID (getopt)
+# define getopt_long __GETOPT_ID (getopt_long)
+# define getopt_long_only __GETOPT_ID (getopt_long_only)
+# define optarg __GETOPT_ID (optarg)
+# define opterr __GETOPT_ID (opterr)
+# define optind __GETOPT_ID (optind)
+# define optopt __GETOPT_ID (optopt)
+#endif
+
+/* Standalone applications get correct prototypes for getopt_long and
+ getopt_long_only; they declare "char **argv". libc uses prototypes
+ with "char *const *argv" that are incorrect because getopt_long and
+ getopt_long_only can permute argv; this is required for backward
+ compatibility (e.g., for LSB 2.0.1).
+
+ This used to be `#if defined __GETOPT_PREFIX && !defined __need_getopt',
+ but it caused redefinition warnings if both unistd.h and getopt.h were
+ included, since unistd.h includes getopt.h having previously defined
+ __need_getopt.
+
+ The only place where __getopt_argv_const is used is in definitions
+ of getopt_long and getopt_long_only below, but these are visible
+ only if __need_getopt is not defined, so it is quite safe to rewrite
+ the conditional as follows:
+*/
+#if !defined __need_getopt
+# if defined __GETOPT_PREFIX
+# define __getopt_argv_const /* empty */
+# else
+# define __getopt_argv_const const
+# endif
+#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
@@ -90,7 +147,7 @@ extern int optopt;
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.
+ 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
@@ -145,22 +202,15 @@ struct option
arguments to the option '\0'. This behavior is specific to the GNU
`getopt'. */
-#ifdef __GNU_LIBRARY__
-/* Many other libraries have conflicting prototypes for getopt, with
- differences in the consts, in stdlib.h. To avoid compilation
- errors, only prototype getopt for the GNU C library. */
extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
__THROW;
-#else /* not __GNU_LIBRARY__ */
-extern int getopt ();
-#endif /* __GNU_LIBRARY__ */
#ifndef __need_getopt
-extern int getopt_long (int ___argc, char *const *___argv,
+extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind)
__THROW;
-extern int getopt_long_only (int ___argc, char *const *___argv,
+extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind)
__THROW;
diff --git a/src/include/getopt_int.h b/src/include/getopt_int.h
index d982c72d..401579fd 100644
--- a/src/include/getopt_int.h
+++ b/src/include/getopt_int.h
@@ -3,28 +3,27 @@
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.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2, or (at your option)
+ any later version.
- The GNU C Library is distributed in the hope that it will be useful,
+ This program 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.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU 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, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation,
+ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifndef _GETOPT_INT_H
#define _GETOPT_INT_H 1
-extern int _getopt_internal (int ___argc, char *const *___argv,
+extern int _getopt_internal (int ___argc, char **___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind,
- int __long_only);
+ int __long_only, int __posixly_correct);
/* Reentrant versions which can handle parsing multiple argument
@@ -65,7 +64,7 @@ struct _getopt_data
This is what Unix does.
This mode of operation is selected by either setting the environment
variable POSIXLY_CORRECT, or using `+' as the first character
- of the list of option characters.
+ of the list of option characters, or by calling getopt.
PERMUTE is the default. We permute the contents of ARGV as we
scan, so that eventually all the non-options are at the end.
@@ -88,7 +87,8 @@ struct _getopt_data
REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
} __ordering;
- /* If the POSIXLY_CORRECT environment variable is set. */
+ /* If the POSIXLY_CORRECT environment variable is set
+ or getopt was called. */
int __posixly_correct;
@@ -111,17 +111,18 @@ struct _getopt_data
default values and to clear the initialization flag. */
#define _GETOPT_DATA_INITIALIZER { 1, 1 }
-extern int _getopt_internal_r (int ___argc, char *const *___argv,
+extern int _getopt_internal_r (int ___argc, char **___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind,
- int __long_only, struct _getopt_data *__data);
+ int __long_only, int __posixly_correct,
+ struct _getopt_data *__data);
-extern int _getopt_long_r (int ___argc, char *const *___argv,
+extern int _getopt_long_r (int ___argc, char **___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind,
struct _getopt_data *__data);
-extern int _getopt_long_only_r (int ___argc, char *const *___argv,
+extern int _getopt_long_only_r (int ___argc, char **___argv,
const char *__shortopts,
const struct option *__longopts,
int *__longind,
diff --git a/src/include/groff-getopt.h b/src/include/groff-getopt.h
deleted file mode 100644
index 02b7e516..00000000
--- a/src/include/groff-getopt.h
+++ /dev/null
@@ -1,71 +0,0 @@
-// -*- C++ -*-
-/* Copyright (C) 2000, 2001, 2005 Free Software Foundation, Inc.
- Written by Werner Lemberg (wl@gnu.org)
-
-This file is part of groff.
-
-groff is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 2, or (at your option) any later
-version.
-
-groff 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 General Public License
-for more details.
-
-You should have received a copy of the GNU General Public License along
-with groff; see the file COPYING. If not, write to the Free Software
-Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
-
-/*
- This file has to be included from within lib.h instead of getopt.h
- to avoid problems with picky C++ compilers.
-*/
-
-#ifndef _GROFF_GETOPT_H
-#define _GROFF_GETOPT_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-extern char *optarg;
-extern int optind;
-extern int opterr;
-extern int optopt;
-
-struct option
-{
- const char *name;
- int has_arg;
- int *flag;
- int val;
-};
-
-#define no_argument 0
-#define required_argument 1
-#define optional_argument 2
-
-extern int getopt(int, // __argc
- char *const *, // __argv
- const char *) // __shortopts
- throw();
-extern int getopt_long(int, // __argc
- char *const *, // __argv
- const char *, // __shortopts
- const struct option *, // __longopts
- int *) // __longind
- throw();
-extern int getopt_long_only(int, // __argc
- char *const *, // __argv
- const char *, // __shortopts
- const struct option *, // __longopts
- int *) // __longind
- throw();
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _GROFF_GETOPT_H */
diff --git a/src/include/lib.h b/src/include/lib.h
index f6dd35fa..bb59337f 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -32,20 +32,8 @@ extern "C" {
const char *if_to_a(int, int);
}
-/* stdio.h on IRIX, OSF/1, emx, UWIN, and MinGW include getopt.h */
-/* unistd.h on CYGWIN and Mac OS X include getopt.h */
-
-#if !(defined(__sgi) \
- || (defined(__osf__) && defined(__alpha)) \
- || defined(_UWIN) \
- || defined(__EMX__) \
- || defined(__CYGWIN__) \
- || defined(__MINGW32__) \
- || defined(__APPLE__))
-#include <groff-getopt.h>
-#else
+#define __GETOPT_PREFIX groff_
#include <getopt.h>
-#endif
#ifdef HAVE_SETLOCALE
#include <locale.h>