Fix problems with relative srcdir in testsuite/runtest.main/options.exp
[dejagnu.git] / dejagnu
blob7e75940ce9caa45b37448fc7027032a0ddad1cd7
1 #!/bin/sh
3 # Copyright (C) 2018 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=SC2209
32 # The shellcheck tool complains about assigning certain constant strings to
33 # variables. In this script, the intended meaning is obvious in context.
35 # ##help
36 # #Usage: dejagnu <command> [--help|options...]
37 # #Usage: dejagnu --help
38 # #Usage: dejagnu --version
39 # # --help Print help text
40 # # --version Print DejaGnu version
41 # ##end
43 # list of extensions supported for commands in priority order
44 readonly Variants="gawk awk tcl exp bash sh"
46 ## Recognize options
48 # For testing and development
49 override_ext=
50 if test x"$1" = x--DGTimpl ; then
51 override_ext=$2
52 shift 2
55 want_help=false
56 want_version=false
57 verbose=0
58 for a in "$@"; do
59 case $a in
60 --help) want_help=true ;;
61 -v|--v|-verbose|--verbose) verbose=$((verbose + 1)) ;;
62 -V|--V|-version|--version) want_version=true ;;
63 esac
64 done
66 if expr "$verbose" \> 0 > /dev/null ; then
67 echo Verbose level is $verbose
70 ## Get the file name of this script and deduce @bindir@.
72 bindir="$(echo "$0" | sed -e 's@/[^/]*$@@')"
73 if expr "$verbose" \> 0 > /dev/null ; then
74 echo Running launcher from "$bindir"
77 ## Find the commands.
79 # If running from source tree, they are in ./commands/ relative to this script.
80 # If installed, they are in @datadir@/dejagnu/commands/ on the system.
82 # This makes the same assumption as in runtest that one of these holds:
84 # @datadir@ == @bindir@/../share
85 # @datadir@ == @bindir@/../../share
86 # @datadir@ == /usr/share
87 # @datadir@ == /usr/local/share
89 if test -n "$DEJAGNULIBS" ; then
90 commdir="${DEJAGNULIBS}/commands"
91 datadir="${DEJAGNULIBS}"
92 elif test -d "${bindir}/commands" && test -f "${bindir}/runtest.exp" ; then
93 if expr "$verbose" \> 1 > /dev/null ; then
94 echo Running from source directory
96 commdir="${bindir}/commands"
97 datadir="${bindir}"
98 else
99 commdir=
100 for i in \
101 "$(echo "$bindir" | sed -e 's@/[^/]*$@/share/dejagnu@')" \
102 "$(echo "$bindir" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@')" \
103 /usr/share/dejagnu /usr/local/share/dejagnu
105 if expr "$verbose" \> 1 > /dev/null ; then
106 echo Probing directory "$i"/commands
108 if test -d "$i"/commands ; then
109 commdir="$i"/commands
110 datadir="$i"
111 break
113 done
116 if test -z "${commdir}" ; then
117 echo ERROR: could not find command directory.
118 exit 2
121 if expr "$verbose" \> 0 > /dev/null ; then
122 echo Looking for commands in "$commdir"
125 ## Get the name of the requested command.
127 # Are we just looking for version information?
128 if $want_version ; then
129 frame_version=$(grep '^set frame_version' "${datadir}/runtest.exp" \
130 | sed 's/^[^0-9]*//')
131 echo 'dejagnu auxiliary launcher (DejaGnu)' "$frame_version"
132 exit 0
135 # Remove any leading autoconf platform prefix and the "dejagnu" prefix.
136 command="$(basename "$0" | sed -e 's/^.*-\?dejagnu-\?//')"
138 while expr $# \> 0 > /dev/null
140 if test -z "${command}" ; then
141 if expr "$1" : - > /dev/null ; then
142 break
144 command="$1"
145 shift
147 if expr "$verbose" \> 2 > /dev/null ; then
148 echo Looking for "${commdir}/${command}.*"
150 for ext in ${Variants}
152 if test -f "${commdir}/${command}.$ext" ; then
153 break 2
155 done
156 if expr "$1" : - > /dev/null ; then
157 break
159 if test -n "$1" ; then
160 command="${command}-$1"
161 shift
162 else
163 break
165 done
167 commext=
168 for ext in ${Variants}
170 if test -f "${commdir}/${command}.$ext" ; then
171 commext="$commext $ext"
173 done
175 if test -z "$commext" && test -n "$command" ; then
176 echo ERROR: could not resolve command "$command"
177 exit 2
180 if expr "$verbose" \> 0 > /dev/null ; then
181 if test -n "$command"; then
182 if expr "$verbose" \> 1 > /dev/null ; then
183 echo Found subcommand "$command" with variants: "$commext"
184 else
185 echo Found subcommand "$command"
187 else
188 echo Running nothing.
192 ## Find interpreters.
194 # Awk and GNU awk
195 if test -n "$AWK" ; then
196 awkbin="$AWK"
197 elif test -x "${bindir}/awk" ; then
198 awkbin="${bindir}/awk"
199 else
200 awkbin=awk
202 if test -n "$GAWK" ; then
203 gawkbin="$GAWK"
204 elif test -x "${bindir}/gawk" ; then
205 gawkbin="${bindir}/gawk"
206 else
207 gawkbin=gawk
209 if command -v "$awkbin" > /dev/null 2>&1 ; then
210 have_awk=true
211 else
212 have_awk=false
214 if command -v "$gawkbin" > /dev/null 2>&1 ; then
215 have_gawk=true
216 else
217 have_gawk=false
219 # substitute GNU awk for awk if needed
220 if $have_gawk ; then
221 if $have_awk ; then : ; else
222 have_awk=$have_gawk
223 awkbin=$gawkbin
226 # is "awk" actually GNU Awk?
227 if $have_awk ; then
228 if "$awkbin" --version | sed 1q | grep -qi 'GNU Awk' ; then
229 have_gawk_as_awk=true
230 else
231 have_gawk_as_awk=false
234 if expr "$verbose" \> 2 > /dev/null ; then
235 if $have_awk ; then
236 echo Awk interpreter is "$awkbin"
237 else
238 echo Awk interpreter was not found
240 if $have_gawk ; then
241 echo GNU Awk interpreter is "$gawkbin"
242 else
243 echo GNU Awk interpreter was not found
248 # Bash
249 if test -n "$BASH" ; then
250 bashbin="$BASH"
251 elif test -x "${bindir}/bash" ; then
252 bashbin="${bindir}/bash"
253 elif test -x /bin/bash ; then
254 bashbin=/bin/bash
255 else
256 bashbin=bash
258 if command -v "$bashbin" > /dev/null 2>&1 ; then
259 have_bash=true
260 else
261 have_bash=false
263 if expr "$verbose" \> 2 > /dev/null ; then
264 if $have_bash ; then
265 echo Bash interpreter is "$bashbin"
266 else
267 echo Bash interpreter was not found
271 # Bourne shell
272 # This script is running, therefore we have a Bourne shell.
273 have_sh=true
275 # Expect
276 # DejaGnu configure bails out if Expect is not available, but this script
277 # can be run from the source directory without first running configure.
278 if test -n "$EXPECT" ; then
279 expectbin="$EXPECT"
280 elif test -x "${bindir}/expect" ; then
281 expectbin="${bindir}/expect"
282 else
283 expectbin=expect
285 if command -v "$expectbin" > /dev/null 2>&1 ; then
286 have_expect=true
287 else
288 have_expect=false
290 if expr "$verbose" \> 2 > /dev/null ; then
291 if $have_expect ; then
292 echo Expect interpreter is "$expectbin"
293 else
294 echo Expect interpreter was not found
298 # Tcl
299 if test -n "$TCLSH" ; then
300 tclbin="$TCLSH"
301 elif test -x "${bindir}/tclsh" ; then
302 tclbin="${bindir}/tclsh"
303 else
304 tclbin=tclsh
306 # substitute expect if needed
307 if command -v "$tclbin" > /dev/null 2>&1 ; then :
308 elif command -v "$expectbin" > /dev/null 2>&1 ; then tclbin="$expectbin"
310 if command -v "$tclbin" > /dev/null 2>&1 ; then
311 have_tcl=true
312 else
313 have_tcl=false
315 if expr "$verbose" \> 2 > /dev/null ; then
316 if $have_tcl ; then
317 echo Tcl interpreter is "$tclbin"
318 else
319 echo Tcl interpreter was not found
323 ## Select a variant.
325 if test -n "$override_ext" ; then
326 selected_ext="$override_ext"
327 else
328 selected_ext=
329 for v in $commext
331 case $v in
332 awk)
333 if $have_awk ; then
334 selected_ext=awk
335 break
338 bash)
339 if $have_bash ; then
340 selected_ext=bash
341 break
344 exp)
345 if $have_expect ; then
346 selected_ext=exp
347 break
350 gawk)
351 if $have_gawk ; then
352 selected_ext=gawk
353 break
356 tcl)
357 if $have_tcl ; then
358 selected_ext=tcl
359 break
363 selected_ext=sh
364 break
367 echo ERROR: '(select-variant)' unrecognized variant "$v"
369 esac
370 done
371 if test -z "$selected_ext" && test -n "$command" ; then
372 echo ERROR: no variant of "$command" was selected
373 exit 2
377 if test -n "$command" && expr "$verbose" \> 0 > /dev/null ; then
378 if test -n "$override_ext" ; then
379 echo Selected variant "$selected_ext" by override
380 else
381 echo Selected variant "$selected_ext"
385 ## Dispatch to the selected command.
387 # Are we just looking for a usage message?
388 if $want_help ; then
389 if test -z "$command" ; then
390 # want help on the launcher itself
391 help_file=$0
392 else
393 help_file="${commdir}/${command}.${selected_ext}"
395 if test ! -r "$help_file" ; then
396 echo ERROR: file "'$help_file'" is not readable
397 exit 2
399 if grep -q '#help' "$help_file" \
400 && grep -q '#end' "$help_file"; then : ; else
401 echo ERROR: file "'$help_file'" does not contain a help message
402 exit 2
404 help_prefix_pat=$(grep '#help' "$help_file" \
405 | sed -e 's/#help.*$//' -e '1q' | tr '[:print:][:blank:]' .)
406 if expr "$verbose" \> 1 > /dev/null ; then
407 echo Extracting help from "'$help_file'" with prefix "'$help_prefix_pat'"
409 sed -n < "$help_file" \
410 -e '1,/#help/d' \
411 -e '/^'"$help_prefix_pat"'#end/q' \
412 -e 's/^'"$help_prefix_pat"'//;p'
413 exit 0
416 if test -z "$command" ; then
417 if test -n "$override_ext" ; then
418 case $selected_ext in
419 awk) $have_awk; exit $? ;;
420 bash) $have_bash; exit $? ;;
421 exp) $have_expect; exit $? ;;
422 gawk) $have_gawk; exit $? ;;
423 tcl) $have_tcl; exit $? ;;
424 sh) $have_sh; exit $? ;;
425 *) exit 2 ;;
426 esac
427 else
428 echo ERROR: no command given
429 exit 2
433 case $selected_ext in
434 awk)
435 if $have_gawk_as_awk ; then
436 exec "$awkbin" --posix -f "${commdir}/${command}.awk" -- ${1+"$@"}
437 else
438 exec "$awkbin" -f "${commdir}/${command}.awk" -- ${1+"$@"}
441 bash) exec "$bashbin" -- "${commdir}/${command}.bash" ${1+"$@"} ;;
442 exp) exec "$expectbin" -- "${commdir}/${command}.exp" ${1+"$@"} ;;
443 gawk) exec "$gawkbin" -f "${commdir}/${command}.gawk" -- ${1+"$@"} ;;
444 tcl) exec "$tclbin" "${commdir}/${command}.tcl" ${1+"$@"} ;;
445 sh) exec /bin/sh "${commdir}/${command}.sh" ${1+"$@"} ;;
446 echo)
447 echo command: "${command}"
448 echo args: ${1+"$@"}
449 exit 0
452 echo ERROR: '(run-variant)' unrecognized variant "$selected_ext"
453 exit 2
455 esac
457 #EOF