Use shell "case" for pattern matching in dejagnu auxiliary launcher
[dejagnu.git] / dejagnu
blob9f6ae4bc89576aef75b4af7dd16a6dc1a4678c6f
1 #!/bin/sh
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.
47 # ##help
48 # #Usage: dejagnu COMMAND [ --help | OPTIONS... ]
49 # #Usage: dejagnu --help
50 # #Usage: dejagnu --version
51 # # --help Print help text
52 # # --version Print DejaGnu version
53 # ##end
55 # list of extensions supported for commands in priority order
56 Variants='gawk awk tcl exp bash sh'
57 readonly Variants
59 ## Recognize options
61 # For testing and development
62 override_ext=
63 if test x"$1" = x--DGTimpl ; then
64 override_ext=$2
65 shift 2
68 want_help=false
69 want_version=false
70 verbose=0
71 for a in "$@"; do
72 case $a in
73 --help) want_help=true ;;
74 -v|--v|-verbose|--verbose) verbose=`expr $verbose + 1` ;;
75 -V|--V|-version|--version) want_version=true ;;
76 esac
77 done
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"
90 ## Find the commands.
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"
110 datadir="${bindir}"
111 else
112 commdir=
113 bindir1up_check=`echo "$bindir" | sed -e 's@/[^/]*$@/share/dejagnu@'`
114 bindir2up_check=`echo "$bindir" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@'`
115 for i in \
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
124 datadir="$i"
125 break
127 done
130 if test -z "${commdir}" ; then
131 echo ERROR: could not find command directory.
132 exit 2
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"
146 exit 0
149 # Remove any leading autoconf platform prefix and the "dejagnu" prefix.
150 command=`basename "$0" | sed -e 's/^.*-\?dejagnu-\?//'`
152 while expr $# \> 0 > /dev/null
154 if test -z "${command}" ; then
155 case $1 in -*) break;; esac
156 command="$1"
157 shift
159 if expr "$verbose" \> 2 > /dev/null ; then
160 echo Looking for "${commdir}/${command}.*"
162 for ext in ${Variants}
164 if test -f "${commdir}/${command}.$ext" ; then
165 break 2
167 done
168 case $1 in -*) break;; esac
169 if test -n "$1" ; then
170 command="${command}-$1"
171 shift
172 else
173 break
175 done
177 commext=
178 for ext in ${Variants}
180 if test -f "${commdir}/${command}.$ext" ; then
181 commext="$commext $ext"
183 done
185 if test -z "$commext" && test -n "$command" ; then
186 echo ERROR: could not resolve command "$command"
187 exit 2
190 if expr "$verbose" \> 0 > /dev/null ; then
191 if test -n "$command"; then
192 if expr "$verbose" \> 1 > /dev/null ; then
193 echo Found subcommand "$command" with variants: "$commext"
194 else
195 echo Found subcommand "$command"
197 else
198 echo Running nothing.
202 ## Find interpreters.
204 # Awk and GNU awk
205 if test -n "$AWK" ; then
206 awkbin="$AWK"
207 elif test -x "${bindir}/awk" ; then
208 awkbin="${bindir}/awk"
209 else
210 awkbin=awk
212 if test -n "$GAWK" ; then
213 gawkbin="$GAWK"
214 elif test -x "${bindir}/gawk" ; then
215 gawkbin="${bindir}/gawk"
216 else
217 gawkbin=gawk
219 if command -v "$awkbin" > /dev/null 2>&1 ; then
220 have_awk=true
221 else
222 have_awk=false
224 if command -v "$gawkbin" > /dev/null 2>&1 ; then
225 have_gawk=true
226 else
227 have_gawk=false
229 # substitute GNU awk for awk if needed
230 if $have_gawk ; then
231 if $have_awk ; then : ; else
232 have_awk=$have_gawk
233 awkbin=$gawkbin
236 # is "awk" actually GNU Awk?
237 if $have_awk ; then
238 if "$awkbin" --version | sed 1q | grep -qi 'GNU Awk' ; then
239 have_gawk_as_awk=true
240 else
241 have_gawk_as_awk=false
244 if expr "$verbose" \> 2 > /dev/null ; then
245 if $have_awk ; then
246 echo Awk interpreter is "$awkbin"
247 else
248 echo Awk interpreter was not found
250 if $have_gawk ; then
251 echo GNU Awk interpreter is "$gawkbin"
252 else
253 echo GNU Awk interpreter was not found
258 # Bash
259 if test -n "$BASH" ; then
260 bashbin="$BASH"
261 elif test -x "${bindir}/bash" ; then
262 bashbin="${bindir}/bash"
263 elif test -x /bin/bash ; then
264 bashbin=/bin/bash
265 else
266 bashbin=bash
268 if command -v "$bashbin" > /dev/null 2>&1 ; then
269 have_bash=true
270 else
271 have_bash=false
273 if expr "$verbose" \> 2 > /dev/null ; then
274 if $have_bash ; then
275 echo Bash interpreter is "$bashbin"
276 else
277 echo Bash interpreter was not found
281 # Bourne shell
282 # This script is running, therefore we have a Bourne shell.
283 have_sh=true
285 # Expect
286 # DejaGnu configure bails out if Expect is not available, but this script
287 # can be run from the source directory without first running configure.
288 if test -n "$EXPECT" ; then
289 expectbin="$EXPECT"
290 elif test -x "${bindir}/expect" ; then
291 expectbin="${bindir}/expect"
292 else
293 expectbin=expect
295 if command -v "$expectbin" > /dev/null 2>&1 ; then
296 have_expect=true
297 else
298 have_expect=false
300 if expr "$verbose" \> 2 > /dev/null ; then
301 if $have_expect ; then
302 echo Expect interpreter is "$expectbin"
303 else
304 echo Expect interpreter was not found
308 # Tcl
309 if test -n "$TCLSH" ; then
310 tclbin="$TCLSH"
311 elif test -x "${bindir}/tclsh" ; then
312 tclbin="${bindir}/tclsh"
313 else
314 tclbin=tclsh
316 # substitute expect if needed
317 if command -v "$tclbin" > /dev/null 2>&1 ; then :
318 elif command -v "$expectbin" > /dev/null 2>&1 ; then tclbin="$expectbin"
320 if command -v "$tclbin" > /dev/null 2>&1 ; then
321 have_tcl=true
322 else
323 have_tcl=false
325 if expr "$verbose" \> 2 > /dev/null ; then
326 if $have_tcl ; then
327 echo Tcl interpreter is "$tclbin"
328 else
329 echo Tcl interpreter was not found
333 ## Select a variant.
335 if test -n "$override_ext" ; then
336 selected_ext="$override_ext"
337 else
338 selected_ext=
339 for v in $commext
341 case $v in
342 awk)
343 if $have_awk ; then
344 selected_ext=awk
345 break
348 bash)
349 if $have_bash ; then
350 selected_ext=bash
351 break
354 exp)
355 if $have_expect ; then
356 selected_ext=exp
357 break
360 gawk)
361 if $have_gawk ; then
362 selected_ext=gawk
363 break
366 tcl)
367 if $have_tcl ; then
368 selected_ext=tcl
369 break
373 selected_ext=sh
374 break
377 echo ERROR: '(select-variant)' unrecognized variant "$v"
379 esac
380 done
381 if test -z "$selected_ext" && test -n "$command" ; then
382 echo ERROR: no variant of "$command" was selected
383 exit 2
387 if test -n "$command" && expr "$verbose" \> 0 > /dev/null ; then
388 if test -n "$override_ext" ; then
389 echo Selected variant "$selected_ext" by override
390 else
391 echo Selected variant "$selected_ext"
395 ## Dispatch to the selected command.
397 # Are we just looking for a usage message?
398 if $want_help ; then
399 if test -z "$command" ; then
400 # want help on the launcher itself
401 help_file=$0
402 else
403 help_file="${commdir}/${command}.${selected_ext}"
405 if test ! -r "$help_file" ; then
406 echo ERROR: file "'$help_file'" is not readable
407 exit 2
409 if grep -q '#help' "$help_file" \
410 && grep -q '#end' "$help_file"; then : ; else
411 echo ERROR: file "'$help_file'" does not contain a help message
412 exit 2
414 help_prefix_pat=`grep '#help' "$help_file" \
415 | sed -e 's/#help.*$//' -e '1q' | tr '[:print:][:blank:]' .`
416 if expr "$verbose" \> 1 > /dev/null ; then
417 echo Extracting help from "'$help_file'" with prefix "'$help_prefix_pat'"
419 sed -n < "$help_file" \
420 -e '1,/#help/d' \
421 -e '/^'"$help_prefix_pat"'#end/q' \
422 -e 's/^'"$help_prefix_pat"'//;p'
423 exit 0
426 if test -z "$command" ; then
427 if test -n "$override_ext" ; then
428 case $selected_ext in
429 awk) $have_awk; exit $? ;;
430 bash) $have_bash; exit $? ;;
431 exp) $have_expect; exit $? ;;
432 gawk) $have_gawk; exit $? ;;
433 tcl) $have_tcl; exit $? ;;
434 sh) $have_sh; exit $? ;;
435 *) exit 2 ;;
436 esac
437 else
438 echo ERROR: no command given
439 exit 2
443 case $selected_ext in
444 awk)
445 if $have_gawk_as_awk ; then
446 exec "$awkbin" --posix -f "${commdir}/${command}.awk" -- ${1+"$@"}
447 else
448 exec "$awkbin" -f "${commdir}/${command}.awk" -- ${1+"$@"}
451 bash) exec "$bashbin" -- "${commdir}/${command}.bash" ${1+"$@"} ;;
452 exp) exec "$expectbin" -- "${commdir}/${command}.exp" ${1+"$@"} ;;
453 gawk) exec "$gawkbin" -f "${commdir}/${command}.gawk" -- ${1+"$@"} ;;
454 tcl) exec "$tclbin" "${commdir}/${command}.tcl" ${1+"$@"} ;;
455 sh) exec /bin/sh "${commdir}/${command}.sh" ${1+"$@"} ;;
456 echo)
457 echo command: "${command}"
458 echo args: ${1+"$@"}
459 exit 0
462 echo ERROR: '(run-variant)' unrecognized variant "$selected_ext"
463 exit 2
465 esac
467 #EOF