Debian Codegraph @

m4/getopt.m4

raw
1 # getopt.m4
2 # serial 50
3 dnl Copyright (C) 2002-2006, 2008-2024 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 http://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, IRIX, OSF/1,
92 dnl Solaris 9, 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, IRIX 6.5,
242 OSF/1 5.1, Solaris 10. */
243 {
244 static char conftest[] = "conftest";
245 static char plus[] = "-+";
246 char *argv[3] = { conftest, plus, NULL };
247 opterr = 0;
248 if (getopt (2, argv, "+a") != '?')
249 result |= 1;
250 }
251 /* This code succeeds on glibc 2.8, mingw,
252 and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
253 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
254 {
255 static char program[] = "program";
256 static char p[] = "-p";
257 static char foo[] = "foo";
258 static char bar[] = "bar";
259 char *argv[] = { program, p, foo, bar, NULL };
260
261 optind = 1;
262 if (getopt (4, argv, "p::") != 'p')
263 result |= 2;
264 else if (optarg != NULL)
265 result |= 4;
266 else if (getopt (4, argv, "p::") != -1)
267 result |= 6;
268 else if (optind != 2)
269 result |= 8;
270 }
271 /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
272 {
273 static char program[] = "program";
274 static char foo[] = "foo";
275 static char p[] = "-p";
276 char *argv[] = { program, foo, p, NULL };
277 optind = 0;
278 if (getopt (3, argv, "-p") != 1)
279 result |= 16;
280 else if (getopt (3, argv, "-p") != 'p')
281 result |= 16;
282 }
283 /* This code fails on glibc 2.11. */
284 {
285 static char program[] = "program";
286 static char b[] = "-b";
287 static char a[] = "-a";
288 char *argv[] = { program, b, a, NULL };
289 optind = opterr = 0;
290 if (getopt (3, argv, "+:a:b") != 'b')
291 result |= 32;
292 else if (getopt (3, argv, "+:a:b") != ':')
293 result |= 32;
294 }
295 /* This code dumps core on glibc 2.14. */
296 {
297 static char program[] = "program";
298 static char w[] = "-W";
299 static char dummy[] = "dummy";
300 char *argv[] = { program, w, dummy, NULL };
301 optind = opterr = 1;
302 if (getopt (3, argv, "W;") != 'W')
303 result |= 64;
304 }
305 return result;
306 ]])],
307 [gl_cv_func_getopt_gnu=yes],
308 [gl_cv_func_getopt_gnu=no],
309 [dnl Cross compiling.
310 dnl Assume the worst, even on glibc platforms.
311 dnl But obey --enable-cross-guesses.
312 gl_cv_func_getopt_gnu="$gl_cross_guess_normal"
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" != yes; then
321 gl_replace_getopt=yes
322 else
323 AC_CACHE_CHECK([for working GNU getopt_long function],
324 [gl_cv_func_getopt_long_gnu],
325 [AC_RUN_IFELSE(
326 [AC_LANG_PROGRAM(
327 [[#include <getopt.h>
328 #include <stddef.h>
329 #include <string.h>
330 ]],
331 [[static const struct option long_options[] =
332 {
333 { "xtremely-",no_argument, NULL, 1003 },
334 { "xtra", no_argument, NULL, 1001 },
335 { "xtreme", no_argument, NULL, 1002 },
336 { "xtremely", no_argument, NULL, 1003 },
337 { NULL, 0, NULL, 0 }
338 };
339 /* This code fails on OpenBSD 5.0. */
340 {
341 static char program[] = "program";
342 static char xtremel[] = "--xtremel";
343 char *argv[] = { program, xtremel, NULL };
344 int option_index;
345 optind = 1; opterr = 0;
346 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
347 return 1;
348 }
349 return 0;
350 ]])],
351 [gl_cv_func_getopt_long_gnu=yes],
352 [gl_cv_func_getopt_long_gnu=no],
353 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
354 case "$host_os" in
355 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
356 *) gl_cv_func_getopt_long_gnu="guessing yes";;
357 esac
358 ])
359 ])
360 case "$gl_cv_func_getopt_long_gnu" in
361 *yes) ;;
362 *) gl_replace_getopt=yes ;;
363 esac
364 fi
365 fi
366 ])
367
368 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
369 [
370 gl_CHECK_HEADER_SYS_CDEFS_H
371 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
372 [Define to rpl_ if the getopt replacement functions and variables
373 should be used.])
374 GL_GENERATE_GETOPT_H=true
375 GL_GENERATE_GETOPT_CDEFS_H=true
376 ])