parse-headers.pl 6.3 KB
Newer Older
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
#!/usr/bin/perl
use strict;
use Text::Tabs;

# Uncomment if debug is needed
#use Data::Dumper;

# change to 1 to generate some debug prints
my $debug = 0;

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;
30
my $is_comment = 0;
31 32
open IN, $file_in or die "Can't open $file_in";
while (<IN>) {
33 34
	$data .= $_;

35
	my $ln = $_;
36 37
	if (!$is_comment) {
		$ln =~ s,/\*.*(\*/),,g;
38

39 40 41 42 43 44 45 46
		$is_comment = 1 if ($ln =~ s,/\*.*,,);
	} else {
		if ($ln =~ s,^(.*\*/),,) {
			$is_comment = 0;
		} else {
			next;
		}
	}
47

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

		$enum_symbols{$s} = $n;

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

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

		$ioctls{$s} = $n;
		next;
	}

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

		$defines{$s} = $n;
		next;
	}

80
	if ($ln =~ m/^\s*typedef\s+.*\s+([_\w][\w\d_]+);/) {
81 82 83 84 85 86 87 88
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

		$typedefs{$s} = $n;
		next;
	}
89
	if ($ln =~ m/^\s*enum\s+([_\w][\w\d_]+)\s+\{/
90 91 92
	    || $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_]+)$/) {
93 94 95 96 97 98 99 100 101 102
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

		$enums{$s} = $n;

		$is_enum = $1;
		next;
	}
103
	if ($ln =~ m/^\s*struct\s+([_\w][\w\d_]+)\s+\{/
104 105 106 107
	    || $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_]+)$/
	    ) {
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		my $s = $1;
		my $n = $1;
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

		$structs{$s} = $n;
		next;
	}
}
close IN;

#
# Handle multi-line typedefs
#

123 124
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,);
125
foreach my $m (@matches) {
126 127
		my $s = $m;
		my $n = $m;
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
		$n =~ tr/A-Z/a-z/;
		$n =~ tr/_/-/;

		$typedefs{$s} = $n;
	next;
}

#
# Handle exceptions, if any
#

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;
		}
166 167 168 169
		if (m/^ignore\s+symbol\s+(\S+)/) {
			delete $enum_symbols{$1} if (exists($enum_symbols{$1}));
			next;
		}
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

		# Parsers to replace a symbol

		if (m/^replace\s+ioctl\s+(\S+)\s+(\S+)/) {
			$ioctls{$1} = $2 if (exists($ioctls{$1}));
			next;
		}
		if (m/^replace\s+define\s+(\S+)\s+(\S+)/) {
			$defines{$1} = $2 if (exists($defines{$1}));
			next;
		}
		if (m/^replace\s+typedef\s+(\S+)\s+(\S+)/) {
			$typedefs{$1} = $2 if (exists($typedefs{$1}));
			next;
		}
		if (m/^replace\s+enum\s+(\S+)\s+(\S+)/) {
			$enums{$1} = $2 if (exists($enums{$1}));
			next;
		}
		if (m/^replace\s+symbol\s+(\S+)\s+(\S+)/) {
			$enum_symbols{$1} = $2 if (exists($enum_symbols{$1}));
			next;
		}
		if (m/^replace\s+struct\s+(\S+)\s+(\S+)/) {
			$structs{$1} = $2 if (exists($structs{$1}));
			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
#
$data =~ s,([\_\`\*\<\>\&\\\\:\/]),\\$1,g;

225 226
$data =~ s,DEPRECATED,**DEPRECATED**,g;

227 228 229 230
#
# Add references
#

231 232
my $start_delim = "[ \n\t\(\=\*\@]";
my $end_delim = "(\\s|,|\\\\=|\\\\:|\\;|\\\)|\\}|\\{)";
233 234 235 236

foreach my $r (keys %ioctls) {
	my $n = $ioctls{$r};

237
	my $s = "\\ :ref:`$r <$n>`\\ ";
238 239 240 241 242

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

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

243
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
244 245 246 247 248
}

foreach my $r (keys %defines) {
	my $n = $defines{$r};

249
	my $s = "\\ :ref:`$r <$n>`\\ ";
250 251 252 253 254

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

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

255
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
256 257 258 259 260
}

foreach my $r (keys %enum_symbols) {
	my $n = $enum_symbols{$r};

261
	my $s = "\\ :ref:`$r <$n>`\\ ";
262 263 264 265 266

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

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

267
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
268 269 270 271 272
}

foreach my $r (keys %enums) {
	my $n = $enums{$r};

273
	my $s = "\\ :ref:`enum $r <$n>`\\ ";
274 275 276 277 278

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

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

279
	$data =~ s/enum\s+($r)$end_delim/$s$2/g;
280 281 282 283 284
}

foreach my $r (keys %structs) {
	my $n = $structs{$r};

285
	my $s = "\\ :ref:`struct $r <$n>`\\ ";
286 287 288 289 290

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

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

291
	$data =~ s/struct\s+($r)$end_delim/$s$2/g;
292 293 294 295 296
}

foreach my $r (keys %typedefs) {
	my $n = $typedefs{$r};

297
	my $s = "\\ :ref:`$r <$n>`\\ ";
298 299 300 301 302

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

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

303
	$data =~ s/($start_delim)($r)$end_delim/$1$s$3/g;
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
}

#
# 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;