Blame tests/init.sh

Packit 33f14e
# source this file; set up for tests
Packit 33f14e
Packit 33f14e
# Copyright (C) 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
# This program is free software: you can redistribute it and/or modify
Packit 33f14e
# it under the terms of the GNU General Public License as published by
Packit 33f14e
# the Free Software Foundation, either version 3 of the License, or
Packit 33f14e
# (at your option) any later version.
Packit 33f14e
Packit 33f14e
# This program is distributed in the hope that it will be useful,
Packit 33f14e
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
# GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
# You should have received a copy of the GNU General Public License
Packit 33f14e
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 33f14e
Packit 33f14e
# Using this file in a test
Packit 33f14e
# =========================
Packit 33f14e
#
Packit 33f14e
# The typical skeleton of a test looks like this:
Packit 33f14e
#
Packit 33f14e
#   #!/bin/sh
Packit 33f14e
#   . "${srcdir=.}/init.sh"; path_prepend_ .
Packit 33f14e
#   Execute some commands.
Packit 33f14e
#   Note that these commands are executed in a subdirectory, therefore you
Packit 33f14e
#   need to prepend "../" to relative filenames in the build directory.
Packit 33f14e
#   Note that the "path_prepend_ ." is useful only if the body of your
Packit 33f14e
#   test invokes programs residing in the initial directory.
Packit 33f14e
#   For example, if the programs you want to test are in src/, and this test
Packit 33f14e
#   script is named tests/test-1, then you would use "path_prepend_ ../src",
Packit 33f14e
#   or perhaps export PATH='$(abs_top_builddir)/src$(PATH_SEPARATOR)'"$$PATH"
Packit 33f14e
#   to all tests via automake's TESTS_ENVIRONMENT.
Packit 33f14e
#   Set the exit code 0 for success, 77 for skipped, or 1 or other for failure.
Packit 33f14e
#   Use the skip_ and fail_ functions to print a diagnostic and then exit
Packit 33f14e
#   with the corresponding exit code.
Packit 33f14e
#   Exit $?
Packit 33f14e
Packit 33f14e
# Executing a test that uses this file
Packit 33f14e
# ====================================
Packit 33f14e
#
Packit 33f14e
# Running a single test:
Packit 33f14e
#   $ make check TESTS=test-foo.sh
Packit 33f14e
#
Packit 33f14e
# Running a single test, with verbose output:
Packit 33f14e
#   $ make check TESTS=test-foo.sh VERBOSE=yes
Packit 33f14e
#
Packit 33f14e
# Running a single test, keeping the temporary directory:
Packit 33f14e
#   $ make check TESTS=test-foo.sh KEEP=yes
Packit 33f14e
#
Packit 33f14e
# Running a single test, with single-stepping:
Packit 33f14e
#   1. Go into a sub-shell:
Packit 33f14e
#   $ bash
Packit 33f14e
#   2. Set relevant environment variables from TESTS_ENVIRONMENT in the
Packit 33f14e
#      Makefile:
Packit 33f14e
#   $ export srcdir=../../tests # this is an example
Packit 33f14e
#   3. Execute the commands from the test, copy&pasting them one by one:
Packit 33f14e
#   $ . "$srcdir/init.sh"; path_prepend_ .
Packit 33f14e
#   ...
Packit 33f14e
#   4. Finally
Packit 33f14e
#   $ exit
Packit 33f14e
Packit 33f14e
ME_=`expr "./$0" : '.*/\(.*\)$'`
Packit 33f14e
Packit 33f14e
# We use a trap below for cleanup.  This requires us to go through
Packit 33f14e
# hoops to get the right exit status transported through the handler.
Packit 33f14e
# So use 'Exit STATUS' instead of 'exit STATUS' inside of the tests.
Packit 33f14e
# Turn off errexit here so that we don't trip the bug with OSF1/Tru64
Packit 33f14e
# sh inside this function.
Packit 33f14e
Exit () { set +e; (exit $1); exit $1; }
Packit 33f14e
Packit 33f14e
# Print warnings (e.g., about skipped and failed tests) to this file number.
Packit 33f14e
# Override by defining to say, 9, in init.cfg, and putting say,
Packit 33f14e
#   export ...ENVVAR_SETTINGS...; $(SHELL) 9>&2
Packit 33f14e
# in the definition of TESTS_ENVIRONMENT in your tests/Makefile.am file.
Packit 33f14e
# This is useful when using automake's parallel tests mode, to print
Packit 33f14e
# the reason for skip/failure to console, rather than to the .log files.
Packit 33f14e
: ${stderr_fileno_=2}
Packit 33f14e
Packit 33f14e
# Note that correct expansion of "$*" depends on IFS starting with ' '.
Packit 33f14e
# Always write the full diagnostic to stderr.
Packit 33f14e
# When stderr_fileno_ is not 2, also emit the first line of the
Packit 33f14e
# diagnostic to that file descriptor.
Packit 33f14e
warn_ ()
Packit 33f14e
{
Packit 33f14e
  # If IFS does not start with ' ', set it and emit the warning in a subshell.
Packit 33f14e
  case $IFS in
Packit 33f14e
    ' '*) printf '%s\n' "$*" >&2
