comparison m4/getopt.m4 @ 112335:d3c691ea0839

Merge: Automate syncing from gnulib.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 17 Jan 2011 12:17:27 -0800
parents ef719132ddfa babd014aab81
children c81ff7082a9a
comparison
equal deleted inserted replaced
112301:4249a8552faf 112335:d3c691ea0839
1 # getopt.m4 serial 10 1 # getopt.m4 serial 32
2 dnl Copyright (C) 2002,2003,2004,2005,2006,2007 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002-2006, 2008-2011 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 your code should 8 AC_DEFUN([gl_FUNC_GETOPT_POSIX],
9 # always include <getopt.h> for the getopt prototypes. 9 [
10 10 m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
11 AC_DEFUN([gl_GETOPT_SUBSTITUTE], 11 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
12 [ 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 ])
27
28 # Request the gnulib implementation of the getopt functions unconditionally.
29 # argp.m4 uses this.
30 AC_DEFUN([gl_REPLACE_GETOPT],
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
42 # emacs' configure.in uses this.
43 AC_DEFUN([gl_GETOPT_IFELSE],
44 [
45 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
46 AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
47 ])
48
49 # Determine whether to replace the entire getopt facility.
50 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
51 [
52 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
53 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
54
55 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
56 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
57
58 gl_CHECK_NEXT_HEADERS([getopt.h])
59 AC_CHECK_HEADERS_ONCE([getopt.h])
60 if test $ac_cv_header_getopt_h = yes; then
61 HAVE_GETOPT_H=1
62 else
63 HAVE_GETOPT_H=0
64 fi
65 AC_SUBST([HAVE_GETOPT_H])
66
67 gl_replace_getopt=
68
69 dnl Test whether <getopt.h> is available.
70 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
71 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
72 fi
73
74 dnl Test whether the function getopt_long is available.
75 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
76 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
77 fi
78
79 dnl BSD getopt_long uses an incompatible method to reset option processing.
80 dnl Existence of the variable, in and of itself, is not a reason to replace
81 dnl getopt, but knowledge of the variable is needed to determine how to
82 dnl reset and whether a reset reparses the environment.
83 dnl Solaris supports neither optreset nor optind=0, but keeps no state that
84 dnl needs a reset beyond setting optind=1; detect Solaris by getopt_clip.
85 if test -z "$gl_replace_getopt"; then
86 AC_CHECK_DECLS([optreset], [],
87 [AC_CHECK_DECLS([getopt_clip], [], [],
88 [[#include <getopt.h>]])
89 ],
90 [[#include <getopt.h>]])
91 fi
92
93 dnl mingw's getopt (in libmingwex.a) does weird things when the options
94 dnl strings starts with '+' and it's not the first call. Some internal state
95 dnl is left over from earlier calls, and neither setting optind = 0 nor
96 dnl setting optreset = 1 get rid of this internal state.
97 dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
98 dnl POSIX 2008 does not specify leading '+' behavior, but see
99 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
100 dnl the next version of POSIX. For now, we only guarantee leading '+'
101 dnl behavior with getopt-gnu.
102 if test -z "$gl_replace_getopt"; then
103 AC_CACHE_CHECK([whether getopt is POSIX compatible],
104 [gl_cv_func_getopt_posix],
105 [
106 dnl This test fails on mingw and succeeds on many other platforms.
107 AC_RUN_IFELSE([AC_LANG_SOURCE([[
108 #include <unistd.h>
109 #include <stdlib.h>
110 #include <string.h>
111
112 #if !HAVE_DECL_OPTRESET && !HAVE_DECL_GETOPT_CLIP
113 # define OPTIND_MIN 0
114 #else
115 # define OPTIND_MIN 1
116 #endif
117
118 int
119 main ()
120 {
121 {
122 int argc = 0;
123 char *argv[10];
124 int c;
125
126 argv[argc++] = "program";
127 argv[argc++] = "-a";
128 argv[argc++] = "foo";
129 argv[argc++] = "bar";
130 argv[argc] = NULL;
131 optind = OPTIND_MIN;
132 opterr = 0;
133
134 c = getopt (argc, argv, "ab");
135 if (!(c == 'a'))
136 return 1;
137 c = getopt (argc, argv, "ab");
138 if (!(c == -1))
139 return 2;
140 if (!(optind == 2))
141 return 3;
142 }
143 /* Some internal state exists at this point. */
144 {
145 int argc = 0;
146 char *argv[10];
147 int c;
148
149 argv[argc++] = "program";
150 argv[argc++] = "donald";
151 argv[argc++] = "-p";
152 argv[argc++] = "billy";
153 argv[argc++] = "duck";
154 argv[argc++] = "-a";
155 argv[argc++] = "bar";
156 argv[argc] = NULL;
157 optind = OPTIND_MIN;
158 opterr = 0;
159
160 c = getopt (argc, argv, "+abp:q:");
161 if (!(c == -1))
162 return 4;
163 if (!(strcmp (argv[0], "program") == 0))
164 return 5;
165 if (!(strcmp (argv[1], "donald") == 0))
166 return 6;
167 if (!(strcmp (argv[2], "-p") == 0))
168 return 7;
169 if (!(strcmp (argv[3], "billy") == 0))
170 return 8;
171 if (!(strcmp (argv[4], "duck") == 0))
172 return 9;
173 if (!(strcmp (argv[5], "-a") == 0))
174 return 10;
175 if (!(strcmp (argv[6], "bar") == 0))
176 return 11;
177 if (!(optind == 1))
178 return 12;
179 }
180 /* Detect MacOS 10.5, AIX 7.1 bug. */
181 {
182 char *argv[3] = { "program", "-ab", NULL };
183 optind = OPTIND_MIN;
184 opterr = 0;
185 if (getopt (2, argv, "ab:") != 'a')
186 return 13;
187 if (getopt (2, argv, "ab:") != '?')
188 return 14;
189 if (optopt != 'b')
190 return 15;
191 if (optind != 2)
192 return 16;
193 }
194
195 return 0;
196 }
197 ]])],
198 [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
199 [case "$host_os" in
200 mingw*) gl_cv_func_getopt_posix="guessing no";;
201 darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
202 *) gl_cv_func_getopt_posix="guessing yes";;
203 esac
204 ])
205 ])
206 case "$gl_cv_func_getopt_posix" in
207 *no) gl_replace_getopt=yes ;;
208 esac
209 fi
210
211 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
212 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
213 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
214 # optstring is necessary for programs like m4 that have POSIX-mandated
215 # semantics for supporting options interspersed with files.
216 # Also, since getopt_long is a GNU extension, we require optind=0.
217 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
218 # so take care to revert to the correct (non-)export state.
219 dnl GNU Coding Standards currently allow awk but not env; besides, env
220 dnl is ambiguous with environment values that contain newlines.
221 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
222 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
223 xx) gl_had_POSIXLY_CORRECT=exported ;;
224 x) gl_had_POSIXLY_CORRECT=yes ;;
225 *) gl_had_POSIXLY_CORRECT= ;;
226 esac
227 POSIXLY_CORRECT=1
228 export POSIXLY_CORRECT
229 AC_RUN_IFELSE(
230 [AC_LANG_PROGRAM([[#include <getopt.h>
231 #include <stddef.h>
232 #include <string.h>
233 ]], [[
234 int result = 0;
235 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
236 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
237 OSF/1 5.1, Solaris 10. */
238 {
239 char *myargv[3];
240 myargv[0] = "conftest";
241 myargv[1] = "-+";
242 myargv[2] = 0;
243 opterr = 0;
244 if (getopt (2, myargv, "+a") != '?')
245 result |= 1;
246 }
247 /* This code succeeds on glibc 2.8, mingw,
248 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
249 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
250 {
251 char *argv[] = { "program", "-p", "foo", "bar", NULL };
252
253 optind = 1;
254 if (getopt (4, argv, "p::") != 'p')
255 result |= 2;
256 else if (optarg != NULL)
257 result |= 4;
258 else if (getopt (4, argv, "p::") != -1)
259 result |= 6;
260 else if (optind != 2)
261 result |= 8;
262 }
263 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
264 {
265 char *argv[] = { "program", "foo", "-p", NULL };
266 optind = 0;
267 if (getopt (3, argv, "-p") != 1)
268 result |= 16;
269 else if (getopt (3, argv, "-p") != 'p')
270 result |= 32;
271 }
272 /* This code fails on glibc 2.11. */
273 {
274 char *argv[] = { "program", "-b", "-a", NULL };
275 optind = opterr = 0;
276 if (getopt (3, argv, "+:a:b") != 'b')
277 result |= 64;
278 else if (getopt (3, argv, "+:a:b") != ':')
279 result |= 64;
280 }
281 return result;
282 ]])],
283 [gl_cv_func_getopt_gnu=yes],
284 [gl_cv_func_getopt_gnu=no],
285 [dnl Cross compiling. Guess based on host and declarations.
286 case $host_os:$ac_cv_have_decl_optreset in
287 *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
288 *:yes) gl_cv_func_getopt_gnu=no;;
289 *) gl_cv_func_getopt_gnu=yes;;
290 esac
291 ])
292 case $gl_had_POSIXLY_CORRECT in
293 exported) ;;
294 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
295 *) AS_UNSET([POSIXLY_CORRECT]) ;;
296 esac
297 ])
298 if test "$gl_cv_func_getopt_gnu" = "no"; then
299 gl_replace_getopt=yes
300 fi
301 fi
302 ])
303
304 # emacs' configure.in uses this.
19 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER], 305 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
20 [ 306 [
21 GETOPT_H=getopt.h 307 GETOPT_H=getopt.h
22 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]], 308 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
23 [Define to rpl_ if the getopt replacement functions and variables 309 [Define to rpl_ if the getopt replacement functions and variables
24 should be used.]) 310 should be used.])
25 AC_SUBST([GETOPT_H]) 311 AC_SUBST([GETOPT_H])
26 ]) 312 ])
27 313
28 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
29 [
30 GETOPT_H=
31 AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
32 if test -z "$GETOPT_H"; then
33 AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h])
34 fi
35
36 dnl BSD getopt_long uses an incompatible method to reset option processing,
37 dnl and (as of 2004-10-15) mishandles optional option-arguments.
38 if test -z "$GETOPT_H"; then
39 AC_CHECK_DECL([optreset], [GETOPT_H=getopt.h], [], [#include <getopt.h>])
40 fi
41
42 dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
43 dnl option string (as of 2005-05-05).
44 if test -z "$GETOPT_H"; then
45 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt],
46 [AC_RUN_IFELSE(
47 [AC_LANG_PROGRAM([#include <getopt.h>],
48 [[
49 char *myargv[3];
50 myargv[0] = "conftest";
51 myargv[1] = "-+";
52 myargv[2] = 0;
53 return getopt (2, myargv, "+a") != '?';
54 ]])],
55 [gl_cv_func_gnu_getopt=yes],
56 [gl_cv_func_gnu_getopt=no],
57 [dnl cross compiling - pessimistically guess based on decls
58 dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
59 dnl option string (as of 2005-05-05).
60 AC_CHECK_DECL([getopt_clip],
61 [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes],
62 [#include <getopt.h>])])])
63 if test "$gl_cv_func_gnu_getopt" = "no"; then
64 GETOPT_H=getopt.h
65 fi
66 fi
67 ])
68
69 AC_DEFUN([gl_GETOPT_IFELSE],
70 [
71 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
72 AS_IF([test -n "$GETOPT_H"], [$1], [$2])
73 ])
74
75 AC_DEFUN([gl_GETOPT], [gl_GETOPT_IFELSE([gl_GETOPT_SUBSTITUTE])])
76
77 # Prerequisites of lib/getopt*. 314 # Prerequisites of lib/getopt*.
78 AC_DEFUN([gl_PREREQ_GETOPT], [:]) 315 # emacs' configure.in uses this.
79 316 AC_DEFUN([gl_PREREQ_GETOPT],
317 [
318 AC_CHECK_DECLS_ONCE([getenv])
319 ])