summaryrefslogtreecommitdiff
path: root/command_util.sh
blob: 62ad96638c16c56b1a010f662d1affb4accf0bc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env bash


read_header()
{
	local key value i

	while read key value; do
		case "$key" in
		---)
			break
			;;
		HC:)
			header_comment="$value"
			;;
		CC:)
			c_file_comment="$value"
			;;
		OF:)
			output_file="$value"
			array_name=${value%command_list}cmds
			;;
		SF:)
			source_files="$value"
			;;
		AT:)
			array_type="$value"
			;;
		IN:)
			for i in $value; do
				includes="$includes
#include \"$i.h\""
			done
			includes="$includes
#include \"$output_file.h\""
			;;
		SN:)
			section_name="$value"
			;;
		TM:)
			template_members="$value"
		esac
	done
	if test -z "$header_comment" -o -z "$c_file_comment" \
			-o -z "$output_file"; then
		echo "header error" 1&>2
		exit 1
	fi
}

read_one_command()
{
	local line

	name_txt=""
	desc_txt=""
	usage_txt=""
	help_txt=""
	perms_txt=""
	line_handler=0
	template=0
	template_name=""
	template_prototype=""
	mkdir -p man/man1
	while read key value; do
		case "$key" in
		---)
			break
			;;
		N:)
			name_txt="$value"
			;;
		T:)
			template_name="$value"
			template=1
			;;
		O:)
			template_prototype="$value"
			template=1
			;;
		P:)
			perms_txt="$value"
			;;
		D:)
			desc_txt="$value"
			;;
		L:)
			line_handler=1
			;;
		U:)
			usage_txt="$value"
			;;
		H:)
			help_txt="${value}"
			while read line; do
				if test "$line" = "---"; then
					break;
				fi
				line=${line#H:}
				help_txt="$help_txt
${line# }"
			done
			break
			;;
		esac
	done
	if test $template -eq 0; then
		if test -n "$name_txt" -a -n "$desc_txt" -a -n "$usage_txt" \
				-a -n "$help_txt"; then
			ret=1
			return
		fi
	else
		if test -n "$template_name" -a -n "$template_prototype" \
				-a -n "$name_txt " -a -n "$template_members" \
				-a -n "$desc_txt" -a -n "$usage_txt" \
				-a -n "$help_txt"; then
			ret=1
			return
		fi
	fi
	if test -z "$name_txt" -a -z "$desc_txt" -a -z "$usage_txt" \
			-a -z "$help_txt"; then
		ret=0
		return
	fi
	ret=-1
	#return
	echo "!ERROR!"
	echo "N: $name_txt"
	echo "D: $desc_txt"
	echo "S: $usage_txt"
	echo "P: $perms_txt"
	echo "H: $help_txt"
	echo "O: $template_prototype"
}

dump_man()
{
	if test $template -eq 0; then
		echo ".SS \"$name_txt\""
		echo "$desc_txt"
		echo
		echo "\\fBUsage: \\fP$usage_txt"
	else
		for member in $template_members; do
			local sed_cmd="sed -e s/@member@/$member/g"
			local t_name_txt=$(echo $name_txt | $sed_cmd)
			echo ".SS \"$t_name_txt\""
		done
		echo "$desc_txt" | sed -e "s/@member@/{$(echo $template_members | sed -e 's/ / | /g')}/g"
		echo
		echo "\\fBUsage: \\fP"
		echo
		echo ".nf"
		for member in $template_members; do
			local sed_cmd="sed -e s/@member@/$member/g"
			local t_usage_txt=$(echo $usage_txt | $sed_cmd)
			printf "\t$t_usage_txt\n"
		done
		echo ".fi"
	fi
	echo
	echo "$help_txt" | sed -e 's/^	//'
	echo
	if test -n "$perms_txt"; then
		echo -n "\\fBpermissions:\\fP "
		if test "$perms_txt" = "0"; then
			echo "(none)"
		else
			echo "$perms_txt"
		fi
	fi
	echo
}


com_man()
{
	echo "[$section_name]"
	echo
	while : ; do
		read_one_command
		if test $ret -lt 0; then
			exit 1
		fi
		if test $ret -eq 0; then
			break
		fi
		dump_man
	done
}

dump_proto()
{
	local source_file match=""

	echo '/**'
	echo " * $desc_txt"
	echo ' *'
	echo ' * \param fd The file descriptor to send output to.'
	if test $line_handler -eq 0; then
		echo ' * \param argc The number of arguments.'
		echo ' * \param argv The argument vector.'
	else
		echo ' * \param cmdline The full command line.'
	fi
	echo ' * '
	echo " * Usage: $usage_txt"
	echo ' * '
	echo "$help_txt" | sed -e 's/^/ * /g'
	echo ' */'
	if test -n "$prototype"; then
		echo "$prototype"
		echo
		return
	fi
	for source_file in $source_files; do
		match=$(grep "^\(__noreturn \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
		if test -n "$match"; then
			echo "$match"
			break
		fi
	done
	echo
}

dump_array_member()
{
	echo '{'
	echo ".name = \"$name_txt\","
	if test $line_handler -eq 0; then
		echo ".handler = com_$name_txt,"
	else
		echo ".handler = NULL,"
		echo ".line_handler = com_$name_txt,"
	fi
	if test -n "$perms_txt"; then
		echo ".perms = $perms_txt,"
	fi
	echo ".description = \"$desc_txt\","
	echo ".usage = \"$usage_txt\","
	echo ".help = "
	printf "%s\n" "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
	echo '},'
}


template_loop()
{
	local t_name="$name_txt"
	local t_perms="$perms_txt"
	local t_desc="$desc_txt"
	local t_usage="$usage_txt"
	local t_help="$help_txt"
	local t_source_files="$source_files"
	local member
	for member in $template_members; do
		local sed_cmd="sed -e s/@member@/$member/g"
		#echo "sed_cmd: $sed_cmd"
		name_txt=$(echo $t_name | $sed_cmd)
		#name_txt="$tname"
		perms_txt=$(echo $t_perms | $sed_cmd)
		desc_txt=$(echo $t_desc | $sed_cmd)
		usage_txt=$(echo $t_usage | $sed_cmd)
		help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
		prototype=$(echo "$template_prototype" | $sed_cmd)
		$1
	done

}

com_c_file()
{
	echo "/** \file $output_file.c $c_file_comment */"
	echo "$includes"
	echo "struct $array_type $array_name[] = {"
	while : ; do
		read_one_command
		if test $ret -lt 0; then
			exit 1
		fi
		if test $ret -eq 0; then
			break
		fi
		if test $template -eq 0; then
			dump_array_member
			continue
		fi
		template_loop dump_array_member
	done
	echo '{.name = NULL}};'
}

com_header()
{
	echo "/** \file $output_file.h $header_comment */"
	echo
	echo "extern struct $array_type $array_name[];"
	while : ; do
		read_one_command
		if test $ret -lt 0; then
			exit 1
		fi
		if test $ret -eq 0; then
			break
		fi
		if test $template -eq 0; then
			dump_proto
			continue
		fi
		template_loop dump_proto
	done
}

read_header
arg="$1"
shift
case "$arg" in
	"c")
		com_c_file
		;;
	"h")
		com_header
		;;
	"man")
		com_man $*
		;;
esac