]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - scripts/enum-extract.pl
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / scripts / enum-extract.pl
1 #!/usr/bin/perl -w
2
3 my $usage = <<EOT;
4 usage: config-enum enum [file ...]
5
6 Returns the elements from an enum declaration.
7
8 "Best effort": we're not building an entire C interpreter here!
9 EOT
10
11 use warnings;
12 use strict;
13 use Getopt::Std;
14
15 my %opts;
16
17 if (!getopts("", \%opts) || @ARGV < 1) {
18         print $usage;
19         exit 2;
20 }
21
22 my $enum = shift;
23
24 my $in_enum = 0;
25
26 while (<>) {
27         # comments
28         s/\/\*.*\*\///;
29         if (m/\/\*/) {
30                 while ($_ .= <>) {
31                         last if s/\/\*.*\*\///s;
32                 }
33         }
34
35         # preprocessor stuff
36         next if /^#/;
37
38         # find our enum
39         $in_enum = 1 if s/^\s*enum\s+${enum}(?:\s|$)//;
40         next unless $in_enum;
41
42         # remove explicit values
43         s/\s*=[^,]+,/,/g;
44
45         # extract each identifier
46         while (m/\b([a-z_][a-z0-9_]*)\b/ig) {
47                 print $1, "\n";
48         }
49
50         #
51         # don't exit: there may be multiple versions of the same enum, e.g.
52         # inside different #ifdef blocks. Let's explicitly return all of
53         # them and let external tooling deal with it.
54         #
55         $in_enum = 0 if m/}\s*;/;
56 }
57
58 exit 0;