command_util.sh: rename proto to header and array to c_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 ".line_handler = com_$name_txt,"
12         fi
13         if test -n "$perms_txt"; then
14                 echo ".perms = $perms_txt,"
15         fi
16         echo ".description = \"$desc_txt\","
17         echo ".synopsis = \"$syn_txt\","
18         echo ".help = "
19         echo "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
20         echo '},'
21 }
22
23 read_header()
24 {
25         local key value i
26
27         while read key value; do
28                 case "$key" in
29                 ---)
30                         break
31                         ;;
32                 HC:)
33                         header_comment="$value"
34                         ;;
35                 CC:)
36                         c_file_comment="$value"
37                         ;;
38                 FN:)
39                         file_name="$value"
40                         ;;
41                 AT:)
42                         array_type="$value"
43                         ;;
44                 AN:)
45                         array_name="$value"
46                         ;;
47                 IN:)
48                         for i in $value; do
49                                 includes="$includes
50 #include \"$i.h\""
51                         done
52                         includes="$includes
53 #include \"$file_name.h\""
54                         ;;
55                 esac
56         done
57         if test -z "$header_comment" -o -z "$c_file_comment" \
58                         -o -z "$file_name"; then
59                 echo "header error" 1&>2
60                 exit 1
61         fi
62 }
63
64 read_one_command()
65 {
66         local line
67
68         name_txt=""
69         desc_txt=""
70         syn_txt=""
71         help_txt=""
72         perms_txt=""
73         line_handler=0
74         mkdir -p man/man1
75         while read key value; do
76                 case "$key" in
77                 ---)
78                         break
79                         ;;
80                 N:)
81                         name_txt="$value"
82                         ;;
83                 P:)
84                         perms_txt="$value"
85                         ;;
86                 D:)
87                         desc_txt="$value"
88                         ;;
89                 L:)
90                         line_handler=1
91                         ;;
92                 S:)
93                         syn_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 -n "$name_txt" -a -n "$desc_txt" -a -n "$syn_txt" \
110                         -a -n "$help_txt"; then
111                 ret=1
112                 return
113         fi
114         if test -z "$name_txt" -a -z "$desc_txt" -a -z "$syn_txt" \
115                         -a -z "$help_txt"; then
116                 ret=0
117                 return
118         fi
119         ret=-1
120         return
121         echo "!ERROR!"
122         echo "N: $name_txt"
123         echo "D: $desc_txt"
124         echo "S: $syn_txt"
125         echo "P: $perms_txt"
126         echo "H: $help_txt"
127 }
128
129 dump_man()
130 {
131         echo "NAME"
132         printf "\t$name_txt - $desc_txt\n"
133         echo "SYNOPSIS"
134         printf "\t$syn_txt\n"
135         echo "DESCRIPTION"
136         echo "$help_txt"
137         if test -n "$perms_txt"; then
138                 echo "PERMISSIONS"
139                 if test "$perms_txt" = "0"; then
140                         printf "\t(none)\n"
141                 else
142                         printf "\t$perms_txt\n"
143                 fi
144         fi
145
146 }
147
148
149 com_man()
150 {
151         local cn="$(grep ^codename Makefile.in)"
152         local ver="$(grep ^AC_INIT configure.ac \
153                 | cut -f 2 -d ',')"
154         cn=${cn#*=}
155         ver=${ver# *[}
156         ver=${ver%]}
157         echo "r=paraslash-$ver (cn: $cn)"
158         local n
159         local txtdir=txt
160         local mandir=man/man1
161         local htmldir=html
162         local pfx="$1"
163         mkdir -p $txtdir $mandir $htmldir || exit 1
164         while : ; do
165                 read_one_command
166                 if test $ret -lt 0; then
167                         exit 1
168                 fi
169                 if test $ret -eq 0; then
170                         break
171                 fi
172                 n=$pfx-$name_txt
173                 echo "pfx: $pfx, name: $n"
174                 dump_man > $txtdir/$n.txt
175                 txt2man -t "$n" -r "$r"  < $txtdir/$n.txt \
176                         | sed -e 1d > $mandir/$n.1
177                 man2html $mandir/$n.1 > $htmldir/$n.html
178         done
179 }
180
181 com_c_file()
182 {
183         echo "/** \file $file_name.c $c_file_comment */"
184         echo "$includes"
185         echo "struct $array_type $array_name[] = {"
186         while : ; do
187                 read_one_command
188                 if test $ret -lt 0; then
189                         exit 1
190                 fi
191                 if test $ret -eq 0; then
192                         break
193                 fi
194                 dump_array_member
195         done
196         echo '{.name = NULL}};'
197 }
198
199 dump_proto()
200 {
201         echo '/**'
202         echo " * $desc_txt"
203         echo ' *'
204         echo ' * \param fd the file descriptor to send output to'
205         if test $line_handler -eq 0; then
206                 echo ' * \param argc the number of arguments'
207                 echo ' * \param argv the argument vector'
208         else
209                 echo ' * \param cmdline the full command line'
210         fi
211         echo ' * '
212         echo " * synopsis: $syn_txt"
213         echo ' * '
214         echo "$help_txt" | sed -e 's/^/ * /g'
215         echo ' */'
216         if test $line_handler -eq 0; then
217                 echo "int com_$name_txt(int fd, int argc, char **argv);"
218         else
219                 echo "int com_$name_txt(int fd, char *cmdline);"
220         fi
221         echo
222 }
223
224 com_header()
225 {
226         echo "/** \file $file_name.h $header_comment */"
227         echo
228         echo "extern struct $array_type $array_name[];"
229         while : ; do
230                 read_one_command
231                 if test $ret -lt 0; then
232                         exit 1
233                 fi
234                 if test $ret -eq 0; then
235                         break
236                 fi
237                 dump_proto
238         done
239 }
240
241 read_header
242 arg="$1"
243 shift
244 case "$arg" in
245         "c_file")
246                 com_c_file
247                 ;;
248         "header")
249                 com_header
250                 ;;
251         "man")
252                 com_man $*
253                 ;;
254 esac