3 # Copyright (C) 2018, 2021 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, write to the Free Software Foundation,
19 # Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21 # Portions from runtest Copyright (C) 1992-2016 Free Software Foundation, Inc.
23 # This script was written by Jacob Bachmeyer. Portions of this script are
24 # adapted from the existing runtest script originally written by Rob Savoye.
26 # This script finds an implementation for the command given, finds the
27 # proper interpreter, and then dispatches the command. This script can
28 # either be run with a command name as the first (few) argument(s), via a
29 # link from the command name, or some combination of those.
31 # shellcheck disable=SC2003
32 # The shellcheck tool complains about use of expr and recommends using
33 # newer shell features instead. Solaris 10 /bin/sh does not support the
34 # newer features, so we must use expr in this script.
36 # shellcheck disable=SC2006
37 # The shellcheck tool complains about the old style backtick command
38 # substitution. Solaris 10 /bin/sh does not support the new style $()
39 # command substitution and the usage of command substitution in this script
40 # is simple enough to work. Most notably, nesting backtick command
41 # substitution is tricky, but we do not do that.
43 # shellcheck disable=SC2209
44 # The shellcheck tool complains about assigning certain constant strings to
45 # variables. In this script, the intended meaning is obvious in context.
48 # #Usage: dejagnu COMMAND [ --help | OPTIONS... ]
49 # #Usage: dejagnu --help
50 # #Usage: dejagnu --version
51 # # --help Print help text
52 # # --version Print DejaGnu version
55 # list of extensions supported for commands in priority order
56 Variants
='gawk awk tcl exp bash sh'
61 # For testing and development
63 if test x
"$1" = x--DGTimpl
; then
73 --help) want_help
=true
;;
74 -v|
--v|
-verbose|
--verbose) verbose
=`expr $verbose + 1` ;;
75 -V|
--V|
-version|
--version) want_version
=true
;;
79 if expr "$verbose" \
> 0 > /dev
/null
; then
80 echo Verbose level is
"$verbose"
83 ## Get the file name of this script and deduce @bindir@.
85 bindir
=`echo "$0" | sed -e 's@/[^/]*$@@'`
86 if expr "$verbose" \
> 0 > /dev
/null
; then
87 echo Running launcher from
"$bindir"
92 # If running from source tree, they are in ./commands/ relative to this script.
93 # If installed, they are in @datadir@/dejagnu/commands/ on the system.
95 # This makes the same assumption as in runtest that one of these holds:
97 # @datadir@ == @bindir@/../share
98 # @datadir@ == @bindir@/../../share
99 # @datadir@ == /usr/share
100 # @datadir@ == /usr/local/share
102 if test -n "$DEJAGNULIBS" ; then
103 commdir
="${DEJAGNULIBS}/commands"
104 datadir
="${DEJAGNULIBS}"
105 elif test -d "${bindir}/commands" && test -f "${bindir}/runtest.exp" ; then
106 if expr "$verbose" \
> 1 > /dev
/null
; then
107 echo Running from
source directory
109 commdir
="${bindir}/commands"
113 bindir1up_check
=`echo "$bindir" | sed -e 's@/[^/]*$@/share/dejagnu@'`
114 bindir2up_check
=`echo "$bindir" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@'`
116 "${bindir1up_check}" "${bindir2up_check}" \
117 /usr
/share
/dejagnu
/usr
/local
/share
/dejagnu
119 if expr "$verbose" \
> 1 > /dev
/null
; then
120 echo Probing directory
"$i"/commands
122 if test -d "$i"/commands
; then
123 commdir
="$i"/commands
130 if test -z "${commdir}" ; then
131 echo ERROR
: could not
find command directory.
135 if expr "$verbose" \
> 0 > /dev
/null
; then
136 echo Looking
for commands
in "$commdir"
139 ## Get the name of the requested command.
141 # Are we just looking for version information?
142 if $want_version ; then
143 frame_version
=`grep '^set frame_version' "${datadir}/runtest.exp" \
144 | sed 's/^[^0-9]*//'`
145 echo 'dejagnu auxiliary launcher (DejaGnu)' "$frame_version"
149 # Remove any leading autoconf platform prefix and the "dejagnu" prefix.
150 # command=`basename "$0" | sed -e 's/^.*-\?dejagnu-\?//'`
152 # The above simple solution is not portable, so we use Awk and two steps:
153 command=`echo "$0" | awk 'BEGIN { FS = "/" } { print $NF }'`
154 # First, we use a simple Awk program to perform the role of basename.
155 command=`echo "$command" | awk 'BEGIN { OFS = FS = "-" }
156 { for (i = 1; i <= NF; i++) if ($i ~ /dejagnu/) break;
157 i++; for (out = ""; i <= NF; i++) out = out OFS $i;
158 print substr(out,2) }'`
159 # Second, we split on "-" and search for a field matching /dejagnu/ to
160 # identify the other prefixes, then skip that field and the second loop
161 # collects any following fields. The spurious leading "-" is then removed
162 # using substr() and the result is returned to the shell.
164 # This roundabout approach maintains compatibility with Solaris 10, where
165 # awk allows only limited manipulation of the record structure.
167 while expr $# \
> 0 > /dev
/null
169 if test -z "${command}" ; then
170 case $1 in -*) break;; esac
174 if expr "$verbose" \
> 2 > /dev
/null
; then
175 echo Looking
for "${commdir}/${command}.*"
177 for ext
in ${Variants}
179 if test -f "${commdir}/${command}.$ext" ; then
183 case $1 in -*) break;; esac
184 if test -n "$1" ; then
185 command="${command}-$1"
193 for ext
in ${Variants}
195 if test -f "${commdir}/${command}.$ext" ; then
196 commext
="$commext $ext"
200 if test -z "$commext" && test -n "$command" ; then
201 echo ERROR
: could not resolve
command "$command"
205 if expr "$verbose" \
> 0 > /dev
/null
; then
206 if test -n "$command"; then
207 if expr "$verbose" \
> 1 > /dev
/null
; then
208 echo Found subcommand
"$command" with variants
: "$commext"
210 echo Found subcommand
"$command"
213 echo Running nothing.
217 ## Find interpreters.
220 if test -n "$AWK" ; then
222 elif test -x "${bindir}/awk" ; then
223 awkbin
="${bindir}/awk"
225 # find what might be a usable awk
226 # on Solaris 10, POSIX awk is in /usr/xpg4/bin
227 for awktest
in mawk
/usr
/xpg
4/bin
/awk nawk
awk ; do
228 if command -v "$awktest" > /dev
/null
2>&1 ; then
234 if test -n "$GAWK" ; then
236 elif test -x "${bindir}/gawk" ; then
237 gawkbin
="${bindir}/gawk"
241 # The non-POSIX awk in /usr/bin on Solaris 10 fails this test
242 if echo |
"$awkbin" '1 && 1 {exit 0}' > /dev
/null
2>&1 ; then
247 if command -v "$gawkbin" > /dev
/null
2>&1 ; then
252 # substitute GNU awk for awk if needed
254 if $have_awk ; then : ; else
259 # is "awk" actually GNU Awk?
261 case `"$awkbin" --version </dev/null 2>&1 | sed 1q` in
262 *'GNU Awk'*) have_gawk_as_awk
=true
;;
263 *) have_gawk_as_awk
=false
;;
266 if expr "$verbose" \
> 2 > /dev
/null
; then
268 echo Awk interpreter is
"$awkbin"
270 echo Awk interpreter was not found
273 echo GNU Awk interpreter is
"$gawkbin"
275 echo GNU Awk interpreter was not found
278 # export chosen Awk and GNU Awk
290 if test -n "$BASH" ; then
292 elif test -x "${bindir}/bash" ; then
293 bashbin
="${bindir}/bash"
294 elif test -x /bin
/bash
; then
299 if command -v "$bashbin" > /dev
/null
2>&1 ; then
304 if expr "$verbose" \
> 2 > /dev
/null
; then
306 echo Bash interpreter is
"$bashbin"
308 echo Bash interpreter was not found
313 # This script is running, therefore we have a Bourne shell.
317 # DejaGnu configure bails out if Expect is not available, but this script
318 # can be run from the source directory without first running configure.
319 if test -n "$EXPECT" ; then
321 elif test -x "${bindir}/expect" ; then
322 expectbin
="${bindir}/expect"
326 if command -v "$expectbin" > /dev
/null
2>&1 ; then
331 if expr "$verbose" \
> 2 > /dev
/null
; then
332 if $have_expect ; then
333 echo Expect interpreter is
"$expectbin"
335 echo Expect interpreter was not found
340 if test -n "$TCLSH" ; then
342 elif test -x "${bindir}/tclsh" ; then
343 tclbin
="${bindir}/tclsh"
347 # substitute expect if needed
348 if command -v "$tclbin" > /dev
/null
2>&1 ; then :
349 elif command -v "$expectbin" > /dev
/null
2>&1 ; then tclbin
="$expectbin"
351 if command -v "$tclbin" > /dev
/null
2>&1 ; then
356 if expr "$verbose" \
> 2 > /dev
/null
; then
358 echo Tcl interpreter is
"$tclbin"
360 echo Tcl interpreter was not found
366 if test -n "$override_ext" ; then
367 selected_ext
="$override_ext"
386 if $have_expect ; then
408 echo ERROR
: '(select-variant)' unrecognized variant
"$v"
412 if test -z "$selected_ext" && test -n "$command" ; then
413 echo ERROR
: no variant of
"$command" was selected
418 if test -n "$command" && expr "$verbose" \
> 0 > /dev
/null
; then
419 if test -n "$override_ext" ; then
420 echo Selected variant
"$selected_ext" by override
422 echo Selected variant
"$selected_ext"
426 ## Dispatch to the selected command.
428 # Are we just looking for a usage message?
430 if $have_awk; then : ; else
431 echo ERROR
: extracting
help message requires POSIX Awk
434 if test -z "$command" ; then
435 # want help on the launcher itself
438 help_file
="${commdir}/${command}.${selected_ext}"
440 if test ! -r "$help_file" ; then
441 echo ERROR
: file "'$help_file'" is not readable
444 if "$AWK" '/#help$/ { pfxlen = length($0) - 4 }
445 pfxlen && substr($0, pfxlen) == "#end" { exit 1 }
446 ' "$help_file" ; then
447 echo ERROR
: file "'$help_file'" does not contain a
help message
450 exec "$AWK" '/#help$/ { pfxlen = length($0) - 4 }
451 pfxlen && substr($0, pfxlen) == "#end" { exit 0 }
452 pfxlen { print substr($0, pfxlen) }' "$help_file"
455 if test -z "$command" ; then
456 if test -n "$override_ext" ; then
457 case $selected_ext in
458 awk) if $have_awk; then exit 0; else exit 1; fi ;;
459 bash
) if $have_bash; then exit 0; else exit 1; fi ;;
460 exp
) if $have_expect; then exit 0; else exit 1; fi ;;
461 gawk
) if $have_gawk; then exit 0; else exit 1; fi ;;
462 tcl
) if $have_tcl; then exit 0; else exit 1; fi ;;
463 sh
) if $have_sh; then exit 0; else exit 1; fi ;;
467 echo ERROR
: no
command given
472 case $selected_ext in
474 if $have_gawk_as_awk ; then
475 exec "$awkbin" --posix -f "${commdir}/${command}.awk" -- ${1+"$@"}
477 exec "$awkbin" -f "${commdir}/${command}.awk" -- ${1+"$@"}
480 bash
) exec "$bashbin" -- "${commdir}/${command}.bash" ${1+"$@"} ;;
481 exp) exec "$expectbin" -- "${commdir}/${command}.exp" ${1+"$@"} ;;
482 gawk
) exec "$gawkbin" -f "${commdir}/${command}.gawk" -- ${1+"$@"} ;;
483 tcl) exec "$tclbin" "${commdir}/${command}.tcl" ${1+"$@"} ;;
484 sh
) exec /bin
/sh
"${commdir}/${command}.sh" ${1+"$@"} ;;
486 echo command: "${command}"
491 echo ERROR: '(run-variant)' unrecognized variant "$selected_ext"