command_util.sh: Make it read the function prototype from the source file
[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 OF:)
40 output_file="$value"
41 array_name=${value%command_list}cmds
42 ;;
43 SF:)
44 source_file="$value"
45 ;;
46 AT:)
47 array_type="$value"
48 ;;
49 IN:)
50 for i in $value; do
51 includes="$includes
52 #include \"$i.h\""
53 done
54 includes="$includes
55 #include \"$output_file.h\""
56 ;;
57 SN:)
58 section_name="$value"
59 esac
60 done
61 if test -z "$header_comment" -o -z "$c_file_comment" \
62 -o -z "$output_file"; then
63 echo "header error" 1&>2
64 exit 1
65 fi
66 }
67
68 read_one_command()
69 {
70 local line
71
72 name_txt=""
73 desc_txt=""
74 usage_txt=""
75 help_txt=""
76 perms_txt=""
77 line_handler=0
78 mkdir -p man/man1
79 while read key value; do
80 case "$key" in
81 ---)
82 break
83 ;;
84 N:)
85 name_txt="$value"
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 -n "$name_txt" -a -n "$desc_txt" -a -n "$usage_txt" \
114 -a -n "$help_txt"; then
115 ret=1
116 return
117 fi
118 if test -z "$name_txt" -a -z "$desc_txt" -a -z "$usage_txt" \
119 -a -z "$help_txt"; then
120 ret=0
121 return
122 fi
123 ret=-1
124 return
125 echo "!ERROR!"
126 echo "N: $name_txt"
127 echo "D: $desc_txt"
128 echo "S: $usage_txt"
129 echo "P: $perms_txt"
130 echo "H: $help_txt"
131 }
132
133 dump_man()
134 {
135 echo ".SS \"$name_txt\""
136 echo "$desc_txt"
137 echo
138 echo "\\fBusage: \\fP$usage_txt"
139 echo
140 echo "$help_txt"
141 echo
142 if test -n "$perms_txt"; then
143 echo -n "\\fBpermissions:\\fP "
144 if test "$perms_txt" = "0"; then
145 echo "(none)"
146 else
147 echo "$perms_txt"
148 fi
149 fi
150 echo
151 }
152
153
154 com_man()
155 {
156 echo "[$section_name]"
157 echo
158 while : ; do
159 read_one_command
160 if test $ret -lt 0; then
161 exit 1
162 fi
163 if test $ret -eq 0; then
164 break
165 fi
166 dump_man
167 done
168 }
169
170 com_c_file()
171 {
172 echo "/** \file $output_file.c $c_file_comment */"
173 echo "$includes"
174 echo "struct $array_type $array_name[] = {"
175 while : ; do
176 read_one_command
177 if test $ret -lt 0; then
178 exit 1
179 fi
180 if test $ret -eq 0; then
181 break
182 fi
183 dump_array_member
184 done
185 echo '{.name = NULL}};'
186 }
187
188 dump_proto()
189 {
190 echo '/**'
191 echo " * $desc_txt"
192 echo ' *'
193 echo ' * \param fd the file descriptor to send output to'
194 if test $line_handler -eq 0; then
195 echo ' * \param argc the number of arguments'
196 echo ' * \param argv the argument vector'
197 else
198 echo ' * \param cmdline the full command line'
199 fi
200 echo ' * '
201 echo " * usage: $usage_txt"
202 echo ' * '
203 echo "$help_txt" | sed -e 's/^/ * /g'
204 echo ' */'
205 grep "^\(__noreturn \)*int com_$name_txt(" $source_file | sed -e 's/$/;/1'
206 echo
207 }
208
209 com_header()
210 {
211 echo "/** \file $output_file.h $header_comment */"
212 echo
213 echo "extern struct $array_type $array_name[];"
214 while : ; do
215 read_one_command
216 if test $ret -lt 0; then
217 exit 1
218 fi
219 if test $ret -eq 0; then
220 break
221 fi
222 dump_proto
223 done
224 }
225
226 read_header
227 arg="$1"
228 shift
229 case "$arg" in
230 "c")
231 com_c_file
232 ;;
233 "h")
234 com_header
235 ;;
236 "man")
237 com_man $*
238 ;;
239 esac