2
2
dnl Copyright (C) 2002-2006, 2008-2011 Free Software Foundation, Inc.
3
3
dnl This file is free software; the Free Software Foundation
4
4
dnl gives unlimited permission to copy and/or distribute it,
10
10
m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
11
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([], [])
25
if test $REPLACE_GETOPT = 1; then
26
dnl Arrange for getopt.h to be created.
27
gl_GETOPT_SUBSTITUTE_HEADER
18
31
# Request a POSIX compliant getopt function with GNU extensions (such as
25
38
AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
28
# Request the gnulib implementation of the getopt functions unconditionally.
30
AC_DEFUN([gl_REPLACE_GETOPT],
32
dnl Arrange for getopt.h to be created.
33
gl_GETOPT_SUBSTITUTE_HEADER
34
dnl Arrange for unistd.h to include getopt.h.
35
GNULIB_UNISTD_H_GETOPT=1
36
dnl Arrange to compile the getopt implementation.
42
41
# emacs' configure.in uses this.
43
42
AC_DEFUN([gl_GETOPT_IFELSE],
88
87
AC_CACHE_CHECK([whether getopt is POSIX compatible],
89
88
[gl_cv_func_getopt_posix],
91
dnl BSD getopt_long uses an incompatible method to reset
92
dnl option processing. Existence of the variable, in and of
90
dnl BSD getopt_long uses an incompatible method to reset option
91
dnl processing. Existence of the optreset variable, in and of
93
92
dnl itself, is not a reason to replace getopt, but knowledge
94
93
dnl of the variable is needed to determine how to reset and
95
94
dnl whether a reset reparses the environment. Solaris
96
95
dnl supports neither optreset nor optind=0, but keeps no state
97
96
dnl that needs a reset beyond setting optind=1; detect Solaris
98
97
dnl by getopt_clip.
101
100
[[#include <unistd.h>]],
102
101
[[int *p = &optreset; return optreset;]])],
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
argv[argc++] = "program";
129
argv[argc++] = "foo";
130
argv[argc++] = "bar";
132
129
optind = OPTIND_MIN;
135
c = getopt (argc, argv, "ab");
132
c = getopt (4, argv, "ab");
138
c = getopt (argc, argv, "ab");
135
c = getopt (4, argv, "ab");
141
138
if (!(optind == 2))
144
141
/* Some internal state exists at this point. */
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 };
150
argv[argc++] = "program";
151
argv[argc++] = "donald";
153
argv[argc++] = "billy";
154
argv[argc++] = "duck";
156
argv[argc++] = "bar";
158
153
optind = OPTIND_MIN;
161
c = getopt (argc, argv, "+abp:q:");
156
c = getopt (7, argv, "+abp:q:");
164
159
if (!(strcmp (argv[0], "program") == 0))
181
176
/* Detect MacOS 10.5, AIX 7.1 bug. */
183
char *argv[3] = { "program", "-ab", NULL };
178
static char program[] = "program";
179
static char ab[] = "-ab";
180
char *argv[3] = { program, ab, NULL };
184
181
optind = OPTIND_MIN;
186
183
if (getopt (2, argv, "ab:") != 'a')
232
229
[AC_LANG_PROGRAM([[#include <getopt.h>
233
230
#include <stddef.h>
234
231
#include <string.h>
237
238
/* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
238
239
and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
239
240
OSF/1 5.1, Solaris 10. */
242
myargv[0] = "conftest";
242
static char conftest[] = "conftest";
243
static char plus[] = "-+";
244
char *argv[3] = { conftest, plus, NULL };
246
if (getopt (2, myargv, "+a") != '?')
246
if (getopt (2, argv, "+a") != '?')
249
249
/* This code succeeds on glibc 2.8, mingw,
250
250
and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
251
251
IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
253
char *argv[] = { "program", "-p", "foo", "bar", NULL };
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 };
256
260
if (getopt (4, argv, "p::") != 'p')
265
269
/* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
267
char *argv[] = { "program", "foo", "-p", NULL };
271
static char program[] = "program";
272
static char foo[] = "foo";
273
static char p[] = "-p";
274
char *argv[] = { program, foo, p, NULL };
269
276
if (getopt (3, argv, "-p") != 1)
274
281
/* This code fails on glibc 2.11. */
276
char *argv[] = { "program", "-b", "-a", NULL };
283
static char program[] = "program";
284
static char b[] = "-b";
285
static char a[] = "-a";
286
char *argv[] = { program, b, a, NULL };
277
287
optind = opterr = 0;
278
288
if (getopt (3, argv, "+:a:b") != 'b')
280
290
else if (getopt (3, argv, "+:a:b") != ':')
293
/* This code dumps core on glibc 2.14. */
295
static char program[] = "program";
296
static char w[] = "-W";
297
static char dummy[] = "dummy";
298
char *argv[] = { program, w, dummy, NULL };
300
if (getopt (3, argv, "W;") != 'W')
285
305
[gl_cv_func_getopt_gnu=yes],