Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
#------------------------------------------------------------------------------
# ========= |
# \\ / 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
# foamCreateCompletionCache
#
# Description
# Create cache of bash completion values for OpenFOAM applications
# The cached values are typically used by the tcsh completion wrapper.
#
#------------------------------------------------------------------------------
defaultOutputFile="$WM_PROJECT_DIR/etc/config.sh/completion_cache"
usage() {
exec 1>&2
while [ "$#" -ge 1 ]; do echo "$1"; shift; done
cat<<USAGE
Usage: ${0##*/} [OPTION] [appName .. [appNameN]]
options:
-d dir | -dir dir Directory to process
-u | -user Add \$FOAM_USER_APPBIN to the search directories
-no-header Suppress header generation
-o FILE Write to alternative output
-h | -help Print the usage
Create cache of bash completion values for OpenFOAM applications.
The cached values are typically used by the tcsh completion wrapper.
Default search: \$FOAM_APPBIN only.
Default output: $defaultOutputFile
Uses the search directory if applications are specified.
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
}
#-------------------------------------------------------------------------------
searchDirs="$FOAM_APPBIN"
optHeader=true
unset outputFile
while [ "$#" -gt 0 ]
do
case "$1" in
-h | -help)
usage
;;
-d | -dir)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
searchDirs="$2"
[ -d "$searchDirs" ] || die "directory not found '$searchDirs'"
shift
;;
-u | -user)
searchDirs="$searchDirs $FOAM_USER_APPBIN"
;;
-no-head*)
optHeader=false
;;
-o | -output)
[ "$#" -ge 2 ] || die "'$1' option requires an argument"
outputFile="$2"
shift
;;
-*)
usage "unknown option: '$1'"
;;
*)
break
;;
esac
shift
done
: ${outputFile:=$defaultOutputFile}
# Verify that output is writeable
if [ -e "$outputFile" ]
then
[ -f "$outputFile" ] || \
die "Cannot overwrite $outputFile" "Not a file"
[ -w "$outputFile" ] || \
die "Cannot overwrite $outputFile" "No permission?"
else
[ -w "$(dirname $outputFile)" ] || \
die "Cannot write $outputFile" "directory is not writeble"
fi
exec 1>| $outputFile || exit $?
echo "Writing $outputFile" 1>&2
echo 1>&2
# Header not disabled
[ "$optHeader" = true ] && cat << HEADER
#----------------------------------*-sh-*--------------------------------------
# Cached options for bash completion of OpenFOAM applications.
# These are the values expected by the '_of_complete_' function
#
# Recreate with "${0##*/}"
# Global associative array (cached options for OpenFOAM applications)
declare -gA _of_complete_cache_;
# Clear existing cache.
_of_complete_cache_=()
#------------------------------------------------------------------------------
HEADER
#-------------------------------------------------------------------------------
# Scans the output of the application -help to detect options with/without
# arguments. Dispatch via _of_complete_
#
extractOptions()
{
local appName="$1"
local helpText=$($appName -help 2>/dev/null | sed -ne '/^ *-/p')
[ -n "$helpText" ] || {
echo "Error calling $appName" 1>&2
return 1
}
# Array of options with args
local argOpts=($(awk '/^ {0,4}-[a-z]/ && /</ {print $1}' <<< "$helpText"))
# Array of options without args
local boolOpts=($(awk '/^ {0,4}-[a-z]/ && !/</ {print $1}' <<< "$helpText"))
appName="${appName##*/}"
echo "$appName" 1>&2
echo "_of_complete_cache_[${appName}]=\"${argOpts[@]} | ${boolOpts[@]}\""
}
#------------------------------------------------------------------------------
[ "$#" -gt 0 ] || set -- ${searchDirs}
for item
do
if [ -d "$item" ]
then
# Process directory for applications - sort with ignore-case
echo "[directory] $item" 1>&2
choices="$(find $item -maxdepth 1 -executable -type f | sort -f 2>/dev/null)"
for appName in $choices
do
extractOptions $appName
done
elif command -v "$item" > /dev/null 2>&1
then
extractOptions $item
else
echo "No such file or directory: $item" 1>&2
fi
done
# Generate footer
[ "$optHeader" = true ] && cat << FOOTER
#------------------------------------------------------------------------------
FOOTER
#------------------------------------------------------------------------------