Packit 33f14e
          test $stderr_fileno_ = 2 \
Packit 33f14e
            || { printf '%s\n' "$*" | sed 1q >&$stderr_fileno_ ; } ;;
Packit 33f14e
    *) (IFS=' '; warn_ "$@");;
Packit 33f14e
  esac
Packit 33f14e
}
Packit 33f14e
fail_ () { warn_ "$ME_: failed test: $@"; Exit 1; }
Packit 33f14e
skip_ () { warn_ "$ME_: skipped test: $@"; Exit 77; }
Packit 33f14e
fatal_ () { warn_ "$ME_: hard error: $@"; Exit 99; }
Packit 33f14e
framework_failure_ () { warn_ "$ME_: set-up failure: $@"; Exit 99; }
Packit 33f14e
Packit 33f14e
# This is used to simplify checking of the return value
Packit 33f14e
# which is useful when ensuring a command fails as desired.
Packit 33f14e
# I.e., just doing `command ... &&fail=1` will not catch
Packit 33f14e
# a segfault in command for example.  With this helper you
Packit 33f14e
# instead check an explicit exit code like
Packit 33f14e
#   returns_ 1 command ... || fail
Packit 33f14e
returns_ () {
Packit 33f14e
  # Disable tracing so it doesn't interfere with stderr of the wrapped command
Packit 33f14e
  { set +x; } 2>/dev/null
Packit 33f14e
Packit 33f14e
  local exp_exit="$1"
Packit 33f14e
  shift
Packit 33f14e
  "$@"
Packit 33f14e
  test $? -eq $exp_exit && ret_=0 || ret_=1
Packit 33f14e
Packit 33f14e
  if test "$VERBOSE" = yes && test "$gl_set_x_corrupts_stderr_" = false; then
Packit 33f14e
    set -x
Packit 33f14e
  fi
Packit 33f14e
  { return $ret_; } 2>/dev/null
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Sanitize this shell to POSIX mode, if possible.
Packit 33f14e
DUALCASE=1; export DUALCASE
Packit 33f14e
if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then
Packit 33f14e
  emulate sh
Packit 33f14e
  NULLCMD=:
Packit 33f14e
  alias -g '${1+"$@"}'='"$@"'
Packit 33f14e
  setopt NO_GLOB_SUBST
Packit 33f14e
else
Packit 33f14e
  case `(set -o) 2>/dev/null` in
Packit 33f14e
    *posix*) set -o posix ;;
Packit 33f14e
  esac
