New module posix-shell, for determining a POSIX shell
[gnulib.git] / m4 / posix-shell.m4
1 # Find a POSIX-conforming shell.
2
3 # Copyright (C) 2007 Free Software Foundation, Inc.
4
5 # This file is free software; the Free Software Foundation
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
8
9 # Written by Paul Eggert.
10
11 # If a POSIX-conforming shell can be found, set POSIX_SHELL and
12 # PREFERABLY_POSIX_SHELL to it.  If not, set POSIX_SHELL to the
13 # empty string and PREFERABLY_POSIX_SHELL to '/bin/sh'.
14
15 AC_DEFUN([gl_POSIX_SHELL],
16 [
17   AC_CACHE_CHECK([for a shell that conforms to POSIX], [gl_cv_posix_shell],
18     [gl_test_POSIX_SHELL='
19        func_return () {
20          (exit [$]1)
21        }
22        func_success () {
23          func_return 0
24        }
25        func_failure () {
26          func_return 1
27        }
28        func_ret_success () {
29          return 0
30        }
31        func_ret_failure () {
32          return 1
33        }
34        test "[$](echo foo)" = foo &&
35        func_success &&
36        ! func_failure &&
37        func_ret_success &&
38        ! func_ret_failure &&
39        (set x && func_ret_success y && test x = "[$]1")
40      '
41      for gl_cv_posix_shell in \
42          "$CONFIG_SHELL" "$SHELL" /bin/sh /bin/bash /bin/ksh /bin/sh5 no; do
43        case $gl_cv_posix_shell in
44          /*)
45            "$gl_cv_posix_shell" -c "$gl_test_POSIX_shell" 2>/dev/null && break;;
46        esac
47      done])
48
49   if test "$gl_cv_posix_shell" != no; then
50     POSIX_SHELL=$gl_cv_posix_shell
51     PREFERABLY_POSIX_SHELL=$POSIX_SHELL
52   else
53     POSIX_SHELL=
54     PREFERABLY_POSIX_SHELL=/bin/sh
55   fi
56   AC_SUBST([POSIX_SHELL])
57   AC_SUBST([PREFERABLY_POSIX_SHELL])
58 ])