openpty: Provide replacement on AIX, HP-UX, IRIX, Solaris.
[gnulib.git] / m4 / pty.m4
1 # pty.m4 serial 2
2 dnl Copyright (C) 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 # gl_PTY_LIB
8 # ----------
9 # Define automake variable PTY_LIB to the library needed (if any).
10 AC_DEFUN([gl_PTY_LIB],
11 [
12   # Check for the library required for forkpty.
13   PTY_LIB=
14   save_LIBS="$LIBS"
15   AC_SEARCH_LIBS([forkpty], [util],
16     [if test "$ac_cv_search_forkpty" != "none required"; then
17        PTY_LIB="$ac_cv_search_forkpty"
18      fi])
19   LIBS="$save_LIBS"
20   AC_SUBST([PTY_LIB])
21 ])
22
23 AC_DEFUN([gl_FORKPTY],
24 [
25   AC_REQUIRE([gl_PTY_LIB])
26   AC_REQUIRE([gl_PTY])
27
28   AC_CHECK_DECLS([forkpty],,, [[
29 #if HAVE_PTY_H
30 # include <pty.h>
31 #endif
32 #if HAVE_UTIL_H
33 # include <util.h>
34 #endif
35 #if HAVE_LIBUTIL_H
36 # include <libutil.h>
37 #endif
38 ]])
39   if test $ac_cv_have_decl_forkpty = no; then
40     AC_MSG_WARN([[Cannot find forkpty, build will likely fail]])
41   fi
42
43   dnl Prefer glibc's const-safe prototype, if available.
44   AC_CACHE_CHECK([for const-safe forkpty signature],
45     [gl_cv_func_forkpty_const],
46     [AC_COMPILE_IFELSE(
47       [AC_LANG_PROGRAM([[
48 #if HAVE_PTY_H
49 # include <pty.h>
50 #endif
51 #if HAVE_UTIL_H
52 # include <util.h>
53 #endif
54 #if HAVE_LIBUTIL_H
55 # include <libutil.h>
56 #endif
57       ]], [[
58         int forkpty (int *, char *, struct termios const *,
59                      struct winsize const *);
60       ]])],
61       [gl_cv_func_forkpty_const=yes], [gl_cv_func_forkpty_const=no])])
62   if test $gl_cv_func_forkpty_const != yes; then
63     REPLACE_FORKPTY=1
64     AC_LIBOBJ([forkpty])
65   fi
66 ])
67
68 AC_DEFUN([gl_OPENPTY],
69 [
70   AC_REQUIRE([gl_PTY_LIB])
71   AC_REQUIRE([gl_PTY])
72
73   dnl Persuade Solaris <stdlib.h> to declare posix_openpt().
74   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
75
76   dnl We assume that openpty exists (possibly in libc, possibly in libutil)
77   dnl if and only if it is declared.
78   AC_CHECK_DECLS([openpty],,, [[
79 #if HAVE_PTY_H
80 # include <pty.h>
81 #endif
82 #if HAVE_UTIL_H
83 # include <util.h>
84 #endif
85 #if HAVE_LIBUTIL_H
86 # include <libutil.h>
87 #endif
88 ]])
89   if test $ac_cv_have_decl_openpty = yes; then
90     dnl The system has openpty.
91     dnl Prefer glibc's const-safe prototype, if available.
92     AC_CACHE_CHECK([for const-safe openpty signature],
93       [gl_cv_func_openpty_const],
94       [AC_COMPILE_IFELSE(
95         [AC_LANG_PROGRAM([[
96 #if HAVE_PTY_H
97 # include <pty.h>
98 #endif
99 #if HAVE_UTIL_H
100 # include <util.h>
101 #endif
102 #if HAVE_LIBUTIL_H
103 # include <libutil.h>
104 #endif
105           ]], [[
106             int openpty (int *, int *, char *, struct termios const *,
107                        struct winsize const *);
108           ]])
109         ],
110         [gl_cv_func_openpty_const=yes], [gl_cv_func_openpty_const=no])
111       ])
112     if test $gl_cv_func_openpty_const != yes; then
113       REPLACE_OPENPTY=1
114       AC_LIBOBJ([openpty])
115     fi
116   else
117     dnl The system does not have openpty.
118     HAVE_OPENPTY=0
119     AC_LIBOBJ([openpty])
120     AC_CHECK_FUNCS([_getpty posix_openpt])
121   fi
122 ])