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