Fix typo in reference manual
[dejagnu.git] / dejagnu
blobf3eb5de6bacf414d4503094d0ee898d41bf6503b
1 #! /bin/sh
3 # Copyright (C) 2018, 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 # Portions from runtest Copyright (C) 1992-2016 Free Software Foundation, Inc.
22 # This script was written by Jacob Bachmeyer. Portions of this script are
23 # adapted from the existing runtest script originally written by Rob Savoye.
25 # This script finds an implementation for the command given, finds the
26 # proper interpreter, and then dispatches the command. This script can
27 # either be run with a command name as the first (few) argument(s), via a
28 # link from the command name, or some combination of those.
30 # shellcheck disable=SC2003
31 # The shellcheck tool complains about use of expr and recommends using
32 # newer shell features instead. Solaris 10 /bin/sh does not support the
33 # newer features, so we must use expr in this script.
35 # shellcheck disable=SC2006
36 # The shellcheck tool complains about the old style backtick command
37 # substitution. Solaris 10 /bin/sh does not support the new style $()
38 # command substitution and the usage of command substitution in this script
39 # is simple enough to work. Most notably, nesting backtick command
40 # substitution is tricky, but we do not do that.
42 # shellcheck disable=SC2016
43 # The shellcheck tool complains about single-quoted strings containing "$",
44 # such as Awk programs that access fields as aid to programmers unfamiliar
45 # with how the shell quoting rules differ from Python and JavaScript.
47 # shellcheck disable=SC2209
48 # The shellcheck tool complains about assigning certain constant strings to
49 # variables. In this script, the intended meaning is obvious in context.
51 # shellcheck disable=SC2268
52 # The shellcheck tool complains about using 'x' as a common prefix on both
53 # sides of a comparison. While this is no longer required, it does no harm.
55 # ##help
56 # #Usage: dejagnu COMMAND [ --help | OPTIONS... ]
57 # #Usage: dejagnu --help
58 # #Usage: dejagnu --version
59 # # --help Print help text
60 # # --version Print DejaGnu version
61 # ##end
63 # Zsh compatibility prologue adapted from configure
64 if test -n "${ZSH_VERSION+set}" && (emulate sh) > /dev/null 2>&1 ; then
65 emulate sh
66 NULLCMD=:
67 alias -g '${1+"$@"}'='"$@"'
68 setopt NO_GLOB_SUBST
71 # list of extensions supported for commands in priority order
72 Variants='gawk awk tcl exp bash sh'
73 readonly Variants
75 # POSIX defines a getconf(1) command; here it returns a default PATH
76 SysPATH=`getconf PATH`
77 # make sure that the "system PATH" mentions at least one directory
78 case $SysPATH in
79 */*) ;; # good
80 *) SysPATH=$PATH ;; # otherwise copy the user PATH
81 esac
82 readonly SysPATH
84 ## Recognize options
86 # For testing and development
87 override_ext=
88 if test x"$1" = x--DGTimpl ; then
89 override_ext=$2
90 shift 2
93 want_help=false
94 want_version=false
95 verbose=0
96 for a in "$@"; do
97 case $a in
98 --help) want_help=true ;;
99 -v|--v|-verbose|--verbose) verbose=`expr "$verbose" + 1` ;;
100 -V|--V|-version|--version) want_version=true ;;
101 esac
102 done
104 if expr "$verbose" \> 0 > /dev/null ; then
105 echo Verbose level is "$verbose"
108 ## Get the file name of this script and deduce @bindir@.
110 bindir=`echo "$0" | sed -e 's@/[^/]*$@@'`
111 if expr "$verbose" \> 0 > /dev/null ; then
112 echo Running launcher from "$bindir"
115 ## Find the commands.
117 # If running from source tree, they are in ./commands/ relative to this script.
118 # If installed, they are in @datadir@/dejagnu/commands/ on the system.
120 # This makes the same assumption as in runtest that one of these holds:
122 # @datadir@ == @bindir@/../share
123 # @datadir@ == @bindir@/../../share
124 # @datadir@ == /usr/share
125 # @datadir@ == /usr/local/share
127 if test -n "$DEJAGNULIBS" ; then
128 commdir="${DEJAGNULIBS}/commands"
129 datadir="${DEJAGNULIBS}"
130 elif test -d "${bindir}/commands" && test -f "${bindir}/runtest.exp" ; then
131 if expr "$verbose" \> 1 > /dev/null ; then
132 echo Running from source directory
134 commdir="${bindir}/commands"
135 datadir="${bindir}"
136 else
137 commdir=
138 bindir1up_check=`echo "$bindir" | sed -e 's@/[^/]*$@/share/dejagnu@'`
139 bindir2up_check=`echo "$bindir" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@'`
140 for i in \
141 "${bindir1up_check}" "${bindir2up_check}" \
142 /usr/share/dejagnu /usr/local/share/dejagnu
144 if expr "$verbose" \> 1 > /dev/null ; then
145 echo Probing directory "$i"/commands
147 if test -d "$i"/commands ; then
148 commdir="$i"/commands
149 datadir="$i"
150 break
152 done
155 if test -z "${commdir}" ; then
156 echo ERROR: could not find command directory.
157 exit 2
160 if expr "$verbose" \> 0 > /dev/null ; then
161 echo Looking for commands in "$commdir"
164 ## Get the name of the requested command.
166 # Are we just looking for version information?
167 if $want_version ; then
168 frame_version=`grep '^set frame_version' "${datadir}/runtest.exp" \
169 | sed 's/^[^0-9]*//'`
170 echo 'dejagnu auxiliary launcher (DejaGnu)' "$frame_version"
171 exit 0
174 # Remove any leading autoconf platform prefix and the "dejagnu" prefix.
175 # command=`basename "$0" | sed -e 's/^.*-\?dejagnu-\?//'`
177 # The above simple solution is not portable, so we use Awk and two steps:
178 command=`echo "$0" | awk 'BEGIN { FS = "/" } { print $NF }'`
179 # First, we use a simple Awk program to perform the role of basename.
180 command=`echo "$command" | awk 'BEGIN { OFS = FS = "-" }
181 { for (i = 1; i <= NF; i++) if ($i ~ /dejagnu/) break;
182 i++; for (out = ""; i <= NF; i++) out = out OFS $i;
183 print substr(out,2) }'`
184 # Second, we split on "-" and search for a field matching /dejagnu/ to
185 # identify the other prefixes, then skip that field and the second loop
186 # collects any following fields. The spurious leading "-" is then removed
187 # using substr() and the result is returned to the shell.
189 # This roundabout approach maintains compatibility with Solaris 10, where
190 # awk allows only limited manipulation of the record structure.
192 while expr $# \> 0 > /dev/null
194 if test -z "${command}" ; then
195 case $1 in -*) break;; esac
196 command="$1"
197 shift
199 if expr "$verbose" \> 2 > /dev/null ; then
200 echo Looking for "${commdir}/${command}.*"
202 for ext in ${Variants}
204 if test -f "${commdir}/${command}.$ext" ; then
205 break 2
207 done
208 case $1 in -*) break;; esac
209 if test -n "$1" ; then
210 command="${command}-$1"
211 shift
212 else
213 break
215 done
217 commext=
218 for ext in ${Variants}
220 if test -f "${commdir}/${command}.$ext" ; then
221 commext="$commext $ext"
223 done
225 if test -z "$commext" && test -n "$command" ; then
226 echo ERROR: could not resolve command "$command"
227 exit 2
230 if expr "$verbose" \> 0 > /dev/null ; then
231 if test -n "$command"; then
232 if expr "$verbose" \> 1 > /dev/null ; then
233 echo Found subcommand "$command" with variants: "$commext"
234 else
235 echo Found subcommand "$command"
237 else
238 echo Running nothing.
242 ## Find interpreters.
244 # Awk and GNU awk
245 have_awk=false
246 if test -n "$AWK" ; then
247 awkbin="$AWK"
248 if command -v "$awkbin" > /dev/null 2>&1 ; then
249 have_awk=true
251 else
252 # find a usable awk
253 # on Solaris 10, POSIX awk is in /usr/xpg4/bin
254 for testPATH in "$SysPATH" "$PATH" ; do
255 for awktest in mawk awk nawk ; do
256 if PATH=$testPATH command -v "$awktest" > /dev/null 2>&1 ; then
257 awkbin=`PATH=$testPATH command -v "$awktest"`
258 # The non-POSIX awk in /usr/bin on Solaris 10 fails this test
259 if echo | "$awkbin" '1 && 1 {exit 0}' > /dev/null 2>&1 ; then
260 have_awk=true
261 break 2
264 done
265 done
267 if test -n "$GAWK" ; then
268 gawkbin="$GAWK"
269 else
270 gawkbin=gawk
272 if command -v "$gawkbin" > /dev/null 2>&1 ; then
273 have_gawk=true
274 else
275 have_gawk=false
277 # substitute GNU awk for awk if needed
278 if $have_gawk ; then
279 if $have_awk ; then : ; else
280 have_awk=$have_gawk
281 awkbin=$gawkbin
284 # is "awk" actually GNU Awk?
285 if $have_awk ; then
286 case `"$awkbin" --version </dev/null 2>&1 | sed 1q` in
287 *'GNU Awk'*) have_gawk_as_awk=true ;;
288 *) have_gawk_as_awk=false ;;
289 esac
291 if expr "$verbose" \> 2 > /dev/null ; then
292 if $have_awk ; then
293 echo Awk interpreter is "$awkbin"
294 else
295 echo Awk interpreter was not found
297 if $have_gawk ; then
298 echo GNU Awk interpreter is "$gawkbin"
299 else
300 echo GNU Awk interpreter was not found
303 # export chosen Awk and GNU Awk
304 if $have_awk ; then
305 AWK=$awkbin
306 export AWK
308 if $have_gawk ; then
309 GAWK=$gawkbin
310 export GAWK
314 # Bash
315 if test -n "$BASH" ; then
316 bashbin="$BASH"
317 elif test -x "${bindir}/bash" ; then
318 bashbin="${bindir}/bash"
319 elif test -x /bin/bash ; then
320 bashbin=/bin/bash
321 else
322 bashbin=bash
324 if command -v "$bashbin" > /dev/null 2>&1 ; then
325 have_bash=true
326 else
327 have_bash=false
329 if expr "$verbose" \> 2 > /dev/null ; then
330 if $have_bash ; then
331 echo Bash interpreter is "$bashbin"
332 else
333 echo Bash interpreter was not found
337 # Expect
338 # DejaGnu configure bails out if Expect is not available, but this script
339 # can be run from the source directory without first running configure.
340 if test -n "$EXPECT" ; then
341 expectbin="$EXPECT"
342 elif test -x "${bindir}/expect" ; then
343 expectbin="${bindir}/expect"
344 else
345 expectbin=expect
347 if command -v "$expectbin" > /dev/null 2>&1 ; then
348 have_expect=true
349 else
350 have_expect=false
352 if expr "$verbose" \> 2 > /dev/null ; then
353 if $have_expect ; then
354 echo Expect interpreter is "$expectbin"
355 else
356 echo Expect interpreter was not found
360 # POSIX shell
361 # This script is running, so we have a Bourne shell, but do we have or can
362 # we easily find a POSIX shell?
363 have_sh=false
364 # As of 2024-09-17, according to the "APPLICATION USAGE" section in
365 # <URL:https://pubs.opengroup.org/onlinepubs/9799919799/utilities/sh.html>,
366 # POSIX says that `command -v sh` using the PATH from `getconf PATH` should
367 # reliably find a POSIX shell; for now, we assume it so...
368 if test -n "$POSIX_SHELL" ; then
369 shbin="$POSIX_SHELL"
370 if command -v "$shbin" > /dev/null 2>&1 ; then
371 have_sh=true
373 else
374 # find a POSIX shell
375 for testPATH in "$SysPATH" "$PATH" ; do
376 if PATH=$testPATH command -v sh > /dev/null 2>&1 ; then
377 shbin=`PATH=$testPATH command -v sh`
378 have_sh=true
379 break
381 done
384 # Tcl
385 if test -n "$TCLSH" ; then
386 tclbin="$TCLSH"
387 elif test -x "${bindir}/tclsh" ; then
388 tclbin="${bindir}/tclsh"
389 else
390 tclbin=tclsh
392 # substitute expect if needed
393 if command -v "$tclbin" > /dev/null 2>&1 ; then :
394 elif command -v "$expectbin" > /dev/null 2>&1 ; then tclbin="$expectbin"
396 if command -v "$tclbin" > /dev/null 2>&1 ; then
397 have_tcl=true
398 else
399 have_tcl=false
401 if expr "$verbose" \> 2 > /dev/null ; then
402 if $have_tcl ; then
403 echo Tcl interpreter is "$tclbin"
404 else
405 echo Tcl interpreter was not found
409 ## Select a variant.
411 if test -n "$override_ext" ; then
412 selected_ext="$override_ext"
413 else
414 selected_ext=
415 for v in $commext
417 case $v in
418 awk)
419 if $have_awk ; then
420 selected_ext=awk
421 break
424 bash)
425 if $have_bash ; then
426 selected_ext=bash
427 break
430 exp)
431 if $have_expect ; then
432 selected_ext=exp
433 break
436 gawk)
437 if $have_gawk ; then
438 selected_ext=gawk
439 break
442 tcl)
443 if $have_tcl ; then
444 selected_ext=tcl
445 break
449 if $have_sh ; then
450 selected_ext=sh
451 break
455 echo ERROR: '(select-variant)' unrecognized variant "$v"
457 esac
458 done
459 if test -z "$selected_ext" && test -n "$command" ; then
460 echo ERROR: no variant of "$command" was selected
461 exit 2
465 if test -n "$command" && expr "$verbose" \> 0 > /dev/null ; then
466 if test -n "$override_ext" ; then
467 echo Selected variant "$selected_ext" by override
468 else
469 echo Selected variant "$selected_ext"
473 ## Dispatch to the selected command.
475 # Are we just looking for a usage message?
476 if $want_help ; then
477 if $have_awk; then : ; else
478 echo ERROR: extracting help message requires POSIX Awk
479 exit 2
481 if test -z "$command" ; then
482 # want help on the launcher itself
483 help_file=$0
484 else
485 help_file="${commdir}/${command}.${selected_ext}"
487 if test ! -r "$help_file" ; then
488 echo ERROR: file "'$help_file'" is not readable
489 exit 2
491 if "$AWK" '/#help$/ { pfxlen = length($0) - 4 }
492 pfxlen && substr($0, pfxlen) == "#end" { exit 1 }
493 ' "$help_file" ; then
494 echo ERROR: file "'$help_file'" does not contain a help message
495 exit 2
497 exec "$AWK" '/#help$/ { pfxlen = length($0) - 4; next }
498 pfxlen && substr($0, pfxlen) == "#end" { exit 0 }
499 pfxlen { print substr($0, pfxlen) }' "$help_file"
502 if test -z "$command" ; then
503 if test -n "$override_ext" ; then
504 case $selected_ext in
505 awk) if $have_awk; then exit 0; else exit 1; fi ;;
506 bash) if $have_bash; then exit 0; else exit 1; fi ;;
507 exp) if $have_expect; then exit 0; else exit 1; fi ;;
508 gawk) if $have_gawk; then exit 0; else exit 1; fi ;;
509 tcl) if $have_tcl; then exit 0; else exit 1; fi ;;
510 sh) if $have_sh; then exit 0; else exit 1; fi ;;
511 *) exit 2 ;;
512 esac
513 else
514 echo ERROR: no command given
515 exit 2
519 case $selected_ext in
520 awk)
521 if $have_gawk_as_awk ; then
522 exec "$awkbin" --posix -f "${commdir}/${command}.awk" -- ${1+"$@"}
523 else
524 exec "$awkbin" -f "${commdir}/${command}.awk" -- ${1+"$@"}
527 bash) exec "$bashbin" -- "${commdir}/${command}.bash" ${1+"$@"} ;;
528 exp) exec "$expectbin" -- "${commdir}/${command}.exp" ${1+"$@"} ;;
529 gawk) exec "$gawkbin" -f "${commdir}/${command}.gawk" -- ${1+"$@"} ;;
530 tcl) exec "$tclbin" "${commdir}/${command}.tcl" ${1+"$@"} ;;
531 sh) exec "$shbin" "${commdir}/${command}.sh" ${1+"$@"} ;;
532 echo)
533 echo command: "${command}"
534 echo args: ${1+"$@"}
535 exit 0
538 echo ERROR: '(run-variant)' unrecognized variant "$selected_ext"
539 exit 2
541 esac
543 #EOF