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 _foamEtc config.sh/aliases
# Bash completions # Bash completions
if command -v complete > /dev/null 2>&1 if [ "${BASH_VERSINFO:-0}" -ge 4 ]
then then
_foamEtc config.sh/bash_completion _foamEtc config.sh/bash_completion
fi fi

View File

@ -25,6 +25,15 @@
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
[ "$#" -ge 1 ] || exit 1 [ "$#" -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 # Preload completion cache
if [ -f $WM_PROJECT_DIR/etc/config.sh/completion_cache ] if [ -f $WM_PROJECT_DIR/etc/config.sh/completion_cache ]
then . $WM_PROJECT_DIR/etc/config.sh/completion_cache then . $WM_PROJECT_DIR/etc/config.sh/completion_cache

View File

@ -1,25 +1,47 @@
#----------------------------------*-sh-*-------------------------------------- #----------------------------------*-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 if ($?tcsh) then # tcsh only
# Remove old completions, which look like: # Remove old completions, which look like:
# complete APPNAME 'p,*,`bash $WM_PROJECT_DIR/etc/ ... # complete APPNAME 'p,*,`bash $WM_PROJECT_DIR/etc/ ...
foreach appName (`complete | sed -ne '/WM_PROJECT/s/\t.*$//p'`) foreach appName (`complete | sed -ne '/WM_PROJECT/s/\t.*$//p'`)
uncomplete $cleaned uncomplete $appName
end end
# Generate completions for predefined directories # Generate completions for predefined directories (if support is possible)
foreach dirName ("$FOAM_APPBIN") bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper -test
if ( ! -d $dirName || ! -f $WM_PROJECT_DIR/etc/config.csh/complete-wrapper ) continue if ($status == 0) then
foreach appName (`find $dirName -maxdepth 1 -executable -type f`) foreach dirName ("$FOAM_APPBIN")
# Pass explicitly if ( ! -d $dirName ) continue
## complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t' "${COMMAND_LINE}"`,' foreach appName (`find $dirName -maxdepth 1 -executable -type f`)
# Pass via environment # Pass explicitly
complete $appName:t 'p,*,`bash $WM_PROJECT_DIR/etc/config.csh/complete-wrapper '$appName:t'`,' ## 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
end endif
endif endif
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------

View File

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