Skip to content
Snippets Groups Projects
Commit feab8e94 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: restrict aliases, completions to interactive shells

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