Commit c3ee6cc5 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: restrict aliases, completions to interactive shells

- minor cleanup of bash completion script
parent efb1e692
......@@ -29,40 +29,56 @@
# Create bash completions for OpenFOAM applications
#
#------------------------------------------------------------------------------
#set -x
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: $Script [OPTION] <file>
* Create bash completions for OpenFOAM applications and write to <file>.
By default searches directories \$FOAM_APPBIN and \$FOAM_USER_APPBIN
Usage: ${0##*/} [OPTION] <outputFile>
options:
-d dir | -dir dir Directory to process
-u | -user Add \$FOAM_USER_APPBIN to the search directories
-h | -help Print the usage
Options:
-d | -directory Directory to process
-h | -help Print the usage
Create bash completions for OpenFOAM applications and write to <outputFile>.
By default searches \$FOAM_APPBIN only.
USAGE
exit 1
}
# Report error and exit
die()
{
exec 1>&2
echo
echo "Error encountered:"
while [ "$#" -ge 1 ]; do echo " $1"; shift; done
echo
echo "See '${0##*/} -help' for usage"
echo
exit 1
}
#-------------------------------------------------------------------------------
#set -x
unset outFile
searchDirs="$FOAM_APPBIN $FOAM_USER_APPBIN"
searchDirs="$FOAM_APPBIN"
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help)
usage
;;
-d | -directory)
-d | -dir)
searchDirs="$2"
[ -d $searchDirs ] || usage "directory not found '$searchDirs'"
shift
;;
-u | -user)
searchDirs="$searchDirs $FOAM_USER_APPBIN"
;;
-*)
usage "unknown option: '$1'"
;;
......@@ -74,14 +90,14 @@ do
shift
done
[ -n "$outFile" ] || usage
\rm -f $outFile
touch $outFile
[ -n "$outFile" ] || usage "No output file specified"
writeFilterFunction()
{
cat<< WRITEFILTER >> $1
# Generate header
cat << HEADER > $outFile
#----------------------------------*-sh-*--------------------------------------
# Bash completions for OpenFOAM applications
unset -f _of_filter_opts 2>/dev/null
_of_filter_opts()
{
......@@ -92,72 +108,74 @@ _of_filter_opts()
done
}
WRITEFILTER
}
HEADER
#------------------------------------------------------------------------------
#
# Produce contents for switch for common options
#
commonOptions()
{
local options=$@
local indent1=" "
local indent2=" "
for o in ${options[@]}; do
case $o in
-case)
echo "${indent1}-case)"
echo "${indent2}COMPREPLY=(\$(compgen -d -- \${cur}))"
echo "${indent2};;"
;;
-srcDoc|-help)
echo "${indent1}-srcDoc|-help)"
echo "${indent2}COMPREPLY=()"
echo "${indent2};;"
;;
-time)
echo "${indent1}-time)"
echo "${indent2}COMPREPLY=(\$(compgen -d -X '![-0-9]*' -- \${cur}))"
echo "${indent2};;"
;;
-region)
echo "${indent1}-region)"
echo "${indent2}local regions=\$(sed 's#/##g' <<< \$([ -d system ] && (\cd system && (\ls -d */ 2>/dev/null))))"
echo "${indent2}COMPREPLY=(\$(compgen -W \"\$regions\" -- \${cur}))"
echo "${indent2};;"
;;
*Dict)
echo "${indent1}*Dict)"
# echo "${indent2}local dirs=\$(\ls -d s*/)"
# echo "${indent2}local files=\$(\ls -f | grep Dict)"
# echo "${indent2}COMPREPLY=(\$(compgen -W \"\$dirs \$files\" -- \${cur}))"
echo "${indent2}COMPREPLY=(\$(compgen -f -- \${cur}))"
echo "${indent2};;"
;;
local indent1=" "
local indent2=" "
for opt
do
case $opt in
-case)
echo "${indent1}-case)"
echo "${indent2}COMPREPLY=(\$(compgen -d -- \${cur}))"
echo "${indent2};;"
;;
-srcDoc|-help)
echo "${indent1}-srcDoc|-help)"
echo "${indent2}COMPREPLY=()"
echo "${indent2};;"
;;
-time)
echo "${indent1}-time)"
echo "${indent2}COMPREPLY=(\$(compgen -d -X '![-0-9]*' -- \${cur}))"
echo "${indent2};;"
;;
-region)
echo "${indent1}-region)"
echo "${indent2}local regions=\$(sed 's#/##g' <<< \$([ -d system ] && (\cd system && (\ls -d */ 2>/dev/null))))"
echo "${indent2}COMPREPLY=(\$(compgen -W \"\$regions\" -- \${cur}))"
echo "${indent2};;"
;;
*Dict)
echo "${indent1}*Dict)"
# echo "${indent2}local dirs=\$(\ls -d s*/)"
# echo "${indent2}local files=\$(\ls -f | grep Dict)"
# echo "${indent2}COMPREPLY=(\$(compgen -W \"\$dirs \$files\" -- \${cur}))"
echo "${indent2}COMPREPLY=(\$(compgen -f -- \${cur}))"
echo "${indent2};;"
;;
esac
done
}
# Generate header
cat << HEADER > $outFile
#----------------------------------*-sh-*--------------------------------------
# Bash completions for OpenFOAM applications
HEADER
writeFilterFunction $outFile
#------------------------------------------------------------------------------
for dir in ${searchDirs}
do
echo "Processing directory $dir" 1>&2
if [ -d "$dir" ]
then
echo "Processing directory $dir" 1>&2
else
echo "No such directory: $dir" 1>&2
continue
fi
# Sort with ignore-case
apps=($(\ls $dir | sort -f))
for appName in "${apps[@]}"
set -- $(\ls $dir | sort -f)
for appName
do
[ -f "$dir/$appName" -a -x "$dir/$appName" ] || continue
appHelp=$($appName -help)
echo "Processing $appName" 1>&2
echo " $appName" 1>&2
# Options with args
optsWithArgs=($(awk '/^ {0,4}-[a-z]/ && /</ {print $1}' <<< "$appHelp"))
......@@ -165,7 +183,7 @@ do
# Options without args
opts=($(awk '/^ {0,4}-[a-z]/ && !/</ {print $1}' <<< "$appHelp"))
cat<<WRITECOMPLETION >> $outFile
cat << WRITECOMPLETION >> $outFile
unset -f _of_${appName} 2>/dev/null
_of_${appName}()
{
......@@ -177,19 +195,19 @@ _of_${appName}()
case \${prev} in
$(commonOptions ${optsWithArgs[@]})
*)
if [ "\${optsWithArgs/\${prev} /}" != "\${optsWithArgs}" ]; then
# Unknown what type of arg follows - set to files
# not always correct but at least can still navigate path if
# needed...
COMPREPLY=(\$(compgen -f -- \${cur}))
else
# Catch-all - present all remaining options
opts=\$(_of_filter_opts "\${opts}" "\${COMP_LINE}")
optsWithArgs=\$(_of_filter_opts "\${optsWithArgs}" "\${COMP_LINE}")
COMPREPLY=(\$(compgen -W "\${opts} \${optsWithArgs}" -- \${cur}))
fi
;;
*)
if [ "\${optsWithArgs/\${prev} /}" != "\${optsWithArgs}" ]
then
# Unknown type of arg follows - set to files.
# Not always correct but can still navigate path if needed...
COMPREPLY=(\$(compgen -f -- \${cur}))
else
# Catch-all - present all remaining options
opts=\$(_of_filter_opts "\${opts}" "\${COMP_LINE}")
optsWithArgs=\$(_of_filter_opts "\${optsWithArgs}" "\${COMP_LINE}")
COMPREPLY=(\$(compgen -W "\${opts} \${optsWithArgs}" -- \${cur}))
fi
;;
esac
return 0
......
......@@ -169,12 +169,16 @@ export PATH LD_LIBRARY_PATH MANPATH
# Source project setup files
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
_foamEtc config.sh/settings
_foamEtc config.sh/aliases
# Bash completions
if command -v complete > /dev/null 2>&1
if /usr/bin/tty -s 2>/dev/null # Interactive shell
then
_foamEtc config.sh/bashcompletion
_foamEtc config.sh/aliases
# Bash completions
if command -v complete > /dev/null 2>&1
then
_foamEtc config.sh/bashcompletion
fi
fi
......
This diff is collapsed.
......@@ -216,7 +216,11 @@ if ( $status == 0 ) setenv MANPATH $cleaned
# Source project setup files
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
_foamEtc config.csh/settings
_foamEtc config.csh/aliases
if ($?prompt) then # Interactive shell
_foamEtc config.csh/aliases
endif
# Source user setup files for optional packages
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment