Add Zsh compatibility prologue to scripts
[dejagnu.git] / runtest
blob73122d512af20288e82e5424f50b071577565b6d
1 #! /bin/sh
3 # Copyright (C) 1992-2016, 2021, 2024 Free Software Foundation, Inc.
5 # This file is part of DejaGnu.
7 # DejaGnu is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # DejaGnu is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with DejaGnu. If not, see <http://www.gnu.org/licenses/>.
20 # This script was written by Rob Savoye. The script finds the proper
21 # expect shell and then starts DejaGnu.
23 # shellcheck disable=SC2003
24 # The shellcheck tool complains about use of expr and recommends using
25 # newer shell features instead. Solaris 10 /bin/sh does not support the
26 # newer features, so we must use expr in this script.
28 # shellcheck disable=SC2006
29 # The shellcheck tool complains about the old style backtick command
30 # substitution. Solaris 10 /bin/sh does not support the new style $()
31 # command substitution and the usage of command substitution in this script
32 # is simple enough to work. Most notably, nesting backtick command
33 # substitution is tricky, but we do not do that.
35 # Zsh compatibility prologue adapted from configure
36 if test -n "${ZSH_VERSION+set}" && (emulate sh) > /dev/null 2>&1 ; then
37 emulate sh
38 NULLCMD=:
39 alias -g '${1+"$@"}'='"$@"'
40 setopt NO_GLOB_SUBST
43 # Get the execution path to this script and the current directory.
45 mypath=${0-.}
46 if expr "$mypath" : '.*/.*' > /dev/null
47 then
49 else
50 IFS="${IFS= }"; save_ifs="$IFS"; IFS="${IFS}:"
51 for dir in $PATH
53 test -z "$dir" && dir=.
54 if test -x "$dir/$mypath"
55 then
56 mypath="$dir/$mypath"
57 break
59 done
60 IFS="$save_ifs"
62 execpath=`echo "$mypath" | sed -e 's@/[^/]*$@@'`
64 # Get the name by which runtest was invoked and extract the config
65 # triplet.
67 runtest=`echo "$mypath" | sed -e 's@^.*/@@'`
68 target=`echo "$runtest" | sed -e 's/-runtest$//'`
69 if [ "$target" != runtest ] ; then
70 target="--target ${target}"
71 else
72 target=""
75 # Find the right expect binary to use. If a variable EXPECT exists, it
76 # takes precedence over all other tests. Otherwise look for a freshly
77 # built one, and then use one in the path.
79 if [ -n "$EXPECT" ] ; then
80 expectbin="$EXPECT"
81 else
82 if [ -x "$execpath/expect" ] ; then
83 expectbin="$execpath/expect"
84 else
85 expectbin=expect
89 # Just to be safe ..
91 if [ -z "$expectbin" ]; then
92 echo "ERROR: No expect shell found"
93 exit 1
96 # This wrapper script will set up run-time library search PATHs.
98 if [ -x "$expectbin-bld.sh" ]; then
99 expectbin="${CONFIG_SHELL-/bin/sh} $expectbin-bld.sh"
102 # Extract a few options from the option list.
104 verbose=0
105 debug=""
106 for a in "$@" ; do
107 case $a in
108 -v|--v|-verb*|--verb*) verbose=`expr "$verbose" + 1` ;;
109 -D0|--D0) debug="-D 0" ;;
110 -D1|--D1) debug="-D 1" ;;
111 esac
112 done
114 if expr "$verbose" \> 0 > /dev/null ; then
115 echo Expect binary is "$expectbin"
118 # Find runtest.exp. First we look in its installed location,
119 # otherwise start if from the source tree.
121 # runtest.exp is found in @datadir@ (set by configure), but $execpath
122 # is @bindir@. We're assuming that:
124 # @datadir@ == @bindir@/../share
125 # or
126 # @datadir@ == @bindir@/../../share
128 # .. which is a very weak assumption
130 bindir1up_check=`echo "$execpath" | sed -e 's@/[^/]*$@/share/dejagnu@'`
131 bindir2up_check=`echo "$execpath" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@'`
133 for i in \
134 "${bindir1up_check}" "${bindir2up_check}" "$execpath" \
135 /usr/share/dejagnu \
136 /usr/local/share/dejagnu ; do
137 if expr "$verbose" \> 1 > /dev/null ; then
138 echo Looking for "$i"/runtest.exp.
140 if [ -f "$i/runtest.exp" ] ; then
141 runpath="$i"
142 if expr "$verbose" \> 0 > /dev/null ; then
143 echo Using "$i"/runtest.exp as main test driver
145 break
147 done
149 # Check for an environment variable.
151 if [ -n "$DEJAGNULIBS" ] ; then
152 runpath="$DEJAGNULIBS"
153 if expr "$verbose" \> 0 > /dev/null ; then
154 echo Using "$DEJAGNULIBS"/runtest.exp as main test driver
157 if [ -z "$runpath" ] ; then
158 echo "ERROR: runtest.exp does not exist"
159 exit 1
162 if command -v "$expectbin" > /dev/null ; then :; else
163 echo "ERROR: unable to find expect in the PATH"
164 exit 1
167 # The `debug' and `target' variables are _intended_ to contain zero or two
168 # words each. Word splitting is desired here.
169 # shellcheck disable=SC2086
170 exec "$expectbin" $debug -- "$runpath"/runtest.exp $target ${1+"$@"}