File: enum-extract.pl

package info (click to toggle)
zfs-linux 0.7.12-2%2Bdeb10u2
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 31,596 kB
  • sloc: ansic: 239,195; sh: 47,412; asm: 6,712; makefile: 3,842; python: 2,143; perl: 765; sed: 24; awk: 5
file content (58 lines) | stat: -rwxr-xr-x 955 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl -w

my $usage = <<EOT;
usage: config-enum enum [file ...]

Returns the elements from an enum declaration.

"Best effort": we're not building an entire C interpreter here!
EOT

use warnings;
use strict;
use Getopt::Std;

my %opts;

if (!getopts("", \%opts) || @ARGV < 1) {
	print $usage;
	exit 2;
}

my $enum = shift;

my $in_enum = 0;

while (<>) {
	# comments
	s/\/\*.*\*\///;
	if (m/\/\*/) {
		while ($_ .= <>) {
			last if s/\/\*.*\*\///s;
		}
	}

	# preprocessor stuff
	next if /^#/;

	# find our enum
	$in_enum = 1 if s/^\s*enum\s+${enum}(?:\s|$)//;
	next unless $in_enum;

	# remove explicit values
	s/\s*=[^,]+,/,/g;

	# extract each identifier
	while (m/\b([a-z_][a-z0-9_]*)\b/ig) {
		print $1, "\n";
	}

	#
	# don't exit: there may be multiple versions of the same enum, e.g.
	# inside different #ifdef blocks. Let's explicitly return all of
	# them and let external tooling deal with it.
	#
	$in_enum = 0 if m/}\s*;/;
}

exit 0;