1# Set a version string for this script.
2scriptversion=2019-02-19.15; # UTC
3
4# General shell script boiler plate, and helper functions.
5# Written by Gary V. Vaughan, 2004
6
7# This is free software.  There is NO warranty; not even for
8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9#
10# Copyright (C) 2004-2019 Bootstrap Authors
11#
12# This file is dual licensed under the terms of the MIT license
13# <https://opensource.org/license/MIT>, and GPL version 3 or later
14# <http://www.gnu.org/licenses/gpl-2.0.html>.  You must apply one of
15# these licenses when using or redistributing this software or any of
16# the files within it.  See the URLs above, or the file `LICENSE`
17# included in the Bootstrap distribution for the full license texts.
18
19# Please report bugs or propose patches to:
20# <https://github.com/gnulib-modules/bootstrap/issues>
21
22
23## ------ ##
24## Usage. ##
25## ------ ##
26
27# Evaluate this file near the top of your script to gain access to
28# the functions and variables defined here:
29#
30#   . `echo "$0" | ${SED-sed} 's|[^/]*$||'`/build-aux/funclib.sh
31#
32# If you need to override any of the default environment variable
33# settings, do that before evaluating this file.
34
35
36## -------------------- ##
37## Shell normalisation. ##
38## -------------------- ##
39
40# Some shells need a little help to be as Bourne compatible as possible.
41# Before doing anything else, make sure all that help has been provided!
42
43DUALCASE=1; export DUALCASE # for MKS sh
44if test -n "${ZSH_VERSION+set}" && (emulate sh) >/dev/null 2>&1; then :
45  emulate sh
46  NULLCMD=:
47  # Pre-4.2 versions of Zsh do word splitting on ${1+"$@"}, which
48  # is contrary to our usage.  Disable this feature.
49  alias -g '${1+"$@"}'='"$@"'
50  setopt NO_GLOB_SUBST
51else
52  case `(set -o) 2>/dev/null` in *posix*) set -o posix ;; esac
53fi
54
55# NLS nuisances: We save the old values in case they are required later.
56_G_user_locale=
57_G_safe_locale=
58for _G_var in LANG LANGUAGE LC_ALL LC_CTYPE LC_COLLATE LC_MESSAGES
59do
60  eval "if test set = \"\${$_G_var+set}\"; then
61          save_$_G_var=\$$_G_var
62          $_G_var=C
63	  export $_G_var
64	  _G_user_locale=\"$_G_var=\\\$save_\$_G_var; \$_G_user_locale\"
65	  _G_safe_locale=\"$_G_var=C; \$_G_safe_locale\"
66	fi"
67done
68
69# Make sure IFS has a sensible default
70sp=' '
71nl='
72'
73IFS="$sp	$nl"
74
75# There are apparently some retarded systems that use ';' as a PATH separator!
76if test "${PATH_SEPARATOR+set}" != set; then
77  PATH_SEPARATOR=:
78  (PATH='/bin;/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 && {
79    (PATH='/bin:/bin'; FPATH=$PATH; sh -c :) >/dev/null 2>&1 ||
80      PATH_SEPARATOR=';'
81  }
82fi
83
84
85# func_unset VAR
86# --------------
87# Portably unset VAR.
88# In some shells, an 'unset VAR' statement leaves a non-zero return
89# status if VAR is already unset, which might be problematic if the
90# statement is used at the end of a function (thus poisoning its return
91# value) or when 'set -e' is active (causing even a spurious abort of
92# the script in this case).
93func_unset ()
94{
95    { eval $1=; (eval unset $1) >/dev/null 2>&1 && eval unset $1 || : ; }
96}
97
98
99# Make sure CDPATH doesn't cause `cd` commands to output the target dir.
100func_unset CDPATH
101
102# Make sure ${,E,F}GREP behave sanely.
103func_unset GREP_OPTIONS
104
105
106## ------------------------- ##
107## Locate command utilities. ##
108## ------------------------- ##
109
110
111# func_executable_p FILE
112# ----------------------
113# Check that FILE is an executable regular file.
114func_executable_p ()
115{
116    test -f "$1" && test -x "$1"
117}
118
119
120# func_path_progs PROGS_LIST CHECK_FUNC [PATH]
121# --------------------------------------------
122# Search for either a program that responds to --version with output
123# containing "GNU", or else returned by CHECK_FUNC otherwise, by
124# trying all the directories in PATH with each of the elements of
125# PROGS_LIST.
126#
127# CHECK_FUNC should accept the path to a candidate program, and
128# set $func_check_prog_result if it truncates its output less than
129# $_G_path_prog_max characters.
130func_path_progs ()
131{
132    _G_progs_list=$1
133    _G_check_func=$2
134    _G_PATH=${3-"$PATH"}
135
136    _G_path_prog_max=0
137    _G_path_prog_found=false
138    _G_save_IFS=$IFS; IFS=${PATH_SEPARATOR-:}
139    for _G_dir in $_G_PATH; do
140      IFS=$_G_save_IFS
141      test -z "$_G_dir" && _G_dir=.
142      for _G_prog_name in $_G_progs_list; do
143        for _exeext in '' .EXE; do
144          _G_path_prog=$_G_dir/$_G_prog_name$_exeext
145          func_executable_p "$_G_path_prog" || continue
146          case `"$_G_path_prog" --version 2>&1` in
147            *GNU*) func_path_progs_result=$_G_path_prog _G_path_prog_found=: ;;
148            *)     $_G_check_func $_G_path_prog
149		   func_path_progs_result=$func_check_prog_result
150		   ;;
151          esac
152          $_G_path_prog_found && break 3
153        done
154      done
155    done
156    IFS=$_G_save_IFS
157    test -z "$func_path_progs_result" && {
158      echo "no acceptable sed could be found in \$PATH" >&2
159      exit 1
160    }
161}
162
163
164# We want to be able to use the functions in this file before configure
165# has figured out where the best binaries are kept, which means we have
166# to search for them ourselves - except when the results are already set
167# where we skip the searches.
168
169# Unless the user overrides by setting SED, search the path for either GNU
170# sed, or the sed that truncates its output the least.
171test -z "$SED" && {
172  _G_sed_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/
173  for _G_i in 1 2 3 4 5 6 7; do
174    _G_sed_script=$_G_sed_script$nl$_G_sed_script
175  done
176  echo "$_G_sed_script" 2>/dev/null | sed 99q >conftest.sed
177  _G_sed_script=
178
179  func_check_prog_sed ()
180  {
181    _G_path_prog=$1
182
183    _G_count=0
184    printf 0123456789 >conftest.in
185    while :
186    do
187      cat conftest.in conftest.in >conftest.tmp
188      mv conftest.tmp conftest.in
189      cp conftest.in conftest.nl
190      echo '' >> conftest.nl
191      "$_G_path_prog" -f conftest.sed <conftest.nl >conftest.out 2>/dev/null || break
192      diff conftest.out conftest.nl >/dev/null 2>&1 || break
193      _G_count=`expr $_G_count + 1`
194      if test "$_G_count" -gt "$_G_path_prog_max"; then
195        # Best one so far, save it but keep looking for a better one
196        func_check_prog_result=$_G_path_prog
197        _G_path_prog_max=$_G_count
198      fi
199      # 10*(2^10) chars as input seems more than enough
200      test 10 -lt "$_G_count" && break
201    done
202    rm -f conftest.in conftest.tmp conftest.nl conftest.out
203  }
204
205  func_path_progs "sed gsed" func_check_prog_sed "$PATH:/usr/xpg4/bin"
206  rm -f conftest.sed
207  SED=$func_path_progs_result
208}
209
210
211# Unless the user overrides by setting GREP, search the path for either GNU
212# grep, or the grep that truncates its output the least.
213test -z "$GREP" && {
214  func_check_prog_grep ()
215  {
216    _G_path_prog=$1
217
218    _G_count=0
219    _G_path_prog_max=0
220    printf 0123456789 >conftest.in
221    while :
222    do
223      cat conftest.in conftest.in >conftest.tmp
224      mv conftest.tmp conftest.in
225      cp conftest.in conftest.nl
226      echo 'GREP' >> conftest.nl
227      "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' <conftest.nl >conftest.out 2>/dev/null || break
228      diff conftest.out conftest.nl >/dev/null 2>&1 || break
229      _G_count=`expr $_G_count + 1`
230      if test "$_G_count" -gt "$_G_path_prog_max"; then
231        # Best one so far, save it but keep looking for a better one
232        func_check_prog_result=$_G_path_prog
233        _G_path_prog_max=$_G_count
234      fi
235      # 10*(2^10) chars as input seems more than enough
236      test 10 -lt "$_G_count" && break
237    done
238    rm -f conftest.in conftest.tmp conftest.nl conftest.out
239  }
240
241  func_path_progs "grep ggrep" func_check_prog_grep "$PATH:/usr/xpg4/bin"
242  GREP=$func_path_progs_result
243}
244
245
246## ------------------------------- ##
247## User overridable command paths. ##
248## ------------------------------- ##
249
250# All uppercase variable names are used for environment variables.  These
251# variables can be overridden by the user before calling a script that
252# uses them if a suitable command of that name is not already available
253# in the command search PATH.
254
255: ${CP="cp -f"}
256: ${ECHO="printf %s\n"}
257: ${EGREP="$GREP -E"}
258: ${FGREP="$GREP -F"}
259: ${LN_S="ln -s"}
260: ${MAKE="make"}
261: ${MKDIR="mkdir"}
262: ${MV="mv -f"}
263: ${RM="rm -f"}
264: ${SHELL="${CONFIG_SHELL-/bin/sh}"}
265
266
267## -------------------- ##
268## Useful sed snippets. ##
269## -------------------- ##
270
271sed_dirname='s|/[^/]*$||'
272sed_basename='s|^.*/||'
273
274# Sed substitution that helps us do robust quoting.  It backslashifies
275# metacharacters that are still active within double-quoted strings.
276sed_quote_subst='s|\([`"$\\]\)|\\\1|g'
277
278# Same as above, but do not quote variable references.
279sed_double_quote_subst='s/\(["`\\]\)/\\\1/g'
280
281# Sed substitution that turns a string into a regex matching for the
282# string literally.
283sed_make_literal_regex='s|[].[^$\\*\/]|\\&|g'
284
285# Sed substitution that converts a w32 file name or path
286# that contains forward slashes, into one that contains
287# (escaped) backslashes.  A very naive implementation.
288sed_naive_backslashify='s|\\\\*|\\|g;s|/|\\|g;s|\\|\\\\|g'
289
290# Re-'\' parameter expansions in output of sed_double_quote_subst that
291# were '\'-ed in input to the same.  If an odd number of '\' preceded a
292# '$' in input to sed_double_quote_subst, that '$' was protected from
293# expansion.  Since each input '\' is now two '\'s, look for any number
294# of runs of four '\'s followed by two '\'s and then a '$'.  '\' that '$'.
295_G_bs='\\'
296_G_bs2='\\\\'
297_G_bs4='\\\\\\\\'
298_G_dollar='\$'
299sed_double_backslash="\
300  s/$_G_bs4/&\\
301/g
302  s/^$_G_bs2$_G_dollar/$_G_bs&/
303  s/\\([^$_G_bs]\\)$_G_bs2$_G_dollar/\\1$_G_bs2$_G_bs$_G_dollar/g
304  s/\n//g"
305
306
307## ----------------- ##
308## Global variables. ##
309## ----------------- ##
310
311# Except for the global variables explicitly listed below, the following
312# functions in the '^func_' namespace, and the '^require_' namespace
313# variables initialised in the 'Resource management' section, sourcing
314# this file will not pollute your global namespace with anything
315# else. There's no portable way to scope variables in Bourne shell
316# though, so actually running these functions will sometimes place
317# results into a variable named after the function, and often use
318# temporary variables in the '^_G_' namespace. If you are careful to
319# avoid using those namespaces casually in your sourcing script, things
320# should continue to work as you expect. And, of course, you can freely
321# overwrite any of the functions or variables defined here before
322# calling anything to customize them.
323
324EXIT_SUCCESS=0
325EXIT_FAILURE=1
326EXIT_MISMATCH=63  # $? = 63 is used to indicate version mismatch to missing.
327EXIT_SKIP=77	  # $? = 77 is used to indicate a skipped test to automake.
328
329# Allow overriding, eg assuming that you follow the convention of
330# putting '$debug_cmd' at the start of all your functions, you can get
331# bash to show function call trace with:
332#
333#    debug_cmd='eval echo "${FUNCNAME[0]} $*" >&2' bash your-script-name
334debug_cmd=${debug_cmd-":"}
335exit_cmd=:
336
337# By convention, finish your script with:
338#
339#    exit $exit_status
340#
341# so that you can set exit_status to non-zero if you want to indicate
342# something went wrong during execution without actually bailing out at
343# the point of failure.
344exit_status=$EXIT_SUCCESS
345
346# Work around backward compatibility issue on IRIX 6.5. On IRIX 6.4+, sh
347# is ksh but when the shell is invoked as "sh" and the current value of
348# the _XPG environment variable is not equal to 1 (one), the special
349# positional parameter $0, within a function call, is the name of the
350# function.
351progpath=$0
352
353# The name of this program.
354progname=`$ECHO "$progpath" |$SED "$sed_basename"`
355
356# Make sure we have an absolute progpath for reexecution:
357case $progpath in
358  [\\/]*|[A-Za-z]:\\*) ;;
359  *[\\/]*)
360     progdir=`$ECHO "$progpath" |$SED "$sed_dirname"`
361     progdir=`cd "$progdir" && pwd`
362     progpath=$progdir/$progname
363     ;;
364  *)
365     _G_IFS=$IFS
366     IFS=${PATH_SEPARATOR-:}
367     for progdir in $PATH; do
368       IFS=$_G_IFS
369       test -x "$progdir/$progname" && break
370     done
371     IFS=$_G_IFS
372     test -n "$progdir" || progdir=`pwd`
373     progpath=$progdir/$progname
374     ;;
375esac
376
377
378## ----------------- ##
379## Standard options. ##
380## ----------------- ##
381
382# The following options affect the operation of the functions defined
383# below, and should be set appropriately depending on run-time para-
384# meters passed on the command line.
385
386opt_dry_run=false
387opt_quiet=false
388opt_verbose=false
389
390# Categories 'all' and 'none' are always available.  Append any others
391# you will pass as the first argument to func_warning from your own
392# code.
393warning_categories=
394
395# By default, display warnings according to 'opt_warning_types'.  Set
396# 'warning_func'  to ':' to elide all warnings, or func_fatal_error to
397# treat the next displayed warning as a fatal error.
398warning_func=func_warn_and_continue
399
400# Set to 'all' to display all warnings, 'none' to suppress all
401# warnings, or a space delimited list of some subset of
402# 'warning_categories' to display only the listed warnings.
403opt_warning_types=all
404
405
406## -------------------- ##
407## Resource management. ##
408## -------------------- ##
409
410# This section contains definitions for functions that each ensure a
411# particular resource (a file, or a non-empty configuration variable for
412# example) is available, and if appropriate to extract default values
413# from pertinent package files. Call them using their associated
414# 'require_*' variable to ensure that they are executed, at most, once.
415#
416# It's entirely deliberate that calling these functions can set
417# variables that don't obey the namespace limitations obeyed by the rest
418# of this file, in order that that they be as useful as possible to
419# callers.
420
421
422# require_term_colors
423# -------------------
424# Allow display of bold text on terminals that support it.
425require_term_colors=func_require_term_colors
426func_require_term_colors ()
427{
428    $debug_cmd
429
430    test -t 1 && {
431      # COLORTERM and USE_ANSI_COLORS environment variables take
432      # precedence, because most terminfo databases neglect to describe
433      # whether color sequences are supported.
434      test -n "${COLORTERM+set}" && : ${USE_ANSI_COLORS="1"}
435
436      if test 1 = "$USE_ANSI_COLORS"; then
437        # Standard ANSI escape sequences
438        tc_reset=''
439        tc_bold='';   tc_standout=''
440        tc_red='';   tc_green=''
441        tc_blue='';  tc_cyan=''
442      else
443        # Otherwise trust the terminfo database after all.
444        test -n "`tput sgr0 2>/dev/null`" && {
445          tc_reset=`tput sgr0`
446          test -n "`tput bold 2>/dev/null`" && tc_bold=`tput bold`
447          tc_standout=$tc_bold
448          test -n "`tput smso 2>/dev/null`" && tc_standout=`tput smso`
449          test -n "`tput setaf 1 2>/dev/null`" && tc_red=`tput setaf 1`
450          test -n "`tput setaf 2 2>/dev/null`" && tc_green=`tput setaf 2`
451          test -n "`tput setaf 4 2>/dev/null`" && tc_blue=`tput setaf 4`
452          test -n "`tput setaf 5 2>/dev/null`" && tc_cyan=`tput setaf 5`
453        }
454      fi
455    }
456
457    require_term_colors=:
458}
459
460
461## ----------------- ##
462## Function library. ##
463## ----------------- ##
464
465# This section contains a variety of useful functions to call in your
466# scripts. Take note of the portable wrappers for features provided by
467# some modern shells, which will fall back to slower equivalents on
468# less featureful shells.
469
470
471# func_append VAR VALUE
472# ---------------------
473# Append VALUE onto the existing contents of VAR.
474
475  # We should try to minimise forks, especially on Windows where they are
476  # unreasonably slow, so skip the feature probes when bash or zsh are
477  # being used:
478  if test set = "${BASH_VERSION+set}${ZSH_VERSION+set}"; then
479    : ${_G_HAVE_ARITH_OP="yes"}
480    : ${_G_HAVE_XSI_OPS="yes"}
481    # The += operator was introduced in bash 3.1
482    case $BASH_VERSION in
483      [12].* | 3.0 | 3.0*) ;;
484      *)
485        : ${_G_HAVE_PLUSEQ_OP="yes"}
486        ;;
487    esac
488  fi
489
490  # _G_HAVE_PLUSEQ_OP
491  # Can be empty, in which case the shell is probed, "yes" if += is
492  # useable or anything else if it does not work.
493  test -z "$_G_HAVE_PLUSEQ_OP" \
494    && (eval 'x=a; x+=" b"; test "a b" = "$x"') 2>/dev/null \
495    && _G_HAVE_PLUSEQ_OP=yes
496
497if test yes = "$_G_HAVE_PLUSEQ_OP"
498then
499  # This is an XSI compatible shell, allowing a faster implementation...
500  eval 'func_append ()
501  {
502    $debug_cmd
503
504    eval "$1+=\$2"
505  }'
506else
507  # ...otherwise fall back to using expr, which is often a shell builtin.
508  func_append ()
509  {
510    $debug_cmd
511
512    eval "$1=\$$1\$2"
513  }
514fi
515
516
517# func_append_quoted VAR VALUE
518# ----------------------------
519# Quote VALUE and append to the end of shell variable VAR, separated
520# by a space.
521if test yes = "$_G_HAVE_PLUSEQ_OP"; then
522  eval 'func_append_quoted ()
523  {
524    $debug_cmd
525
526    func_quote_arg pretty "$2"
527    eval "$1+=\\ \$func_quote_arg_result"
528  }'
529else
530  func_append_quoted ()
531  {
532    $debug_cmd
533
534    func_quote_arg pretty "$2"
535    eval "$1=\$$1\\ \$func_quote_arg_result"
536  }
537fi
538
539
540# func_append_uniq VAR VALUE
541# --------------------------
542# Append unique VALUE onto the existing contents of VAR, assuming
543# entries are delimited by the first character of VALUE.  For example:
544#
545#   func_append_uniq options " --another-option option-argument"
546#
547# will only append to $options if " --another-option option-argument "
548# is not already present somewhere in $options already (note spaces at
549# each end implied by leading space in second argument).
550func_append_uniq ()
551{
552    $debug_cmd
553
554    eval _G_current_value='`$ECHO $'$1'`'
555    _G_delim=`expr "$2" : '\(.\)'`
556
557    case $_G_delim$_G_current_value$_G_delim in
558      *"$2$_G_delim"*) ;;
559      *) func_append "$@" ;;
560    esac
561}
562
563
564# func_arith TERM...
565# ------------------
566# Set func_arith_result to the result of evaluating TERMs.
567  test -z "$_G_HAVE_ARITH_OP" \
568    && (eval 'test 2 = $(( 1 + 1 ))') 2>/dev/null \
569    && _G_HAVE_ARITH_OP=yes
570
571if test yes = "$_G_HAVE_ARITH_OP"; then
572  eval 'func_arith ()
573  {
574    $debug_cmd
575
576    func_arith_result=$(( $* ))
577  }'
578else
579  func_arith ()
580  {
581    $debug_cmd
582
583    func_arith_result=`expr "$@"`
584  }
585fi
586
587
588# func_basename FILE
589# ------------------
590# Set func_basename_result to FILE with everything up to and including
591# the last / stripped.
592if test yes = "$_G_HAVE_XSI_OPS"; then
593  # If this shell supports suffix pattern removal, then use it to avoid
594  # forking. Hide the definitions single quotes in case the shell chokes
595  # on unsupported syntax...
596  _b='func_basename_result=${1##*/}'
597  _d='case $1 in
598        */*) func_dirname_result=${1%/*}$2 ;;
599        *  ) func_dirname_result=$3        ;;
600      esac'
601
602else
603  # ...otherwise fall back to using sed.
604  _b='func_basename_result=`$ECHO "$1" |$SED "$sed_basename"`'
605  _d='func_dirname_result=`$ECHO "$1"  |$SED "$sed_dirname"`
606      if test "X$func_dirname_result" = "X$1"; then
607        func_dirname_result=$3
608      else
609        func_append func_dirname_result "$2"
610      fi'
611fi
612
613eval 'func_basename ()
614{
615    $debug_cmd
616
617    '"$_b"'
618}'
619
620
621# func_dirname FILE APPEND NONDIR_REPLACEMENT
622# -------------------------------------------
623# Compute the dirname of FILE.  If nonempty, add APPEND to the result,
624# otherwise set result to NONDIR_REPLACEMENT.
625eval 'func_dirname ()
626{
627    $debug_cmd
628
629    '"$_d"'
630}'
631
632
633# func_dirname_and_basename FILE APPEND NONDIR_REPLACEMENT
634# --------------------------------------------------------
635# Perform func_basename and func_dirname in a single function
636# call:
637#   dirname:  Compute the dirname of FILE.  If nonempty,
638#             add APPEND to the result, otherwise set result
639#             to NONDIR_REPLACEMENT.
640#             value returned in "$func_dirname_result"
641#   basename: Compute filename of FILE.
642#             value retuned in "$func_basename_result"
643# For efficiency, we do not delegate to the functions above but instead
644# duplicate the functionality here.
645eval 'func_dirname_and_basename ()
646{
647    $debug_cmd
648
649    '"$_b"'
650    '"$_d"'
651}'
652
653
654# func_echo ARG...
655# ----------------
656# Echo program name prefixed message.
657func_echo ()
658{
659    $debug_cmd
660
661    _G_message=$*
662
663    func_echo_IFS=$IFS
664    IFS=$nl
665    for _G_line in $_G_message; do
666      IFS=$func_echo_IFS
667      $ECHO "$progname: $_G_line"
668    done
669    IFS=$func_echo_IFS
670}
671
672
673# func_echo_all ARG...
674# --------------------
675# Invoke $ECHO with all args, space-separated.
676func_echo_all ()
677{
678    $ECHO "$*"
679}
680
681
682# func_echo_infix_1 INFIX ARG...
683# ------------------------------
684# Echo program name, followed by INFIX on the first line, with any
685# additional lines not showing INFIX.
686func_echo_infix_1 ()
687{
688    $debug_cmd
689
690    $require_term_colors
691
692    _G_infix=$1; shift
693    _G_indent=$_G_infix
694    _G_prefix="$progname: $_G_infix: "
695    _G_message=$*
696
697    # Strip color escape sequences before counting printable length
698    for _G_tc in "$tc_reset" "$tc_bold" "$tc_standout" "$tc_red" "$tc_green" "$tc_blue" "$tc_cyan"
699    do
700      test -n "$_G_tc" && {
701        _G_esc_tc=`$ECHO "$_G_tc" | $SED "$sed_make_literal_regex"`
702        _G_indent=`$ECHO "$_G_indent" | $SED "s|$_G_esc_tc||g"`
703      }
704    done
705    _G_indent="$progname: "`echo "$_G_indent" | $SED 's|.| |g'`"  " ## exclude from sc_prohibit_nested_quotes
706
707    func_echo_infix_1_IFS=$IFS
708    IFS=$nl
709    for _G_line in $_G_message; do
710      IFS=$func_echo_infix_1_IFS
711      $ECHO "$_G_prefix$tc_bold$_G_line$tc_reset" >&2
712      _G_prefix=$_G_indent
713    done
714    IFS=$func_echo_infix_1_IFS
715}
716
717
718# func_error ARG...
719# -----------------
720# Echo program name prefixed message to standard error.
721func_error ()
722{
723    $debug_cmd
724
725    $require_term_colors
726
727    func_echo_infix_1 "  $tc_standout${tc_red}error$tc_reset" "$*" >&2
728}
729
730
731# func_fatal_error ARG...
732# -----------------------
733# Echo program name prefixed message to standard error, and exit.
734func_fatal_error ()
735{
736    $debug_cmd
737
738    func_error "$*"
739    exit $EXIT_FAILURE
740}
741
742
743# func_grep EXPRESSION FILENAME
744# -----------------------------
745# Check whether EXPRESSION matches any line of FILENAME, without output.
746func_grep ()
747{
748    $debug_cmd
749
750    $GREP "$1" "$2" >/dev/null 2>&1
751}
752
753
754# func_len STRING
755# ---------------
756# Set func_len_result to the length of STRING. STRING may not
757# start with a hyphen.
758  test -z "$_G_HAVE_XSI_OPS" \
759    && (eval 'x=a/b/c;
760      test 5aa/bb/cc = "${#x}${x%%/*}${x%/*}${x#*/}${x##*/}"') 2>/dev/null \
761    && _G_HAVE_XSI_OPS=yes
762
763if test yes = "$_G_HAVE_XSI_OPS"; then
764  eval 'func_len ()
765  {
766    $debug_cmd
767
768    func_len_result=${#1}
769  }'
770else
771  func_len ()
772  {
773    $debug_cmd
774
775    func_len_result=`expr "$1" : ".*" 2>/dev/null || echo $max_cmd_len`
776  }
777fi
778
779
780# func_mkdir_p DIRECTORY-PATH
781# ---------------------------
782# Make sure the entire path to DIRECTORY-PATH is available.
783func_mkdir_p ()
784{
785    $debug_cmd
786
787    _G_directory_path=$1
788    _G_dir_list=
789
790    if test -n "$_G_directory_path" && test : != "$opt_dry_run"; then
791
792      # Protect directory names starting with '-'
793      case $_G_directory_path in
794        -*) _G_directory_path=./$_G_directory_path ;;
795      esac
796
797      # While some portion of DIR does not yet exist...
798      while test ! -d "$_G_directory_path"; do
799        # ...make a list in topmost first order.  Use a colon delimited
800	# list incase some portion of path contains whitespace.
801        _G_dir_list=$_G_directory_path:$_G_dir_list
802
803        # If the last portion added has no slash in it, the list is done
804        case $_G_directory_path in */*) ;; *) break ;; esac
805
806        # ...otherwise throw away the child directory and loop
807        _G_directory_path=`$ECHO "$_G_directory_path" | $SED -e "$sed_dirname"`
808      done
809      _G_dir_list=`$ECHO "$_G_dir_list" | $SED 's|:*$||'`
810
811      func_mkdir_p_IFS=$IFS; IFS=:
812      for _G_dir in $_G_dir_list; do
813	IFS=$func_mkdir_p_IFS
814        # mkdir can fail with a 'File exist' error if two processes
815        # try to create one of the directories concurrently.  Don't
816        # stop in that case!
817        $MKDIR "$_G_dir" 2>/dev/null || :
818      done
819      IFS=$func_mkdir_p_IFS
820
821      # Bail out if we (or some other process) failed to create a directory.
822      test -d "$_G_directory_path" || \
823        func_fatal_error "Failed to create '$1'"
824    fi
825}
826
827
828# func_mktempdir [BASENAME]
829# -------------------------
830# Make a temporary directory that won't clash with other running
831# libtool processes, and avoids race conditions if possible.  If
832# given, BASENAME is the basename for that directory.
833func_mktempdir ()
834{
835    $debug_cmd
836
837    _G_template=${TMPDIR-/tmp}/${1-$progname}
838
839    if test : = "$opt_dry_run"; then
840      # Return a directory name, but don't create it in dry-run mode
841      _G_tmpdir=$_G_template-$$
842    else
843
844      # If mktemp works, use that first and foremost
845      _G_tmpdir=`mktemp -d "$_G_template-XXXXXXXX" 2>/dev/null`
846
847      if test ! -d "$_G_tmpdir"; then
848        # Failing that, at least try and use $RANDOM to avoid a race
849        _G_tmpdir=$_G_template-${RANDOM-0}$$
850
851        func_mktempdir_umask=`umask`
852        umask 0077
853        $MKDIR "$_G_tmpdir"
854        umask $func_mktempdir_umask
855      fi
856
857      # If we're not in dry-run mode, bomb out on failure
858      test -d "$_G_tmpdir" || \
859        func_fatal_error "cannot create temporary directory '$_G_tmpdir'"
860    fi
861
862    $ECHO "$_G_tmpdir"
863}
864
865
866# func_normal_abspath PATH
867# ------------------------
868# Remove doubled-up and trailing slashes, "." path components,
869# and cancel out any ".." path components in PATH after making
870# it an absolute path.
871func_normal_abspath ()
872{
873    $debug_cmd
874
875    # These SED scripts presuppose an absolute path with a trailing slash.
876    _G_pathcar='s|^/\([^/]*\).*$|\1|'
877    _G_pathcdr='s|^/[^/]*||'
878    _G_removedotparts=':dotsl
879		s|/\./|/|g
880		t dotsl
881		s|/\.$|/|'
882    _G_collapseslashes='s|/\{1,\}|/|g'
883    _G_finalslash='s|/*$|/|'
884
885    # Start from root dir and reassemble the path.
886    func_normal_abspath_result=
887    func_normal_abspath_tpath=$1
888    func_normal_abspath_altnamespace=
889    case $func_normal_abspath_tpath in
890      "")
891        # Empty path, that just means $cwd.
892        func_stripname '' '/' "`pwd`"
893        func_normal_abspath_result=$func_stripname_result
894        return
895        ;;
896      # The next three entries are used to spot a run of precisely
897      # two leading slashes without using negated character classes;
898      # we take advantage of case's first-match behaviour.
899      ///*)
900        # Unusual form of absolute path, do nothing.
901        ;;
902      //*)
903        # Not necessarily an ordinary path; POSIX reserves leading '//'
904        # and for example Cygwin uses it to access remote file shares
905        # over CIFS/SMB, so we conserve a leading double slash if found.
906        func_normal_abspath_altnamespace=/
907        ;;
908      /*)
909        # Absolute path, do nothing.
910        ;;
911      *)
912        # Relative path, prepend $cwd.
913        func_normal_abspath_tpath=`pwd`/$func_normal_abspath_tpath
914        ;;
915    esac
916
917    # Cancel out all the simple stuff to save iterations.  We also want
918    # the path to end with a slash for ease of parsing, so make sure
919    # there is one (and only one) here.
920    func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \
921          -e "$_G_removedotparts" -e "$_G_collapseslashes" -e "$_G_finalslash"`
922    while :; do
923      # Processed it all yet?
924      if test / = "$func_normal_abspath_tpath"; then
925        # If we ascended to the root using ".." the result may be empty now.
926        if test -z "$func_normal_abspath_result"; then
927          func_normal_abspath_result=/
928        fi
929        break
930      fi
931      func_normal_abspath_tcomponent=`$ECHO "$func_normal_abspath_tpath" | $SED \
932          -e "$_G_pathcar"`
933      func_normal_abspath_tpath=`$ECHO "$func_normal_abspath_tpath" | $SED \
934          -e "$_G_pathcdr"`
935      # Figure out what to do with it
936      case $func_normal_abspath_tcomponent in
937        "")
938          # Trailing empty path component, ignore it.
939          ;;
940        ..)
941          # Parent dir; strip last assembled component from result.
942          func_dirname "$func_normal_abspath_result"
943          func_normal_abspath_result=$func_dirname_result
944          ;;
945        *)
946          # Actual path component, append it.
947          func_append func_normal_abspath_result "/$func_normal_abspath_tcomponent"
948          ;;
949      esac
950    done
951    # Restore leading double-slash if one was found on entry.
952    func_normal_abspath_result=$func_normal_abspath_altnamespace$func_normal_abspath_result
953}
954
955
956# func_notquiet ARG...
957# --------------------
958# Echo program name prefixed message only when not in quiet mode.
959func_notquiet ()
960{
961    $debug_cmd
962
963    $opt_quiet || func_echo ${1+"$@"}
964
965    # A bug in bash halts the script if the last line of a function
966    # fails when set -e is in force, so we need another command to
967    # work around that:
968    :
969}
970
971
972# func_relative_path SRCDIR DSTDIR
973# --------------------------------
974# Set func_relative_path_result to the relative path from SRCDIR to DSTDIR.
975func_relative_path ()
976{
977    $debug_cmd
978
979    func_relative_path_result=
980    func_normal_abspath "$1"
981    func_relative_path_tlibdir=$func_normal_abspath_result
982    func_normal_abspath "$2"
983    func_relative_path_tbindir=$func_normal_abspath_result
984
985    # Ascend the tree starting from libdir
986    while :; do
987      # check if we have found a prefix of bindir
988      case $func_relative_path_tbindir in
989        $func_relative_path_tlibdir)
990          # found an exact match
991          func_relative_path_tcancelled=
992          break
993          ;;
994        $func_relative_path_tlibdir*)
995          # found a matching prefix
996          func_stripname "$func_relative_path_tlibdir" '' "$func_relative_path_tbindir"
997          func_relative_path_tcancelled=$func_stripname_result
998          if test -z "$func_relative_path_result"; then
999            func_relative_path_result=.
1000          fi
1001          break
1002          ;;
1003        *)
1004          func_dirname $func_relative_path_tlibdir
1005          func_relative_path_tlibdir=$func_dirname_result
1006          if test -z "$func_relative_path_tlibdir"; then
1007            # Have to descend all the way to the root!
1008            func_relative_path_result=../$func_relative_path_result
1009            func_relative_path_tcancelled=$func_relative_path_tbindir
1010            break
1011          fi
1012          func_relative_path_result=../$func_relative_path_result
1013          ;;
1014      esac
1015    done
1016
1017    # Now calculate path; take care to avoid doubling-up slashes.
1018    func_stripname '' '/' "$func_relative_path_result"
1019    func_relative_path_result=$func_stripname_result
1020    func_stripname '/' '/' "$func_relative_path_tcancelled"
1021    if test -n "$func_stripname_result"; then
1022      func_append func_relative_path_result "/$func_stripname_result"
1023    fi
1024
1025    # Normalisation. If bindir is libdir, return '.' else relative path.
1026    if test -n "$func_relative_path_result"; then
1027      func_stripname './' '' "$func_relative_path_result"
1028      func_relative_path_result=$func_stripname_result
1029    fi
1030
1031    test -n "$func_relative_path_result" || func_relative_path_result=.
1032
1033    :
1034}
1035
1036
1037# func_quote_portable EVAL ARG
1038# ----------------------------
1039# Internal function to portably implement func_quote_arg.  Note that we still
1040# keep attention to performance here so we as much as possible try to avoid
1041# calling sed binary (so far O(N) complexity as long as func_append is O(1)).
1042func_quote_portable ()
1043{
1044    $debug_cmd
1045
1046    func_quote_portable_result=$2
1047
1048    # one-time-loop (easy break)
1049    while true
1050    do
1051      if $1; then
1052        func_quote_portable_result=`$ECHO "$2" | $SED \
1053          -e "$sed_double_quote_subst" -e "$sed_double_backslash"`
1054        break
1055      fi
1056
1057      # Quote for eval.
1058      case $func_quote_portable_result in
1059        *[\\\`\"\$]*)
1060          case $func_quote_portable_result in
1061            *[\[\*\?]*)
1062              func_quote_portable_result=`$ECHO "$func_quote_portable_result" \
1063                  | $SED "$sed_quote_subst"`
1064              break
1065              ;;
1066          esac
1067
1068          func_quote_portable_old_IFS=$IFS
1069          for _G_char in '\' '`' '"' '$'
1070          do
1071            # STATE($1) PREV($2) SEPARATOR($3)
1072            set start "" ""
1073            func_quote_portable_result=dummy"$_G_char$func_quote_portable_result$_G_char"dummy
1074            IFS=$_G_char
1075            for _G_part in $func_quote_portable_result
1076            do
1077              case $1 in
1078              quote)
1079                func_append func_quote_portable_result "$3$2"
1080                set quote "$_G_part" "\\$_G_char"
1081                ;;
1082              start)
1083                set first "" ""
1084                func_quote_portable_result=
1085                ;;
1086              first)
1087                set quote "$_G_part" ""
1088                ;;
1089              esac
1090            done
1091          done
1092          IFS=$func_quote_portable_old_IFS
1093          ;;
1094        *) ;;
1095      esac
1096      break
1097    done
1098
1099    func_quote_portable_unquoted_result=$func_quote_portable_result
1100    case $func_quote_portable_result in
1101      # double-quote args containing shell metacharacters to delay
1102      # word splitting, command substitution and variable expansion
1103      # for a subsequent eval.
1104      # many bourne shells cannot handle close brackets correctly
1105      # in scan sets, so we specify it separately.
1106      *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \	]*|*]*|"")
1107        func_quote_portable_result=\"$func_quote_portable_result\"
1108        ;;
1109    esac
1110}
1111
1112
1113# func_quotefast_eval ARG
1114# -----------------------
1115# Quote one ARG (internal).  This is equivalent to 'func_quote_arg eval ARG',
1116# but optimized for speed.  Result is stored in $func_quotefast_eval.
1117if test xyes = `(x=; printf -v x %q yes; echo x"$x") 2>/dev/null`; then
1118  printf -v _GL_test_printf_tilde %q '~'
1119  if test '\~' = "$_GL_test_printf_tilde"; then
1120    func_quotefast_eval ()
1121    {
1122      printf -v func_quotefast_eval_result %q "$1"
1123    }
1124  else
1125    # Broken older Bash implementations.  Make those faster too if possible.
1126    func_quotefast_eval ()
1127    {
1128      case $1 in
1129        '~'*)
1130          func_quote_portable false "$1"
1131          func_quotefast_eval_result=$func_quote_portable_result
1132          ;;
1133        *)
1134          printf -v func_quotefast_eval_result %q "$1"
1135          ;;
1136      esac
1137    }
1138  fi
1139else
1140  func_quotefast_eval ()
1141  {
1142    func_quote_portable false "$1"
1143    func_quotefast_eval_result=$func_quote_portable_result
1144  }
1145fi
1146
1147
1148# func_quote_arg MODEs ARG
1149# ------------------------
1150# Quote one ARG to be evaled later.  MODEs argument may contain zero or more
1151# specifiers listed below separated by ',' character.  This function returns two
1152# values:
1153#   i) func_quote_arg_result
1154#      double-quoted (when needed), suitable for a subsequent eval
1155#  ii) func_quote_arg_unquoted_result
1156#      has all characters that are still active within double
1157#      quotes backslashified.  Available only if 'unquoted' is specified.
1158#
1159# Available modes:
1160# ----------------
1161# 'eval' (default)
1162#       - escape shell special characters
1163# 'expand'
1164#       - the same as 'eval';  but do not quote variable references
1165# 'pretty'
1166#       - request aesthetic output, i.e. '"a b"' instead of 'a\ b'.  This might
1167#         be used later in func_quote to get output like: 'echo "a b"' instead
1168#         of 'echo a\ b'.  This is slower than default on some shells.
1169# 'unquoted'
1170#       - produce also $func_quote_arg_unquoted_result which does not contain
1171#         wrapping double-quotes.
1172#
1173# Examples for 'func_quote_arg pretty,unquoted string':
1174#
1175#   string      | *_result              | *_unquoted_result
1176#   ------------+-----------------------+-------------------
1177#   "           | \"                    | \"
1178#   a b         | "a b"                 | a b
1179#   "a b"       | "\"a b\""             | \"a b\"
1180#   *           | "*"                   | *
1181#   z="${x-$y}" | "z=\"\${x-\$y}\""     | z=\"\${x-\$y}\"
1182#
1183# Examples for 'func_quote_arg pretty,unquoted,expand string':
1184#
1185#   string        |   *_result          |  *_unquoted_result
1186#   --------------+---------------------+--------------------
1187#   z="${x-$y}"   | "z=\"${x-$y}\""     | z=\"${x-$y}\"
1188func_quote_arg ()
1189{
1190    _G_quote_expand=false
1191    case ,$1, in
1192      *,expand,*)
1193        _G_quote_expand=:
1194        ;;
1195    esac
1196
1197    case ,$1, in
1198      *,pretty,*|*,expand,*|*,unquoted,*)
1199        func_quote_portable $_G_quote_expand "$2"
1200        func_quote_arg_result=$func_quote_portable_result
1201        func_quote_arg_unquoted_result=$func_quote_portable_unquoted_result
1202        ;;
1203      *)
1204        # Faster quote-for-eval for some shells.
1205        func_quotefast_eval "$2"
1206        func_quote_arg_result=$func_quotefast_eval_result
1207        ;;
1208    esac
1209}
1210
1211
1212# func_quote MODEs ARGs...
1213# ------------------------
1214# Quote all ARGs to be evaled later and join them into single command.  See
1215# func_quote_arg's description for more info.
1216func_quote ()
1217{
1218    $debug_cmd
1219    _G_func_quote_mode=$1 ; shift
1220    func_quote_result=
1221    while test 0 -lt $#; do
1222      func_quote_arg "$_G_func_quote_mode" "$1"
1223      if test -n "$func_quote_result"; then
1224        func_append func_quote_result " $func_quote_arg_result"
1225      else
1226        func_append func_quote_result "$func_quote_arg_result"
1227      fi
1228      shift
1229    done
1230}
1231
1232
1233# func_stripname PREFIX SUFFIX NAME
1234# ---------------------------------
1235# strip PREFIX and SUFFIX from NAME, and store in func_stripname_result.
1236# PREFIX and SUFFIX must not contain globbing or regex special
1237# characters, hashes, percent signs, but SUFFIX may contain a leading
1238# dot (in which case that matches only a dot).
1239if test yes = "$_G_HAVE_XSI_OPS"; then
1240  eval 'func_stripname ()
1241  {
1242    $debug_cmd
1243
1244    # pdksh 5.2.14 does not do ${X%$Y} correctly if both X and Y are
1245    # positional parameters, so assign one to ordinary variable first.
1246    func_stripname_result=$3
1247    func_stripname_result=${func_stripname_result#"$1"}
1248    func_stripname_result=${func_stripname_result%"$2"}
1249  }'
1250else
1251  func_stripname ()
1252  {
1253    $debug_cmd
1254
1255    case $2 in
1256      .*) func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%\\\\$2\$%%"`;;
1257      *)  func_stripname_result=`$ECHO "$3" | $SED -e "s%^$1%%" -e "s%$2\$%%"`;;
1258    esac
1259  }
1260fi
1261
1262
1263# func_show_eval CMD [FAIL_EXP]
1264# -----------------------------
1265# Unless opt_quiet is true, then output CMD.  Then, if opt_dryrun is
1266# not true, evaluate CMD.  If the evaluation of CMD fails, and FAIL_EXP
1267# is given, then evaluate it.
1268func_show_eval ()
1269{
1270    $debug_cmd
1271
1272    _G_cmd=$1
1273    _G_fail_exp=${2-':'}
1274
1275    func_quote_arg pretty,expand "$_G_cmd"
1276    eval "func_notquiet $func_quote_arg_result"
1277
1278    $opt_dry_run || {
1279      eval "$_G_cmd"
1280      _G_status=$?
1281      if test 0 -ne "$_G_status"; then
1282	eval "(exit $_G_status); $_G_fail_exp"
1283      fi
1284    }
1285}
1286
1287
1288# func_show_eval_locale CMD [FAIL_EXP]
1289# ------------------------------------
1290# Unless opt_quiet is true, then output CMD.  Then, if opt_dryrun is
1291# not true, evaluate CMD.  If the evaluation of CMD fails, and FAIL_EXP
1292# is given, then evaluate it.  Use the saved locale for evaluation.
1293func_show_eval_locale ()
1294{
1295    $debug_cmd
1296
1297    _G_cmd=$1
1298    _G_fail_exp=${2-':'}
1299
1300    $opt_quiet || {
1301      func_quote_arg expand,pretty "$_G_cmd"
1302      eval "func_echo $func_quote_arg_result"
1303    }
1304
1305    $opt_dry_run || {
1306      eval "$_G_user_locale
1307	    $_G_cmd"
1308      _G_status=$?
1309      eval "$_G_safe_locale"
1310      if test 0 -ne "$_G_status"; then
1311	eval "(exit $_G_status); $_G_fail_exp"
1312      fi
1313    }
1314}
1315
1316
1317# func_tr_sh
1318# ----------
1319# Turn $1 into a string suitable for a shell variable name.
1320# Result is stored in $func_tr_sh_result.  All characters
1321# not in the set a-zA-Z0-9_ are replaced with '_'. Further,
1322# if $1 begins with a digit, a '_' is prepended as well.
1323func_tr_sh ()
1324{
1325    $debug_cmd
1326
1327    case $1 in
1328    [0-9]* | *[!a-zA-Z0-9_]*)
1329      func_tr_sh_result=`$ECHO "$1" | $SED -e 's/^\([0-9]\)/_\1/' -e 's/[^a-zA-Z0-9_]/_/g'`
1330      ;;
1331    * )
1332      func_tr_sh_result=$1
1333      ;;
1334    esac
1335}
1336
1337
1338# func_verbose ARG...
1339# -------------------
1340# Echo program name prefixed message in verbose mode only.
1341func_verbose ()
1342{
1343    $debug_cmd
1344
1345    $opt_verbose && func_echo "$*"
1346
1347    :
1348}
1349
1350
1351# func_warn_and_continue ARG...
1352# -----------------------------
1353# Echo program name prefixed warning message to standard error.
1354func_warn_and_continue ()
1355{
1356    $debug_cmd
1357
1358    $require_term_colors
1359
1360    func_echo_infix_1 "${tc_red}warning$tc_reset" "$*" >&2
1361}
1362
1363
1364# func_warning CATEGORY ARG...
1365# ----------------------------
1366# Echo program name prefixed warning message to standard error. Warning
1367# messages can be filtered according to CATEGORY, where this function
1368# elides messages where CATEGORY is not listed in the global variable
1369# 'opt_warning_types'.
1370func_warning ()
1371{
1372    $debug_cmd
1373
1374    # CATEGORY must be in the warning_categories list!
1375    case " $warning_categories " in
1376      *" $1 "*) ;;
1377      *) func_internal_error "invalid warning category '$1'" ;;
1378    esac
1379
1380    _G_category=$1
1381    shift
1382
1383    case " $opt_warning_types " in
1384      *" $_G_category "*) $warning_func ${1+"$@"} ;;
1385    esac
1386}
1387
1388
1389# func_sort_ver VER1 VER2
1390# -----------------------
1391# 'sort -V' is not generally available.
1392# Note this deviates from the version comparison in automake
1393# in that it treats 1.5 < 1.5.0, and treats 1.4.4a < 1.4-p3a
1394# but this should suffice as we won't be specifying old
1395# version formats or redundant trailing .0 in bootstrap.conf.
1396# If we did want full compatibility then we should probably
1397# use m4_version_compare from autoconf.
1398func_sort_ver ()
1399{
1400    $debug_cmd
1401
1402    printf '%s\n%s\n' "$1" "$2" \
1403      | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n -k 5,5n -k 6,6n -k 7,7n -k 8,8n -k 9,9n
1404}
1405
1406# func_lt_ver PREV CURR
1407# ---------------------
1408# Return true if PREV and CURR are in the correct order according to
1409# func_sort_ver, otherwise false.  Use it like this:
1410#
1411#  func_lt_ver "$prev_ver" "$proposed_ver" || func_fatal_error "..."
1412func_lt_ver ()
1413{
1414    $debug_cmd
1415
1416    test "x$1" = x`func_sort_ver "$1" "$2" | $SED 1q`
1417}
1418
1419
1420# Local variables:
1421# mode: shell-script
1422# sh-indentation: 2
1423# eval: (add-hook 'before-save-hook 'time-stamp)
1424# time-stamp-pattern: "10/scriptversion=%:y-%02m-%02d.%02H; # UTC"
1425# time-stamp-time-zone: "UTC"
1426# End:
1427