From: Gary V. Vaughan Date: Mon, 28 Oct 2013 05:50:36 +0000 (+1300) Subject: refactor: simplify program path searching in funclib.sh. X-Git-Tag: v2.4.2.427~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=94271692f0bd4933b712be802818b90422975795;p=thirdparty%2Flibtool.git refactor: simplify program path searching in funclib.sh. * gl/build-aux/funclib.sh (func_path_progs): New function. Factor out common code from GREP and SED searches. (func_check_prog_sed, func_check_prog_grep): New functions, for non-common code. (scriptversion): Update. * bootstrap: Regenerate. Signed-off-by: Gary V. Vaughan --- diff --git a/bootstrap b/bootstrap index ad8c09c53..a3742f0f2 100755 --- a/bootstrap +++ b/bootstrap @@ -9,7 +9,7 @@ # Source required external libraries: # Set a version string for this script. -scriptversion=2013-10-28.02; # UTC +scriptversion=2013-10-28.05; # UTC # General shell script boiler plate, and helper functions. # Written by Gary V. Vaughan, 2004 @@ -119,6 +119,50 @@ func_executable_p () } +# func_path_progs PROGS_LIST CHECK_FUNC [PATH] +# -------------------------------------------- +# Search for either a program that responds to --version with output +# containing "GNU", or else returned by CHECK_FUNC otherwise, by +# trying all the directories in PATH with each of the elements of +# PROGS_LIST. +# +# CHECK_FUNC should accept the path to a candidate program, and +# set $func_check_prog_result if it truncates its output less than +# $_G_path_prog_max characters. +func_path_progs () +{ + _G_progs_list=$1 + _G_check_func=$2 + _G_PATH=${3-"$PATH"} + + _G_path_prog_max=0 + _G_path_prog_found=false + _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR + for _G_dir in $_G_PATH; do + IFS=$_G_save_IFS + test -z "$_G_dir" && _G_dir=. + for _G_prog_name in $_G_progs_list; do + for _exeext in '' .EXE; do + _G_path_prog=$_G_dir/$_G_prog_name$_exeext + func_executable_p "$_G_path_prog" || continue + case `"$_G_path_prog" --version 2>&1` in + *GNU*) func_path_progs_result=$_G_path_prog _G_path_prog_found=: ;; + *) $_G_check_func $_G_path_prog + func_path_progs_result=$func_check_prog_result + ;; + esac + $_G_path_prog_found && break 3 + done + done + done + IFS=$_G_save_IFS + test -z "$func_path_progs_result" && { + echo "no acceptable sed could be found in \$PATH" >&2 + exit 1 + } +} + + # There are still modern systems that have problems with 'echo' mis- # handling backslashes, among others, so make sure $bs_echo is set to a # command that correctly interprets backslashes. @@ -165,109 +209,74 @@ fi test -z "$SED" && { _G_sed_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ for _G_i in 1 2 3 4 5 6 7; do - _G_sed_script="$_G_sed_script$nl$_G_sed_script" + _G_sed_script=$_G_sed_script$nl$_G_sed_script done echo "$_G_sed_script" 2>/dev/null | sed 99q >conftest.sed _G_sed_script= - _G_path_prog_found=false - _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR - for _G_dir in $PATH:/usr/xpg4/bin; do - IFS=$_G_save_IFS - test -z "$_G_dir" && _G_dir=. - for _G_prog_name in sed gsed; do - for _exeext in '' .EXE; do - _G_path_prog="$_G_dir/$_G_prog_name$_exeext" - func_executable_p "$_G_path_prog" || continue - case `"$_G_path_prog" --version 2>&1` in - *GNU*) _G_path_SED=$_G_path_prog _G_path_prog_found=: ;; - *) - _G_count=0 - _G_path_prog_max=0 - $bs_echo_n 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - $bs_echo '' >> conftest.nl - "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test $_G_count -gt $_G_path_prog_max; then - # Best one so far, save it but keep looking for a better one - _G_path_SED=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test $_G_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - ;; - esac + func_check_prog_sed () + { + _G_path_prog=$1 - $_G_path_prog_found && break 3 - done + _G_count=0 + $bs_echo_n 0123456789 >conftest.in + while : + do + cat conftest.in conftest.in >conftest.tmp + mv conftest.tmp conftest.in + cp conftest.in conftest.nl + $bs_echo '' >> conftest.nl + "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break + diff conftest.out conftest.nl >/dev/null 2>&1 || break + _G_count=`expr $_G_count + 1` + if test "$_G_count" -gt "$_G_path_prog_max"; then + # Best one so far, save it but keep looking for a better one + func_check_prog_result=$_G_path_prog + _G_path_prog_max=$_G_count + fi + # 10*(2^10) chars as input seems more than enough + test 10 -lt "$_G_count" && break done - done - IFS=$_G_save_IFS - test -z "$_G_path_SED" && { - echo "no acceptable sed could be found in \$PATH" >&2 - exit 1 + rm -f conftest.in conftest.tmp conftest.nl conftest.out } - SED=$_G_path_SED + + func_path_progs "sed gsed" func_check_prog_sed $PATH:/usr/xpg4/bin + SED=$func_path_progs_result } # Unless the user overrides by setting GREP, search the path for either GNU # grep, or the sed that truncates its output the least. test -z "$GREP" && { - _G_path_prog_max=0 - _G_path_prog_found=false - _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR - for _G_dir in $PATH:/usr/xpg4/bin; do - IFS=$_G_save_IFS - test -z "$_G_dir" && _G_dir=. - for _G_prog_name in grep ggrep; do - for _exeext in '' .EXE; do - _G_path_prog="$_G_dir/$_G_prog_name$_exeext" - func_executable_p "$_G_path_prog" || continue - case `"$_G_path_prog" --version 2>&1` in - *GNU*) _G_path_GREP=$_G_path_prog _G_path_prog_found=: ;; - *) - _G_count=0 - $bs_echo_n 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - $bs_echo 'GREP' >> conftest.nl - "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test $_G_count -gt $_G_path_prog_max; then - # Best one so far, save it but keep looking for a better one - _G_path_GREP=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test $_G_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - ;; - esac + func_check_prog_grep () + { + _G_path_prog=$1 - $_G_path_prog_found && break 3 - done + _G_count=0 + _G_path_prog_max=0 + $bs_echo_n 0123456789 >conftest.in + while : + do + cat conftest.in conftest.in >conftest.tmp + mv conftest.tmp conftest.in + cp conftest.in conftest.nl + $bs_echo 'GREP' >> conftest.nl + "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break + diff conftest.out conftest.nl >/dev/null 2>&1 || break + _G_count=`expr $_G_count + 1` + if test "$_G_count" -gt "$_G_path_prog_max"; then + # Best one so far, save it but keep looking for a better one + func_check_prog_result=$_G_path_prog + _G_path_prog_max=$_G_count + fi + # 10*(2^10) chars as input seems more than enough + test 10 -lt "$_G_count" && break done - done - IFS=$_G_save_IFS - test -z "$_G_path_GREP" && { - echo "no acceptable grep could be found in \$PATH" >&2 - exit 1 + rm -f conftest.in conftest.tmp conftest.nl conftest.out } - GREP=$_G_path_GREP + + func_path_progs "grep ggrep" func_check_prog_grep $PATH:/usr/xpg4/bin + GREP=$func_path_progs_result } diff --git a/gl/build-aux/funclib.sh b/gl/build-aux/funclib.sh index db5f6c433..beb2aa47e 100644 --- a/gl/build-aux/funclib.sh +++ b/gl/build-aux/funclib.sh @@ -1,5 +1,5 @@ # Set a version string for this script. -scriptversion=2013-10-28.02; # UTC +scriptversion=2013-10-28.05; # UTC # General shell script boiler plate, and helper functions. # Written by Gary V. Vaughan, 2004 @@ -109,6 +109,50 @@ func_executable_p () } +# func_path_progs PROGS_LIST CHECK_FUNC [PATH] +# -------------------------------------------- +# Search for either a program that responds to --version with output +# containing "GNU", or else returned by CHECK_FUNC otherwise, by +# trying all the directories in PATH with each of the elements of +# PROGS_LIST. +# +# CHECK_FUNC should accept the path to a candidate program, and +# set $func_check_prog_result if it truncates its output less than +# $_G_path_prog_max characters. +func_path_progs () +{ + _G_progs_list=$1 + _G_check_func=$2 + _G_PATH=${3-"$PATH"} + + _G_path_prog_max=0 + _G_path_prog_found=false + _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR + for _G_dir in $_G_PATH; do + IFS=$_G_save_IFS + test -z "$_G_dir" && _G_dir=. + for _G_prog_name in $_G_progs_list; do + for _exeext in '' .EXE; do + _G_path_prog=$_G_dir/$_G_prog_name$_exeext + func_executable_p "$_G_path_prog" || continue + case `"$_G_path_prog" --version 2>&1` in + *GNU*) func_path_progs_result=$_G_path_prog _G_path_prog_found=: ;; + *) $_G_check_func $_G_path_prog + func_path_progs_result=$func_check_prog_result + ;; + esac + $_G_path_prog_found && break 3 + done + done + done + IFS=$_G_save_IFS + test -z "$func_path_progs_result" && { + echo "no acceptable sed could be found in \$PATH" >&2 + exit 1 + } +} + + # There are still modern systems that have problems with 'echo' mis- # handling backslashes, among others, so make sure $bs_echo is set to a # command that correctly interprets backslashes. @@ -155,109 +199,74 @@ fi test -z "$SED" && { _G_sed_script=s/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/ for _G_i in 1 2 3 4 5 6 7; do - _G_sed_script="$_G_sed_script$nl$_G_sed_script" + _G_sed_script=$_G_sed_script$nl$_G_sed_script done echo "$_G_sed_script" 2>/dev/null | sed 99q >conftest.sed _G_sed_script= - _G_path_prog_found=false - _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR - for _G_dir in $PATH:/usr/xpg4/bin; do - IFS=$_G_save_IFS - test -z "$_G_dir" && _G_dir=. - for _G_prog_name in sed gsed; do - for _exeext in '' .EXE; do - _G_path_prog="$_G_dir/$_G_prog_name$_exeext" - func_executable_p "$_G_path_prog" || continue - case `"$_G_path_prog" --version 2>&1` in - *GNU*) _G_path_SED=$_G_path_prog _G_path_prog_found=: ;; - *) - _G_count=0 - _G_path_prog_max=0 - $bs_echo_n 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - $bs_echo '' >> conftest.nl - "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test $_G_count -gt $_G_path_prog_max; then - # Best one so far, save it but keep looking for a better one - _G_path_SED=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test $_G_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - ;; - esac - - $_G_path_prog_found && break 3 - done + func_check_prog_sed () + { + _G_path_prog=$1 + + _G_count=0 + $bs_echo_n 0123456789 >conftest.in + while : + do + cat conftest.in conftest.in >conftest.tmp + mv conftest.tmp conftest.in + cp conftest.in conftest.nl + $bs_echo '' >> conftest.nl + "$_G_path_prog" -f conftest.sed conftest.out 2>/dev/null || break + diff conftest.out conftest.nl >/dev/null 2>&1 || break + _G_count=`expr $_G_count + 1` + if test "$_G_count" -gt "$_G_path_prog_max"; then + # Best one so far, save it but keep looking for a better one + func_check_prog_result=$_G_path_prog + _G_path_prog_max=$_G_count + fi + # 10*(2^10) chars as input seems more than enough + test 10 -lt "$_G_count" && break done - done - IFS=$_G_save_IFS - test -z "$_G_path_SED" && { - echo "no acceptable sed could be found in \$PATH" >&2 - exit 1 + rm -f conftest.in conftest.tmp conftest.nl conftest.out } - SED=$_G_path_SED + + func_path_progs "sed gsed" func_check_prog_sed $PATH:/usr/xpg4/bin + SED=$func_path_progs_result } # Unless the user overrides by setting GREP, search the path for either GNU # grep, or the sed that truncates its output the least. test -z "$GREP" && { - _G_path_prog_max=0 - _G_path_prog_found=false - _G_save_IFS=$IFS; IFS=$PATH_SEPARATOR - for _G_dir in $PATH:/usr/xpg4/bin; do - IFS=$_G_save_IFS - test -z "$_G_dir" && _G_dir=. - for _G_prog_name in grep ggrep; do - for _exeext in '' .EXE; do - _G_path_prog="$_G_dir/$_G_prog_name$_exeext" - func_executable_p "$_G_path_prog" || continue - case `"$_G_path_prog" --version 2>&1` in - *GNU*) _G_path_GREP=$_G_path_prog _G_path_prog_found=: ;; - *) - _G_count=0 - $bs_echo_n 0123456789 >conftest.in - while : - do - cat conftest.in conftest.in >conftest.tmp - mv conftest.tmp conftest.in - cp conftest.in conftest.nl - $bs_echo 'GREP' >> conftest.nl - "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break - diff conftest.out conftest.nl >/dev/null 2>&1 || break - _G_count=`expr $_G_count + 1` - if test $_G_count -gt $_G_path_prog_max; then - # Best one so far, save it but keep looking for a better one - _G_path_GREP=$_G_path_prog - _G_path_prog_max=$_G_count - fi - # 10*(2^10) chars as input seems more than enough - test $_G_count -gt 10 && break - done - rm -f conftest.in conftest.tmp conftest.nl conftest.out - ;; - esac - - $_G_path_prog_found && break 3 - done + func_check_prog_grep () + { + _G_path_prog=$1 + + _G_count=0 + _G_path_prog_max=0 + $bs_echo_n 0123456789 >conftest.in + while : + do + cat conftest.in conftest.in >conftest.tmp + mv conftest.tmp conftest.in + cp conftest.in conftest.nl + $bs_echo 'GREP' >> conftest.nl + "$_G_path_prog" -e 'GREP$' -e '-(cannot match)-' conftest.out 2>/dev/null || break + diff conftest.out conftest.nl >/dev/null 2>&1 || break + _G_count=`expr $_G_count + 1` + if test "$_G_count" -gt "$_G_path_prog_max"; then + # Best one so far, save it but keep looking for a better one + func_check_prog_result=$_G_path_prog + _G_path_prog_max=$_G_count + fi + # 10*(2^10) chars as input seems more than enough + test 10 -lt "$_G_count" && break done - done - IFS=$_G_save_IFS - test -z "$_G_path_GREP" && { - echo "no acceptable grep could be found in \$PATH" >&2 - exit 1 + rm -f conftest.in conftest.tmp conftest.nl conftest.out } - GREP=$_G_path_GREP + + func_path_progs "grep ggrep" func_check_prog_grep $PATH:/usr/xpg4/bin + GREP=$func_path_progs_result }