Debian Codegraph @

m4/getopt.m4

raw
1 # getopt.m4 serial 48
2 dnl Copyright (C) 2002-2006, 2008-2022 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 AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
13 dnl Other modules can request the gnulib implementation of the getopt
14 dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
15 dnl argp.m4 does this.
16 m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
17 REPLACE_GETOPT=1
18 ], [
19 REPLACE_GETOPT=0
20 if test -n "$gl_replace_getopt"; then
21 REPLACE_GETOPT=1
22 fi
23 ])
24 GL_GENERATE_GETOPT_H=false
25 GL_GENERATE_GETOPT_CDEFS_H=false
26 if test $REPLACE_GETOPT = 1; then
27 dnl Arrange for getopt.h to be created.
28 gl_GETOPT_SUBSTITUTE_HEADER
29 fi
30 ])
31
32 # Request a POSIX compliant getopt function with GNU extensions (such as
33 # options with optional arguments) and the functions getopt_long,
34 # getopt_long_only.
35 AC_DEFUN([gl_FUNC_GETOPT_GNU],
36 [
37 dnl Set the variable gl_getopt_required, so that all invocations of
38 dnl gl_GETOPT_CHECK_HEADERS in the scope of the current configure file
39 dnl will check for getopt with GNU extensions.
40 dnl This means that if one gnulib-tool invocation requests getopt-posix
41 dnl and another gnulib-tool invocation requests getopt-gnu, it is as if
42 dnl both had requested getopt-gnu.
43 m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
44
45 dnl No need to invoke gl_FUNC_GETOPT_POSIX here; this is automatically
46 dnl done through the module dependency getopt-gnu -> getopt-posix.
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 if test $ac_cv_header_getopt_h = yes; then
60 HAVE_GETOPT_H=1
61 else
62 HAVE_GETOPT_H=0
63 fi
64 AC_SUBST([HAVE_GETOPT_H])
65
66 gl_replace_getopt=
67
68 dnl Test whether <getopt.h> is available.
69 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
70 AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
71 fi
72
73 dnl Test whether the function getopt_long is available.
74 if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
75 AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
76 fi
77
78 dnl POSIX 2008 does not specify leading '+' behavior, but see
79 dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
80 dnl the next version of POSIX. For now, we only guarantee leading '+'
81 dnl behavior with getopt-gnu.
82 if test -z "$gl_replace_getopt"; then
83 AC_CACHE_CHECK([whether getopt is POSIX compatible],
84 [gl_cv_func_getopt_posix],
85 [
86 dnl Merging these three different test programs into a single one
87 dnl would require a reset mechanism. On BSD systems, it can be done
88 dnl through 'optreset'; on some others (glibc), it can be done by
89 dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
90 dnl Solaris 9, musl libc), there is no such mechanism.
91 if test $cross_compiling = no; then
92 dnl Sanity check. Succeeds everywhere (except on MSVC,
93 dnl which lacks <unistd.h> and getopt() entirely).
94 AC_RUN_IFELSE(
95 [AC_LANG_SOURCE([[
96 #include <unistd.h>
97 #include <stdlib.h>
98 #include <string.h>
99
100 int
101 main ()
102 {
103 static char program[] = "program";
104 static char a[] = "-a";
105 static char foo[] = "foo";
106 static char bar[] = "bar";
107 char *argv[] = { program, a, foo, bar, NULL };
108 int c;
109
110 c = getopt (4, argv, "ab");
111 if (!(c == 'a'))
112 return 1;
113 c = getopt (4, argv, "ab");
114 if (!(c == -1))
115 return 2;
116 if (!(optind == 2))
117 return 3;
118 return 0;
119 }
120 ]])],
121 [gl_cv_func_getopt_posix=maybe],
122 [gl_cv_func_getopt_posix=no])
123 if test $gl_cv_func_getopt_posix = maybe; then
124 dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
125 dnl which lacks <unistd.h> and getopt() entirely).
126 AC_RUN_IFELSE(
127 [AC_LANG_SOURCE([[
128 #include <unistd.h>
129 #include <stdlib.h>
130 #include <string.h>
131
132 int
133 main ()
134 {
135 static char program[] = "program";
136 static char donald[] = "donald";
137 static char p[] = "-p";
138 static char billy[] = "billy";
139 static char duck[] = "duck";
140 static char a[] = "-a";
141 static char bar[] = "bar";
142 char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
143 int c;
144
145 c = getopt (7, argv, "+abp:q:");
146 if (!(c == -1))
147 return 4;
148 if (!(strcmp (argv[0], "program") == 0))
149 return 5;
150 if (!(strcmp (argv[1], "donald") == 0))
151 return 6;
152 if (!(strcmp (argv[2], "-p") == 0))
153 return 7;
154 if (!(strcmp (argv[3], "billy") == 0))
155 return 8;
156 if (!(strcmp (argv[4], "duck") == 0))
157 return 9;
158 if (!(strcmp (argv[5], "-a") == 0))
159 return 10;
160 if (!(strcmp (argv[6], "bar") == 0))
161 return 11;
162 if (!(optind == 1))
163 return 12;
164 return 0;
165 }
166 ]])],
167 [gl_cv_func_getopt_posix=maybe],
168 [gl_cv_func_getopt_posix=no])
169 fi
170 if test $gl_cv_func_getopt_posix = maybe; then
171 dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
172 AC_RUN_IFELSE(
173 [AC_LANG_SOURCE([[
174 #include <unistd.h>
175 #include <stdlib.h>
176 #include <string.h>
177
178 int
179 main ()
180 {
181 static char program[] = "program";
182 static char ab[] = "-ab";
183 char *argv[3] = { program, ab, NULL };
184 if (getopt (2, argv, "ab:") != 'a')
185 return 13;
186 if (getopt (2, argv, "ab:") != '?')
187 return 14;
188 if (optopt != 'b')
189 return 15;
190 if (optind != 2)
191 return 16;
192 return 0;
193 }
194 ]])],
195 [gl_cv_func_getopt_posix=yes],
196 [gl_cv_func_getopt_posix=no])
197 fi
198 else
199 case "$host_os" in
200 darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
201 *) gl_cv_func_getopt_posix="guessing yes";;
202 esac
203 fi
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 Mac OS 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 Mac OS 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 |= 16;
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 |= 32;
290 else if (getopt (3, argv, "+:a:b") != ':')
291 result |= 32;
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 |= 64;
302 }
303 return result;
304 ]])],
305 [gl_cv_func_getopt_gnu=yes],
306 [gl_cv_func_getopt_gnu=no],
307 [dnl Cross compiling.
308 dnl Assume the worst, even on glibc platforms.
309 dnl But obey --enable-cross-guesses.
310 gl_cv_func_getopt_gnu="$gl_cross_guess_normal"
311 ])
312 case $gl_had_POSIXLY_CORRECT in
313 exported) ;;
314 yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
315 *) AS_UNSET([POSIXLY_CORRECT]) ;;
316 esac
317 ])
318 if test "$gl_cv_func_getopt_gnu" != yes; then
319 gl_replace_getopt=yes
320 else
321 AC_CACHE_CHECK([for working GNU getopt_long function],
322 [gl_cv_func_getopt_long_gnu],
323 [AC_RUN_IFELSE(
324 [AC_LANG_PROGRAM(
325 [[#include <getopt.h>
326 #include <stddef.h>
327 #include <string.h>
328 ]],
329 [[static const struct option long_options[] =
330 {
331 { "xtremely-",no_argument, NULL, 1003 },
332 { "xtra", no_argument, NULL, 1001 },
333 { "xtreme", no_argument, NULL, 1002 },
334 { "xtremely", no_argument, NULL, 1003 },
335 { NULL, 0, NULL, 0 }
336 };
337 /* This code fails on OpenBSD 5.0. */
338 {
339 static char program[] = "program";
340 static char xtremel[] = "--xtremel";
341 char *argv[] = { program, xtremel, NULL };
342 int option_index;
343 optind = 1; opterr = 0;
344 if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
345 return 1;
346 }
347 return 0;
348 ]])],
349 [gl_cv_func_getopt_long_gnu=yes],
350 [gl_cv_func_getopt_long_gnu=no],
351 [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
352 case "$host_os" in
353 openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
354 *) gl_cv_func_getopt_long_gnu="guessing yes";;
355 esac
356 ])
357 ])
358 case "$gl_cv_func_getopt_long_gnu" in
359 *yes) ;;
360 *) gl_replace_getopt=yes ;;
361 esac
362 fi
363 fi
364 ])
365
366 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
367 [
368 AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
369 if test $ac_cv_header_sys_cdefs_h = yes; then
370 HAVE_SYS_CDEFS_H=1
371 else
372 HAVE_SYS_CDEFS_H=0
373 fi
374 AC_SUBST([HAVE_SYS_CDEFS_H])
375
376 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
377 [Define to rpl_ if the getopt replacement functions and variables
378 should be used.])
379 GL_GENERATE_GETOPT_H=true
380 GL_GENERATE_GETOPT_CDEFS_H=true
381 ])