xref: /netbsd-src/external/bsd/zstd/dist/tests/gzip/init.sh (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos# source this file; set up for tests
2*3117ece4Schristos
3*3117ece4Schristos# Copyright (C) 2009-2016 Free Software Foundation, Inc.
4*3117ece4Schristos
5*3117ece4Schristos# This program is free software: you can redistribute it and/or modify
6*3117ece4Schristos# it under the terms of the GNU General Public License as published by
7*3117ece4Schristos# the Free Software Foundation, either version 3 of the License, or
8*3117ece4Schristos# (at your option) any later version.
9*3117ece4Schristos
10*3117ece4Schristos# This program is distributed in the hope that it will be useful,
11*3117ece4Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
12*3117ece4Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*3117ece4Schristos# GNU General Public License for more details.
14*3117ece4Schristos
15*3117ece4Schristos# You should have received a copy of the GNU General Public License
16*3117ece4Schristos# along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*3117ece4Schristos
18*3117ece4Schristos# Using this file in a test
19*3117ece4Schristos# =========================
20*3117ece4Schristos#
21*3117ece4Schristos# The typical skeleton of a test looks like this:
22*3117ece4Schristos#
23*3117ece4Schristos#   #!/bin/sh
24*3117ece4Schristos#   . "${srcdir=.}/init.sh"; path_prepend_ .
25*3117ece4Schristos#   Execute some commands.
26*3117ece4Schristos#   Note that these commands are executed in a subdirectory, therefore you
27*3117ece4Schristos#   need to prepend "../" to relative filenames in the build directory.
28*3117ece4Schristos#   Note that the "path_prepend_ ." is useful only if the body of your
29*3117ece4Schristos#   test invokes programs residing in the initial directory.
30*3117ece4Schristos#   For example, if the programs you want to test are in src/, and this test
31*3117ece4Schristos#   script is named tests/test-1, then you would use "path_prepend_ ../src",
32*3117ece4Schristos#   or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
33*3117ece4Schristos#   to all tests via automake's TESTS_ENVIRONMENT.
34*3117ece4Schristos#   Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
35*3117ece4Schristos#   Use the skip_ and fail_ functions to print a diagnostic and then exit
36*3117ece4Schristos#   with the corresponding exit code.
37*3117ece4Schristos#   Exit $?
38*3117ece4Schristos
39*3117ece4Schristos# Executing a test that uses this file
40*3117ece4Schristos# ====================================
41*3117ece4Schristos#
42*3117ece4Schristos# Running a single test:
43*3117ece4Schristos#   $ make check TESTS=test-foo.sh
44*3117ece4Schristos#
45*3117ece4Schristos# Running a single test, with verbose output:
46*3117ece4Schristos#   $ make check TESTS=test-foo.sh VERBOSE=yes
47*3117ece4Schristos#
48*3117ece4Schristos# Running a single test, with single-stepping:
49*3117ece4Schristos#   1. Go into a sub-shell:
50*3117ece4Schristos#   $ bash
51*3117ece4Schristos#   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
52*3117ece4Schristos#      Makefile:
53*3117ece4Schristos#   $ export srcdir=../../tests # this is an example
54*3117ece4Schristos#   3. Execute the commands from the test, copy&pasting them one by one:
55*3117ece4Schristos#   $ . "$srcdir/init.sh"; path_prepend_ .
56*3117ece4Schristos#   ...
57*3117ece4Schristos#   4. Finally
58*3117ece4Schristos#   $ exit
59*3117ece4Schristos
60*3117ece4SchristosME_=`expr "./$0" : '.*/\(.*\)$'`
61*3117ece4Schristos
62*3117ece4Schristos# We use a trap below for cleanup.  This requires us to go through
63*3117ece4Schristos# hoops to get the right exit status transported through the handler.
64*3117ece4Schristos# So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
65*3117ece4Schristos# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
66*3117ece4Schristos# sh inside this function.
67*3117ece4SchristosExit () { set +e; (exit $1); exit $1; }
68*3117ece4Schristos
69*3117ece4Schristos# Print warnings (e.g., about skipped and failed tests) to this file number.
70*3117ece4Schristos# Override by defining to say, 9, in init.cfg, and putting say,
71*3117ece4Schristos#   export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
72*3117ece4Schristos# in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
73*3117ece4Schristos# This is useful when using automake's parallel tests mode, to print
74*3117ece4Schristos# the reason for skip/failure to console, rather than to the .log files.
75*3117ece4Schristos: ${stderr_fileno_=2}
76*3117ece4Schristos
77*3117ece4Schristos# Note that correct expansion of "$*" depends on IFS starting with ' '.
78*3117ece4Schristos# Always write the full diagnostic to stderr.
79*3117ece4Schristos# When stderr_fileno_ is not 2, also emit the first line of the
80*3117ece4Schristos# diagnostic to that file descriptor.
81*3117ece4Schristoswarn_ ()
82*3117ece4Schristos{
83*3117ece4Schristos  # If IFS does not start with ' ', set it and emit the warning in a subshell.
84*3117ece4Schristos  case $IFS in
85*3117ece4Schristos    ' '*) printf '%s\n' "$*" >&2
86*3117ece4Schristos          test $stderr_fileno_ = 2 \
87*3117ece4Schristos            || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
88*3117ece4Schristos    *) (IFS=' '; warn_ "$@");;
89*3117ece4Schristos  esac
90*3117ece4Schristos}
91*3117ece4Schristosfail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
92*3117ece4Schristosskip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
93*3117ece4Schristosfatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
94*3117ece4Schristosframework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
95*3117ece4Schristos
96*3117ece4Schristos# This is used to simplify checking of the return value
97*3117ece4Schristos# which is useful when ensuring a command fails as desired.
98*3117ece4Schristos# I.e., just doing `command ... &&fail=1` will not catch
99*3117ece4Schristos# a segfault in command for example.  With this helper you
100*3117ece4Schristos# instead check an explicit exit code like
101*3117ece4Schristos#   returns_ 1 command ... || fail
102*3117ece4Schristosreturns_ () {
103*3117ece4Schristos  # Disable tracing so it doesn't interfere with stderr of the wrapped command
104*3117ece4Schristos  { set +x; } 2>/dev/null
105*3117ece4Schristos
106*3117ece4Schristos  local exp_exit="$1"
107*3117ece4Schristos  shift
108*3117ece4Schristos  "$@"
109*3117ece4Schristos  test $? -eq $exp_exit && ret_=0 || ret_=1
110*3117ece4Schristos
111*3117ece4Schristos  if test "$VERBOSE" = yes && test "$gl_set_x_corrupts_stderr_" = false; then
112*3117ece4Schristos    set -x
113*3117ece4Schristos  fi
114*3117ece4Schristos  { return $ret_; } 2>/dev/null
115*3117ece4Schristos}
116*3117ece4Schristos
117*3117ece4Schristos# Sanitize this shell to POSIX mode, if possible.
118*3117ece4SchristosDUALCASE=1; export DUALCASE
119*3117ece4Schristosif test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
120*3117ece4Schristos  emulate sh
121*3117ece4Schristos  NULLCMD=:
122*3117ece4Schristos  alias -g '${1+"$@"}'='"$@"'
123*3117ece4Schristos  setopt NO_GLOB_SUBST
124*3117ece4Schristoselse
125*3117ece4Schristos  case `(set -o) 2>/dev/null` in
126*3117ece4Schristos    *posix*) set -o posix ;;
127*3117ece4Schristos  esac
128*3117ece4Schristosfi
129*3117ece4Schristos
130*3117ece4Schristos# We require $(...) support unconditionally.
131*3117ece4Schristos# We require a few additional shell features only when $EXEEXT is nonempty,
132*3117ece4Schristos# in order to support automatic $EXEEXT emulation:
133*3117ece4Schristos# - hyphen-containing alias names
134*3117ece4Schristos# - we prefer to use ${var#...} substitution, rather than having
135*3117ece4Schristos#   to work around lack of support for that feature.
136*3117ece4Schristos# The following code attempts to find a shell with support for these features.
137*3117ece4Schristos# If the current shell passes the test, we're done.  Otherwise, test other
138*3117ece4Schristos# shells until we find one that passes.  If one is found, re-exec it.
139*3117ece4Schristos# If no acceptable shell is found, skip the current test.
140*3117ece4Schristos#
141*3117ece4Schristos# The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
142*3117ece4Schristos# emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
143*3117ece4Schristos#
144*3117ece4Schristos# Use "9" to indicate success (rather than 0), in case some shell acts
145*3117ece4Schristos# like Solaris 10's /bin/sh but exits successfully instead of with status 2.
146*3117ece4Schristos
147*3117ece4Schristos# Eval this code in a subshell to determine a shell's suitability.
148*3117ece4Schristos# 10 - passes all tests; ok to use
149*3117ece4Schristos#  9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
150*3117ece4Schristos#  ? - not ok
151*3117ece4Schristosgl_shell_test_script_='
152*3117ece4Schristostest $(echo y) = y || exit 1
153*3117ece4Schristosf_local_() { local v=1; }; f_local_ || exit 1
154*3117ece4Schristosscore_=10
155*3117ece4Schristosif test "$VERBOSE" = yes; then
156*3117ece4Schristos  test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
157*3117ece4Schristosfi
158*3117ece4Schristostest -z "$EXEEXT" && exit $score_
159*3117ece4Schristosshopt -s expand_aliases
160*3117ece4Schristosalias a-b="echo zoo"
161*3117ece4Schristosv=abx
162*3117ece4Schristos     test ${v%x} = ab \
163*3117ece4Schristos  && test ${v#a} = bx \
164*3117ece4Schristos  && test $(a-b) = zoo \
165*3117ece4Schristos  && exit $score_
166*3117ece4Schristos'
167*3117ece4Schristos
168*3117ece4Schristosif test "x$1" = "x--no-reexec"; then
169*3117ece4Schristos  shift
170*3117ece4Schristoselse
171*3117ece4Schristos  # Assume a working shell.  Export to subshells (setup_ needs this).
172*3117ece4Schristos  gl_set_x_corrupts_stderr_=false
173*3117ece4Schristos  export gl_set_x_corrupts_stderr_
174*3117ece4Schristos
175*3117ece4Schristos  # Record the first marginally acceptable shell.
176*3117ece4Schristos  marginal_=
177*3117ece4Schristos
178*3117ece4Schristos  # Search for a shell that meets our requirements.
179*3117ece4Schristos  for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
180*3117ece4Schristos      /bin/sh bash dash zsh pdksh fail
181*3117ece4Schristos  do
182*3117ece4Schristos    test "$re_shell_" = no_shell && continue
183*3117ece4Schristos
184*3117ece4Schristos    # If we've made it all the way to the sentinel, "fail" without
185*3117ece4Schristos    # finding even a marginal shell, skip this test.
186*3117ece4Schristos    if test "$re_shell_" = fail; then
187*3117ece4Schristos      test -z "$marginal_" && skip_ failed to find an adequate shell
188*3117ece4Schristos      re_shell_=$marginal_
189*3117ece4Schristos      break
190*3117ece4Schristos    fi
191*3117ece4Schristos
192*3117ece4Schristos    # When testing the current shell, simply "eval" the test code.
193*3117ece4Schristos    # Otherwise, run it via $re_shell_ -c ...
194*3117ece4Schristos    if test "$re_shell_" = __current__; then
195*3117ece4Schristos      # 'eval'ing this code makes Solaris 10's /bin/sh exit with
196*3117ece4Schristos      # $? set to 2.  It does not evaluate any of the code after the
197*3117ece4Schristos      # "unexpected" first '('.  Thus, we must run it in a subshell.
198*3117ece4Schristos      ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
199*3117ece4Schristos    else
200*3117ece4Schristos      "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
201*3117ece4Schristos    fi
202*3117ece4Schristos
203*3117ece4Schristos    st_=$?
204*3117ece4Schristos
205*3117ece4Schristos    # $re_shell_ works just fine.  Use it.
206*3117ece4Schristos    if test $st_ = 10; then
207*3117ece4Schristos      gl_set_x_corrupts_stderr_=false
208*3117ece4Schristos      break
209*3117ece4Schristos    fi
210*3117ece4Schristos
211*3117ece4Schristos    # If this is our first marginally acceptable shell, remember it.
212*3117ece4Schristos    if test "$st_:$marginal_" = 9: ; then
213*3117ece4Schristos      marginal_="$re_shell_"
214*3117ece4Schristos      gl_set_x_corrupts_stderr_=true
215*3117ece4Schristos    fi
216*3117ece4Schristos  done
217*3117ece4Schristos
218*3117ece4Schristos  if test "$re_shell_" != __current__; then
219*3117ece4Schristos    # Found a usable shell.  Preserve -v and -x.
220*3117ece4Schristos    case $- in
221*3117ece4Schristos      *v*x* | *x*v*) opts_=-vx ;;
222*3117ece4Schristos      *v*) opts_=-v ;;
223*3117ece4Schristos      *x*) opts_=-x ;;
224*3117ece4Schristos      *) opts_= ;;
225*3117ece4Schristos    esac
226*3117ece4Schristos    re_shell=$re_shell_
227*3117ece4Schristos    export re_shell
228*3117ece4Schristos    exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
229*3117ece4Schristos    echo "$ME_: exec failed" 1>&2
230*3117ece4Schristos    exit 127
231*3117ece4Schristos  fi
232*3117ece4Schristosfi
233*3117ece4Schristos
234*3117ece4Schristos# If this is bash, turn off all aliases.
235*3117ece4Schristostest -n "$BASH_VERSION" && unalias -a
236*3117ece4Schristos
237*3117ece4Schristos# Note that when supporting $EXEEXT (transparently mapping from PROG_NAME to
238*3117ece4Schristos# PROG_NAME.exe), we want to support hyphen-containing names like test-acos.
239*3117ece4Schristos# That is part of the shell-selection test above.  Why use aliases rather
240*3117ece4Schristos# than functions?  Because support for hyphen-containing aliases is more
241*3117ece4Schristos# widespread than that for hyphen-containing function names.
242*3117ece4Schristostest -n "$EXEEXT" && shopt -s expand_aliases
243*3117ece4Schristos
244*3117ece4Schristos# Enable glibc's malloc-perturbing option.
245*3117ece4Schristos# This is useful for exposing code that depends on the fact that
246*3117ece4Schristos# malloc-related functions often return memory that is mostly zeroed.
247*3117ece4Schristos# If you have the time and cycles, use valgrind to do an even better job.
248*3117ece4Schristos: ${MALLOC_PERTURB_=87}
249*3117ece4Schristosexport MALLOC_PERTURB_
250*3117ece4Schristos
251*3117ece4Schristos# This is a stub function that is run upon trap (upon regular exit and
252*3117ece4Schristos# interrupt).  Override it with a per-test function, e.g., to unmount
253*3117ece4Schristos# a partition, or to undo any other global state changes.
254*3117ece4Schristoscleanup_ () { :; }
255*3117ece4Schristos
256*3117ece4Schristos# Emit a header similar to that from diff -u;  Print the simulated "diff"
257*3117ece4Schristos# command so that the order of arguments is clear.  Don't bother with @@ lines.
258*3117ece4Schristosemit_diff_u_header_ ()
259*3117ece4Schristos{
260*3117ece4Schristos  printf '%s\n' "diff -u $*" \
261*3117ece4Schristos    "--- $1	1970-01-01" \
262*3117ece4Schristos    "+++ $2	1970-01-01"
263*3117ece4Schristos}
264*3117ece4Schristos
265*3117ece4Schristos# Arrange not to let diff or cmp operate on /dev/null,
266*3117ece4Schristos# since on some systems (at least OSF/1 5.1), that doesn't work.
267*3117ece4Schristos# When there are not two arguments, or no argument is /dev/null, return 2.
268*3117ece4Schristos# When one argument is /dev/null and the other is not empty,
269*3117ece4Schristos# cat the nonempty file to stderr and return 1.
270*3117ece4Schristos# Otherwise, return 0.
271*3117ece4Schristoscompare_dev_null_ ()
272*3117ece4Schristos{
273*3117ece4Schristos  test $# = 2 || return 2
274*3117ece4Schristos
275*3117ece4Schristos  if test "x$1" = x/dev/null; then
276*3117ece4Schristos    test -s "$2" || return 0
277*3117ece4Schristos    emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
278*3117ece4Schristos    return 1
279*3117ece4Schristos  fi
280*3117ece4Schristos
281*3117ece4Schristos  if test "x$2" = x/dev/null; then
282*3117ece4Schristos    test -s "$1" || return 0
283*3117ece4Schristos    emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
284*3117ece4Schristos    return 1
285*3117ece4Schristos  fi
286*3117ece4Schristos
287*3117ece4Schristos  return 2
288*3117ece4Schristos}
289*3117ece4Schristos
290*3117ece4Schristosif diff_out_=`exec 2>/dev/null; diff -u "$0" "$0" < /dev/null` \
291*3117ece4Schristos   && diff -u Makefile "$0" 2>/dev/null | grep '^[+]#!' >/dev/null; then
292*3117ece4Schristos  # diff accepts the -u option and does not (like AIX 7 'diff') produce an
293*3117ece4Schristos  # extra space on column 1 of every content line.
294*3117ece4Schristos  if test -z "$diff_out_"; then
295*3117ece4Schristos    compare_ () { diff -u "$@"; }
296*3117ece4Schristos  else
297*3117ece4Schristos    compare_ ()
298*3117ece4Schristos    {
299*3117ece4Schristos      if diff -u "$@" > diff.out; then
300*3117ece4Schristos        # No differences were found, but Solaris 'diff' produces output
301*3117ece4Schristos        # "No differences encountered". Hide this output.
302*3117ece4Schristos        rm -f diff.out
303*3117ece4Schristos        true
304*3117ece4Schristos      else
305*3117ece4Schristos        cat diff.out
306*3117ece4Schristos        rm -f diff.out
307*3117ece4Schristos        false
308*3117ece4Schristos      fi
309*3117ece4Schristos    }
310*3117ece4Schristos  fi
311*3117ece4Schristoselif
312*3117ece4Schristos  for diff_opt_ in -U3 -c '' no; do
313*3117ece4Schristos    test "$diff_opt_" = no && break
314*3117ece4Schristos    diff_out_=`exec 2>/dev/null; diff $diff_opt_ "$0" "$0" </dev/null` && break
315*3117ece4Schristos  done
316*3117ece4Schristos  test "$diff_opt_" != no
317*3117ece4Schristosthen
318*3117ece4Schristos  if test -z "$diff_out_"; then
319*3117ece4Schristos    compare_ () { diff $diff_opt_ "$@"; }
320*3117ece4Schristos  else
321*3117ece4Schristos    compare_ ()
322*3117ece4Schristos    {
323*3117ece4Schristos      if diff $diff_opt_ "$@" > diff.out; then
324*3117ece4Schristos        # No differences were found, but AIX and HP-UX 'diff' produce output
325*3117ece4Schristos        # "No differences encountered" or "There are no differences between the
326*3117ece4Schristos        # files.". Hide this output.
327*3117ece4Schristos        rm -f diff.out
328*3117ece4Schristos        true
329*3117ece4Schristos      else
330*3117ece4Schristos        cat diff.out
331*3117ece4Schristos        rm -f diff.out
332*3117ece4Schristos        false
333*3117ece4Schristos      fi
334*3117ece4Schristos    }
335*3117ece4Schristos  fi
336*3117ece4Schristoselif cmp -s /dev/null /dev/null 2>/dev/null; then
337*3117ece4Schristos  compare_ () { cmp -s "$@"; }
338*3117ece4Schristoselse
339*3117ece4Schristos  compare_ () { cmp "$@"; }
340*3117ece4Schristosfi
341*3117ece4Schristos
342*3117ece4Schristos# Usage: compare EXPECTED ACTUAL
343*3117ece4Schristos#
344*3117ece4Schristos# Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
345*3117ece4Schristos# Otherwise, propagate $? to caller: any diffs have already been printed.
346*3117ece4Schristoscompare ()
347*3117ece4Schristos{
348*3117ece4Schristos  # This looks like it can be factored to use a simple "case $?"
349*3117ece4Schristos  # after unchecked compare_dev_null_ invocation, but that would
350*3117ece4Schristos  # fail in a "set -e" environment.
351*3117ece4Schristos  if compare_dev_null_ "$@"; then
352*3117ece4Schristos    return 0
353*3117ece4Schristos  else
354*3117ece4Schristos    case $? in
355*3117ece4Schristos      1) return 1;;
356*3117ece4Schristos      *) compare_ "$@";;
357*3117ece4Schristos    esac
358*3117ece4Schristos  fi
359*3117ece4Schristos}
360*3117ece4Schristos
361*3117ece4Schristos# An arbitrary prefix to help distinguish test directories.
362*3117ece4Schristostestdir_prefix_ () { printf gt; }
363*3117ece4Schristos
364*3117ece4Schristos# Run the user-overridable cleanup_ function, remove the temporary
365*3117ece4Schristos# directory and exit with the incoming value of $?.
366*3117ece4Schristosremove_tmp_ ()
367*3117ece4Schristos{
368*3117ece4Schristos  __st=$?
369*3117ece4Schristos  cleanup_
370*3117ece4Schristos  # cd out of the directory we're about to remove
371*3117ece4Schristos  cd "$initial_cwd_" || cd / || cd /tmp
372*3117ece4Schristos  chmod -R u+rwx "$test_dir_"
373*3117ece4Schristos  # If removal fails and exit status was to be 0, then change it to 1.
374*3117ece4Schristos  rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
375*3117ece4Schristos  exit $__st
376*3117ece4Schristos}
377*3117ece4Schristos
378*3117ece4Schristos# Given a directory name, DIR, if every entry in it that matches *.exe
379*3117ece4Schristos# contains only the specified bytes (see the case stmt below), then print
380*3117ece4Schristos# a space-separated list of those names and return 0.  Otherwise, don't
381*3117ece4Schristos# print anything and return 1.  Naming constraints apply also to DIR.
382*3117ece4Schristosfind_exe_basenames_ ()
383*3117ece4Schristos{
384*3117ece4Schristos  feb_dir_=$1
385*3117ece4Schristos  feb_fail_=0
386*3117ece4Schristos  feb_result_=
387*3117ece4Schristos  feb_sp_=
388*3117ece4Schristos  for feb_file_ in $feb_dir_/*.exe; do
389*3117ece4Schristos    # If there was no *.exe file, or there existed a file named "*.exe" that
390*3117ece4Schristos    # was deleted between the above glob expansion and the existence test
391*3117ece4Schristos    # below, just skip it.
392*3117ece4Schristos    test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
393*3117ece4Schristos      && continue
394*3117ece4Schristos    # Exempt [.exe, since we can't create a function by that name, yet
395*3117ece4Schristos    # we can't invoke [ by PATH search anyways due to shell builtins.
396*3117ece4Schristos    test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
397*3117ece4Schristos    case $feb_file_ in
398*3117ece4Schristos      *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
399*3117ece4Schristos      *) # Remove leading file name components as well as the .exe suffix.
400*3117ece4Schristos         feb_file_=${feb_file_##*/}
401*3117ece4Schristos         feb_file_=${feb_file_%.exe}
402*3117ece4Schristos         feb_result_="$feb_result_$feb_sp_$feb_file_";;
403*3117ece4Schristos    esac
404*3117ece4Schristos    feb_sp_=' '
405*3117ece4Schristos  done
406*3117ece4Schristos  test $feb_fail_ = 0 && printf %s "$feb_result_"
407*3117ece4Schristos  return $feb_fail_
408*3117ece4Schristos}
409*3117ece4Schristos
410*3117ece4Schristos# Consider the files in directory, $1.
411*3117ece4Schristos# For each file name of the form PROG.exe, create an alias named
412*3117ece4Schristos# PROG that simply invokes PROG.exe, then return 0.  If any selected
413*3117ece4Schristos# file name or the directory name, $1, contains an unexpected character,
414*3117ece4Schristos# define no alias and return 1.
415*3117ece4Schristoscreate_exe_shims_ ()
416*3117ece4Schristos{
417*3117ece4Schristos  case $EXEEXT in
418*3117ece4Schristos    '') return 0 ;;
419*3117ece4Schristos    .exe) ;;
420*3117ece4Schristos    *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
421*3117ece4Schristos  esac
422*3117ece4Schristos
423*3117ece4Schristos  base_names_=`find_exe_basenames_ $1` \
424*3117ece4Schristos    || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
425*3117ece4Schristos
426*3117ece4Schristos  if test -n "$base_names_"; then
427*3117ece4Schristos    for base_ in $base_names_; do
428*3117ece4Schristos      alias "$base_"="$base_$EXEEXT"
429*3117ece4Schristos    done
430*3117ece4Schristos  fi
431*3117ece4Schristos
432*3117ece4Schristos  return 0
433*3117ece4Schristos}
434*3117ece4Schristos
435*3117ece4Schristos# Use this function to prepend to PATH an absolute name for each
436*3117ece4Schristos# specified, possibly-$initial_cwd_-relative, directory.
437*3117ece4Schristospath_prepend_ ()
438*3117ece4Schristos{
439*3117ece4Schristos  while test $# != 0; do
440*3117ece4Schristos    path_dir_=$1
441*3117ece4Schristos    case $path_dir_ in
442*3117ece4Schristos      '') fail_ "invalid path dir: '$1'";;
443*3117ece4Schristos      /*) abs_path_dir_=$path_dir_;;
444*3117ece4Schristos      *) abs_path_dir_=$initial_cwd_/$path_dir_;;
445*3117ece4Schristos    esac
446*3117ece4Schristos    case $abs_path_dir_ in
447*3117ece4Schristos      *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
448*3117ece4Schristos    esac
449*3117ece4Schristos    PATH="$abs_path_dir_:$PATH"
450*3117ece4Schristos
451*3117ece4Schristos    # Create an alias, FOO, for each FOO.exe in this directory.
452*3117ece4Schristos    create_exe_shims_ "$abs_path_dir_" \
453*3117ece4Schristos      || fail_ "something failed (above): $abs_path_dir_"
454*3117ece4Schristos    shift
455*3117ece4Schristos  done
456*3117ece4Schristos  export PATH
457*3117ece4Schristos}
458*3117ece4Schristos
459*3117ece4Schristossetup_ ()
460*3117ece4Schristos{
461*3117ece4Schristos  if test "$VERBOSE" = yes; then
462*3117ece4Schristos    # Test whether set -x may cause the selected shell to corrupt an
463*3117ece4Schristos    # application's stderr.  Many do, including zsh-4.3.10 and the /bin/sh
464*3117ece4Schristos    # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
465*3117ece4Schristos    # If enabling verbose output this way would cause trouble, simply
466*3117ece4Schristos    # issue a warning and refrain.
467*3117ece4Schristos    if $gl_set_x_corrupts_stderr_; then
468*3117ece4Schristos      warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
469*3117ece4Schristos    else
470*3117ece4Schristos      set -x
471*3117ece4Schristos    fi
472*3117ece4Schristos  fi
473*3117ece4Schristos
474*3117ece4Schristos  initial_cwd_=$PWD
475*3117ece4Schristos
476*3117ece4Schristos  pfx_=`testdir_prefix_`
477*3117ece4Schristos  test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
478*3117ece4Schristos    || fail_ "failed to create temporary directory in $initial_cwd_"
479*3117ece4Schristos  cd "$test_dir_" || fail_ "failed to cd to temporary directory"
480*3117ece4Schristos
481*3117ece4Schristos  # As autoconf-generated configure scripts do, ensure that IFS
482*3117ece4Schristos  # is defined initially, so that saving and restoring $IFS works.
483*3117ece4Schristos  gl_init_sh_nl_='
484*3117ece4Schristos'
485*3117ece4Schristos  IFS=" ""	$gl_init_sh_nl_"
486*3117ece4Schristos
487*3117ece4Schristos  # This trap statement, along with a trap on 0 below, ensure that the
488*3117ece4Schristos  # temporary directory, $test_dir_, is removed upon exit as well as
489*3117ece4Schristos  # upon receipt of any of the listed signals.
490*3117ece4Schristos  for sig_ in 1 2 3 13 15; do
491*3117ece4Schristos    eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
492*3117ece4Schristos  done
493*3117ece4Schristos}
494*3117ece4Schristos
495*3117ece4Schristos# Create a temporary directory, much like mktemp -d does.
496*3117ece4Schristos# Written by Jim Meyering.
497*3117ece4Schristos#
498*3117ece4Schristos# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
499*3117ece4Schristos#
500*3117ece4Schristos# First, try to use the mktemp program.
501*3117ece4Schristos# Failing that, we'll roll our own mktemp-like function:
502*3117ece4Schristos#  - try to get random bytes from /dev/urandom
503*3117ece4Schristos#  - failing that, generate output from a combination of quickly-varying
504*3117ece4Schristos#      sources and gzip.  Ignore non-varying gzip header, and extract
505*3117ece4Schristos#      "random" bits from there.
506*3117ece4Schristos#  - given those bits, map to file-name bytes using tr, and try to create
507*3117ece4Schristos#      the desired directory.
508*3117ece4Schristos#  - make only $MAX_TRIES_ attempts
509*3117ece4Schristos
510*3117ece4Schristos# Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
511*3117ece4Schristosrand_bytes_ ()
512*3117ece4Schristos{
513*3117ece4Schristos  n_=$1
514*3117ece4Schristos
515*3117ece4Schristos  # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
516*3117ece4Schristos  # But if they have openssl, they probably have mktemp, too.
517*3117ece4Schristos
518*3117ece4Schristos  chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
519*3117ece4Schristos  dev_rand_=/dev/urandom
520*3117ece4Schristos  if test -r "$dev_rand_"; then
521*3117ece4Schristos    # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
522*3117ece4Schristos    dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
523*3117ece4Schristos      | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
524*3117ece4Schristos    return
525*3117ece4Schristos  fi
526*3117ece4Schristos
527*3117ece4Schristos  n_plus_50_=`expr $n_ + 50`
528*3117ece4Schristos  cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
529*3117ece4Schristos  data_=` (eval "$cmds_") 2>&1 | gzip `
530*3117ece4Schristos
531*3117ece4Schristos  # Ensure that $data_ has length at least 50+$n_
532*3117ece4Schristos  while :; do
533*3117ece4Schristos    len_=`echo "$data_"|wc -c`
534*3117ece4Schristos    test $n_plus_50_ -le $len_ && break;
535*3117ece4Schristos    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
536*3117ece4Schristos  done
537*3117ece4Schristos
538*3117ece4Schristos  echo "$data_" \
539*3117ece4Schristos    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
540*3117ece4Schristos    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
541*3117ece4Schristos}
542*3117ece4Schristos
543*3117ece4Schristosmktempd_ ()
544*3117ece4Schristos{
545*3117ece4Schristos  case $# in
546*3117ece4Schristos  2);;
547*3117ece4Schristos  *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
548*3117ece4Schristos  esac
549*3117ece4Schristos
550*3117ece4Schristos  destdir_=$1
551*3117ece4Schristos  template_=$2
552*3117ece4Schristos
553*3117ece4Schristos  MAX_TRIES_=4
554*3117ece4Schristos
555*3117ece4Schristos  # Disallow any trailing slash on specified destdir:
556*3117ece4Schristos  # it would subvert the post-mktemp "case"-based destdir test.
557*3117ece4Schristos  case $destdir_ in
558*3117ece4Schristos  / | //) destdir_slash_=$destdir;;
559*3117ece4Schristos  */) fail_ "invalid destination dir: remove trailing slash(es)";;
560*3117ece4Schristos  *) destdir_slash_=$destdir_/;;
561*3117ece4Schristos  esac
562*3117ece4Schristos
563*3117ece4Schristos  case $template_ in
564*3117ece4Schristos  *XXXX) ;;
565*3117ece4Schristos  *) fail_ \
566*3117ece4Schristos       "invalid template: $template_ (must have a suffix of at least 4 X's)";;
567*3117ece4Schristos  esac
568*3117ece4Schristos
569*3117ece4Schristos  # First, try to use mktemp.
570*3117ece4Schristos  d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` &&
571*3117ece4Schristos
572*3117ece4Schristos  # The resulting name must be in the specified directory.
573*3117ece4Schristos  case $d in "$destdir_slash_"*) :;; *) false;; esac &&
574*3117ece4Schristos
575*3117ece4Schristos  # It must have created the directory.
576*3117ece4Schristos  test -d "$d" &&
577*3117ece4Schristos
578*3117ece4Schristos  # It must have 0700 permissions.  Handle sticky "S" bits.
579*3117ece4Schristos  perms=`ls -dgo "$d" 2>/dev/null` &&
580*3117ece4Schristos  case $perms in drwx--[-S]---*) :;; *) false;; esac && {
581*3117ece4Schristos    echo "$d"
582*3117ece4Schristos    return
583*3117ece4Schristos  }
584*3117ece4Schristos
585*3117ece4Schristos  # If we reach this point, we'll have to create a directory manually.
586*3117ece4Schristos
587*3117ece4Schristos  # Get a copy of the template without its suffix of X's.
588*3117ece4Schristos  base_template_=`echo "$template_"|sed 's/XX*$//'`
589*3117ece4Schristos
590*3117ece4Schristos  # Calculate how many X's we've just removed.
591*3117ece4Schristos  template_length_=`echo "$template_" | wc -c`
592*3117ece4Schristos  nx_=`echo "$base_template_" | wc -c`
593*3117ece4Schristos  nx_=`expr $template_length_ - $nx_`
594*3117ece4Schristos
595*3117ece4Schristos  err_=
596*3117ece4Schristos  i_=1
597*3117ece4Schristos  while :; do
598*3117ece4Schristos    X_=`rand_bytes_ $nx_`
599*3117ece4Schristos    candidate_dir_="$destdir_slash_$base_template_$X_"
600*3117ece4Schristos    err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
601*3117ece4Schristos      && { echo "$candidate_dir_"; return; }
602*3117ece4Schristos    test $MAX_TRIES_ -le $i_ && break;
603*3117ece4Schristos    i_=`expr $i_ + 1`
604*3117ece4Schristos  done
605*3117ece4Schristos  fail_ "$err_"
606*3117ece4Schristos}
607*3117ece4Schristos
608*3117ece4Schristos# If you want to override the testdir_prefix_ function,
609*3117ece4Schristos# or to add more utility functions, use this file.
610*3117ece4Schristostest -f "$srcdir/init.cfg" \
611*3117ece4Schristos  && . "$srcdir/init.cfg"
612*3117ece4Schristos
613*3117ece4Schristossetup_ "$@"
614*3117ece4Schristos# This trap is here, rather than in the setup_ function, because some
615*3117ece4Schristos# shells run the exit trap at shell function exit, rather than script exit.
616*3117ece4Schristostrap remove_tmp_ 0
617