Tweak comment.
[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 (except that
24 # BSD echo interprets '-n' as an option, which is also not desirable).
25 # Nowadays the problem occurs in 4 situations:
26 # - in bash, when the shell option xpg_echo is set (bash >= 2.04)
27 #            or when it was built with --enable-usg-echo-default (bash >= 2.0)
28 #            or when it was built with DEFAULT_ECHO_TO_USG (bash < 2.0),
29 # - in zsh, when sh-emulation is not set,
30 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
31 #           and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
32 # - in Solaris /bin/sh and OSF/1 /bin/sh.
33 # We try the following workarounds:
34 # - for all: respawn using $CONFIG_SHELL if that is set and works.
35 # - for bash >= 2.04: unset the shell option xpg_echo.
36 # - for bash >= 2.0: define echo to a function that uses the printf built-in.
37 # - for bash < 2.0: define echo to a function that uses cat of a here document.
38 # - for zsh: turn sh-emulation on.
39 # - for ksh: alias echo to a function that uses cat of a here document.
40 # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround.
41 # - otherwise: respawn using /bin/sh and rely on the workarounds.
42 # When respawning, we pass --no-reexec as first argument, so as to avoid
43 # turning this script into a fork bomb in unlucky situations.
44 have_echo=
45 if echo '\t' | grep t > /dev/null; then
46   have_echo=yes # Lucky!
47 fi
48 # Try the workarounds.
49 # Respawn using $CONFIG_SHELL if that is set and works.
50 if test -z "$have_echo" \
51    && test "X$1" != "X--no-reexec" \
52    && test -n "$CONFIG_SHELL" \
53    && test -f "$CONFIG_SHELL" \
54    && $CONFIG_SHELL -c 'echo '\t' | grep t > /dev/null'; then
55   exec $CONFIG_SHELL "$0" --no-reexec "$@"
56   exit 127
57 fi
58 # For bash >= 2.04: unset the shell option xpg_echo.
59 if test -z "$have_echo" \
60    && test -n "$BASH_VERSION" \
61    && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
62   shopt -o xpg_echo
63   have_echo=yes
64 fi
65 # For bash >= 2.0: define echo to a function that uses the printf built-in.
66 # For bash < 2.0: define echo to a function that uses cat of a here document.
67 # (There is no win in using 'printf' over 'cat' if it is not a shell built-in.)
68 if test -z "$have_echo" \
69    && test -n "$BASH_VERSION"; then \
70   if type printf 2>/dev/null | grep / > /dev/null; then
71     # 'printf' is not a shell built-in.
72 echo ()
73 {
74 cat <<EOF
75 $*
76 EOF
77 }
78   else
79     # 'printf' is a shell built-in.
80 echo ()
81 {
82   printf '%s\n' "$*"
83 }
84   fi
85   if echo '\t' | grep t > /dev/null; then
86     have_echo=yes
87   fi
88 fi
89 # For zsh: turn sh-emulation on.
90 if test -z "$have_echo" \
91    && test -n "$ZSH_VERSION" \
92    && (emulate sh) >/dev/null 2>&1; then
93   emulate sh
94 fi
95 # For ksh: alias echo to a function that uses cat of a here document.
96 # The ksh manual page says:
97 #   "Aliasing is performed when scripts are read, not while they are executed.
98 #    Therefore, for an alias to take effect, the alias definition command has
99 #    to be executed before the command which references the alias is read."
100 # Because of this, we have to play strange tricks with have_echo, to ensure
101 # that the top-level statement containing the test starts after the 'alias'
102 # command.
103 if test -z "$have_echo"; then
104 bsd_echo ()
105 {
106 cat <<EOF
107 $*
108 EOF
109 }
110 alias echo=bsd_echo 2>/dev/null
111 fi
112 if test -z "$have_echo" \
113    && echo '\t' | grep t > /dev/null; then
114   have_echo=yes
115 fi
116 if test -z "$have_echo"; then
117   unalias echo 2>/dev/null
118 fi
119 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
120 if test -z "$have_echo" \
121    && test "X$1" != "X--no-reexec" \
122    && test -f /bin/ksh; then
123   exec /bin/ksh "$0" --no-reexec "$@"
124   exit 127
125 fi
126 # Otherwise: respawn using /bin/sh.
127 if test -z "$have_echo" \
128    && test "X$1" != "X--no-reexec" \
129    && test -f /bin/sh; then
130   exec /bin/sh "$0" --no-reexec "$@"
131   exit 127
132 fi
133 if test -z "$have_echo"; then
134   func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
135 fi
136 if echo '\t' | grep t > /dev/null; then
137   : # Works fine now.
138 else
139   func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
140 fi
141 if test "X$1" = "X--no-reexec"; then
142   shift
143 fi
144
145 # This command determines the exit code.
146 echo '\t' | grep t > /dev/null