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