067838fcc55dd49545a0474b26e0f85a2e710a57
[pspp] / m4 / getopt.m4
1 # getopt.m4 serial 15
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 AC_DEFUN([gl_GETOPT_SUBSTITUTE],
12 [
13   AC_LIBOBJ([getopt])
14   AC_LIBOBJ([getopt1])
15   gl_GETOPT_SUBSTITUTE_HEADER
16   gl_PREREQ_GETOPT
17 ])
18
19 # emacs' configure.in uses this.
20 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
21 [
22   GETOPT_H=getopt.h
23   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
24     [Define to rpl_ if the getopt replacement functions and variables
25      should be used.])
26   AC_SUBST([GETOPT_H])
27 ])
28
29 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
30 [
31   if test -z "$GETOPT_H"; then
32     AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
33   fi
34
35   if test -z "$GETOPT_H"; then
36     AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h])
37   fi
38
39   dnl BSD getopt_long uses an incompatible method to reset option processing,
40   dnl and (as of 2004-10-15) mishandles optional option-arguments.
41   if test -z "$GETOPT_H"; then
42     AC_CHECK_DECL([optreset], [GETOPT_H=getopt.h], [], [#include <getopt.h>])
43   fi
44
45   dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
46   dnl option string (as of 2005-05-05).
47   if test -z "$GETOPT_H"; then
48     AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt],
49       [AC_RUN_IFELSE(
50         [AC_LANG_PROGRAM([[#include <getopt.h>]],
51            [[
52              char *myargv[3];
53              myargv[0] = "conftest";
54              myargv[1] = "-+";
55              myargv[2] = 0;
56              return getopt (2, myargv, "+a") != '?';
57            ]])],
58         [gl_cv_func_gnu_getopt=yes],
59         [gl_cv_func_gnu_getopt=no],
60         [dnl cross compiling - pessimistically guess based on decls
61          dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
62          dnl option string (as of 2005-05-05).
63          AC_CHECK_DECL([getopt_clip],
64            [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes],
65            [#include <getopt.h>])])])
66     if test "$gl_cv_func_gnu_getopt" = "no"; then
67       GETOPT_H=getopt.h
68     fi
69   fi
70 ])
71
72 # emacs' configure.in uses this.
73 AC_DEFUN([gl_GETOPT_IFELSE],
74 [
75   AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
76   AS_IF([test -n "$GETOPT_H"], [$1], [$2])
77 ])
78
79 # This is gnulib's entry-point.
80 AC_DEFUN([gl_GETOPT], [gl_GETOPT_IFELSE([gl_GETOPT_SUBSTITUTE])])
81
82 # Prerequisites of lib/getopt*.
83 # emacs' configure.in uses this.
84 AC_DEFUN([gl_PREREQ_GETOPT],
85 [
86   AC_CHECK_DECLS_ONCE([getenv])
87 ])