parse-headers.pl 6.7 KB
Newer Older
1 2 3 4 5 6
#!/usr/bin/perl
use strict;
use Text::Tabs;

my $debug = 0;

7 8 9 10 11 12 13 14 15 16
while ($ARGV[0] =~ m/^-(.*)/) {
	my $cmd = shift @ARGV;
	if ($cmd eq "--debug") {
		require Data::Dumper;
		$debug = 1;
		next;
	}
	die "argument $cmd unknown";
}

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
if (scalar @ARGV < 2 || scalar @ARGV > 3) {
	die "Usage:\n\t$0 <file in> <file out> [<exceptions file>]\n";
}

my ($file_in, $file_out, $file_exceptions) = @ARGV;

my $data;
my %ioctls;
my %defines;
my %typedefs;
my %enums;
my %enum_symbols;
my %structs;

#
# read the file and get identifiers
#

my $is_enum = 0;
36
my $is_comment = 0;
37 38
open IN, $file_in or die "Can't open $file_in";
while (<IN>) {
39 40
	$data .= $_;

41
	my $ln = $_;
42 43
	if (!$is_comment) {
		$ln =~ s,/\*.*(\*/),,g;
44

45 46 47 48 49 50 51 52
		$is_comment = 1 if ($ln =~ s,/\*.*,,);
	} else {
		if ($ln =~ s,^(.*\*/),,) {
			$is_comment = 0;
		} else {
			next;
		}
	}
53

54
	if ($is_enum && $ln =~ m/^\s*([_\w][\w\d_]+)\s*[\,=]?/) {
55 56 57 58 59
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

60
		$enum_symbols{$s} =  "\\ :ref:`$s <$n>`\\ ";
61 62 63 64 65 66

		$is_enum = 0 if ($is_enum && m/\}/);
		next;
	}
	$is_enum = 0 if ($is_enum && m/\}/);

67
	if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+_IO/) {
68 69 70 71
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;

72
		$ioctls{$s} = "\\ :ref:`$s <$n>`\\ ";
73 74 75
		next;
	}

76
	if ($ln =~ m/^\s*#\s*define\s+([_\w][\w\d_]+)\s+/) {
77 78 79 80 81
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

82
		$defines{$s} = "\\ :ref:`$s <$n>`\\ ";
83 84 85
		next;
	}

86 87 88
	if ($ln =~ m/^\s*typedef\s+([_\w][\w\d_]+)\s+(.*)\s+([_\w][\w\d_]+);/) {
		my $s = $2;
		my $n = $3;
89

90
		$typedefs{$n} = "\\ :c:type:`$n <$s>`\\ ";
91 92
		next;
	}
93
	if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
94 95 96
	    || $ln =~ m/^\s*enum\s+([_\w][\w\d_]+)$/
	    || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)\s+\{/
	    || $ln =~ m/^\s*typedef\s*enum\s+([_\w][\w\d_]+)$/) {
97 98
		my $s = $1;

99
		$enums{$s} =  "enum :c:type:`$s`\\ ";
100 101 102 103

		$is_enum = $1;
		next;
	}
104
	if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
105 106 107 108
	    || $ln =~ m/^\s*struct\s+([[_\w][\w\d_]+)$/
	    || $ln =~ m/^\s*typedef\s*struct\s+([_\w][\w\d_]+)\s+\{/
	    || $ln =~ m/^\s*typedef\s*struct\s+([[_\w][\w\d_]+)$/
	    ) {
109 110
		my $s = $1;

111
		$structs{$s} = "struct :c:type:`$s`\\ ";
112 113 114 115 116 117 118 119 120
		next;
	}
}
close IN;

#
# Handle multi-line typedefs
#

121 122
my @matches = ($data =~ m/typedef\s+struct\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,
	       $data =~ m/typedef\s+enum\s+\S+?\s*\{[^\}]+\}\s*(\S+)\s*\;/g,);
123
foreach my $m (@matches) {
124
	my $s = $m;
125

126
	$typedefs{$s} = "\\ :c:type:`$s`\\ ";
127 128 129 130 131 132 133
	next;
}

#
# Handle exceptions, if any
#

