Ensure that optarg etc. get declared by <unistd.h>.
[pspp] / m4 / getopt.m4
1 # getopt.m4 serial 20
2 dnl Copyright (C) 2002-2006, 2008-2009 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 # The getopt module assume you want GNU getopt, with getopt_long etc,
8 # rather than vanilla POSIX getopt.  This means your code should
9 # always include <getopt.h> for the getopt prototypes.
10
11 # This is gnulib's entry-point.
12 AC_DEFUN([gl_GETOPT],
13 [
14   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
15   gl_GETOPT_IFELSE([
16     gl_REPLACE_GETOPT
17   ])
18 ])
19
20 # Request the gnulib implementation of the getopt functions unconditionally.
21 # argp.m4 uses this.
22 AC_DEFUN([gl_REPLACE_GETOPT],
23 [
24   dnl Arrange for getopt.h to be created.
25   gl_GETOPT_SUBSTITUTE_HEADER
26   dnl Arrange for unistd.h to include getopt.h.
27   GNULIB_UNISTD_H_GETOPT=1
28   dnl Arrange to compile the getopt implementation.
29   AC_LIBOBJ([getopt])
30   AC_LIBOBJ([getopt1])
31   gl_PREREQ_GETOPT
32 ])
33
34 # emacs' configure.in uses this.
35 AC_DEFUN([gl_GETOPT_IFELSE],
36 [
37   AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
38   AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
39 ])
40
41 # Determine whether to replace the entire getopt facility.
42 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
43 [
44   dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
45   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
46
47   gl_replace_getopt=
48   if test -z "$gl_replace_getopt"; then
49     AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
50   fi
51
52   if test -z "$gl_replace_getopt"; then
53     AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
54   fi
55
56   dnl BSD getopt_long uses an incompatible method to reset option processing,
57   dnl and (as of 2004-10-15) mishandles optional option-arguments.
58   if test -z "$gl_replace_getopt"; then
59     AC_CHECK_DECL([optreset], [gl_replace_getopt=yes], [],
60       [#include <getopt.h>])
61   fi
62
63   dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
64   dnl option string (as of 2005-05-05).
65   if test -z "$gl_replace_getopt"; then
66     AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt],
67       [AC_RUN_IFELSE(
68         [AC_LANG_PROGRAM([[#include <getopt.h>]],
69            [[
70              char *myargv[3];
71              myargv[0] = "conftest";
72              myargv[1] = "-+";
73              myargv[2] = 0;
74              return getopt (2, myargv, "+a") != '?';
75            ]])],
76         [gl_cv_func_gnu_getopt=yes],
77         [gl_cv_func_gnu_getopt=no],
78         [dnl cross compiling - pessimistically guess based on decls
79          dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
80          dnl option string (as of 2005-05-05).
81          AC_CHECK_DECL([getopt_clip],
82            [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes],
83            [#include <getopt.h>])])])
84     if test "$gl_cv_func_gnu_getopt" = "no"; then
85       gl_replace_getopt=yes
86     fi
87   fi
88 ])
89
90 # emacs' configure.in uses this.
91 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
92 [
93   GETOPT_H=getopt.h
94   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
95     [Define to rpl_ if the getopt replacement functions and variables
96      should be used.])
97   AC_SUBST([GETOPT_H])
98 ])
99
100 # Prerequisites of lib/getopt*.
101 # emacs' configure.in uses this.
102 AC_DEFUN([gl_PREREQ_GETOPT],
103 [
104   AC_CHECK_DECLS_ONCE([getenv])
105 ])