diff options
Diffstat (limited to 'gl/m4/getopt.m4')
| -rw-r--r-- | gl/m4/getopt.m4 | 288 |
1 files changed, 239 insertions, 49 deletions
diff --git a/gl/m4/getopt.m4 b/gl/m4/getopt.m4 index 9b683c2f..a19805ea 100644 --- a/gl/m4/getopt.m4 +++ b/gl/m4/getopt.m4 | |||
| @@ -1,82 +1,272 @@ | |||
| 1 | # getopt.m4 serial 14 | 1 | # getopt.m4 serial 24 |
| 2 | dnl Copyright (C) 2002-2006, 2008 Free Software Foundation, Inc. | 2 | dnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc. |
| 3 | dnl This file is free software; the Free Software Foundation | 3 | dnl This file is free software; the Free Software Foundation |
| 4 | dnl gives unlimited permission to copy and/or distribute it, | 4 | dnl gives unlimited permission to copy and/or distribute it, |
| 5 | dnl with or without modifications, as long as this notice is preserved. | 5 | dnl with or without modifications, as long as this notice is preserved. |
| 6 | 6 | ||
| 7 | # The getopt module assume you want GNU getopt, with getopt_long etc, | 7 | # Request a POSIX compliant getopt function. |
| 8 | # rather than vanilla POSIX getopt. This means your code should | 8 | AC_DEFUN([gl_FUNC_GETOPT_POSIX], |
| 9 | # always include <getopt.h> for the getopt prototypes. | 9 | [ |
| 10 | m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX]) | ||
| 11 | AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) | ||
| 12 | gl_GETOPT_IFELSE([ | ||
| 13 | gl_REPLACE_GETOPT | ||
| 14 | ], | ||
| 15 | []) | ||
| 16 | ]) | ||
| 17 | |||
| 18 | # Request a POSIX compliant getopt function with GNU extensions (such as | ||
| 19 | # options with optional arguments) and the functions getopt_long, | ||
| 20 | # getopt_long_only. | ||
| 21 | AC_DEFUN([gl_FUNC_GETOPT_GNU], | ||
| 22 | [ | ||
| 23 | m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU]) | ||
| 24 | |||
| 25 | AC_REQUIRE([gl_FUNC_GETOPT_POSIX]) | ||
| 26 | ]) | ||
| 10 | 27 | ||
| 11 | AC_DEFUN([gl_GETOPT_SUBSTITUTE], | 28 | # Request the gnulib implementation of the getopt functions unconditionally. |
| 29 | # argp.m4 uses this. | ||
| 30 | AC_DEFUN([gl_REPLACE_GETOPT], | ||
| 12 | [ | 31 | [ |
| 32 | dnl Arrange for getopt.h to be created. | ||
| 33 | gl_GETOPT_SUBSTITUTE_HEADER | ||
| 34 | dnl Arrange for unistd.h to include getopt.h. | ||
| 35 | GNULIB_UNISTD_H_GETOPT=1 | ||
| 36 | dnl Arrange to compile the getopt implementation. | ||
| 13 | AC_LIBOBJ([getopt]) | 37 | AC_LIBOBJ([getopt]) |
| 14 | AC_LIBOBJ([getopt1]) | 38 | AC_LIBOBJ([getopt1]) |
| 15 | gl_GETOPT_SUBSTITUTE_HEADER | ||
| 16 | gl_PREREQ_GETOPT | 39 | gl_PREREQ_GETOPT |
| 17 | ]) | 40 | ]) |
| 18 | 41 | ||
| 19 | AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER], | 42 | # emacs' configure.in uses this. |
| 43 | AC_DEFUN([gl_GETOPT_IFELSE], | ||
| 20 | [ | 44 | [ |
| 21 | GETOPT_H=getopt.h | 45 | AC_REQUIRE([gl_GETOPT_CHECK_HEADERS]) |
| 22 | AC_DEFINE([__GETOPT_PREFIX], [[rpl_]], | 46 | AS_IF([test -n "$gl_replace_getopt"], [$1], [$2]) |
| 23 | [Define to rpl_ if the getopt replacement functions and variables | ||
| 24 | should be used.]) | ||
| 25 | AC_SUBST([GETOPT_H]) | ||
| 26 | ]) | 47 | ]) |
| 27 | 48 | ||
| 49 | # Determine whether to replace the entire getopt facility. | ||
| 28 | AC_DEFUN([gl_GETOPT_CHECK_HEADERS], | 50 | AC_DEFUN([gl_GETOPT_CHECK_HEADERS], |
| 29 | [ | 51 | [ |
| 30 | if test -z "$GETOPT_H"; then | 52 | AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 31 | AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h]) | 53 | |
| 54 | dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt. | ||
| 55 | AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS]) | ||
| 56 | |||
| 57 | gl_CHECK_NEXT_HEADERS([getopt.h]) | ||
| 58 | AC_CHECK_HEADERS_ONCE([getopt.h]) | ||
| 59 | if test $ac_cv_header_getopt_h = yes; then | ||
| 60 | HAVE_GETOPT_H=1 | ||
| 61 | else | ||
| 62 | HAVE_GETOPT_H=0 | ||
| 63 | fi | ||
| 64 | AC_SUBST([HAVE_GETOPT_H]) | ||
| 65 | |||
| 66 | gl_replace_getopt= | ||
| 67 | |||
| 68 | dnl Test whether <getopt.h> is available. | ||
| 69 | if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then | ||
| 70 | AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes]) | ||
| 71 | fi | ||
| 72 | |||
| 73 | dnl Test whether the function getopt_long is available. | ||
| 74 | if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then | ||
| 75 | AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes]) | ||
| 32 | fi | 76 | fi |
| 33 | 77 | ||
| 34 | if test -z "$GETOPT_H"; then | 78 | dnl BSD getopt_long uses an incompatible method to reset option processing. |
| 35 | AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h]) | 79 | dnl Existence of the variable, in and of itself, is not a reason to replace |
| 80 | dnl getopt, but knowledge of the variable is needed to determine how to | ||
| 81 | dnl reset and whether a reset reparses the environment. | ||
| 82 | if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then | ||
| 83 | AC_CHECK_DECLS([optreset], [], [], | ||
| 84 | [[#include <getopt.h>]]) | ||
| 36 | fi | 85 | fi |
| 37 | 86 | ||
| 38 | dnl BSD getopt_long uses an incompatible method to reset option processing, | 87 | dnl mingw's getopt (in libmingwex.a) does weird things when the options |
| 39 | dnl and (as of 2004-10-15) mishandles optional option-arguments. | 88 | dnl strings starts with '+' and it's not the first call. Some internal state |
| 40 | if test -z "$GETOPT_H"; then | 89 | dnl is left over from earlier calls, and neither setting optind = 0 nor |
| 41 | AC_CHECK_DECL([optreset], [GETOPT_H=getopt.h], [], [#include <getopt.h>]) | 90 | dnl setting optreset = 1 get rid of this internal state. |
| 91 | dnl POSIX is silent on optind vs. optreset, so we allow either behavior. | ||
| 92 | if test -z "$gl_replace_getopt"; then | ||
| 93 | AC_CACHE_CHECK([whether getopt is POSIX compatible], | ||
| 94 | [gl_cv_func_getopt_posix], | ||
| 95 | [ | ||
| 96 | dnl This test fails on mingw and succeeds on all other platforms. | ||
| 97 | AC_RUN_IFELSE([AC_LANG_SOURCE([[ | ||
| 98 | #include <unistd.h> | ||
| 99 | #include <stdlib.h> | ||
| 100 | #include <string.h> | ||
| 101 | |||
| 102 | #if !HAVE_DECL_OPTRESET | ||
| 103 | # define OPTIND_MIN 0 | ||
| 104 | #else | ||
| 105 | # define OPTIND_MIN 1 | ||
| 106 | #endif | ||
| 107 | |||
| 108 | int | ||
| 109 | main () | ||
| 110 | { | ||
| 111 | { | ||
| 112 | int argc = 0; | ||
| 113 | char *argv[10]; | ||
| 114 | int c; | ||
| 115 | |||
| 116 | argv[argc++] = "program"; | ||
| 117 | argv[argc++] = "-a"; | ||
| 118 | argv[argc++] = "foo"; | ||
| 119 | argv[argc++] = "bar"; | ||
| 120 | argv[argc] = NULL; | ||
| 121 | optind = OPTIND_MIN; | ||
| 122 | opterr = 0; | ||
| 123 | |||
| 124 | c = getopt (argc, argv, "ab"); | ||
| 125 | if (!(c == 'a')) | ||
| 126 | return 1; | ||
| 127 | c = getopt (argc, argv, "ab"); | ||
| 128 | if (!(c == -1)) | ||
| 129 | return 2; | ||
| 130 | if (!(optind == 2)) | ||
| 131 | return 3; | ||
| 132 | } | ||
| 133 | /* Some internal state exists at this point. */ | ||
| 134 | { | ||
| 135 | int argc = 0; | ||
| 136 | char *argv[10]; | ||
| 137 | int c; | ||
| 138 | |||
| 139 | argv[argc++] = "program"; | ||
| 140 | argv[argc++] = "donald"; | ||
| 141 | argv[argc++] = "-p"; | ||
| 142 | argv[argc++] = "billy"; | ||
| 143 | argv[argc++] = "duck"; | ||
| 144 | argv[argc++] = "-a"; | ||
| 145 | argv[argc++] = "bar"; | ||
| 146 | argv[argc] = NULL; | ||
| 147 | optind = OPTIND_MIN; | ||
| 148 | opterr = 0; | ||
| 149 | |||
| 150 | c = getopt (argc, argv, "+abp:q:"); | ||
| 151 | if (!(c == -1)) | ||
| 152 | return 4; | ||
| 153 | if (!(strcmp (argv[0], "program") == 0)) | ||
| 154 | return 5; | ||
| 155 | if (!(strcmp (argv[1], "donald") == 0)) | ||
| 156 | return 6; | ||
| 157 | if (!(strcmp (argv[2], "-p") == 0)) | ||
| 158 | return 7; | ||
| 159 | if (!(strcmp (argv[3], "billy") == 0)) | ||
| 160 | return 8; | ||
| 161 | if (!(strcmp (argv[4], "duck") == 0)) | ||
| 162 | return 9; | ||
| 163 | if (!(strcmp (argv[5], "-a") == 0)) | ||
| 164 | return 10; | ||
| 165 | if (!(strcmp (argv[6], "bar") == 0)) | ||
| 166 | return 11; | ||
| 167 | if (!(optind == 1)) | ||
| 168 | return 12; | ||
| 169 | } | ||
| 170 | |||
| 171 | return 0; | ||
| 172 | } | ||
| 173 | ]])], | ||
| 174 | [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no], | ||
| 175 | [case "$host_os" in | ||
| 176 | mingw*) gl_cv_func_getopt_posix="guessing no";; | ||
| 177 | *) gl_cv_func_getopt_posix="guessing yes";; | ||
| 178 | esac | ||
| 179 | ]) | ||
| 180 | ]) | ||
| 181 | case "$gl_cv_func_getopt_posix" in | ||
| 182 | *no) gl_replace_getopt=yes ;; | ||
| 183 | esac | ||
| 42 | fi | 184 | fi |
| 43 | 185 | ||
| 44 | dnl Solaris 10 getopt doesn't handle `+' as a leading character in an | 186 | if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then |
| 45 | dnl option string (as of 2005-05-05). | 187 | AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu], |
| 46 | if test -z "$GETOPT_H"; then | 188 | [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the |
| 47 | AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt], | 189 | # optstring is necessary for programs like m4 that have POSIX-mandated |
| 48 | [AC_RUN_IFELSE( | 190 | # semantics for supporting options interspersed with files. |
| 49 | [AC_LANG_PROGRAM([[#include <getopt.h>]], | 191 | # Also, since getopt_long is a GNU extension, we require optind=0. |
| 50 | [[ | 192 | gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes} |
| 51 | char *myargv[3]; | 193 | POSIXLY_CORRECT=1 |
| 52 | myargv[0] = "conftest"; | 194 | export POSIXLY_CORRECT |
| 53 | myargv[1] = "-+"; | 195 | AC_RUN_IFELSE( |
| 54 | myargv[2] = 0; | 196 | [AC_LANG_PROGRAM([[#include <getopt.h> |
| 55 | return getopt (2, myargv, "+a") != '?'; | 197 | #include <stddef.h> |
| 56 | ]])], | 198 | #include <string.h> |
| 57 | [gl_cv_func_gnu_getopt=yes], | 199 | ]], [[ |
| 58 | [gl_cv_func_gnu_getopt=no], | 200 | /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw, |
| 59 | [dnl cross compiling - pessimistically guess based on decls | 201 | and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5, |
| 60 | dnl Solaris 10 getopt doesn't handle `+' as a leading character in an | 202 | OSF/1 5.1, Solaris 10. */ |
| 61 | dnl option string (as of 2005-05-05). | 203 | { |
| 62 | AC_CHECK_DECL([getopt_clip], | 204 | char *myargv[3]; |
| 63 | [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes], | 205 | myargv[0] = "conftest"; |
| 64 | [#include <getopt.h>])])]) | 206 | myargv[1] = "-+"; |
| 65 | if test "$gl_cv_func_gnu_getopt" = "no"; then | 207 | myargv[2] = 0; |
| 66 | GETOPT_H=getopt.h | 208 | opterr = 0; |
| 209 | if (getopt (2, myargv, "+a") != '?') | ||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | /* This code succeeds on glibc 2.8, mingw, | ||
| 213 | and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11, | ||
| 214 | IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */ | ||
| 215 | { | ||
| 216 | char *argv[] = { "program", "-p", "foo", "bar", NULL }; | ||
| 217 | |||
| 218 | optind = 1; | ||
| 219 | if (getopt (4, argv, "p::") != 'p') | ||
| 220 | return 2; | ||
| 221 | if (optarg != NULL) | ||
| 222 | return 3; | ||
| 223 | if (getopt (4, argv, "p::") != -1) | ||
| 224 | return 4; | ||
| 225 | if (optind != 2) | ||
| 226 | return 5; | ||
| 227 | } | ||
| 228 | /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */ | ||
| 229 | { | ||
| 230 | char *argv[] = { "program", "foo", "-p", NULL }; | ||
| 231 | optind = 0; | ||
| 232 | if (getopt (3, argv, "-p") != 1) | ||
| 233 | return 6; | ||
| 234 | if (getopt (3, argv, "-p") != 'p') | ||
| 235 | return 7; | ||
| 236 | } | ||
| 237 | return 0; | ||
| 238 | ]])], | ||
| 239 | [gl_cv_func_getopt_gnu=yes], | ||
| 240 | [gl_cv_func_getopt_gnu=no], | ||
| 241 | [dnl Cross compiling. Guess based on host and declarations. | ||
| 242 | case $host_os:$ac_cv_have_decl_optreset in | ||
| 243 | *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;; | ||
| 244 | *:yes) gl_cv_func_getopt_gnu=no;; | ||
| 245 | *) gl_cv_func_getopt_gnu=yes;; | ||
| 246 | esac | ||
| 247 | ]) | ||
| 248 | if test "$gl_had_POSIXLY_CORRECT" != yes; then | ||
| 249 | AS_UNSET([POSIXLY_CORRECT]) | ||
| 250 | fi | ||
| 251 | ]) | ||
| 252 | if test "$gl_cv_func_getopt_gnu" = "no"; then | ||
| 253 | gl_replace_getopt=yes | ||
| 67 | fi | 254 | fi |
| 68 | fi | 255 | fi |
| 69 | ]) | 256 | ]) |
| 70 | 257 | ||
| 71 | AC_DEFUN([gl_GETOPT_IFELSE], | 258 | # emacs' configure.in uses this. |
| 259 | AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER], | ||
| 72 | [ | 260 | [ |
| 73 | AC_REQUIRE([gl_GETOPT_CHECK_HEADERS]) | 261 | GETOPT_H=getopt.h |
| 74 | AS_IF([test -n "$GETOPT_H"], [$1], [$2]) | 262 | AC_DEFINE([__GETOPT_PREFIX], [[rpl_]], |
| 263 | [Define to rpl_ if the getopt replacement functions and variables | ||
| 264 | should be used.]) | ||
| 265 | AC_SUBST([GETOPT_H]) | ||
| 75 | ]) | 266 | ]) |
| 76 | 267 | ||
| 77 | AC_DEFUN([gl_GETOPT], [gl_GETOPT_IFELSE([gl_GETOPT_SUBSTITUTE])]) | ||
| 78 | |||
| 79 | # Prerequisites of lib/getopt*. | 268 | # Prerequisites of lib/getopt*. |
| 269 | # emacs' configure.in uses this. | ||
| 80 | AC_DEFUN([gl_PREREQ_GETOPT], | 270 | AC_DEFUN([gl_PREREQ_GETOPT], |
| 81 | [ | 271 | [ |
| 82 | AC_CHECK_DECLS_ONCE([getenv]) | 272 | AC_CHECK_DECLS_ONCE([getenv]) |
