]> code.delx.au - gnu-emacs/blob - m4/getopt.m4
Merge from gnulib.
[gnu-emacs] / m4 / getopt.m4
1 # getopt.m4 serial 43
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 POSIX 2008 does not specify leading '+' behavior, but see
78 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
79 dnl the next version of POSIX. For now, we only guarantee leading '+'
80 dnl behavior with getopt-gnu.
81 if test -z "$gl_replace_getopt"; then
82 AC_CACHE_CHECK([whether getopt is POSIX compatible],
83 [gl_cv_func_getopt_posix],
84 [
85 dnl Merging these three different test programs into a single one
86 dnl would require a reset mechanism. On BSD systems, it can be done
87 dnl through 'optreset'; on some others (glibc), it can be done by
88 dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
89 dnl Solaris 9, musl libc), there is no such mechanism.
90 if test $cross_compiling = no; then
91 dnl Sanity check. Succeeds everywhere (except on MSVC,
92 dnl which lacks <unistd.h> and getopt() entirely).
93 AC_RUN_IFELSE(
94 [AC_LANG_SOURCE([[
95 #include <unistd.h>
96 #include <stdlib.h>
97 #include <string.h>
98
99 int
100 main ()
101 {
102 static char program[] = "program";
103 static char a[] = "-a";
104 static char foo[] = "foo";
105 static char bar[] = "bar";
106 char *argv[] = { program, a, foo, bar, NULL };
107 int c;
108
109 c = getopt (4, argv, "ab");
110 if (!(c == 'a'))
111 return 1;
112 c = getopt (4, argv, "ab");
113 if (!(c == -1))
114 return 2;
115 if (!(optind == 2))
116 return 3;
117 return 0;
118 }
119 ]])],
120 [gl_cv_func_getopt_posix=maybe],
121 [gl_cv_func_getopt_posix=no])
122 if test $gl_cv_func_getopt_posix = maybe; then
123 dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
124 dnl which lacks <unistd.h> and getopt() entirely).
125 AC_RUN_IFELSE(
126 [AC_LANG_SOURCE([[
127 #include <unistd.h>
128 #include <stdlib.h>
129 #include <string.h>
130
131 int
132 main ()
133 {
134 static char program[] = "program";
135 static char donald[] = "donald";
136 static char p[] = "-p";
137 static char billy[] = "billy";
138 static char duck[] = "duck";
139 static char a[] = "-a";
140 static char bar[] = "bar";
141 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
142 int c;
143
144 c = getopt (7, argv, "+abp:q:");
145 if (!(c == -1))
146 return 4;
147 if (!(strcmp (argv[0], "program") == 0))
148 return 5;
149 if (!(strcmp (argv[1], "donald") == 0))
150 return 6;
151 if (!(strcmp (argv[2], "-p") == 0))
152 return 7;
153 if (!(strcmp (argv[3], "billy") == 0))
154 return 8;
155 if (!(strcmp (argv[4], "duck") == 0))
156 return 9;
157 if (!(strcmp (argv[5], "-a") == 0))
158 return 10;
159 if (!(strcmp (argv[6], "bar") == 0))
160 return 11;
161 if (!(optind == 1))
162 return 12;
163 return 0;
164 }
165 ]])],
166 [gl_cv_func_getopt_posix=maybe],
167 [gl_cv_func_getopt_posix=no])
168 fi
169 if test $gl_cv_func_getopt_posix = maybe; then
170 dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
171 AC_RUN_IFELSE(
172 [AC_LANG_SOURCE([[
173 #include <unistd.h>
174 #include <stdlib.h>
175 #include <string.h>
176
177 int
178 main ()
179 {
180 static char program[] = "program";
181 static char ab[] = "-ab";
182 char *argv[3] = { program, ab, NULL };
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 return 0;
192 }
193 ]])],
194 [gl_cv_func_getopt_posix=yes],
195 [gl_cv_func_getopt_posix=no])
196 fi
197 else
198 case "$host_os" in
199 darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
200 *) gl_cv_func_getopt_posix="guessing yes";;
201 esac
202 fi
203 ])
204 case "$gl_cv_func_getopt_posix" in
205 *no) gl_replace_getopt=yes ;;
206 esac
207 fi
208
209 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
210 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
211 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
212 # optstring is necessary for programs like m4 that have POSIX-mandated
213 # semantics for supporting options interspersed with files.
214 # Also, since getopt_long is a GNU extension, we require optind=0.
215 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
216 # so take care to revert to the correct (non-)export state.
217 dnl GNU Coding Standards currently allow awk but not env; besides, env
218 dnl is ambiguous with environment values that contain newlines.
219 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
220 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
221 xx) gl_had_POSIXLY_CORRECT=exported ;;
222 x) gl_had_POSIXLY_CORRECT=yes ;;
223 *) gl_had_POSIXLY_CORRECT= ;;
224 esac
225 POSIXLY_CORRECT=1
226 export POSIXLY_CORRECT
227 AC_RUN_IFELSE(
228 [AC_LANG_PROGRAM([[#include <getopt.h>
229 #include <stddef.h>
230 #include <string.h>
231 ]GL_NOCRASH[
232 ]], [[
233 int result = 0;
234
235 nocrash_init();
236
237 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
238 and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
239 OSF/1 5.1, Solaris 10. */
240 {
241 static char conftest[] = "conftest";
242 static char plus[] = "-+";
243 char *argv[3] = { conftest, plus, NULL };
244 opterr = 0;
245 if (getopt (2, argv, "+a") != '?')
246 result |= 1;
247 }
248 /* This code succeeds on glibc 2.8, mingw,
249 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
250 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
251 {
252 static char program[] = "program";
253 static char p[] = "-p";
254 static char foo[] = "foo";
255 static char bar[] = "bar";
256 char *argv[] = { program, p, foo, bar, NULL };
257
258 optind = 1;
259 if (getopt (4, argv, "p::") != 'p')
260 result |= 2;
261 else if (optarg != NULL)
262 result |= 4;
263 else if (getopt (4, argv, "p::") != -1)
264 result |= 6;
265 else if (optind != 2)
266 result |= 8;
267 }
268 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
269 {
270 static char program[] = "program";
271 static char foo[] = "foo";
272 static char p[] = "-p";
273 char *argv[] = { program, foo, p, NULL };
274 optind = 0;
275 if (getopt (3, argv, "-p") != 1)
276 result |= 16;
277 else if (getopt (3, argv, "-p") != 'p')
278 result |= 16;
279 }
280 /* This code fails on glibc 2.11. */
281 {
282 static char program[] = "program";
283 static char b[] = "-b";
284 static char a[] = "-a";
285 char *argv[] = { program, b, a, NULL };
286 optind = opterr = 0;
287 if (getopt (3, argv, "+:a:b") != 'b')
288 result |= 32;
289 else if (getopt (3, argv, "+:a:b") != ':')
290 result |= 32;
291 }
292 /* This code dumps core on glibc 2.14. */
293 {
294 static char program[] = "program";
295 static char w[] = "-W";
296 static char dummy[] = "dummy";
297 char *argv[] = { program, w, dummy, NULL };
298 optind = opterr = 1;
299 if (getopt (3, argv, "W;") != 'W')
300 result |= 64;
301 }
302 return result;
303 ]])],
304 [gl_cv_func_getopt_gnu=yes],
305 [gl_cv_func_getopt_gnu=no],
306 [dnl Cross compiling. Assume the worst, even on glibc platforms.
307 gl_cv_func_getopt_gnu="guessing no"
308 ])
309 case $gl_had_POSIXLY_CORRECT in
310 exported) ;;
311 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
312 *) AS_UNSET([POSIXLY_CORRECT]) ;;
313 esac
314 ])
315 if test "$gl_cv_func_getopt_gnu" != yes; then
316 gl_replace_getopt=yes
317 else
318 AC_CACHE_CHECK([for working GNU getopt_long function],
319 [gl_cv_func_getopt_long_gnu],
320 [AC_RUN_IFELSE(
321 [AC_LANG_PROGRAM(
322 [[#include <getopt.h>
323 #include <stddef.h>
324 #include <string.h>
325 ]],
326 [[static const struct option long_options[] =
327 {
328 { "xtremely-",no_argument, NULL, 1003 },
329 { "xtra", no_argument, NULL, 1001 },
330 { "xtreme", no_argument, NULL, 1002 },
331 { "xtremely", no_argument, NULL, 1003 },
332 { NULL, 0, NULL, 0 }
333 };
334 /* This code fails on OpenBSD 5.0. */
335 {
336 static char program[] = "program";
337 static char xtremel[] = "--xtremel";
338 char *argv[] = { program, xtremel, NULL };
339 int option_index;
340 optind = 1; opterr = 0;
341 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
342 return 1;
343 }
344 return 0;
345 ]])],
346 [gl_cv_func_getopt_long_gnu=yes],
347 [gl_cv_func_getopt_long_gnu=no],
348 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
349 case "$host_os" in
350 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
351 *) gl_cv_func_getopt_long_gnu="guessing yes";;
352 esac
353 ])
354 ])
355 case "$gl_cv_func_getopt_long_gnu" in
356 *yes) ;;
357 *) gl_replace_getopt=yes ;;
358 esac
359 fi
360 fi
361 ])
362
363 # emacs' configure.in uses this.
364 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
365 [
366 GETOPT_H=getopt.h
367 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
368 [Define to rpl_ if the getopt replacement functions and variables
369 should be used.])
370 AC_SUBST([GETOPT_H])
371 ])
372
373 # Prerequisites of lib/getopt*.
374 # emacs' configure.in uses this.
375 AC_DEFUN([gl_PREREQ_GETOPT],
376 [
377 AC_CHECK_DECLS_ONCE([getenv])
378 ])