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