Packit 33f14e
fi
Packit 33f14e
Packit 33f14e
# We require $(...) support unconditionally.
Packit 33f14e
# We require non-surprising "local" semantics (this eliminates dash).
Packit 33f14e
# This takes the admittedly draconian step of eliminating dash, because the
Packit 33f14e
# assignment tab=$(printf '\t') works fine, yet preceding it with "local "
Packit 33f14e
# transforms it into an assignment that sets the variable to the empty string.
Packit 33f14e
# That is too counter-intuitive, and can lead to subtle run-time malfunction.
Packit 33f14e
# The example below is less subtle in that with dash, it evokes the run-time
Packit 33f14e
# exception "dash: 1: local: 1: bad variable name".
Packit 33f14e
# We require a few additional shell features only when $EXEEXT is nonempty,
Packit 33f14e
# in order to support automatic $EXEEXT emulation:
Packit 33f14e
# - hyphen-containing alias names
Packit 33f14e
# - we prefer to use ${var#...} substitution, rather than having
Packit 33f14e
#   to work around lack of support for that feature.
Packit 33f14e
# The following code attempts to find a shell with support for these features.
Packit 33f14e
# If the current shell passes the test, we're done.  Otherwise, test other
Packit 33f14e
# shells until we find one that passes.  If one is found, re-exec it.
Packit 33f14e
# If no acceptable shell is found, skip the current test.
Packit 33f14e
#
Packit 33f14e
# The "...set -x; P=1 true 2>err..." test is to disqualify any shell that
Packit 33f14e
# emits "P=1" into err, as /bin/sh from SunOS 5.11 and OpenBSD 4.7 do.
Packit 33f14e
#
Packit 33f14e
# Use "9" to indicate success (rather than 0), in case some shell acts
Packit 33f14e
# like Solaris 10's /bin/sh but exits successfully instead of with status 2.
Packit 33f14e
Packit 33f14e
# Eval this code in a subshell to determine a shell's suitability.
Packit 33f14e
# 10 - passes all tests; ok to use
Packit 33f14e
#  9 - ok, but enabling "set -x" corrupts app stderr; prefer higher score
Packit 33f14e
#  ? - not ok
Packit 33f14e
gl_shell_test_script_='
Packit 33f14e
test $(echo y) = y || exit 1
Packit 33f14e
f_local_() { local v=1; }; f_local_ || exit 1
Packit 33f14e
f_dash_local_fail_() { local t=$(printf " 1"); }; f_dash_local_fail_
Packit 33f14e
score_=10
Packit 33f14e
if test "$VERBOSE" = yes; then
Packit 33f14e
  test -n "$( (exec 3>&1; set -x; P=1 true 2>&3) 2> /dev/null)" && score_=9
Packit 33f14e
fi
Packit 33f14e
test -z "$EXEEXT" && exit $score_
Packit 33f14e
shopt -s expand_aliases
Packit 33f14e
alias a-b="echo zoo"
Packit 33f14e
v=abx
Packit 33f14e
     test ${v%x} = ab \
Packit 33f14e
  && test ${v#a} = bx \
Packit 33f14e
  && test $(a-b) = zoo \
Packit 33f14e
  && exit $score_
Packit 33f14e
'
Packit 33f14e
Packit 33f14e
if test "x$1" = "x--no-reexec"; then
Packit 33f14e
  shift
Packit 33f14e
else
Packit 33f14e
  # Assume a working shell.  Export to subshells (setup_ needs this).
Packit 33f14e
  gl_set_x_corrupts_stderr_=false
Packit 33f14e
  export gl_set_x_corrupts_stderr_
Packit 33f14e
Packit 33f14e
  # Record the first marginally acceptable shell.
Packit 33f14e
  marginal_=
Packit 33f14e
Packit 33f14e
  # Search for a shell that meets our requirements.
Packit 33f14e
  for re_shell_ in __current__ "${CONFIG_SHELL:-no_shell}" \
Packit 33f14e
      /bin/sh bash dash zsh pdksh fail
Packit 33f14e
  do
Packit 33f14e
    test "$re_shell_" = no_shell && continue
Packit 33f14e
Packit 33f14e
    # If we've made it all the way to the sentinel, "fail" without
Packit 33f14e
    # finding even a marginal shell, skip this test.
Packit 33f14e
    if test "$re_shell_" = fail; then
Packit 33f14e
      test -z "$marginal_" && skip_ failed to find an adequate shell
Packit 33f14e
      re_shell_=$marginal_
Packit 33f14e
      break
Packit 33f14e
    fi
Packit 33f14e
Packit 33f14e
    # When testing the current shell, simply "eval" the test code.
Packit 33f14e
    # Otherwise, run it via $re_shell_ -c ...
Packit 33f14e
    if test "$re_shell_" = __current__; then
Packit 33f14e
      # 'eval'ing this code makes Solaris 10's /bin/sh exit with
Packit 33f14e
      # $? set to 2.  It does not evaluate any of the code after the
Packit 33f14e
      # "unexpected" first '('.  Thus, we must run it in a subshell.
Packit 33f14e
      ( eval "$gl_shell_test_script_" ) > /dev/null 2>&1
Packit 33f14e
    else
Packit 33f14e
      "$re_shell_" -c "$gl_shell_test_script_" 2>/dev/null
Packit 33f14e
    fi
Packit 33f14e
Packit 33f14e
    st_=$?
Packit 33f14e
Packit 33f14e
    # $re_shell_ works just fine.  Use it.
Packit 33f14e
    if test $st_ = 10; then
Packit 33f14e
      gl_set_x_corrupts_stderr_=false
Packit 33f14e
      break
Packit 33f14e
    fi
Packit 33f14e
Packit 33f14e
    # If this is our first marginally acceptable shell, remember it.
Packit 33f14e
    if test "$st_:$marginal_" = 9: ; then
Packit 33f14e
      marginal_="$re_shell_"
Packit 33f14e
      gl_set_x_corrupts_stderr_=true
Packit 33f14e
    fi
Packit 33f14e
  done
Packit 33f14e
Packit 33f14e
  if test "$re_shell_" != __current__; then
Packit 33f14e
    # Found a usable shell.  Preserve -v and -x.
Packit 33f14e
    case $- in
Packit 33f14e
      *v*x* | *x*v*) opts_=-vx ;;
