Imported Upstream version 3.2.1
[debian/amanda] / config / amanda / readline.m4
1 # SYNOPSIS
2 #
3 #   AMANDA_CHECK_READLINE
4 #
5 # OVERVIEW
6 #
7 #   Check for readline support.  Defines HAVE_LIBREADLINE if readline
8 #   is available, and also checks for a number of readline headers and
9 #   adds readline libraries to READLINE_LIBS.
10 #
11 #   See common-src/util.{c,h}.
12 #
13 #
14 # Some idea taken from AX_LIB_READLINE:
15 # ===========================================================================
16 #      http://www.gnu.org/software/autoconf-archive/ax_lib_readline.html
17 # ===========================================================================
18 #
19 # SYNOPSIS
20 #
21 #   AX_LIB_READLINE
22 #
23 # DESCRIPTION
24 #
25 #   Searches for a readline compatible library. If found, defines
26 #   `HAVE_LIBREADLINE'. If the found library has the `add_history' function,
27 #   sets also `HAVE_READLINE_HISTORY'. Also checks for the locations of the
28 #   necessary include files and sets `HAVE_READLINE_H' or
29 #   `HAVE_READLINE_READLINE_H' and `HAVE_READLINE_HISTORY_H' or
30 #   'HAVE_HISTORY_H' if the corresponding include files exists.
31 #
32 #   The libraries that may be readline compatible are `libedit',
33 #   `libeditline' and `libreadline'. Sometimes we need to link a termcap
34 #   library for readline to work, this macro tests these cases too by trying
35 #   to link with `libtermcap', `libcurses' or `libncurses' before giving up.
36 #
37 #   Here is an example of how to use the information provided by this macro
38 #   to perform the necessary includes or declarations in a C file:
39 #
40 #     #ifdef HAVE_LIBREADLINE
41 #     #  if defined(HAVE_READLINE_READLINE_H)
42 #     #    include <readline/readline.h>
43 #     #  elif defined(HAVE_READLINE_H)
44 #     #    include <readline.h>
45 #     #  else /* !defined(HAVE_READLINE_H) */
46 #     extern char *readline ();
47 #     #  endif /* !defined(HAVE_READLINE_H) */
48 #     char *cmdline = NULL;
49 #     #else /* !defined(HAVE_READLINE_READLINE_H) */
50 #       /* no readline */
51 #     #endif /* HAVE_LIBREADLINE */
52 #
53 #     #ifdef HAVE_READLINE_HISTORY
54 #     #  if defined(HAVE_READLINE_HISTORY_H)
55 #     #    include <readline/history.h>
56 #     #  elif defined(HAVE_HISTORY_H)
57 #     #    include <history.h>
58 #     #  else /* !defined(HAVE_HISTORY_H) */
59 #     extern void add_history ();
60 #     extern int write_history ();
61 #     extern int read_history ();
62 #     #  endif /* defined(HAVE_READLINE_HISTORY_H) */
63 #       /* no history */
64 #     #endif /* HAVE_READLINE_HISTORY */
65 #
66 # LICENSE
67 #
68 #   Copyright (c) 2008 Ville Laurikari <vl@iki.fi>
69 #
70 #   Copying and distribution of this file, with or without modification, are
71 #   permitted in any medium without royalty provided the copyright notice
72 #   and this notice are preserved. This file is offered as-is, without any
73 #   warranty.
74
75 #serial 6
76
77 AC_DEFUN([AMANDA_CHECK_READLINE], [
78   AC_ARG_WITH(readline,
79   dnl no initial space here, so the results line up properly
80 AS_HELP_STRING([--with-readline], [require readline support (for amrecover)])
81 AS_HELP_STRING([--without-readline], [don't search for readline]),
82         [
83             case "$withval" in
84                 y | ye | yes | n | no) : ;;
85                 *) AC_MSG_ERROR([*** --with-readline does not take a value])
86             esac
87             want_readline="$withval"
88         ], [
89             want_readline="maybe" # meaning "only if we can find it"
90 ]       )
91
92
93   # unless the user said "no", look for readline.
94   if test x"$want_readline" != x"no"; then
95     AC_CACHE_CHECK([for a readline compatible library],
96                    ax_cv_lib_readline, [
97       ORIG_LIBS="$LIBS"
98       for readline_lib in readline edit editline; do
99         for termcap_lib in "" termcap curses ncurses; do
100           if test -z "$termcap_lib"; then
101             TRY_LIB="-l$readline_lib"
102           else
103             TRY_LIB="-l$readline_lib -l$termcap_lib"
104           fi
105           LIBS="$ORIG_LIBS $TRY_LIB"
106           AC_TRY_LINK_FUNC(readline, ax_cv_lib_readline="$TRY_LIB")
107           if test -n "$ax_cv_lib_readline"; then
108             break
109           fi
110         done
111         if test -n "$ax_cv_lib_readline"; then
112           break
113         fi
114       done
115       if test -z "$ax_cv_lib_readline"; then
116         ax_cv_lib_readline="no"
117       fi
118       LIBS="$ORIG_LIBS"
119     ])
120
121     if test "$ax_cv_lib_readline" != "no"; then
122       ORIG_LIBS="$LIBS"
123       LIBS="$LIBS $ax_cv_lib_readline"
124       READLINE_LIBS="$ax_cv_lib_readline"
125       AC_DEFINE(HAVE_LIBREADLINE, 1,
126                 [Define if you have a readline compatible library])
127       AC_CHECK_HEADERS(readline.h readline/readline.h)
128       AC_CACHE_CHECK([whether readline supports history],
129                      ax_cv_lib_readline_history, [
130         ax_cv_lib_readline_history="no"
131         AC_TRY_LINK_FUNC(add_history, ax_cv_lib_readline_history="yes")
132       ])
133       if test "$ax_cv_lib_readline_history" = "yes"; then
134         AC_DEFINE(HAVE_READLINE_HISTORY, 1,
135                   [Define if your readline library has \`add_history'])
136         AC_CHECK_HEADERS(history.h readline/history.h)
137       fi
138       LIBS="$ORIG_LIBS"
139
140     else
141       # no readline.  if the user *really* wanted it, bail out.
142       if test x"$want_readline" = x"yes"; then
143         AC_MSG_ERROR([*** No readline implementation found.  Try using --with-libraries and --with-includes])
144       fi
145       READLINE_LIBS=""
146     fi
147   fi
148   AC_SUBST(READLINE_LIBS)
149 ])dnl