8f82b39318103dd73b44315ddaa3548912320bbc
[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 echo " * Usage: $usage_txt"
204 echo ' * '
205 echo "$help_txt" | sed -e 's/^/ * /g'
206 echo ' */'
207 if test -n "$prototype"; then
208 echo "$prototype"
209 echo
210 return
211 fi
212 for source_file in $source_files; do
213 match=$(grep "^\(__noreturn \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
214 if test -n "$match"; then
215 echo "$match"
216 break
217 fi
218 done
219 echo
220 }
221
222 dump_array_member()
223 {
224 local TAB=' '
225 echo '{'
226 echo ".name = \"$name_txt\","
227 echo ".handler = com_$name_txt,"
228 if test -n "$perms_txt"; then
229 echo ".perms = $perms_txt,"
230 fi
231 echo ".description = \"$desc_txt\","
232 echo ".usage = \"$usage_txt\","
233 echo ".help = "
234 printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g' \
235 -e "s/$TAB/\\\t/g"
236 echo '},'
237 }
238
239
240 template_loop()
241 {
242 local t_name="$name_txt"
243 local t_perms="$perms_txt"
244 local t_desc="$desc_txt"
245 local t_usage="$usage_txt"
246 local t_help="$help_txt"
247 local t_source_files="$source_files"
248 local member
249 for member in $template_members; do
250 local sed_cmd="sed -e s/@member@/$member/g"
251 #echo "sed_cmd: $sed_cmd"
252 name_txt=$(echo $t_name | $sed_cmd)
253 #name_txt="$tname"
254 perms_txt=$(echo $t_perms | $sed_cmd)
255 desc_txt=$(echo $t_desc | $sed_cmd)
256 usage_txt=$(echo $t_usage | $sed_cmd)
257 help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
258 prototype=$(echo "$template_prototype" | $sed_cmd)
259 $1
260 done
261
262 }
263
264 com_c_file()
265 {
266 echo "/** \file $output_file.c $c_file_comment */"
267 echo "$system_includes"
268 echo "$includes"
269 echo "struct $array_type $array_name[] = {"
270 while : ; do
271 read_one_command
272 if test $ret -lt 0; then
273 exit 1
274 fi
275 if test $ret -eq 0; then
276 break
277 fi
278 if test $template -eq 0; then
279 dump_array_member
280 continue
281 fi
282 template_loop dump_array_member
283 done
284 echo '{.name = NULL}};'
285 }
286
287 com_header()
288 {
289 echo "/** \file $output_file.h $header_comment */"
290 echo
291 echo "extern struct $array_type $array_name[];"
292 while : ; do
293 read_one_command
294 if test $ret -lt 0; then
295 exit 1
296 fi
297 if test $ret -eq 0; then
298 break
299 fi
300 if test $template -eq 0; then
301 dump_proto
302 continue
303 fi
304 template_loop dump_proto
305 done
306 }
307
308 read_header
309 arg="$1"
310 shift
311 case "$arg" in
312 "c")
313 com_c_file
314 ;;
315 "h")
316 com_header
317 ;;
318 "man")
319 com_man $*
320 ;;
321 esac