230 lines
6.3 KiB
Bash
Executable File
230 lines
6.3 KiB
Bash
Executable File
#!/bin/sh
|
|
#------------------------------------------------------------------------------
|
|
# ========= |
|
|
# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
|
# \\ / O peration |
|
|
# \\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
|
|
# \\/ M anipulation |
|
|
#------------------------------------------------------------------------------
|
|
# License
|
|
# This file is part of OpenFOAM.
|
|
#
|
|
# OpenFOAM is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
# for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
# Script
|
|
# foamCreateBashCompletions
|
|
#
|
|
# Description
|
|
# Create bash completions for OpenFOAM applications
|
|
#
|
|
#------------------------------------------------------------------------------
|
|
usage() {
|
|
exec 1>&2
|
|
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
|
|
cat<<USAGE
|
|
|
|
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
|
|
|
|
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"
|
|
while [ "$#" -gt 0 ]
|
|
do
|
|
case "$1" in
|
|
-h | -help)
|
|
usage
|
|
;;
|
|
-d | -dir)
|
|
searchDirs="$2"
|
|
[ -d $searchDirs ] || usage "directory not found '$searchDirs'"
|
|
shift
|
|
;;
|
|
-u | -user)
|
|
searchDirs="$searchDirs $FOAM_USER_APPBIN"
|
|
;;
|
|
-*)
|
|
usage "unknown option: '$1'"
|
|
;;
|
|
*)
|
|
outFile=$1
|
|
break
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
[ -n "$outFile" ] || usage "No output file specified"
|
|
|
|
|
|
# Generate header
|
|
cat << HEADER > $outFile
|
|
#----------------------------------*-sh-*--------------------------------------
|
|
# Bash completions for OpenFOAM applications
|
|
# Formatted as "complete ... -F _of_APPNAME APPNAME
|
|
|
|
unset -f _of_filter_opts 2>/dev/null
|
|
_of_filter_opts()
|
|
{
|
|
local allOpts=\$1
|
|
local applied=\$2
|
|
for o in \${allOpts}; do
|
|
[ "\${applied/\$o/}" == "\${applied}" ] && echo \$o
|
|
done
|
|
}
|
|
|
|
HEADER
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
#
|
|
# Produce contents for switch for common options
|
|
#
|
|
commonOptions()
|
|
{
|
|
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
|
|
}
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
for dir in ${searchDirs}
|
|
do
|
|
if [ -d "$dir" ]
|
|
then
|
|
echo "Processing directory $dir" 1>&2
|
|
else
|
|
echo "No such directory: $dir" 1>&2
|
|
continue
|
|
fi
|
|
|
|
# Sort with ignore-case
|
|
set -- $(\ls $dir | sort -f)
|
|
for appName
|
|
do
|
|
[ -f "$dir/$appName" -a -x "$dir/$appName" ] || continue
|
|
appHelp=$($appName -help)
|
|
|
|
echo " $appName" 1>&2
|
|
|
|
# Options with args
|
|
optsWithArgs=($(awk '/^ {0,4}-[a-z]/ && /</ {print $1}' <<< "$appHelp"))
|
|
|
|
# Options without args
|
|
opts=($(awk '/^ {0,4}-[a-z]/ && !/</ {print $1}' <<< "$appHelp"))
|
|
|
|
cat << WRITECOMPLETION >> $outFile
|
|
unset -f _of_${appName} 2>/dev/null
|
|
_of_${appName}()
|
|
{
|
|
local cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
local prev="\${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
local opts="${opts[@]} "
|
|
local optsWithArgs="${optsWithArgs[@]} "
|
|
|
|
case \${prev} in
|
|
$(commonOptions ${optsWithArgs[@]})
|
|
*)
|
|
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
|
|
}
|
|
complete -o nospace -F _of_${appName} $appName
|
|
|
|
WRITECOMPLETION
|
|
done
|
|
done
|
|
|
|
|
|
# Generate footer
|
|
cat << FOOTER >> $outFile
|
|
|
|
#------------------------------------------------------------------------------
|
|
FOOTER
|
|
|
|
#------------------------------------------------------------------------------
|