summaryrefslogtreecommitdiff
path: root/command_util.sh
blob: 4a9a03cc77f9637e20fc797d99f655ca3ce90a30 (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
#!/bin/bash


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 = "
	echo "$help_txt" | sed -e 's/^/\"/g' -e 's/$/\\n\"/g'
	echo '},'
}

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_file="$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"
		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
	mkdir -p man/man1
	while read key value; do
		case "$key" in
		---)
			break
			;;
		N:)
			name_txt="$value"
			;;
		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 -n "$name_txt" -a -n "$desc_txt" -a -n "$usage_txt" \
			-a -n "$help_txt"; then
		ret=1
		return
	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"
}

dump_man()
{
	echo ".SS \"$name_txt\""
	echo "$desc_txt"
	echo
	echo "\\fBusage: \\fP$usage_txt"
	echo
	echo "$help_txt"
	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
}

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
		dump_array_member
	done
	echo '{.name = NULL}};'
}

dump_proto()
{
	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 ' */'
	grep "^\(__noreturn \)*int com_$name_txt(" $source_file | sed -e 's/$/;/1'
	echo
}

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
		dump_proto
	done
}

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