Packit 33f14e
      *v*) opts_=-v ;;
Packit 33f14e
      *x*) opts_=-x ;;
Packit 33f14e
      *) opts_= ;;
Packit 33f14e
    esac
Packit 33f14e
    re_shell=$re_shell_
Packit 33f14e
    export re_shell
Packit 33f14e
    exec "$re_shell_" $opts_ "$0" --no-reexec "$@"
Packit 33f14e
    echo "$ME_: exec failed" 1>&2
Packit 33f14e
    exit 127
Packit 33f14e
  fi
Packit 33f14e
fi
Packit 33f14e
Packit 33f14e
# If this is bash, turn off all aliases.
Packit 33f14e
test -n "$BASH_VERSION" && unalias -a
Packit 33f14e
Packit 33f14e
# Note that when supporting $EXEEXT (transparently mapping from PROG_NAME to
Packit 33f14e
# PROG_NAME.exe), we want to support hyphen-containing names like test-acos.
Packit 33f14e
# That is part of the shell-selection test above.  Why use aliases rather
Packit 33f14e
# than functions?  Because support for hyphen-containing aliases is more
Packit 33f14e
# widespread than that for hyphen-containing function names.
Packit 33f14e
test -n "$EXEEXT" && shopt -s expand_aliases
Packit 33f14e
Packit 33f14e
# Enable glibc's malloc-perturbing option.
Packit 33f14e
# This is useful for exposing code that depends on the fact that
Packit 33f14e
# malloc-related functions often return memory that is mostly zeroed.
Packit 33f14e
# If you have the time and cycles, use valgrind to do an even better job.
Packit 33f14e
: ${MALLOC_PERTURB_=87}
Packit 33f14e
export MALLOC_PERTURB_
Packit 33f14e
Packit 33f14e
# This is a stub function that is run upon trap (upon regular exit and
Packit 33f14e
# interrupt).  Override it with a per-test function, e.g., to unmount
Packit 33f14e
# a partition, or to undo any other global state changes.
Packit 33f14e
cleanup_ () { :; }
Packit 33f14e
Packit 33f14e
# Emit a header similar to that from diff -u;  Print the simulated "diff"
Packit 33f14e
# command so that the order of arguments is clear.  Don't bother with @@ lines.
Packit 33f14e
emit_diff_u_header_ ()
Packit 33f14e
{
Packit 33f14e
  printf '%s\n' "diff -u $*" \
Packit 33f14e
    "--- $1	1970-01-01" \
Packit 33f14e
    "+++ $2	1970-01-01"
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Arrange not to let diff or cmp operate on /dev/null,
Packit 33f14e
# since on some systems (at least OSF/1 5.1), that doesn't work.
Packit 33f14e
# When there are not two arguments, or no argument is /dev/null, return 2.
Packit 33f14e
# When one argument is /dev/null and the other is not empty,
Packit 33f14e
# cat the nonempty file to stderr and return 1.
Packit 33f14e
# Otherwise, return 0.
Packit 33f14e
compare_dev_null_ ()
Packit 33f14e
{
Packit 33f14e
  test $# = 2 || return 2
Packit 33f14e
Packit 33f14e
  if test "x$1" = x/dev/null; then
Packit 33f14e
    test -s "$2" || return 0
Packit 33f14e
    emit_diff_u_header_ "$@"; sed 's/^/+/' "$2"
Packit 33f14e
    return 1
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  if test "x$2" = x/dev/null; then
Packit 33f14e
    test -s "$1" || return 0
Packit 33f14e
    emit_diff_u_header_ "$@"; sed 's/^/-/' "$1"
Packit 33f14e
    return 1
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  return 2
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
for diff_opt_ in -u -U3 -c '' no; do
Packit 33f14e
  test "$diff_opt_" != no &&
Packit 33f14e
    diff_out_=`exec 2>/dev/null; diff $diff_opt_ "$0" "$0" < /dev/null` &&
Packit 33f14e
    break
Packit 33f14e
done
Packit 33f14e
if test "$diff_opt_" != no; then
Packit 33f14e
  if test -z "$diff_out_"; then
Packit 33f14e
    compare_ () { diff $diff_opt_ "$@"; }
Packit 33f14e
  else
Packit 33f14e
    compare_ ()
Packit 33f14e
    {
Packit 33f14e
      # If no differences were found, AIX and HP-UX 'diff' produce output
Packit 33f14e
      # like "No differences encountered".  Hide this output.
Packit 33f14e
      diff $diff_opt_ "$@" > diff.out
Packit 33f14e
      diff_status_=$?
Packit 33f14e
      test $diff_status_ -eq 0 || cat diff.out || diff_status_=2
Packit 33f14e
      rm -f diff.out || diff_status_=2
Packit 33f14e
      return $diff_status_
Packit 33f14e
    }
Packit 33f14e
  fi
Packit 33f14e
elif cmp -s /dev/null /dev/null 2>/dev/null; then
Packit 33f14e
  compare_ () { cmp -s "$@"; }
Packit 33f14e
else
Packit 33f14e
  compare_ () { cmp "$@"; }
Packit 33f14e
fi
Packit 33f14e
Packit 33f14e
# Usage: compare EXPECTED ACTUAL
Packit 33f14e
#
Packit 33f14e
# Given compare_dev_null_'s preprocessing, defer to compare_ if 2 or more.
Packit 33f14e
# Otherwise, propagate $? to caller: any diffs have already been printed.
Packit 33f14e
compare ()
Packit 33f14e
{
Packit 33f14e
  # This looks like it can be factored to use a simple "case $?"
Packit 33f14e
  # after unchecked compare_dev_null_ invocation, but that would
Packit 33f14e
  # fail in a "set -e" environment.
Packit 33f14e
  if compare_dev_null_ "$@"; then
Packit 33f14e
    return 0
Packit 33f14e
  else
Packit 33f14e
    case $? in
Packit 33f14e
      1) return 1;;
Packit 33f14e
      *) compare_ "$@";;
Packit 33f14e
    esac
Packit 33f14e
  fi
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# An arbitrary prefix to help distinguish test directories.
Packit 33f14e
testdir_prefix_ () { printf gt; }
Packit 33f14e
Packit 33f14e
# Run the user-overridable cleanup_ function, remove the temporary
Packit 33f14e
# directory and exit with the incoming value of $?.
Packit 33f14e
remove_tmp_ ()
Packit 33f14e
{
Packit 33f14e
  __st=$?
Packit 33f14e
  cleanup_
Packit 33f14e
  if test "$KEEP" = yes; then
Packit 33f14e
    echo "Not removing temporary directory $test_dir_"
Packit 33f14e
  else
Packit 33f14e
    # cd out of the directory we're about to remove
Packit 33f14e
    cd "$initial_cwd_" || cd / || cd /tmp
Packit 33f14e
    chmod -R u+rwx "$test_dir_"
Packit 33f14e
    # If removal fails and exit status was to be 0, then change it to 1.
Packit 33f14e
    rm -rf "$test_dir_" || { test $__st = 0 && __st=1; }
Packit 33f14e
  fi
Packit 33f14e
  exit $__st
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Given a directory name, DIR, if every entry in it that matches *.exe
Packit 33f14e
# contains only the specified bytes (see the case stmt below), then print
Packit 33f14e
# a space-separated list of those names and return 0.  Otherwise, don't
Packit 33f14e
# print anything and return 1.  Naming constraints apply also to DIR.
Packit 33f14e
find_exe_basenames_ ()
Packit 33f14e
{
Packit 33f14e
  feb_dir_=$1
Packit 33f14e
  feb_fail_=0
Packit 33f14e
  feb_result_=
Packit 33f14e
  feb_sp_=
Packit 33f14e
  for feb_file_ in $feb_dir_/*.exe; do
Packit 33f14e
    # If there was no *.exe file, or there existed a file named "*.exe" that
Packit 33f14e
    # was deleted between the above glob expansion and the existence test
Packit 33f14e
    # below, just skip it.
Packit 33f14e
    test "x$feb_file_" = "x$feb_dir_/*.exe" && test ! -f "$feb_file_" \
Packit 33f14e
      && continue
Packit 33f14e
    # Exempt [.exe, since we can't create a function by that name, yet
Packit 33f14e
    # we can't invoke [ by PATH search anyways due to shell builtins.
Packit 33f14e
    test "x$feb_file_" = "x$feb_dir_/[.exe" && continue
Packit 33f14e
    case $feb_file_ in
Packit 33f14e
      *[!-a-zA-Z/0-9_.+]*) feb_fail_=1; break;;
Packit 33f14e
      *) # Remove leading file name components as well as the .exe suffix.
Packit 33f14e
         feb_file_=${feb_file_##*/}
Packit 33f14e
         feb_file_=${feb_file_%.exe}
Packit 33f14e
         feb_result_="$feb_result_$feb_sp_$feb_file_";;
Packit 33f14e
    esac
Packit 33f14e
    feb_sp_=' '
Packit 33f14e
  done
Packit 33f14e
  test $feb_fail_ = 0 && printf %s "$feb_result_"
Packit 33f14e
  return $feb_fail_
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Consider the files in directory, $1.
Packit 33f14e
# For each file name of the form PROG.exe, create an alias named
Packit 33f14e
# PROG that simply invokes PROG.exe, then return 0.  If any selected
Packit 33f14e
# file name or the directory name, $1, contains an unexpected character,
Packit 33f14e
# define no alias and return 1.
Packit 33f14e
create_exe_shims_ ()
Packit 33f14e
{
Packit 33f14e
  case $EXEEXT in
Packit 33f14e
    '') return 0 ;;
Packit 33f14e
    .exe) ;;
Packit 33f14e
    *) echo "$0: unexpected \$EXEEXT value: $EXEEXT" 1>&2; return 1 ;;
