rename vector_fmul_add() vector_fmul_reverse_c() and make them static.
[paraslash.git] / command_util.sh
1 #!/usr/bin/env bash
2
3
4 read_header()
5 {
6 local key value i
7
8 while read key value; do
9 case "$key" in
10 ---)
11 break
12 ;;
13 HC:)
14 header_comment="$value"
15 ;;
16 CC:)
17 c_file_comment="$value"
18 ;;
19 OF:)
20 output_file="$value"
21 array_name=${value%command_list}cmds
22 ;;
23 SF:)
24 source_files="$value"
25 ;;
26 AT:)
27 array_type="$value"
28 ;;
29 SI:)
30 for i in $value; do
31 system_includes="$system_includes
32 #include <$i.h>"
33 done
34 ;;
35 IN:)
36 for i in $value; do
37 includes="$includes
38 #include \"$i.h\""
39 done
40 includes="$includes
41 #include \"$output_file.h\""
42 ;;
43 SN:)
44 section_name="$value"
45 ;;
46 TM:)
47 template_members="$value"
48 esac
49 done
50 if test -z "$header_comment" -o -z "$c_file_comment" \
51 -o -z "$output_file"; then
52 echo "header error" 1&>2
53 exit 1
54 fi
55 }
56
57 read_one_command()
58 {
59 local line
60
61 name_txt=""
62 desc_txt=""
63 usage_txt=""
64 help_txt=""
65 perms_txt=""
66 template=0
67 template_name=""
68 template_prototype=""
69 mkdir -p man/man1
70 while read key value; do
71 case "$key" in
72 ---)
73 break
74 ;;
75 N:)
76 name_txt="$value"
77 ;;
78 T:)
79 template_name="$value"
80 template=1
81 ;;
82 O:)
83 template_prototype="$value"
84 template=1
85 ;;
86 P:)
87 perms_txt="$value"
88 ;;
89 D:)
90 desc_txt="$value"
91 ;;
92 U:)
93 usage_txt="$value"
94 ;;
95 H:)
96 help_txt="${value}"
97 while read line; do
98 if test "$line" = "---"; then
99 break;
100 fi
101 line=${line#H:}
102 help_txt="$help_txt
103 ${line# }"
104 done
105 break
106 ;;
107 esac
108 done
109 if test $template -eq 0; then
110 if test -n "$name_txt" -a -n "$desc_txt" -a -n "$usage_txt" \
111 -a -n "$help_txt"; then
112 ret=1
113 return
114 fi
115 else
116 if test -n "$template_name" -a -n "$template_prototype" \
117 -a -n "$name_txt " -a -n "$template_members" \
118 -a -n "$desc_txt" -a -n "$usage_txt" \
119 -a -n "$help_txt"; then
120 ret=1
121 return
122 fi
123 fi
124 if test -z "$name_txt" -a -z "$desc_txt" -a -z "$usage_txt" \
125 -a -z "$help_txt"; then
126 ret=0
127 return
128 fi
129 ret=-1
130 #return
131 echo "!ERROR!"
132 echo "N: $name_txt"
133 echo "D: $desc_txt"
134 echo "S: $usage_txt"
135 echo "P: $perms_txt"
136 echo "H: $help_txt"
137 echo "O: $template_prototype"
138 }
139
140 dump_man()
141 {
142 if test $template -eq 0; then
143 echo ".SS \"$name_txt\""
144 echo "$desc_txt"
145 echo
146 echo "\\fBUsage: \\fP$usage_txt"
147 else
148 for member in $template_members; do
149 local sed_cmd="sed -e s/@member@/$member/g"
150 local t_name_txt=$(echo $name_txt | $sed_cmd)
151 echo ".SS \"$t_name_txt\""
152 done
153 echo "$desc_txt" | sed -e "s/@member@/{$(echo $template_members | sed -e 's/ / | /g')}/g"
154 echo
155 echo "\\fBUsage: \\fP"
156 echo
157 echo ".nf"
158 for member in $template_members; do
159 local sed_cmd="sed -e s/@member@/$member/g"
160 local t_usage_txt=$(echo $usage_txt | $sed_cmd)
161 printf "\t$t_usage_txt\n"
162 done
163 echo ".fi"
164 fi
165 echo
166 echo "$help_txt" | sed -e 's/^ //'
167 echo
168 if test -n "$perms_txt"; then
169 echo -n "\\fBpermissions:\\fP "
170 if test "$perms_txt" = "0"; then
171 echo "(none)"
172 else
173 echo "$perms_txt"
174 fi
175 fi
176 echo
177 }
178
179
180 com_man()
181 {
182 echo "[$section_name]"
183 echo
184 while : ; do
185 read_one_command
186 if test $ret -lt 0; then
187 exit 1
188 fi
189 if test $ret -eq 0; then
190 break
191 fi
192 dump_man
193 done
194 }
195
196 dump_proto()
197 {
198 local source_file match=""
199
200 echo '/**'
201 echo " * $desc_txt"
202 echo ' *'
203 if [[ "$system_includes" =~ openssl/rc4.h ]]; then
204 echo ' * \param rc4c The rc4 crypt context.'
205 else
206 echo ' * \param fd The file descriptor to send output to.'
207 fi
208 echo ' * \param argc The number of arguments.'
209 echo ' * \param argv The argument vector.'
210 echo ' * '
211 echo " * Usage: $usage_txt"
212 echo ' * '
213 echo "$help_txt" | sed -e 's/^/ * /g'
214 echo ' */'
215 if test -n "$prototype"; then
216 echo "$prototype"
217 echo
218 return
219 fi
220 for source_file in $source_files; do
221 match=$(grep "^\(__noreturn \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
222 if test -n "$match"; then
223 echo "$match"
224 break
225 fi
226 done
227 echo
228 }
229
230 dump_array_member()
231 {
232 echo '{'
233 echo ".name = \"$name_txt\","
234 echo ".handler = com_$name_txt,"
235 if test -n "$perms_txt"; then
236 echo ".perms = $perms_txt,"
237 fi
238 echo ".description = \"$desc_txt\","
239 echo ".usage = \"$usage_txt\","
240 echo ".help = "
241 printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
242 echo '},'
243 }
244
245
246 template_loop()
247 {
248 local t_name="$name_txt"
249 local t_perms="$perms_txt"
250 local t_desc="$desc_txt"
251 local t_usage="$usage_txt"
252 local t_help="$help_txt"
253 local t_source_files="$source_files"
254 local member
255 for member in $template_members; do
256 local sed_cmd="sed -e s/@member@/$member/g"
257 #echo "sed_cmd: $sed_cmd"
258 name_txt=$(echo $t_name | $sed_cmd)
259 #name_txt="$tname"
260 perms_txt=$(echo $t_perms | $sed_cmd)
261 desc_txt=$(echo $t_desc | $sed_cmd)
262 usage_txt=$(echo $t_usage | $sed_cmd)
263 help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
264 prototype=$(echo "$template_prototype" | $sed_cmd)
265 $1
266 done
267
268 }
269
270 com_c_file()
271 {
272 echo "/** \file $output_file.c $c_file_comment */"
273 echo "$system_includes"
274 echo "$includes"
275 echo "struct $array_type $array_name[] = {"
276 while : ; do
277 read_one_command
278 if test $ret -lt 0; then
279 exit 1
280 fi
281 if test $ret -eq 0; then
282 break
283 fi
284 if test $template -eq 0; then
285 dump_array_member
286 continue
287 fi
288 template_loop dump_array_member
289 done
290 echo '{.name = NULL}};'
291 }
292
293 com_header()
294 {
295 echo "/** \file $output_file.h $header_comment */"
296 echo
297 echo "extern struct $array_type $array_name[];"
298 while : ; do
299 read_one_command
300 if test $ret -lt 0; then
301 exit 1
302 fi
303 if test $ret -eq 0; then
304 break
305 fi
306 if test $template -eq 0; then
307 dump_proto
308 continue
309 fi
310 template_loop dump_proto
311 done
312 }
313
314 read_header
315 arg="$1"
316 shift
317 case "$arg" in
318 "c")
319 com_c_file
320 ;;
321 "h")
322 com_header
323 ;;
324 "man")
325 com_man $*
326 ;;
327 esac