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