Revise usage messages to better fit GNU conventions
[dejagnu.git] / dejagnu
blob695c4e564fb9fa346cc1ed1c81640a3f01f374c8
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 readonly Variants="gawk awk tcl exp bash sh"
58 ## Recognize options
60 # For testing and development
61 override_ext=
62 if test x"$1" = x--DGTimpl ; then
63 override_ext=$2
64 shift 2
67 want_help=false
68 want_version=false
69 verbose=0
70 for a in "$@"; do
71 case $a in
72 --help) want_help=true ;;
73 -v|--v|-verbose|--verbose) verbose=`expr $verbose + 1` ;;
74 -V|--V|-version|--version) want_version=true ;;
75 esac
76 done
78 if expr "$verbose" \> 0 > /dev/null ; then
79 echo Verbose level is "$verbose"
82 ## Get the file name of this script and deduce @bindir@.
84 bindir=`echo "$0" | sed -e 's@/[^/]*$@@'`
85 if expr "$verbose" \> 0 > /dev/null ; then
86 echo Running launcher from "$bindir"
89 ## Find the commands.
91 # If running from source tree, they are in ./commands/ relative to this script.
92 # If installed, they are in @datadir@/dejagnu/commands/ on the system.
94 # This makes the same assumption as in runtest that one of these holds:
96 # @datadir@ == @bindir@/../share
97 # @datadir@ == @bindir@/../../share
98 # @datadir@ == /usr/share
99 # @datadir@ == /usr/local/share
101 if test -n "$DEJAGNULIBS" ; then
102 commdir="${DEJAGNULIBS}/commands"
103 datadir="${DEJAGNULIBS}"
104 elif test -d "${bindir}/commands" && test -f "${bindir}/runtest.exp" ; then
105 if expr "$verbose" \> 1 > /dev/null ; then
106 echo Running from source directory
108 commdir="${bindir}/commands"
109 datadir="${bindir}"
110 else
111 commdir=
112 bindir1up_check=`echo "$bindir" | sed -e 's@/[^/]*$@/share/dejagnu@'`
113 bindir2up_check=`echo "$bindir" | sed -e 's@/[^/]*/[^/]*$@/share/dejagnu@'`
114 for i in \
115 "${bindir1up_check}" "${bindir2up_check}" \
116 /usr/share/dejagnu /usr/local/share/dejagnu
118 if expr "$verbose" \> 1 > /dev/null ; then
119 echo Probing directory "$i"/commands
121 if test -d "$i"/commands ; then
122 commdir="$i"/commands
123 datadir="$i"
124 break
126 done
129 if test -z "${commdir}" ; then
130 echo ERROR: could not find command directory.
131 exit 2
134 if expr "$verbose" \> 0 > /dev/null ; then
135 echo Looking for commands in "$commdir"
138 ## Get the name of the requested command.
140 # Are we just looking for version information?
141 if $want_version ; then
142 frame_version=`grep '^set frame_version' "${datadir}/runtest.exp" \
143 | sed 's/^[^0-9]*//'`
144 echo 'dejagnu auxiliary launcher (DejaGnu)' "$frame_version"
145 exit 0
148 # Remove any leading autoconf platform prefix and the "dejagnu" prefix.
149 command=`basename "$0" | sed -e 's/^.*-\?dejagnu-\?//'`
151 while expr $# \> 0 > /dev/null
153 if test -z "${command}" ; then
154 if expr "$1" : - > /dev/null ; then
155 break
157 command="$1"
158 shift
160 if expr "$verbose" \> 2 > /dev/null ; then
161 echo Looking for "${commdir}/${command}.*"
163 for ext in ${Variants}
165 if test -f "${commdir}/${command}.$ext" ; then
166 break 2
168 done
169 if expr "$1" : - > /dev/null ; then
170 break
172 if test -n "$1" ; then
173 command="${command}-$1"
174 shift
175 else
176 break
178 done
180 commext=
181 for ext in ${Variants}
183 if test -f "${commdir}/${command}.$ext" ; then
184 commext="$commext $ext"
186 done
188 if test -z "$commext" && test -n "$command" ; then
189 echo ERROR: could not resolve command "$command"
190 exit 2
193 if expr "$verbose" \> 0 > /dev/null ; then
194 if test -n "$command"; then
195 if expr "$verbose" \> 1 > /dev/null ; then
196 echo Found subcommand "$command" with variants: "$commext"
197 else
198 echo Found subcommand "$command"
200 else
201 echo Running nothing.
205 ## Find interpreters.
207 # Awk and GNU awk
208 if test -n "$AWK" ; then
209 awkbin="$AWK"
210 elif test -x "${bindir}/awk" ; then
211 awkbin="${bindir}/awk"
212 else
213 awkbin=awk
215 if test -n "$GAWK" ; then
216 gawkbin="$GAWK"
217 elif test -x "${bindir}/gawk" ; then
218 gawkbin="${bindir}/gawk"
219 else
220 gawkbin=gawk
222 if command -v "$awkbin" > /dev/null 2>&1 ; then
223 have_awk=true
224 else
225 have_awk=false
227 if command -v "$gawkbin" > /dev/null 2>&1 ; then
228 have_gawk=true
229 else
230 have_gawk=false
232 # substitute GNU awk for awk if needed
233 if $have_gawk ; then
234 if $have_awk ; then : ; else
235 have_awk=$have_gawk
236 awkbin=$gawkbin
239 # is "awk" actually GNU Awk?
240 if $have_awk ; then
241 if "$awkbin" --version | sed 1q | grep -qi 'GNU Awk' ; then
242 have_gawk_as_awk=true
243 else
244 have_gawk_as_awk=false
247 if expr "$verbose" \> 2 > /dev/null ; then
248 if $have_awk ; then
249 echo Awk interpreter is "$awkbin"
250 else
251 echo Awk interpreter was not found
253 if $have_gawk ; then
254 echo GNU Awk interpreter is "$gawkbin"
255 else
256 echo GNU Awk interpreter was not found
261 # Bash
262 if test -n "$BASH" ; then
263 bashbin="$BASH"
264 elif test -x "${bindir}/bash" ; then
265 bashbin="${bindir}/bash"
266 elif test -x /bin/bash ; then
267 bashbin=/bin/bash
268 else
269 bashbin=bash
271 if command -v "$bashbin" > /dev/null 2>&1 ; then
272 have_bash=true
273 else
274 have_bash=false
276 if expr "$verbose" \> 2 > /dev/null ; then
277 if $have_bash ; then
278 echo Bash interpreter is "$bashbin"
279 else
280 echo Bash interpreter was not found
284 # Bourne shell
285 # This script is running, therefore we have a Bourne shell.
286 have_sh=true
288 # Expect
289 # DejaGnu configure bails out if Expect is not available, but this script
290 # can be run from the source directory without first running configure.
291 if test -n "$EXPECT" ; then
292 expectbin="$EXPECT"
293 elif test -x "${bindir}/expect" ; then
294 expectbin="${bindir}/expect"
295 else
296 expectbin=expect
298 if command -v "$expectbin" > /dev/null 2>&1 ; then
299 have_expect=true
300 else
301 have_expect=false
303 if expr "$verbose" \> 2 > /dev/null ; then
304 if $have_expect ; then
305 echo Expect interpreter is "$expectbin"
306 else
307 echo Expect interpreter was not found
311 # Tcl
312 if test -n "$TCLSH" ; then
313 tclbin="$TCLSH"
314 elif test -x "${bindir}/tclsh" ; then
315 tclbin="${bindir}/tclsh"
316 else
317 tclbin=tclsh
319 # substitute expect if needed
320 if command -v "$tclbin" > /dev/null 2>&1 ; then :
321 elif command -v "$expectbin" > /dev/null 2>&1 ; then tclbin="$expectbin"
323 if command -v "$tclbin" > /dev/null 2>&1 ; then
324 have_tcl=true
325 else
326 have_tcl=false
328 if expr "$verbose" \> 2 > /dev/null ; then
329 if $have_tcl ; then
330 echo Tcl interpreter is "$tclbin"
331 else
332 echo Tcl interpreter was not found
336 ## Select a variant.
338 if test -n "$override_ext" ; then
339 selected_ext="$override_ext"
340 else
341 selected_ext=
342 for v in $commext
344 case $v in
345 awk)
346 if $have_awk ; then
347 selected_ext=awk
348 break
351 bash)
352 if $have_bash ; then
353 selected_ext=bash
354 break
357 exp)
358 if $have_expect ; then
359 selected_ext=exp
360 break
363 gawk)
364 if $have_gawk ; then
365 selected_ext=gawk
366 break
369 tcl)
370 if $have_tcl ; then
371 selected_ext=tcl
372 break
376 selected_ext=sh
377 break
380 echo ERROR: '(select-variant)' unrecognized variant "$v"
382 esac
383 done
384 if test -z "$selected_ext" && test -n "$command" ; then
385 echo ERROR: no variant of "$command" was selected
386 exit 2
390 if test -n "$command" && expr "$verbose" \> 0 > /dev/null ; then
391 if test -n "$override_ext" ; then
392 echo Selected variant "$selected_ext" by override
393 else
394 echo Selected variant "$selected_ext"
398 ## Dispatch to the selected command.
400 # Are we just looking for a usage message?
401 if $want_help ; then
402 if test -z "$command" ; then
403 # want help on the launcher itself
404 help_file=$0
405 else
406 help_file="${commdir}/${command}.${selected_ext}"
408 if test ! -r "$help_file" ; then
409 echo ERROR: file "'$help_file'" is not readable
410 exit 2
412 if grep -q '#help' "$help_file" \
413 && grep -q '#end' "$help_file"; then : ; else
414 echo ERROR: file "'$help_file'" does not contain a help message
415 exit 2
417 help_prefix_pat=`grep '#help' "$help_file" \
418 | sed -e 's/#help.*$//' -e '1q' | tr '[:print:][:blank:]' .`
419 if expr "$verbose" \> 1 > /dev/null ; then
420 echo Extracting help from "'$help_file'" with prefix "'$help_prefix_pat'"
422 sed -n < "$help_file" \
423 -e '1,/#help/d' \
424 -e '/^'"$help_prefix_pat"'#end/q' \
425 -e 's/^'"$help_prefix_pat"'//;p'
426 exit 0
429 if test -z "$command" ; then
430 if test -n "$override_ext" ; then
431 case $selected_ext in
432 awk) $have_awk; exit $? ;;
433 bash) $have_bash; exit $? ;;
434 exp) $have_expect; exit $? ;;
435 gawk) $have_gawk; exit $? ;;
436 tcl) $have_tcl; exit $? ;;
437 sh) $have_sh; exit $? ;;
438 *) exit 2 ;;
439 esac
440 else
441 echo ERROR: no command given
442 exit 2
446 case $selected_ext in
447 awk)
448 if $have_gawk_as_awk ; then
449 exec "$awkbin" --posix -f "${commdir}/${command}.awk" -- ${1+"$@"}
450 else
451 exec "$awkbin" -f "${commdir}/${command}.awk" -- ${1+"$@"}
454 bash) exec "$bashbin" -- "${commdir}/${command}.bash" ${1+"$@"} ;;
455 exp) exec "$expectbin" -- "${commdir}/${command}.exp" ${1+"$@"} ;;
456 gawk) exec "$gawkbin" -f "${commdir}/${command}.gawk" -- ${1+"$@"} ;;
457 tcl) exec "$tclbin" "${commdir}/${command}.tcl" ${1+"$@"} ;;
458 sh) exec /bin/sh "${commdir}/${command}.sh" ${1+"$@"} ;;
459 echo)
460 echo command: "${command}"
461 echo args: ${1+"$@"}
462 exit 0
465 echo ERROR: '(run-variant)' unrecognized variant "$selected_ext"
466 exit 2
468 esac
470 #EOF