config/dracut/90zfs: handle cases where hostid(1) returns all zeros
[zfs.git] / scripts / enum-extract.pl
blob5112cc807f678055135bb046a3f67a474ce94c2e
1 #!/usr/bin/perl -w
3 my $usage = <<EOT;
4 usage: config-enum enum [file ...]
6 Returns the elements from an enum declaration.
8 "Best effort": we're not building an entire C interpreter here!
9 EOT
11 use warnings;
12 use strict;
13 use Getopt::Std;
15 my %opts;
17 if (!getopts("", \%opts) || @ARGV < 1) {
18 print $usage;
19 exit 2;
22 my $enum = shift;
24 my $in_enum = 0;
26 while (<>) {
27 # comments
28 s/\/\*.*\*\///;
29 if (m/\/\*/) {
30 while ($_ .= <>) {
31 last if s/\/\*.*\*\///s;
35 # preprocessor stuff
36 next if /^#/;
38 # find our enum
39 $in_enum = 1 if s/^\s*enum\s+${enum}(?:\s|$)//;
40 next unless $in_enum;
42 # remove explicit values
43 s/\s*=[^,]+,/,/g;
45 # extract each identifier
46 while (m/\b([a-z_][a-z0-9_]*)\b/ig) {
47 print $1, "\n";
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.
55 $in_enum = 0 if m/}\s*;/;
58 exit 0;