a7f1b542a6532d0042af51e462ab264ee4f10e95
[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 line_handler=0
67 template=0
68 template_name=""
69 template_prototype=""
70 mkdir -p man/man1
71 while read key value; do
72 case "$key" in
73 ---)
74 break
75 ;;
76 N:)
77 name_txt="$value"
78 ;;
79 T:)
80 template_name="$value"
81 template=1
82 ;;
83 O:)
84 template_prototype="$value"
85 template=1
86 ;;
87 P:)
88 perms_txt="$value"
89 ;;
90 D:)
91 desc_txt="$value"
92 ;;
93 L:)
94 line_handler=1
95 ;;
96 U:)
97 usage_txt="$value"
98 ;;
99 H:)
100 help_txt="${value}"
101 while read line; do
102 if test "$line" = "---"; then
103 break;
104 fi
105 line=${line#H:}
106 help_txt="$help_txt
107 ${line# }"
108 done
109 break
110 ;;
111 esac
112 done
113 if test $template -eq 0; then
114 if test -n "$name_txt" -a -n "$desc_txt" -a -n "$usage_txt" \
115 -a -n "$help_txt"; then
116 ret=1
117 return
118 fi
119 else
120 if test -n "$template_name" -a -n "$template_prototype" \
121 -a -n "$name_txt " -a -n "$template_members" \
122 -a -n "$desc_txt" -a -n "$usage_txt" \
123 -a -n "$help_txt"; then
124 ret=1
125 return
126 fi
127 fi
128 if test -z "$name_txt" -a -z "$desc_txt" -a -z "$usage_txt" \
129 -a -z "$help_txt"; then
130 ret=0
131 return
132 fi
133 ret=-1
134 #return
135 echo "!ERROR!"
136 echo "N: $name_txt"
137 echo "D: $desc_txt"
138 echo "S: $usage_txt"
139 echo "P: $perms_txt"
140 echo "H: $help_txt"
141 echo "O: $template_prototype"
142 }
143
144 dump_man()
145 {
146 if test $template -eq 0; then
147 echo ".SS \"$name_txt\""
148 echo "$desc_txt"
149 echo
150 echo "\\fBUsage: \\fP$usage_txt"
151 else
152 for member in $template_members; do
153 local sed_cmd="sed -e s/@member@/$member/g"
154 local t_name_txt=$(echo $name_txt | $sed_cmd)
155 echo ".SS \"$t_name_txt\""
156 done
157 echo "$desc_txt" | sed -e "s/@member@/{$(echo $template_members | sed -e 's/ / | /g')}/g"
158 echo
159 echo "\\fBUsage: \\fP"
160 echo
161 echo ".nf"
162 for member in $template_members; do
163 local sed_cmd="sed -e s/@member@/$member/g"
164 local t_usage_txt=$(echo $usage_txt | $sed_cmd)
165 printf "\t$t_usage_txt\n"
166 done
167 echo ".fi"
168 fi
169 echo
170 echo "$help_txt" | sed -e 's/^ //'
171 echo
172 if test -n "$perms_txt"; then
173 echo -n "\\fBpermissions:\\fP "
174 if test "$perms_txt" = "0"; then
175 echo "(none)"
176 else
177 echo "$perms_txt"
178 fi
179 fi
180 echo
181 }
182
183
184 com_man()
185 {
186 echo "[$section_name]"
187 echo
188 while : ; do
189 read_one_command
190 if test $ret -lt 0; then
191 exit 1
192 fi
193 if test $ret -eq 0; then
194 break
195 fi
196 dump_man
197 done
198 }
199
200 dump_proto()
201 {
202 local source_file match=""
203
204 echo '/**'
205 echo " * $desc_txt"
206 echo ' *'
207 echo ' * \param fd The file descriptor to send output to.'
208 if test $line_handler -eq 0; then
209 echo ' * \param argc The number of arguments.'
210 echo ' * \param argv The argument vector.'
211 else
212 echo ' * \param cmdline The full command line.'
213 fi
214 echo ' * '
215 echo " * Usage: $usage_txt"
216 echo ' * '
217 echo "$help_txt" | sed -e 's/^/ * /g'
218 echo ' */'
219 if test -n "$prototype"; then
220 echo "$prototype"
221 echo
222 return
223 fi
224 for source_file in $source_files; do
225 match=$(grep "^\(__noreturn \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
226 if test -n "$match"; then
227 echo "$match"
228 break
229 fi
230 done
231 echo
232 }
233
234 dump_array_member()
235 {
236 echo '{'
237 echo ".name = \"$name_txt\","
238 if test $line_handler -eq 0; then
239 echo ".handler = com_$name_txt,"
240 else
241 echo ".handler = NULL,"
242 echo ".line_handler = com_$name_txt,"
243 fi
244 if test -n "$perms_txt"; then
245 echo ".perms = $perms_txt,"
246 fi
247 echo ".description = \"$desc_txt\","
248 echo ".usage = \"$usage_txt\","
249 echo ".help = "
250 printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
251 echo '},'
252 }
253
254
255 template_loop()
256 {
257 local t_name="$name_txt"
258 local t_perms="$perms_txt"
259 local t_desc="$desc_txt"
260 local t_usage="$usage_txt"
261 local t_help="$help_txt"
262 local t_source_files="$source_files"
263 local member
264 for member in $template_members; do
265 local sed_cmd="sed -e s/@member@/$member/g"
266 #echo "sed_cmd: $sed_cmd"
267 name_txt=$(echo $t_name | $sed_cmd)
268 #name_txt="$tname"
269 perms_txt=$(echo $t_perms | $sed_cmd)
270 desc_txt=$(echo $t_desc | $sed_cmd)
271 usage_txt=$(echo $t_usage | $sed_cmd)
272 help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
273 prototype=$(echo "$template_prototype" | $sed_cmd)
274 $1
275 done
276
277 }
278
279 com_c_file()
280 {
281 echo "/** \file $output_file.c $c_file_comment */"
282 echo "$system_includes"
283 echo "$includes"
284 echo "struct $array_type $array_name[] = {"
285 while : ; do
286 read_one_command
287 if test $ret -lt 0; then
288 exit 1
289 fi
290 if test $ret -eq 0; then
291 break
292 fi
293 if test $template -eq 0; then
294 dump_array_member
295 continue
296 fi
297 template_loop dump_array_member
298 done
299 echo '{.name = NULL}};'
300 }
301
302 com_header()
303 {
304 echo "/** \file $output_file.h $header_comment */"
305 echo
306 echo "extern struct $array_type $array_name[];"
307 while : ; do
308 read_one_command
309 if test $ret -lt 0; then
310 exit 1
311 fi
312 if test $ret -eq 0; then
313 break
314 fi
315 if test $template -eq 0; then
316 dump_proto
317 continue
318 fi
319 template_loop dump_proto
320 done
321 }
322
323 read_header
324 arg="$1"
325 shift
326 case "$arg" in
327 "c")
328 com_c_file
329 ;;
330 "h")
331 com_header
332 ;;
333 "man")
334 com_man $*
335 ;;
336 esac