[go: up one dir, main page]

File: config.pl

package info (click to toggle)
uhub 0.4.1-3.3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,508 kB
  • sloc: ansic: 18,133; xml: 575; perl: 568; sh: 368; makefile: 24
file content (289 lines) | stat: -rwxr-xr-x 7,922 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl -w

use strict;
use XML::DOM;

sub write_c_header(@);
sub write_sql_dump(@);
sub get_data($);

my $dump_to_sql = 0;

# initialize parser and read the file
my $input = "./config.xml";
my $parser = new XML::DOM::Parser;
my $tree = $parser->parsefile($input) || die "Unable to parse XML file.";

# Get data
my $nodes = $tree->getElementsByTagName("option");
my @options = ();
for (my $i = 0; $i < $nodes->getLength; $i++)
{
	my @data = get_data($nodes->item($i));
	push @options, \@data;
}

write_c_header(@options);
write_sql_dump(@options) if ($dump_to_sql);

my $config_defaults = "void config_defaults(struct hub_config* config)\n{\n";
my $config_apply = "static int apply_config(struct hub_config* config, char* key, char* data, int line_count)\n{\n\tint max = 0;\n\tint min = 0;\n\n";
my $config_free = "void free_config(struct hub_config* config)\n{\n";
my $config_dump = "void dump_config(struct hub_config* config, int ignore_defaults)\n{\n";

foreach my $option (@options)
{
	my ($type, $name, $default, $advanced, $short, $desc, $since, $example, $check, $ifdef) = @$option;
	my $string = ($type =~ /(string|file|message)/);
	my $min = undef;
	my $max = undef;
	my $regexp = undef;

	if (defined $check)
	{
		$min = $check->getAttribute("min");
		$max = $check->getAttribute("max");
		$regexp = $check->getAttribute("regexp");

		$max = undef if ($max eq "");
		$min = undef if ($min eq "");
		$regexp = undef if ($regexp eq "");
	}

	$config_defaults .= "#ifdef $ifdef\n" if ($ifdef ne "");
	$config_defaults .= "\tconfig->$name = ";
	$config_defaults .= "hub_strdup(\"" if ($string);
	$config_defaults .= $default;
	$config_defaults .= "\")" if ($string);
	$config_defaults .= ";\n";
	$config_defaults .= "#endif /* $ifdef */\n" if ($ifdef ne "");

	$config_apply .= "#ifdef $ifdef\n" if ($ifdef ne "");
	$config_apply .= "\tif (!strcmp(key, \"" . $name . "\"))\n\t{\n";

	if ($type eq "int")
	{
		$config_apply .= "\t\tmin = $min;\n" if (defined $min);
		$config_apply .= "\t\tmax = $max;\n" if (defined $max);
		$config_apply .= "\t\tif (!apply_integer(key, data, &config->$name, ";
		if (defined $min) { $config_apply .= "&min"; } else { $config_apply .= "0"; }
		$config_apply .= ", ";
		if (defined $max) { $config_apply .= "&max"; } else { $config_apply .= "0"; }
		$config_apply .= "))\n";
	}
	elsif ($type eq "boolean")
	{
		$config_apply .= "\t\tif (!apply_boolean(key, data, &config->$name))\n";
	}
	elsif ($string)
	{
		$config_apply .="\t\tif (!apply_string(key, data, &config->$name, (char*) \"\"))\n";
	}

	$config_apply .= "\t\t{\n" .
				  "\t\t\tLOG_ERROR(\"Configuration parse error on line %d\", line_count);\n" .
				  "\t\t\treturn -1;\n" .
				  "\t\t}\n" .
				  "\t\treturn 0;\n" .
				  "\t}\n";
	$config_apply .= "#endif /* $ifdef */\n" if ($ifdef ne "");
	$config_apply .= "\n";

	if ($string)
	{
		$config_free .= "#ifdef $ifdef\n" if ($ifdef ne "");
		$config_free .= "\thub_free(config->" . $name . ");\n";
		$config_free .= "#endif /* $ifdef */\n" if ($ifdef ne "");
		$config_free .= "\n";
	}

	my $out = "%s";
	my $val = "config->$name";
	my $test = "config->$name != $default";

	$out = "%d" if ($type eq "int");
	$val = "config->$name ? \"yes\" : \"no\"" if ($type eq "boolean");

	if ($string)
	{
		$out = "\\\"%s\\\"";
		$test = "strcmp(config->$name, \"$default\") != 0";
	}

	$config_dump .= "#ifdef $ifdef\n" if ($ifdef ne "");
	$config_dump .= "\tif (!ignore_defaults || $test)\n";
	$config_dump .= "\t\tfprintf(stdout, \"$name = $out\\n\", $val);\n";
	$config_dump .= "#endif /* $ifdef */\n" if ($ifdef ne "");
	$config_dump .= "\n";
}

$config_apply .= "\t/* Still here -- unknown directive */\n";
$config_apply .= "\tLOG_ERROR(\"Unknown configuration directive: '%s'\", key);\n";
$config_apply .= "\treturn -1;\n";
$config_apply .= "}\n\n";
$config_defaults .= "}\n\n";
$config_free .= "}\n\n";
$config_dump .= "}\n\n";

