CONFIG: verify bash version for completion support

- the (global) associative array requires bash >= 4.2
This commit is contained in:
Mark Olesen 2017-08-10 10:05:22 +02:00
parent 5da321b6e6
commit 92d1d1f037
4 changed files with 84 additions and 29 deletions

View File

@ -175,7 +175,7 @@ then
_foamEtc config.sh/aliases
# Bash completions
if command -v complete > /dev/null 2>&1
if [ "${BASH_VERSINFO:-0}" -ge 4 ]
then
_foamEtc config.sh/bash_completion
fi

View File

@ -25,6 +25,15 @@
#------------------------------------------------------------------------------
[ "$#" -ge 1 ] || exit 1
# Support '-test' option to check bash version
if [ "$1" = "-test" ]
then
# Uses 'declare -gA' for the implementation
# The '-A' requires bash >= 4.0 and the '-g' requires bash >= 4.2
[ "${BASH_VERSINFO[0]:-0}${BASH_VERSINFO[1]:-0}" -ge 42 ]
exit $?
fi
# Preload completion cache
if [ -f $WM_PROJECT_DIR/etc/config.sh/completion_cache ]
then . $WM_PROJECT_DIR/etc/config.sh/completion_cache

View File

@ -1,25 +1,47 @@
#----------------------------------*-sh-*--------------------------------------
# Tcsh completions for OpenFOAM applications
# Using bash_completion functions for the hard work
# ========= |
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
# \\ / O peration |
# \\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
# \\/ M anipulation |
#------------------------------------------------------------------------------
# This file is part of OpenFOAM, licensed under the GNU General Public License
# <http://www.gnu.org/licenses/>.
#
# File
# etc/config.csh/tcsh_completion
#
# Description
# Tcsh completions for OpenFOAM applications
# Using bash completion for the hard work
#
# Requires
# bash 4.2 or newer
#
#------------------------------------------------------------------------------
if ($?tcsh) then # tcsh only
# Remove old completions, which look like:
# complete APPNAME 'p,*,`bash $WM_PROJECT_DIR/etc/ ...
foreach appName (`complete | sed -ne '/WM_PROJECT/s/\t.*$//p'`)
uncomplete $cleaned
uncomplete $appName
end
# Generate completions for predefined directories
foreach dirName ("$FOAM_APPBIN")
if ( ! -d $dirName || ! -f $WM_PROJECT_DIR/etc/config.csh/complete-wrapper ) continue
foreach appName (`find $dirName -maxdepth 1 -executable -type f`)
# Pass explicitly
## complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t' "${COMMAND_LINE}"`,'
# Pass via environment
complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t'`,'
# Generate completions for predefined directories (if support is possible)
bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper -test
if ($status == 0) then
foreach dirName ("$FOAM_APPBIN")
if ( ! -d $dirName ) continue
foreach appName (`find $dirName -maxdepth 1 -executable -type f`)
# Pass explicitly
## complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t' "${COMMAND_LINE}"`,'
# Pass via environment
complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t'`,'
end
end
end
endif
endif
#------------------------------------------------------------------------------

View File

@ -22,8 +22,24 @@
# Uses
# _of_complete_cache_
#
# Requires
# bash 4.2 or newer
#
#------------------------------------------------------------------------------
# Remove old completions (skip for tcsh wrapper), which look like:
# "complete ... -F _of_complete_ APPNAME
if [ -z "$_of_complete_tcsh" ]
then
# For economy, obtain list first
foamOldDirs="$(complete 2>/dev/null | sed -ne 's/^.*-F _of_.* \(..*\)$/\1/p')"
for cleaned in $foamOldDirs
do
complete -r $cleaned 2>/dev/null
done
fi
# Add completion for command or directory of commands
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
unset -f foamAddCompletion 2>/dev/null
@ -173,26 +189,34 @@ _of_complete_()
#------------------------------------------------------------------------------
# Global associative array (cached options for OpenFOAM applications)
declare -gA _of_complete_cache_;
# Clear existing cache and reassign bash completions.
# But for tcsh wrapper make use of caching and avoid this overhead.
if [ -z "$_of_complete_tcsh" ]
# Uses 'declare -gA' for the implementation
# The '-A' requires bash >= 4.0 and the '-g' requires bash >= 4.2
if [ "${BASH_VERSINFO[0]:-0}${BASH_VERSINFO[1]:-0}" -ge 42 ]
then
_of_complete_cache_=()
# Global associative array (cached options for OpenFOAM applications)
declare -gA _of_complete_cache_;
# Remove old completions, which look like:
# "complete ... -F _of_complete_ APPNAME
# For economy, obtain list first
foamOldDirs="$(complete 2>/dev/null | sed -ne 's/^.*-F _of_.* \(..*\)$/\1/p')"
for cleaned in $foamOldDirs
do
complete -r $cleaned 2>/dev/null
done
# Clear existing cache and reassign bash completions.
# But for tcsh wrapper make use of caching and avoid this overhead.
if [ -z "$_of_complete_tcsh" ]
then
_of_complete_cache_=()
# Generate completions for predefined directories
foamAddCompletion $FOAM_APPBIN
# Generate completions for predefined directories
foamAddCompletion $FOAM_APPBIN
fi
else
# Bash version is too old.
## echo "No bash completions - requires bash >= 4.2" 1>&2
unset -f foamAddCompletion 2>/dev/null
foamAddCompletion()
{
echo "foamAddCompletion disabled - requires bash >= 4.2" 1>&2
}
unset -f _of_complete_ 2>/dev/null
fi
#------------------------------------------------------------------------------