Debian Codegraph @

m4/getopt.m4

raw
1 # getopt.m4
2 # serial 52
3 dnl Copyright (C) 2002-2006, 2008-2026 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 # Request a POSIX compliant getopt function.
10 AC_DEFUN([gl_FUNC_GETOPT_POSIX],
11 [
12 m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
13 AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
14 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
15 dnl Other modules can request the gnulib implementation of the getopt
16 dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
17 dnl argp.m4 does this.
18 m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
19 REPLACE_GETOPT=1
20 ], [
21 REPLACE_GETOPT=0
22 if test -n "$gl_replace_getopt"; then
23 REPLACE_GETOPT=1
24 fi
25 ])
26 GL_GENERATE_GETOPT_H=false
27 GL_GENERATE_GETOPT_CDEFS_H=false
28 if test $REPLACE_GETOPT = 1; then
29 dnl Arrange for getopt.h to be created.
30 gl_GETOPT_SUBSTITUTE_HEADER
31 fi
32 ])
33
34 # Request a POSIX compliant getopt function with GNU extensions (such as
35 # options with optional arguments) and the functions getopt_long,
36 # getopt_long_only.
37 AC_DEFUN([gl_FUNC_GETOPT_GNU],
38 [
39 dnl Set the variable gl_getopt_required, so that all invocations of
40 dnl gl_GETOPT_CHECK_HEADERS in the scope of the current configure file
41 dnl will check for getopt with GNU extensions.
42 dnl This means that if one gnulib-tool invocation requests getopt-posix
43 dnl and another gnulib-tool invocation requests getopt-gnu, it is as if
44 dnl both had requested getopt-gnu.
45 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
46
47 dnl No need to invoke gl_FUNC_GETOPT_POSIX here; this is automatically
48 dnl done through the module dependency getopt-gnu -> getopt-posix.
49 ])
50
51 # Determine whether to replace the entire getopt facility.
52 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
53 [
54 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
55 AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
56
57 dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
58 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
59
60 gl_CHECK_NEXT_HEADERS([getopt.h])
61 if test $ac_cv_header_getopt_h = yes; then
62 HAVE_GETOPT_H=1
63 else
64 HAVE_GETOPT_H=0
65 fi
66 AC_SUBST([HAVE_GETOPT_H])
67
68 gl_replace_getopt=
69
70 dnl Test whether <getopt.h> is available.
71 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
72 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
73 fi
74
75 dnl Test whether the function getopt_long is available.
76 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
77 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
78 fi
79
80 dnl POSIX 2008 does not specify leading '+' behavior, but see
81 dnl https://austingroupbugs.net/view.php?id=191 for a recommendation on
82 dnl the next version of POSIX. For now, we only guarantee leading '+'
83 dnl behavior with getopt-gnu.
84 if test -z "$gl_replace_getopt"; then
85 AC_CACHE_CHECK([whether getopt is POSIX compatible],
86 [gl_cv_func_getopt_posix],
87 [
88 dnl Merging these three different test programs into a single one
89 dnl would require a reset mechanism. On BSD systems, it can be done
90 dnl through 'optreset'; on some others (glibc), it can be done by
91 dnl setting 'optind' to 0; on others again (HP-UX, Solaris 9,
92 dnl musl libc), there is no such mechanism.
93 if test $cross_compiling = no; then
94 dnl Sanity check. Succeeds everywhere (except on MSVC,
95 dnl which lacks <unistd.h> and getopt() entirely).
96 AC_RUN_IFELSE(
97 [AC_LANG_SOURCE([[
98 #include <unistd.h>
99 #include <stdlib.h>
100 #include <string.h>
101
102 int
103 main ()
104 {
105 static char program[] = "program";
106 static char a[] = "-a";
107 static char foo[] = "foo";
108 static char bar[] = "bar";
109 char *argv[] = { program, a, foo, bar, NULL };
110 int c;
111
112 c = getopt (4, argv, "ab");
113 if (!(c == 'a'))
114 return 1;
115 c = getopt (4, argv, "ab");
116 if (!(c == -1))
117 return 2;
118 if (!(optind == 2))
119 return 3;
120 return 0;
121 }
122 ]])],
123 [gl_cv_func_getopt_posix=maybe],
124 [gl_cv_func_getopt_posix=no])
125 if test $gl_cv_func_getopt_posix = maybe; then
126 dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
127 dnl which lacks <unistd.h> and getopt() entirely).
128 AC_RUN_IFELSE(
129 [AC_LANG_SOURCE([[
130 #include <unistd.h>
131 #include <stdlib.h>
132 #include <string.h>
133
134 int
135 main ()
136 {
137 static char program[] = "program";
138 static char donald[] = "donald";
139 static char p[] = "-p";
140 static char billy[] = "billy";
141 static char duck[] = "duck";
142 static char a[] = "-a";
143 static char bar[] = "bar";
144 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
145 int c;
146
147 c = getopt (7, argv, "+abp:q:");
148 if (!(c == -1))
149 return 4;
150 if (!(strcmp (argv[0], "program") == 0))
151 return 5;
152 if (!(strcmp (argv[1], "donald") == 0))
153 return 6;
154 if (!(strcmp (argv[2], "-p") == 0))
155 return 7;
156 if (!(strcmp (argv[3], "billy") == 0))
157 return 8;
158 if (!(strcmp (argv[4], "duck") == 0))
159 return 9;
160 if (!(strcmp (argv[5], "-a") == 0))
161 return 10;
162 if (!(strcmp (argv[6], "bar") == 0))
163 return 11;
164 if (!(optind == 1))
165 return 12;
166 return 0;
167 }
168 ]])],
169 [gl_cv_func_getopt_posix=maybe],
170 [gl_cv_func_getopt_posix=no])
171 fi
172 if test $gl_cv_func_getopt_posix = maybe; then
173 dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
174 AC_RUN_IFELSE(
175 [AC_LANG_SOURCE([[
176 #include <unistd.h>
177 #include <stdlib.h>
178 #include <string.h>
179
180 int
181 main ()
182 {
183 static char program[] = "program";
184 static char ab[] = "-ab";
185 char *argv[3] = { program, ab, NULL };
186 if (getopt (2, argv, "ab:") != 'a')
187 return 13;
188 if (getopt (2, argv, "ab:") != '?')
189 return 14;
190 if (optopt != 'b')
191 return 15;
192 if (optind != 2)
193 return 16;
194 return 0;
195 }
196 ]])],
197 [gl_cv_func_getopt_posix=yes],
198 [gl_cv_func_getopt_posix=no])
199 fi
200 else
201 case "$host_os" in
202 darwin* | aix* | mingw* | windows*) gl_cv_func_getopt_posix="guessing no";;
203 *) gl_cv_func_getopt_posix="guessing yes";;
204 esac
205 fi
206 ])
207 case "$gl_cv_func_getopt_posix" in
208 *no) gl_replace_getopt=yes ;;
209 esac
210 fi
211
212 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
213 AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
214 [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
215 # optstring is necessary for programs like m4 that have POSIX-mandated
216 # semantics for supporting options interspersed with files.
217 # Also, since getopt_long is a GNU extension, we require optind=0.
218 # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
219 # so take care to revert to the correct (non-)export state.
220 dnl GNU Coding Standards currently allow awk but not env; besides, env
221 dnl is ambiguous with environment values that contain newlines.
222 gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
223 case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
224 xx) gl_had_POSIXLY_CORRECT=exported ;;
225 x) gl_had_POSIXLY_CORRECT=yes ;;
226 *) gl_had_POSIXLY_CORRECT= ;;
227 esac
228 POSIXLY_CORRECT=1
229 export POSIXLY_CORRECT
230 AC_RUN_IFELSE(
231 [AC_LANG_PROGRAM([[#include <getopt.h>
232 #include <stddef.h>
233 #include <string.h>
234 ]GL_NOCRASH[
235 ]], [[
236 int result = 0;
237
238 nocrash_init();
239
240 /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
241 and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, Solaris 10. */
242 {
243 static char conftest[] = "conftest";
244 static char plus[] = "-+";
245 char *argv[3] = { conftest, plus, NULL };
246 opterr = 0;
247 if (getopt (2, argv, "+a") != '?')
248 result |= 1;
249 }
250 /* This code succeeds on glibc 2.8, mingw,
251 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
252 Solaris 10, Cygwin 1.5.x. */
253 {
254 static char program[] = "program";
255 static char p[] = "-p";
256 static char foo[] = "foo";
257 static char bar[] = "bar";
258 char *argv[] = { program, p, foo, bar, NULL };
259
260 optind = 1;
261 if (getopt (4, argv, "p::") != 'p')
262 result |= 2;
263 else if (optarg != NULL)
264 result |= 4;
265 else if (getopt (4, argv, "p::") != -1)
266 result |= 6;
267 else if (optind != 2)
268 result |= 8;
269 }
270 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
271 {
272 static char program[] = "program";
273 static char foo[] = "foo";
274 static char p[] = "-p";
275 char *argv[] = { program, foo, p, NULL };
276 optind = 0;
277 if (getopt (3, argv, "-p") != 1)
278 result |= 16;
279 else if (getopt (3, argv, "-p") != 'p')
280 result |= 16;
281 }
282 /* This code fails on glibc 2.11. */
283 {
284 static char program[] = "program";
285 static char b[] = "-b";
286 static char a[] = "-a";
287 char *argv[] = { program, b, a, NULL };
288 optind = opterr = 0;
289 if (getopt (3, argv, "+:a:b") != 'b')
290 result |= 32;
291 else if (getopt (3, argv, "+:a:b") != ':')
292 result |= 32;
293 }
294 /* This code dumps core on glibc 2.14. */
295 {
296 static char program[] = "program";
297 static char w[] = "-W";
298 static char dummy[] = "dummy";
299 char *argv[] = { program, w, dummy, NULL };
300 optind = opterr = 1;
301 if (getopt (3, argv, "W;") != 'W')
302 result |= 64;
303 }
304 return result;
305 ]])],
306 [gl_cv_func_getopt_gnu=yes],
307 [gl_cv_func_getopt_gnu=no],
308 [dnl Cross compiling.
309 dnl Assume the worst, even on glibc platforms.
310 dnl But obey --enable-cross-guesses.
311 gl_cv_func_getopt_gnu="$gl_cross_guess_normal"
312 ])
313 case $gl_had_POSIXLY_CORRECT in
314 exported) ;;
315 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
316 *) AS_UNSET([POSIXLY_CORRECT]) ;;
317 esac
318 ])
319 if test "$gl_cv_func_getopt_gnu" != yes; then
320 gl_replace_getopt=yes
321 else
322 AC_CACHE_CHECK([for working GNU getopt_long function],
323 [gl_cv_func_getopt_long_gnu],
324 [AC_RUN_IFELSE(
325 [AC_LANG_PROGRAM(
326 [[#include <getopt.h>
327 #include <stddef.h>
328 #include <string.h>
329 ]],
330 [[static const struct option long_options[] =
331 {
332 { "xtremely-",no_argument, NULL, 1003 },
333 { "xtra", no_argument, NULL, 1001 },
334 { "xtreme", no_argument, NULL, 1002 },
335 { "xtremely", no_argument, NULL, 1003 },
336 { NULL, 0, NULL, 0 }
337 };
338 /* This code fails on OpenBSD 5.0. */
339 {
340 static char program[] = "program";
341 static char xtremel[] = "--xtremel";
342 char *argv[] = { program, xtremel, NULL };
343 int option_index;
344 optind = 1; opterr = 0;
345 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
346 return 1;
347 }
348 return 0;
349 ]])],
350 [gl_cv_func_getopt_long_gnu=yes],
351 [gl_cv_func_getopt_long_gnu=no],
352 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
353 case "$host_os" in
354 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
355 *) gl_cv_func_getopt_long_gnu="guessing yes";;
356 esac
357 ])
358 ])
359 case "$gl_cv_func_getopt_long_gnu" in
360 *yes) ;;
361 *) gl_replace_getopt=yes ;;
362 esac
363 fi
364 fi
365 ])
366
367 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
368 [
369 gl_CHECK_HEADER_SYS_CDEFS_H
370 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
371 [Define to rpl_ if the getopt replacement functions and variables
372 should be used.])
373 GL_GENERATE_GETOPT_H=true
374 GL_GENERATE_GETOPT_CDEFS_H=true
375 ])