Packit 33f14e
  esac
Packit 33f14e
Packit 33f14e
  base_names_=`find_exe_basenames_ $1` \
Packit 33f14e
    || { echo "$0 (exe_shim): skipping directory: $1" 1>&2; return 0; }
Packit 33f14e
Packit 33f14e
  if test -n "$base_names_"; then
Packit 33f14e
    for base_ in $base_names_; do
Packit 33f14e
      alias "$base_"="$base_$EXEEXT"
Packit 33f14e
    done
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  return 0
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Use this function to prepend to PATH an absolute name for each
Packit 33f14e
# specified, possibly-$initial_cwd_-relative, directory.
Packit 33f14e
path_prepend_ ()
Packit 33f14e
{
Packit 33f14e
  while test $# != 0; do
Packit 33f14e
    path_dir_=$1
Packit 33f14e
    case $path_dir_ in
Packit 33f14e
      '') fail_ "invalid path dir: '$1'";;
Packit 33f14e
      /*) abs_path_dir_=$path_dir_;;
Packit 33f14e
      *) abs_path_dir_=$initial_cwd_/$path_dir_;;
Packit 33f14e
    esac
Packit 33f14e
    case $abs_path_dir_ in
Packit 33f14e
      *:*) fail_ "invalid path dir: '$abs_path_dir_'";;
Packit 33f14e
    esac
Packit 33f14e
    PATH="$abs_path_dir_:$PATH"
Packit 33f14e
Packit 33f14e
    # Create an alias, FOO, for each FOO.exe in this directory.
Packit 33f14e
    create_exe_shims_ "$abs_path_dir_" \
Packit 33f14e
      || fail_ "something failed (above): $abs_path_dir_"
Packit 33f14e
    shift
Packit 33f14e
  done
Packit 33f14e
  export PATH
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
setup_ ()
Packit 33f14e
{
Packit 33f14e
  if test "$VERBOSE" = yes; then
Packit 33f14e
    # Test whether set -x may cause the selected shell to corrupt an
Packit 33f14e
    # application's stderr.  Many do, including zsh-4.3.10 and the /bin/sh
Packit 33f14e
    # from SunOS 5.11, OpenBSD 4.7 and Irix 5.x and 6.5.
Packit 33f14e
    # If enabling verbose output this way would cause trouble, simply
Packit 33f14e
    # issue a warning and refrain.
Packit 33f14e
    if $gl_set_x_corrupts_stderr_; then
Packit 33f14e
      warn_ "using SHELL=$SHELL with 'set -x' corrupts stderr"
Packit 33f14e
    else
Packit 33f14e
      set -x
Packit 33f14e
    fi
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  initial_cwd_=$PWD
Packit 33f14e
Packit 33f14e
  pfx_=`testdir_prefix_`
Packit 33f14e
  test_dir_=`mktempd_ "$initial_cwd_" "$pfx_-$ME_.XXXX"` \
Packit 33f14e
    || fail_ "failed to create temporary directory in $initial_cwd_"
Packit 33f14e
  cd "$test_dir_" || fail_ "failed to cd to temporary directory"
Packit 33f14e
Packit 33f14e
  # As autoconf-generated configure scripts do, ensure that IFS
Packit 33f14e
  # is defined initially, so that saving and restoring $IFS works.
Packit 33f14e
  gl_init_sh_nl_='
Packit 33f14e
'
Packit 33f14e
  IFS=" ""	$gl_init_sh_nl_"
Packit 33f14e
Packit 33f14e
  # This trap statement, along with a trap on 0 below, ensure that the
Packit 33f14e
  # temporary directory, $test_dir_, is removed upon exit as well as
Packit 33f14e
  # upon receipt of any of the listed signals.
Packit 33f14e
  for sig_ in 1 2 3 13 15; do
Packit 33f14e
    eval "trap 'Exit $(expr $sig_ + 128)' $sig_"
Packit 33f14e
  done
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# Create a temporary directory, much like mktemp -d does.
Packit 33f14e
# Written by Jim Meyering.
Packit 33f14e
#
Packit 33f14e
# Usage: mktempd_ /tmp phoey.XXXXXXXXXX
Packit 33f14e
#
Packit 33f14e
# First, try to use the mktemp program.
Packit 33f14e
# Failing that, we'll roll our own mktemp-like function:
Packit 33f14e
#  - try to get random bytes from /dev/urandom
Packit 33f14e
#  - failing that, generate output from a combination of quickly-varying
Packit 33f14e
#      sources and gzip.  Ignore non-varying gzip header, and extract
Packit 33f14e
#      "random" bits from there.
Packit 33f14e
#  - given those bits, map to file-name bytes using tr, and try to create
Packit 33f14e
#      the desired directory.
Packit 33f14e
#  - make only $MAX_TRIES_ attempts
Packit 33f14e
Packit 33f14e
# Helper function.  Print $N pseudo-random bytes from a-zA-Z0-9.
Packit 33f14e
rand_bytes_ ()
Packit 33f14e
{
Packit 33f14e
  n_=$1
Packit 33f14e
Packit 33f14e
  # Maybe try openssl rand -base64 $n_prime_|tr '+/=\012' abcd first?
Packit 33f14e
  # But if they have openssl, they probably have mktemp, too.
Packit 33f14e
Packit 33f14e
  chars_=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
Packit 33f14e
  dev_rand_=/dev/urandom
Packit 33f14e
  if test -r "$dev_rand_"; then
Packit 33f14e
    # Note: 256-length($chars_) == 194; 3 copies of $chars_ is 186 + 8 = 194.
Packit 33f14e
    dd ibs=$n_ count=1 if=$dev_rand_ 2>/dev/null \
Packit 33f14e
      | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
Packit 33f14e
    return
Packit 33f14e
  fi
Packit 33f14e
Packit 33f14e
  n_plus_50_=`expr $n_ + 50`
Packit 33f14e
  cmds_='date; date +%N; free; who -a; w; ps auxww; ps ef; netstat -n'
Packit 33f14e
  data_=` (eval "$cmds_") 2>&1 | gzip `
Packit 33f14e
Packit 33f14e
  # Ensure that $data_ has length at least 50+$n_
Packit 33f14e
  while :; do
Packit 33f14e
    len_=`echo "$data_"|wc -c`
Packit 33f14e
    test $n_plus_50_ -le $len_ && break;
Packit 33f14e
    data_=` (echo "$data_"; eval "$cmds_") 2>&1 | gzip `
Packit 33f14e
  done
Packit 33f14e
Packit 33f14e
  echo "$data_" \
Packit 33f14e
    | dd bs=1 skip=50 count=$n_ 2>/dev/null \
Packit 33f14e
    | LC_ALL=C tr -c $chars_ 01234567$chars_$chars_$chars_
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
mktempd_ ()
Packit 33f14e
{
Packit 33f14e
  case $# in
Packit 33f14e
  2);;
Packit 33f14e
  *) fail_ "Usage: mktempd_ DIR TEMPLATE";;
Packit 33f14e
  esac
Packit 33f14e
Packit 33f14e
  destdir_=$1
Packit 33f14e
  template_=$2
Packit 33f14e
Packit 33f14e
  MAX_TRIES_=4
Packit 33f14e
Packit 33f14e
  # Disallow any trailing slash on specified destdir:
Packit 33f14e
  # it would subvert the post-mktemp "case"-based destdir test.
Packit 33f14e
  case $destdir_ in
Packit 33f14e
  / | //) destdir_slash_=$destdir;;
Packit 33f14e
  */) fail_ "invalid destination dir: remove trailing slash(es)";;
