getopt-posix: avoid spurious failure on Solaris
[gnulib.git] / m4 / getopt.m4
1 # getopt.m4 serial 26
2 dnl Copyright (C) 2002-2006, 2008-2010 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   gl_GETOPT_IFELSE([
13     gl_REPLACE_GETOPT
14   ],
15   [])
16 ])
17
18 # Request a POSIX compliant getopt function with GNU extensions (such as
19 # options with optional arguments) and the functions getopt_long,
20 # getopt_long_only.
21 AC_DEFUN([gl_FUNC_GETOPT_GNU],
22 [
23   m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
24
25   AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
26 ])
27
28 # Request the gnulib implementation of the getopt functions unconditionally.
29 # argp.m4 uses this.
30 AC_DEFUN([gl_REPLACE_GETOPT],
31 [
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.
37   AC_LIBOBJ([getopt])
38   AC_LIBOBJ([getopt1])
39   gl_PREREQ_GETOPT
40 ])
41
42 # emacs' configure.in uses this.
43 AC_DEFUN([gl_GETOPT_IFELSE],
44 [
45   AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
46   AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
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
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   AC_CHECK_HEADERS_ONCE([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 BSD getopt_long uses an incompatible method to reset option processing.
79   dnl Existence of the variable, in and of itself, is not a reason to replace
80   dnl getopt, but knowledge of the variable is needed to determine how to
81   dnl reset and whether a reset reparses the environment.
82   dnl Solaris supports neither optreset nor optind=0, but keeps no state that
83   dnl needs a reset beyond setting optind=1; detect Solaris by getopt_clip.
84   if test -z "$gl_replace_getopt"; then
85     AC_CHECK_DECLS([optreset], [],
86       [AC_CHECK_DECLS([getopt_clip], [], [],
87         [[#include <getopt.h>]])
88       ],
89       [[#include <getopt.h>]])
90   fi
91
92   dnl mingw's getopt (in libmingwex.a) does weird things when the options
93   dnl strings starts with '+' and it's not the first call.  Some internal state
94   dnl is left over from earlier calls, and neither setting optind = 0 nor
95   dnl setting optreset = 1 get rid of this internal state.
96   dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
97   if test -z "$gl_replace_getopt"; then
98     AC_CACHE_CHECK([whether getopt is POSIX compatible],
99       [gl_cv_func_getopt_posix],
100       [
101         dnl This test fails on mingw and succeeds on all other platforms.
102         AC_RUN_IFELSE([AC_LANG_SOURCE([[
103 #include <unistd.h>
104 #include <stdlib.h>
105 #include <string.h>
106
107 #if !HAVE_DECL_OPTRESET && !HAVE_DECL_GETOPT_CLIP
108 # define OPTIND_MIN 0
109 #else
110 # define OPTIND_MIN 1
111 #endif
112
113 int
114 main ()
115 {
116   {
117     int argc = 0;
118     char *argv[10];
119     int c;
120
121     argv[argc++] = "program";
122     argv[argc++] = "-a";
123     argv[argc++] = "foo";
124     argv[argc++] = "bar";
125     argv[argc] = NULL;
126     optind = OPTIND_MIN;
127     opterr = 0;
128
129     c = getopt (argc, argv, "ab");
130     if (!(c == 'a'))
131       return 1;
132     c = getopt (argc, argv, "ab");
133     if (!(c == -1))
134       return 2;
135     if (!(optind == 2))
136       return 3;
137   }
138   /* Some internal state exists at this point.  */
139   {
140     int argc = 0;
141     char *argv[10];
142     int c;
143
144     argv[argc++] = "program";
145     argv[argc++] = "donald";
146     argv[argc++] = "-p";
147     argv[argc++] = "billy";
148     argv[argc++] = "duck";
149     argv[argc++] = "-a";
150     argv[argc++] = "bar";
151     argv[argc] = NULL;
152     optind = OPTIND_MIN;
153     opterr = 0;
154
155     c = getopt (argc, argv, "+abp:q:");
156     if (!(c == -1))
157       return 4;
158     if (!(strcmp (argv[0], "program") == 0))
159       return 5;
160     if (!(strcmp (argv[1], "donald") == 0))
161       return 6;
162     if (!(strcmp (argv[2], "-p") == 0))
163       return 7;
164     if (!(strcmp (argv[3], "billy") == 0))
165       return 8;
166     if (!(strcmp (argv[4], "duck") == 0))
167       return 9;
168     if (!(strcmp (argv[5], "-a") == 0))
169       return 10;
170     if (!(strcmp (argv[6], "bar") == 0))
171       return 11;
172     if (!(optind == 1))
173       return 12;
174   }
175
176   return 0;
177 }
178 ]])],
179           [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
180           [case "$host_os" in
181              mingw*) gl_cv_func_getopt_posix="guessing no";;
182              *)      gl_cv_func_getopt_posix="guessing yes";;
183            esac
184           ])
185       ])
186     case "$gl_cv_func_getopt_posix" in
187       *no) gl_replace_getopt=yes ;;
188     esac
189   fi
190
191   if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
192     AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
193       [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
194        # optstring is necessary for programs like m4 that have POSIX-mandated
195        # semantics for supporting options interspersed with files.
196        # Also, since getopt_long is a GNU extension, we require optind=0.
197        gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
198        POSIXLY_CORRECT=1
199        export POSIXLY_CORRECT
200        AC_RUN_IFELSE(
201         [AC_LANG_PROGRAM([[#include <getopt.h>
202                            #include <stddef.h>
203                            #include <string.h>
204            ]], [[
205              /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
206                 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
207                 OSF/1 5.1, Solaris 10.  */
208              {
209                char *myargv[3];
210                myargv[0] = "conftest";
211                myargv[1] = "-+";
212                myargv[2] = 0;
213                opterr = 0;
214                if (getopt (2, myargv, "+a") != '?')
215                  return 1;
216              }
217              /* This code succeeds on glibc 2.8, mingw,
218                 and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
219                 IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x.  */
220              {
221                char *argv[] = { "program", "-p", "foo", "bar", NULL };
222
223                optind = 1;
224                if (getopt (4, argv, "p::") != 'p')
225                  return 2;
226                if (optarg != NULL)
227                  return 3;
228                if (getopt (4, argv, "p::") != -1)
229                  return 4;
230                if (optind != 2)
231                  return 5;
232              }
233              /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0.  */
234              {
235                char *argv[] = { "program", "foo", "-p", NULL };
236                optind = 0;
237                if (getopt (3, argv, "-p") != 1)
238                  return 6;
239                if (getopt (3, argv, "-p") != 'p')
240                  return 7;
241              }
242              return 0;
243            ]])],
244         [gl_cv_func_getopt_gnu=yes],
245         [gl_cv_func_getopt_gnu=no],
246         [dnl Cross compiling. Guess based on host and declarations.
247          case $host_os:$ac_cv_have_decl_optreset in
248            *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
249            *:yes)               gl_cv_func_getopt_gnu=no;;
250            *)                   gl_cv_func_getopt_gnu=yes;;
251          esac
252         ])
253        if test "$gl_had_POSIXLY_CORRECT" != yes; then
254          AS_UNSET([POSIXLY_CORRECT])
255        fi
256       ])
257     if test "$gl_cv_func_getopt_gnu" = "no"; then
258       gl_replace_getopt=yes
259     fi
260   fi
261 ])
262
263 # emacs' configure.in uses this.
264 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
265 [
266   GETOPT_H=getopt.h
267   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
268     [Define to rpl_ if the getopt replacement functions and variables
269      should be used.])
270   AC_SUBST([GETOPT_H])
271 ])
272
273 # Prerequisites of lib/getopt*.
274 # emacs' configure.in uses this.
275 AC_DEFUN([gl_PREREQ_GETOPT],
276 [
277   AC_CHECK_DECLS_ONCE([getenv])
278 ])