d6626c253057675cceedb689d82957d607911a20
[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 ".synopsis = \"$syn_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 syn_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 S:)
94 syn_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 "$syn_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 "$syn_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: $syn_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$syn_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 local n
154 echo "[$section_name]"
155 echo
156 while : ; do
157 read_one_command
158 if test $ret -lt 0; then
159 exit 1
160 fi
161 if test $ret -eq 0; then
162 break
163 fi
164 dump_man #> $txtdir/$n.txt
165 # txt2man -t "$n" -r "$r" < $txtdir/$n.txt \
166 # | sed -e 1d > $mandir/$n.1
167 # man2html $mandir/$n.1 > $htmldir/$n.html
168 done
169 }
170
171 com_c_file()
172 {
173 echo "/** \file $file_name.c $c_file_comment */"
174 echo "$includes"
175 echo "struct $array_type $array_name[] = {"
176 while : ; do
177 read_one_command
178 if test $ret -lt 0; then
179 exit 1
180 fi
181 if test $ret -eq 0; then
182 break
183 fi
184 dump_array_member
185 done
186 echo '{.name = NULL}};'
187 }
188
189 dump_proto()
190 {
191 echo '/**'
192 echo " * $desc_txt"
193 echo ' *'
194 echo ' * \param fd the file descriptor to send output to'
195 if test $line_handler -eq 0; then
196 echo ' * \param argc the number of arguments'
197 echo ' * \param argv the argument vector'
198 else
199 echo ' * \param cmdline the full command line'
200 fi
201 echo ' * '
202 echo " * synopsis: $syn_txt"
203 echo ' * '
204 echo "$help_txt" | sed -e 's/^/ * /g'
205 echo ' */'
206 if test $line_handler -eq 0; then
207 echo "int com_$name_txt(int fd, int argc, char **argv);"
208 else
209 echo "int com_$name_txt(int fd, char *cmdline);"
210 fi
211 echo
212 }
213
214 com_header()
215 {
216 echo "/** \file $file_name.h $header_comment */"
217 echo
218 echo "extern struct $array_type $array_name[];"
219 while : ; do
220 read_one_command
221 if test $ret -lt 0; then
222 exit 1
223 fi
224 if test $ret -eq 0; then
225 break
226 fi
227 dump_proto
228 done
229 }
230
231 read_header
232 arg="$1"
233 shift
234 case "$arg" in
235 "c")
236 com_c_file
237 ;;
238 "h")
239 com_header
240 ;;
241 "man")
242 com_man $*
243 ;;
244 esac