summaryrefslogtreecommitdiff
path: root/source3/script/genstruct.pl
blob: 081b81f5102f986f36ba6ebb8ace83c0c024bca2 (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
#!/usr/bin/perl -w
# a simple system for generating C parse info
# this can be used to write generic C structer load/save routines
# Copyright 2002 Andrew Tridgell <genstruct@tridgell.net>
# released under the GNU General Public License v2 or later

use strict;

my(%enum_done) = ();
my(%struct_done) = ();

###################################################
# general handler
sub handle_general($$$$$$$$)
{
	my($name) = shift;
	my($ptr_count) = shift;
	my($size) = shift;
	my($element) = shift;
	my($flags) = shift;
	my($dump_fn) = shift;
	my($parse_fn) = shift;
	my($tflags) = shift;
	my($array_len) = 0;
	my($dynamic_len) = "NULL";

	# handle arrays, currently treat multidimensional arrays as 1 dimensional
	while ($element =~ /(.*)\[(.*?)\]$/) {
		$element = $1;
		if ($array_len == 0) {
			$array_len = $2;
		} else {
			$array_len = "$2 * $array_len";
		}
	}

	if ($flags =~ /_LEN\((\w*?)\)/) {
		$dynamic_len = "\"$1\"";
	}

	if ($flags =~ /_NULLTERM/) {
		$tflags = "FLAG_NULLTERM";
	}

	print OFILE "{\"$element\", $ptr_count, $size, offsetof(struct $name, $element), $array_len, $dynamic_len, $tflags, $dump_fn, $parse_fn},\n";
}


####################################################
# parse one element
sub parse_one($$$$)
{
	my($name) = shift;
	my($type) = shift;
	my($element) = shift;
	my($flags) = shift;
	my($ptr_count) = 0;
	my($size) = "sizeof($type)";
	my($tflags) = "0";
	
	# enums get the FLAG_ALWAYS flag
	if ($type =~ /^enum /) {
		$tflags = "FLAG_ALWAYS";
	}


	# make the pointer part of the base type 
	while ($element =~ /^\*(.*)/) {
		$ptr_count++;
		$element = $1;
	}

	# convert spaces to _
	$type =~ s/ /_/g;

	my($dump_fn) = "gen_dump_$type";
	my($parse_fn) = "gen_parse_$type";

	handle_general($name, $ptr_count, $size, $element, $flags, $dump_fn, $parse_fn, $tflags);
}

####################################################
# parse one element
sub parse_element($$$)
{
	my($name) = shift;
	my($element) = shift;
	my($flags) = shift;
	my($type);
	my($data);

	# pull the base type
	if ($element =~ /^struct (\S*) (.*)/) {
		$type = "struct $1";
		$data = $2;
	} elsif ($element =~ /^enum (\S*) (.*)/) {
		$type = "enum $1";
		$data = $2;
	} elsif ($element =~ /^unsigned (\S*) (.*)/) {
		$type = "unsigned $1";
		$data = $2;
	} elsif ($element =~ /^(\S*) (.*)/) {
		$type = $1;
		$data = $2;
	} else {
		die "Can't parse element '$element'";
	}

	# handle comma separated lists 
	while ($data =~ /(\S*),[\s]?(.*)/) {
		parse_one($name, $type, $1, $flags);
		$data = $2;
	}
	parse_one($name, $type, $data, $flags);
}


my($first_struct) = 1;

####################################################
# parse the elements of one structure
sub parse_elements($$)
{
	my($name) = shift;
	my($elements) = shift;

	if ($first_struct) {
		$first_struct = 0;
		print "Parsing structs: $name";
	} else {
		print ", $name";
	}

	print OFILE "int gen_dump_struct_$name(struct parse_string *, const char *, unsigned);\n";
	print OFILE "int gen_parse_struct_$name(char *, const char *);\n";

	print OFILE "static const struct parse_struct pinfo_" . $name . "[] = {\n";

	while ($elements =~ /^.*?([a-z].*?);\s*?(\S*?)\s*?\$(.*)/msi) {
		my($element) = $1;
		my($flags) = $2;
		$elements = $3;
		parse_element($name, $element, $flags);
	}

	print OFILE "{NULL, 0, 0, 0, 0, NULL, 0, NULL, NULL}};\n";

	print OFILE "
int gen_dump_struct_$name(struct parse_string *p, const char *ptr, unsigned indent) {
	return gen_dump_struct(pinfo_$name, p, ptr, indent);
}
int gen_parse_struct_$name(char *ptr, const char *str) {
	return gen_parse_struct(pinfo_$name, ptr, str);
}

";
}

my($first_enum) = 1;

####################################################
# parse out the enum declarations
sub parse_enum_elements($$)
{
	my($name) = shift;
	my($elements) = shift;

	if ($first_enum) {
		$first_enum = 0;
		print "Parsing enums: $name";
	} else {
		print ", $name";
	}

	print OFILE "static const struct enum_struct einfo_" . $name . "[] = {\n";

	my(@enums) = split(/,/s, $elements);
	for (my($i)=0; $i <= $#{@enums}; $i++) {
		my($enum) = $enums[$i];
		if ($enum =~ /\s*(\w*)/) {
			my($e) = $1;
			print OFILE "{\"$e\", $e},\n";
		}
	}

	print OFILE "{NULL, 0}};\n";

	print OFILE "
int gen_dump_enum_$name(struct parse_string *p, const char *ptr, unsigned indent) {
	return gen_dump_enum(einfo_$name, p, ptr, indent);
}

int gen_parse_enum_$name(char *ptr, const char *str) {
	return gen_parse_enum(einfo_$name, ptr, str);
}

";
}

####################################################
# parse out the enum declarations
sub parse_enums($)
{
	my($data) = shift;

	while ($data =~ /^GENSTRUCT\s+enum\s+(\w*?)\s*{(.*?)}\s*;(.*)/ms) {
		my($name) = $1;
		my($elements) = $2;
		$data = $3;

		if (!defined($enum_done{$name})) {
			$enum_done{$name} = 1;
			parse_enum_elements($name, $elements);
		}
	}

	if (! $first_enum) {
		print "\n";
	}
}

####################################################
# parse all the structures
sub parse_structs($)
{
	my($data) = shift;

	# parse into structures 
	while ($data =~ /^GENSTRUCT\s+struct\s+(\w+?)\s*{\s*(.*?)\s*}\s*;(.*)/ms) {
		my($name) = $1;
		my($elements) = $2;
		$data = $3;
		if (!defined($struct_done{$name})) {
			$struct_done{$name} = 1;
			parse_elements($name, $elements);
		}
	}

	if (! $first_struct) {
		print "\n";
	} else {
		print "No GENSTRUCT structures found?\n";
	}
}


####################################################
# parse a header file, generating a dumper structure
sub parse_data($)
{
	my($data) = shift;

	# collapse spaces 
	$data =~ s/[\t ]+/ /sg;
	$data =~ s/\s*\n\s+/\n/sg;
	# strip debug lines
	$data =~ s/^\#.*?\n//smg;

	parse_enums($data);
	parse_structs($data);
}


#########################################
# display help text
sub ShowHelp()
{
    print "
generator for C structure dumpers
Copyright Andrew Tridgell <genstruct\@tridgell.net>

Sample usage:
   genstruct -o output.h gcc -E -O2 -g test.h

Options:
    --help                this help page
    -o OUTPUT             place output in OUTPUT
";
    exit(0);
}

########################################
# main program
if ($ARGV[0] ne "-o" || $#ARGV < 2) {
	ShowHelp();
}

shift;
my($opt_ofile)=shift;

print "creating $opt_ofile\n";

open(OFILE, ">$opt_ofile") || die "can't open $opt_ofile";    

print OFILE "/* This is an automatically generated file - DO NOT EDIT! */\n\n";

parse_data(`@ARGV -DGENSTRUCT=GENSTRUCT`);
exit(0);