134 135 136 137 138 139 140 141 142
my %def_reftype = (
	"ioctl"   => ":ref",
	"define"  => ":ref",
	"symbol"  => ":ref",
	"typedef" => ":c:type",
	"enum"    => ":c:type",
	"struct"  => ":c:type",
);

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
if ($file_exceptions) {
	open IN, $file_exceptions or die "Can't read $file_exceptions";
	while (<IN>) {
		next if (m/^\s*$/ || m/^\s*#/);

		# Parsers to ignore a symbol

		if (m/^ignore\s+ioctl\s+(\S+)/) {
			delete $ioctls{$1} if (exists($ioctls{$1}));
			next;
		}
		if (m/^ignore\s+define\s+(\S+)/) {
			delete $defines{$1} if (exists($defines{$1}));
			next;
		}
		if (m/^ignore\s+typedef\s+(\S+)/) {
			delete $typedefs{$1} if (exists($typedefs{$1}));
			next;
		}
		if (m/^ignore\s+enum\s+(\S+)/) {
			delete $enums{$1} if (exists($enums{$1}));
			next;
		}
		if (m/^ignore\s+struct\s+(\S+)/) {
			delete $structs{$1} if (exists($structs{$1}));
			next;
		}
170 171 172 173
		if (m/^ignore\s+symbol\s+(\S+)/) {
			delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
			next;
		}
174 175

		# Parsers to replace a symbol
176
		my ($type, $old, $new, $reftype);
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		if (m/^replace\s+(\S+)\s+(\S+)\s+(\S+)/) {
			$type = $1;
			$old = $2;
			$new = $3;
		} else {
			die "Can't parse $file_exceptions: $_";
		}

		if ($new =~ m/^\:c\:(data|func|macro|type)\:\`(.+)\`/) {
			$reftype = ":c:$1";
			$new = $2;
		} elsif ($new =~ m/\:ref\:\`(.+)\`/) {
			$reftype = ":ref";
			$new = $1;
		} else {
			$reftype = $def_reftype{$type};
		}
		$new = "$reftype:`$old <$new>`";

		if ($type eq "ioctl") {
			$ioctls{$old} = $new if (exists($ioctls{$old}));
199 200
			next;
		}
201 202
		if ($type eq "define") {
			$defines{$old} = $new if (exists($defines{$old}));
203 204
			next;
		}
205 206
		if ($type eq "symbol") {
			$enum_symbols{$old} = $new if (exists($enum_symbols{$old}));
207 208
			next;
		}
209 210
		if ($type eq "typedef") {
			$typedefs{$old} = $new if (exists($typedefs{$old}));
211 212
			next;
		}
213 214
		if ($type eq "enum") {
			$enums{$old} = $new if (exists($enums{$old}));
215 216
			next;
		}
217 218
		if ($type eq "struct") {
			$structs{$old} = $new if (exists($structs{$old}));
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
			next;
		}

		die "Can't parse $file_exceptions: $_";
	}
}

if ($debug) {
	print Data::Dumper->Dump([\%ioctls], [qw(*ioctls)]) if (%ioctls);
	print Data::Dumper->Dump([\%typedefs], [qw(*typedefs)]) if (%typedefs);
	print Data::Dumper->Dump([\%enums], [qw(*enums)]) if (%enums);
	print Data::Dumper->Dump([\%structs], [qw(*structs)]) if (%structs);
	print Data::Dumper->Dump([\%defines], [qw(*defines)]) if (%defines);
	print Data::Dumper->Dump([\%enum_symbols], [qw(*enum_symbols)]) if (%enum_symbols);
}

#
# Align block
#
$data = expand($data);
$data = "    " . $data;
$data =~ s/\n/\n    /g;
$data =~ s/\n\s+$/\n/g;
$data =~ s/\n\s+\n/\n\n/g;

#
# Add escape codes for special characters
#
247
$data =~ s,([\_\`\*\<\>\&\\\\:\/\|\%\$\#\{\}\~\^]),\\$1,g;
248

249 250
$data =~ s,DEPRECATED,**DEPRECATED**,g;

251 252 253 254
#
# Add references
#

255 256
my $start_delim = "[ \n\t\(\=\*\@]";
my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
257 258

foreach my $r (keys %ioctls) {
259
	my $s = $ioctls{$r};
260 261 262 263 264

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);

265
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
266 267 268
}

foreach my $r (keys %defines) {
269
	my $s = $defines{$r};
270 271 272 273 274

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);

275
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
276 277 278
}

foreach my $r (keys %enum_symbols) {
279
	my $s = $enum_symbols{$r};
280 281 282 283 284

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);

285
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
286 287 288
}

foreach my $r (keys %enums) {
289
	my $s = $enums{$r};
290 291 292 293 294

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);

295
	$data =~ s/enum\s+($r)$end_delim/$s$2/g;
296 297 298
}

foreach my $r (keys %structs) {
299
	my $s = $structs{$r};
300 301 302 303 304

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);

305
	$data =~ s/struct\s+($r)$end_delim/$s$2/g;
306 307 308
}

foreach my $r (keys %typedefs) {
309
	my $s = $typedefs{$r};
310 311 312 313

	$r =~ s,([\_\`\*\<\>\&\\\\:\/]),\\\\$1,g;

	print "$r -> $s\n" if ($debug);
314
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
315 316
}

317
$data =~ s/\\ ([\n\s])/\1/g;
318

319 320 321 322 323 324 325 326 327 328 329 330 331 332
#
# Generate output file
#

my $title = $file_in;
$title =~ s,.*/,,;

open OUT, "> $file_out" or die "Can't open $file_out";
print OUT ".. -*- coding: utf-8; mode: rst -*-\n\n";
print OUT "$title\n";
print OUT "=" x length($title);
print OUT "\n\n.. parsed-literal::\n\n";
print OUT $data;
close OUT;