Packit 33f14e
  *) destdir_slash_=$destdir_/;;
Packit 33f14e
  esac
Packit 33f14e
Packit 33f14e
  case $template_ in
Packit 33f14e
  *XXXX) ;;
Packit 33f14e
  *) fail_ \
Packit 33f14e
       "invalid template: $template_ (must have a suffix of at least 4 X's)";;
Packit 33f14e
  esac
Packit 33f14e
Packit 33f14e
  # First, try to use mktemp.
Packit 33f14e
  d=`unset TMPDIR; { mktemp -d -t -p "$destdir_" "$template_"; } 2>/dev/null` &&
Packit 33f14e
Packit 33f14e
  # The resulting name must be in the specified directory.
Packit 33f14e
  case $d in "$destdir_slash_"*) :;; *) false;; esac &&
Packit 33f14e
Packit 33f14e
  # It must have created the directory.
Packit 33f14e
  test -d "$d" &&
Packit 33f14e
Packit 33f14e
  # It must have 0700 permissions.  Handle sticky "S" bits.
Packit 33f14e
  perms=`ls -dgo "$d" 2>/dev/null` &&
Packit 33f14e
  case $perms in drwx--[-S]---*) :;; *) false;; esac && {
Packit 33f14e
    echo "$d"
Packit 33f14e
    return
Packit 33f14e
  }
