Test for the 'echo' command from gnulib-tool.
[pspp] / tests / test-echo.sh
1 #! /bin/sh
2
3 # func_exit STATUS
4 # exit with status
5 func_exit ()
6 {
7   (exit $1); exit $1
8 }
9
10 # func_fatal_error message
11 # outputs to stderr a fatal error message, and terminates the program.
12 func_fatal_error ()
13 {
14   echo "test-echo.sh: *** $1" 1>&2
15   echo "test-echo.sh: *** Stop." 1>&2
16   func_exit 1
17 }
18
19 # Ensure an 'echo' command that does not interpret backslashes.
20 # Test cases:
21 #   echo '\n' | wc -l                 prints 1 when OK, 2 when KO
22 #   echo '\t' | grep t > /dev/null    has return code 0 when OK, 1 when KO
23 # This problem is a weird heritage from SVR4. BSD got it right.
24 # Nowadays the problem occurs in 4 situations:
25 # - in bash, when the shell option xpg_echo is set,
26 # - in zsh, when sh-emulation is not set,
27 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
28 #           and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
29 # - in Solaris /bin/sh and OSF/1 /bin/sh.
30 # We try the following workarounds:
31 # - for all: respawn using $CONFIG_SHELL if that is set and works.
32 # - for bash: unset the shell option xpg_echo.
33 # - for zsh: turn sh-emulation on.
34 # - for ksh: alias echo to a function that uses cat of a here document.
35 # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround.
36 # - otherwise: respawn using /bin/sh and rely on the workarounds.
37 # When respawning, we pass --no-reexec as first argument, so as to avoid
38 # turning this script into a fork bomb in unlucky situations.
39 have_echo=
40 if echo '\t' | grep t > /dev/null; then
41   have_echo=yes # Lucky!
42 fi
43 # Try the workarounds.
44 # Respawn using $CONFIG_SHELL if that is set and works.
45 if test -z "$have_echo" \
46    && test "X$1" != "X--no-reexec" \
47    && test -n "$CONFIG_SHELL" \
48    && test -f "$CONFIG_SHELL" \
49    && $CONFIG_SHELL -c 'echo '\t' | grep t > /dev/null'; then
50   exec $CONFIG_SHELL "$0" --no-reexec "$@"
51   exit 127
52 fi
53 # For bash: unset the shell option xpg_echo.
54 if test -z "$have_echo" \
55    && test -n "$BASH_VERSION" \
56    && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
57   shopt -o xpg_echo
58   have_echo=yes
59 fi
60 # For zsh: turn sh-emulation on.
61 if test -z "$have_echo" \
62    && test -n "$ZSH_VERSION" \
63    && (emulate sh) >/dev/null 2>&1; then
64   emulate sh
65 fi
66 # For ksh: alias echo to a function that uses cat of a here document.
67 # The ksh manual page says:
68 #   "Aliasing is performed when scripts are read, not while they are executed.
69 #    Therefore, for an alias to take effect, the alias definition command has
70 #    to be executed before the command which references the alias is read."
71 # Because of this, we have to play strange tricks with have_echo, to ensure
72 # that the top-level statement containing the test start after the 'alias'
73 # command.
74 if test -z "$have_echo"; then
75 bsd_echo ()
76 {
77 cat <<EOF
78 $*
79 EOF
80 }
81 alias echo=bsd_echo 2>/dev/null
82 fi
83 if test -z "$have_echo" \
84    && echo '\t' | grep t > /dev/null; then
85   have_echo=yes
86 fi
87 if test -z "$have_echo"; then
88   unalias echo 2>/dev/null
89 fi
90 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
91 if test -z "$have_echo" \
92    && test "X$1" != "X--no-reexec" \
93    && test -f /bin/ksh; then
94   exec /bin/ksh "$0" --no-reexec "$@"
95   exit 127
96 fi
97 # Otherwise: respawn using /bin/sh.
98 if test -z "$have_echo" \
99    && test "X$1" != "X--no-reexec" \
100    && test -f /bin/sh; then
101   exec /bin/sh "$0" --no-reexec "$@"
102   exit 127
103 fi
104 if test -z "$have_echo"; then
105   func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
106 fi
107 if echo '\t' | grep t > /dev/null; then
108   : # Works fine now.
109 else
110   func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
111 fi
112 if test "X$1" = "X--no-reexec"; then
113   shift
114 fi
115
116 # This command determines the exit code.
117 echo '\t' | grep t > /dev/null