open GENIMPL, ">gen_config.c" || die "Unable to write source file";
print GENIMPL "/* THIS FILE IS AUTOGENERATED - DO NOT CHANGE IT! */\n\n";
print GENIMPL $config_defaults;
print GENIMPL $config_apply;
print GENIMPL $config_free;
print GENIMPL $config_dump;


sub get_data($)
{
	my $p = shift;

	my $short = "";
	my $example = "";
	my $description = "";
	my $since = "";
	my $ifdef = "";

	$short = $p->getElementsByTagName("short")->item(0)->getFirstChild()->getData() if ($p->getElementsByTagName("short")->getLength());
	$since = $p->getElementsByTagName("since")->item(0)->getFirstChild()->getData() if ($p->getElementsByTagName("since")->getLength());
	$example = $p->getElementsByTagName("example")->item(0)->getFirstChild()->getData() if ($p->getElementsByTagName("example")->getLength());
	$description = $p->getElementsByTagName("description")->item(0)->getFirstChild()->getData() if ($p->getElementsByTagName("description")->getLength());
	my $check = $p->getElementsByTagName("check")->item(0);
	$ifdef = $p->getElementsByTagName("ifdef")->item(0)->getFirstChild()->getData() if ($p->getElementsByTagName("ifdef")->getLength());

	my @data = (
			$p->getAttribute("type"),
			$p->getAttribute("name"),
			$p->getAttribute("default"),
			$p->getAttribute("advanced"),
			$short,
			$description,
			$since,
			$example,
			$check,
			$ifdef
		);
	return @data;
}

# Write header file
sub write_c_header(@)
{
	my @data = @_;

	open GENHEAD, ">gen_config.h" || die "Unable to write header file";
	print GENHEAD "/* THIS FILE IS AUTOGENERATED - DO NOT CHANGE IT! */\n\n";
	print GENHEAD "struct hub_config\n{\n";

	foreach my $option (@data)
	{
		my ($type, $name, $default, $advanced, $short, $desc, $since, $example, $check, $ifdef) = @$option;

		my $string = ($type =~ /(string|file|message)/);

		print GENHEAD "#ifdef $ifdef\n" if ($ifdef ne "");

		print GENHEAD "\t";
		print GENHEAD "int  " if ($type eq "int");
		print GENHEAD "int  " if ($type eq "boolean");
		print GENHEAD "char*" if ($string);
		print GENHEAD " " . $name . ";";

		my $comment = "";
		if ($type eq "message")
		{
			$comment = "\"" . $default . "\"";
		}
		elsif (defined $short && length $short > 0)
		{
			$comment = $short;
			if (defined $default)
			{
				$comment .= " (default: ";
				$comment .= "\"" if ($string);
				$comment .= $default;
				$comment .= "\"" if ($string);
				$comment .= ")";
			}
		}

		if (length $comment > 0)
		{
			my $pad = "";
			for (my $i = length $name; $i < 32; $i++)
			{
				$pad .= " ";
			}
			$comment = $pad . "/*<<< " . $comment . " */";
		}
		print GENHEAD $comment . "\n";
		print GENHEAD "#endif /* $ifdef */\n" if ($ifdef ne "");
	}

	print GENHEAD "};\n\n";
}


sub write_sql_dump(@)
{
	my @data = @_;

	# Write SQL dump code
	open GENSQL, ">gen_config.sql" || die "Unable to write SQL dump";
	print GENSQL "START TRANSACTION;\n\n
		DROP TABLE uhub_config IF EXISTS;\n\n
		CREATE TABLE uhub_config (
			name VARCHAR(32) UNIQUE NOT NULL,
			defaultValue TINYTEXT NOT NULL,
			description LONGTEXT NOT NULL,
			type TINYTEXT NOT NULL,
			advanced BOOLEAN,
			example LONGTEXT,
			since TINYTEXT
		);\n\n";

	foreach my $option (@data)
	{
		my ($type, $name, $default, $advanced, $short, $desc, $since, $example, $check, $ifdef) = @$option;

		if ($type =~ /(string|file|message)/ )
		{
			$default = "\\\"$default\\\"";
		}

		$desc =~ s/\"/\\\"/g;
		$type =~ s/^int$/integer/;

		my $stmt = "INSERT INTO uhub_config VALUES(";
		$stmt .= "\"$name\", ";
		$stmt .= "\"$default\", ";
		$stmt .= "\"$desc\", ";
		$stmt .= "\"$type\", ";

		if (defined $example)
		{
			my $example_str = $example;
			$example_str =~ s/\\/\\\\/g;
			$example_str =~ s/\"/\\\"/g;
			$stmt .= "\"$example_str\", ";
		} else {
			$stmt .= "NULL, ";
		}

		if (defined $since) {
			$stmt .= "\"$since\", ";
		} else {
			$stmt .= "NULL, ";
		}

		if (defined $advanced) {
			$stmt .= "\"$advanced\"";
		} else {
			$stmt .= "NULL";
		}

		$stmt .= ");\n";

		print GENSQL $stmt;
	}
	print GENSQL "\n\nCOMMIT;\n\n";
}