Packit 33f14e
Packit 33f14e
  # If we reach this point, we'll have to create a directory manually.
Packit 33f14e
Packit 33f14e
  # Get a copy of the template without its suffix of X's.
Packit 33f14e
  base_template_=`echo "$template_"|sed 's/XX*$//'`
Packit 33f14e
Packit 33f14e
  # Calculate how many X's we've just removed.
Packit 33f14e
  template_length_=`echo "$template_" | wc -c`
Packit 33f14e
  nx_=`echo "$base_template_" | wc -c`
Packit 33f14e
  nx_=`expr $template_length_ - $nx_`
Packit 33f14e
Packit 33f14e
  err_=
Packit 33f14e
  i_=1
Packit 33f14e
  while :; do
Packit 33f14e
    X_=`rand_bytes_ $nx_`
Packit 33f14e
    candidate_dir_="$destdir_slash_$base_template_$X_"
Packit 33f14e
    err_=`mkdir -m 0700 "$candidate_dir_" 2>&1` \
Packit 33f14e
      && { echo "$candidate_dir_"; return; }
Packit 33f14e
    test $MAX_TRIES_ -le $i_ && break;
Packit 33f14e
    i_=`expr $i_ + 1`
Packit 33f14e
  done
Packit 33f14e
  fail_ "$err_"
Packit 33f14e
}
Packit 33f14e
Packit 33f14e
# If you want to override the testdir_prefix_ function,
Packit 33f14e
# or to add more utility functions, use this file.
Packit 33f14e
test -f "$srcdir/init.cfg" \
Packit 33f14e
  && . "$srcdir/init.cfg"
Packit 33f14e
Packit 33f14e
setup_ "$@"
Packit 33f14e
# This trap is here, rather than in the setup_ function, because some
Packit 33f14e
# shells run the exit trap at shell function exit, rather than script exit.
Packit 33f14e
trap remove_tmp_ 0