server: Include git version in output of com_si().
[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 local TAB=' '
233 echo '{'
234 echo ".name = \"$name_txt\","
235 echo ".handler = com_$name_txt,"
236 if test -n "$perms_txt"; then
237 echo ".perms = $perms_txt,"
238 fi
239 echo ".description = \"$desc_txt\","
240 echo ".usage = \"$usage_txt\","
241 echo ".help = "
242 printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g' \
243 -e "s/$TAB/\\\t/g"
244 echo '},'
245 }
246
247
248 template_loop()
249 {
250 local t_name="$name_txt"
251 local t_perms="$perms_txt"
252 local t_desc="$desc_txt"
253 local t_usage="$usage_txt"
254 local t_help="$help_txt"
255 local t_source_files="$source_files"
256 local member
257 for member in $template_members; do
258 local sed_cmd="sed -e s/@member@/$member/g"
259 #echo "sed_cmd: $sed_cmd"
260 name_txt=$(echo $t_name | $sed_cmd)
261 #name_txt="$tname"
262 perms_txt=$(echo $t_perms | $sed_cmd)
263 desc_txt=$(echo $t_desc | $sed_cmd)
264 usage_txt=$(echo $t_usage | $sed_cmd)
265 help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
266 prototype=$(echo "$template_prototype" | $sed_cmd)
267 $1
268 done
269
270 }
271
272 com_c_file()
273 {
274 echo "/** \file $output_file.c $c_file_comment */"
275 echo "$system_includes"
276 echo "$includes"
277 echo "struct $array_type $array_name[] = {"
278 while : ; do
279 read_one_command
280 if test $ret -lt 0; then
281 exit 1
282 fi
283 if test $ret -eq 0; then
284 break
285 fi
286 if test $template -eq 0; then
287 dump_array_member
288 continue
289 fi
290 template_loop dump_array_member
291 done
292 echo '{.name = NULL}};'
293 }
294
295 com_header()
296 {
297 echo "/** \file $output_file.h $header_comment */"
298 echo
299 echo "extern struct $array_type $array_name[];"
300 while : ; do
301 read_one_command
302 if test $ret -lt 0; then
303 exit 1
304 fi
305 if test $ret -eq 0; then
306 break
307 fi
308 if test $template -eq 0; then
309 dump_proto
310 continue
311 fi
312 template_loop dump_proto
313 done
314 }
315
316 read_header
317 arg="$1"
318 shift
319 case "$arg" in
320 "c")
321 com_c_file
322 ;;
323 "h")
324 com_header
325 ;;
326 "man")
327 com_man $*
328 ;;
329 esac