audiod: kill handle_signal()
[paraslash.git] / command_util.sh
1 #!/bin/bash
2
3
4 dump_array_member()
5 {
6         echo '{'
7         echo ".name = \"$name_txt\","
8         if test $line_handler -eq 0; then
9                 echo ".handler = com_$name_txt,"
10         else
11                 echo ".handler = NULL,"
12                 echo ".line_handler = com_$name_txt,"
13         fi
14         if test -n "$perms_txt"; then
15                 echo ".perms = $perms_txt,"
16         fi
17         echo ".description = \"$desc_txt\","
18         echo ".usage = \"$usage_txt\","
19         echo ".help = "
20         echo "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
21         echo '},'
22 }
23
24 read_header()
25 {
26         local key value i
27
28         while read key value; do
29                 case "$key" in
30                 ---)
31                         break
32                         ;;
33                 HC:)
34                         header_comment="$value"
35                         ;;
36                 CC:)
37                         c_file_comment="$value"
38                         ;;
39                 FN:)
40                         file_name="$value"
41                         array_name=${value%command_list}cmds
42                         ;;
43                 AT:)
44                         array_type="$value"
45                         ;;
46                 IN:)
47                         for i in $value; do
48                                 includes="$includes
49 #include \"$i.h\""
50                         done
51                         includes="$includes
52 #include \"$file_name.h\""
53                         ;;
54                 SN:)
55                         section_name="$value"
56                 esac
57         done
58         if test -z "$header_comment" -o -z "$c_file_comment" \
59                         -o -z "$file_name"; then
60                 echo "header error" 1&>2
61                 exit 1
62         fi
63 }
64
65 read_one_command()
66 {
67         local line
68
69         name_txt=""
70         desc_txt=""
71         usage_txt=""
72         help_txt=""
73         perms_txt=""
74         line_handler=0
75         mkdir -p man/man1
76         while read key value; do
77                 case "$key" in
78                 ---)
79                         break
80                         ;;
81                 N:)
82                         name_txt="$value"
83                         ;;
84                 P:)
85                         perms_txt="$value"
86                         ;;
87                 D:)
88                         desc_txt="$value"
89                         ;;
90                 L:)
91                         line_handler=1
92                         ;;
93                 U:)
94                         usage_txt="$value"
95                         ;;
96                 H:)
97                         help_txt="${value}"
98                         while read line; do
99                                 if test "$line" = "---"; then
100                                         break;
101                                 fi
102                                 line=${line#H:}
103                                 help_txt="$help_txt
104 ${line# }"
105                         done
106                         break
107                         ;;
108                 esac
109         done
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         if test -z "$name_txt" -a -z "$desc_txt" -a -z "$usage_txt" \
116                         -a -z "$help_txt"; then
117                 ret=0
118                 return
119         fi
120         ret=-1
121         return
122         echo "!ERROR!"
123         echo "N: $name_txt"
124         echo "D: $desc_txt"
125         echo "S: $usage_txt"
126         echo "P: $perms_txt"
127         echo "H: $help_txt"
128 }
129
130 dump_man()
131 {
132         echo ".SS \"$name_txt\""
133         echo "$desc_txt"
134         echo
135         echo "\\fBusage: \\fP$usage_txt"
136         echo
137         echo "$help_txt"
138         echo
139         if test -n "$perms_txt"; then
140                 echo -n "\\fBpermissions:\\fP "
141                 if test "$perms_txt" = "0"; then
142                         echo "(none)"
143                 else
144                         echo "$perms_txt"
145                 fi
146         fi
147         echo
148 }
149
150
151 com_man()
152 {
153         echo "[$section_name]"
154         echo
155         while : ; do
156                 read_one_command
157                 if test $ret -lt 0; then
158                         exit 1
159                 fi
160                 if test $ret -eq 0; then
161                         break
162                 fi
163                 dump_man
164         done
165 }
166
167 com_c_file()
168 {
169         echo "/** \file $file_name.c $c_file_comment */"
170         echo "$includes"
171         echo "struct $array_type $array_name[] = {"
172         while : ; do
173                 read_one_command
174                 if test $ret -lt 0; then
175                         exit 1
176                 fi
177                 if test $ret -eq 0; then
178                         break
179                 fi
180                 dump_array_member
181         done
182         echo '{.name = NULL}};'
183 }
184
185 dump_proto()
186 {
187         echo '/**'
188         echo " * $desc_txt"
189         echo ' *'
190         echo ' * \param fd the file descriptor to send output to'
191         if test $line_handler -eq 0; then
192                 echo ' * \param argc the number of arguments'
193                 echo ' * \param argv the argument vector'
194         else
195                 echo ' * \param cmdline the full command line'
196         fi
197         echo ' * '
198         echo " * usage: $usage_txt"
199         echo ' * '
200         echo "$help_txt" | sed -e 's/^/ * /g'
201         echo ' */'
202         if test $line_handler -eq 0; then
203                 echo "int com_$name_txt(int fd, int argc, char **argv);"
204         else
205                 echo "int com_$name_txt(int fd, char *cmdline);"
206         fi
207         echo
208 }
209
210 com_header()
211 {
212         echo "/** \file $file_name.h $header_comment */"
213         echo
214         echo "extern struct $array_type $array_name[];"
215         while : ; do
216                 read_one_command
217                 if test $ret -lt 0; then
218                         exit 1
219                 fi
220                 if test $ret -eq 0; then
221                         break
222                 fi
223                 dump_proto
224         done
225 }
226
227 read_header
228 arg="$1"
229 shift
230 case "$arg" in
231         "c")
232                 com_c_file
233                 ;;
234         "h")
235                 com_header
236                 ;;
237         "man")
238                 com_man $*
239                 ;;
240 esac