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