command_util.sh: Make sure the "handler" pointer is NULL if a line handler exists.
[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 esac
55 done
56 if test -z "$header_comment" -o -z "$c_file_comment" \
57 -o -z "$file_name"; then
58 echo "header error" 1&>2
59 exit 1
60 fi
61 }
62
63 read_one_command()
64 {
65 local line
66
67 name_txt=""
68 desc_txt=""
69 syn_txt=""
70 help_txt=""
71 perms_txt=""
72 line_handler=0
73 mkdir -p man/man1
74 while read key value; do
75 case "$key" in
76 ---)
77 break
78 ;;
79 N:)
80 name_txt="$value"
81 ;;
82 P:)
83 perms_txt="$value"
84 ;;
85 D:)
86 desc_txt="$value"
87 ;;
88 L:)
89 line_handler=1
90 ;;
91 S:)
92 syn_txt="$value"
93 ;;
94 H:)
95 help_txt="${value}"
96 while read line; do
97 if test "$line" = "---"; then
98 break;
99 fi
100 line=${line#H:}
101 help_txt="$help_txt
102 ${line# }"
103 done
104 break
105 ;;
106 esac
107 done
108 if test -n "$name_txt" -a -n "$desc_txt" -a -n "$syn_txt" \
109 -a -n "$help_txt"; then
110 ret=1
111 return
112 fi
113 if test -z "$name_txt" -a -z "$desc_txt" -a -z "$syn_txt" \
114 -a -z "$help_txt"; then
115 ret=0
116 return
117 fi
118 ret=-1
119 return
120 echo "!ERROR!"
121 echo "N: $name_txt"
122 echo "D: $desc_txt"
123 echo "S: $syn_txt"
124 echo "P: $perms_txt"
125 echo "H: $help_txt"
126 }
127
128 dump_man()
129 {
130 echo "NAME"
131 printf "\t$name_txt - $desc_txt\n"
132 echo "SYNOPSIS"
133 printf "\t$syn_txt\n"
134 echo "DESCRIPTION"
135 echo "$help_txt"
136 if test -n "$perms_txt"; then
137 echo "PERMISSIONS"
138 if test "$perms_txt" = "0"; then
139 printf "\t(none)\n"
140 else
141 printf "\t$perms_txt\n"
142 fi
143 fi
144
145 }
146
147
148 com_man()
149 {
150 local cn="$(grep ^codename Makefile.in)"
151 local ver="$(grep ^AC_INIT configure.ac \
152 | cut -f 2 -d ',')"
153 cn=${cn#*=}
154 ver=${ver# *[}
155 ver=${ver%]}
156 echo "r=paraslash-$ver (cn: $cn)"
157 local n
158 local txtdir=txt
159 local mandir=man/man1
160 local htmldir=html
161 local pfx="$1"
162 mkdir -p $txtdir $mandir $htmldir || exit 1
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 n=$pfx-$name_txt
172 echo "pfx: $pfx, name: $n"
173 dump_man > $txtdir/$n.txt
174 txt2man -t "$n" -r "$r" < $txtdir/$n.txt \
175 | sed -e 1d > $mandir/$n.1
176 man2html $mandir/$n.1 > $htmldir/$n.html
177 done
178 }
179
180 com_c_file()
181 {
182 echo "/** \file $file_name.c $c_file_comment */"
183 echo "$includes"
184 echo "struct $array_type $array_name[] = {"
185 while : ; do
186 read_one_command
187 if test $ret -lt 0; then
188 exit 1
189 fi
190 if test $ret -eq 0; then
191 break
192 fi
193 dump_array_member
194 done
195 echo '{.name = NULL}};'
196 }
197
198 dump_proto()
199 {
200 echo '/**'
201 echo " * $desc_txt"
202 echo ' *'
203 echo ' * \param fd the file descriptor to send output to'
204 if test $line_handler -eq 0; then
205 echo ' * \param argc the number of arguments'
206 echo ' * \param argv the argument vector'
207 else
208 echo ' * \param cmdline the full command line'
209 fi
210 echo ' * '
211 echo " * synopsis: $syn_txt"
212 echo ' * '
213 echo "$help_txt" | sed -e 's/^/ * /g'
214 echo ' */'
215 if test $line_handler -eq 0; then
216 echo "int com_$name_txt(int fd, int argc, char **argv);"
217 else
218 echo "int com_$name_txt(int fd, char *cmdline);"
219 fi
220 echo
221 }
222
223 com_header()
224 {
225 echo "/** \file $file_name.h $header_comment */"
226 echo
227 echo "extern struct $array_type $array_name[];"
228 while : ; do
229 read_one_command
230 if test $ret -lt 0; then
231 exit 1
232 fi
233 if test $ret -eq 0; then
234 break
235 fi
236 dump_proto
237 done
238 }
239
240 read_header
241 arg="$1"
242 shift
243 case "$arg" in
244 "c")
245 com_c_file
246 ;;
247 "h")
248 com_header
249 ;;
250 "man")
251 com_man $*
252 ;;
253 esac