1#!/usr/bin/perl -w
2
3my $usage = <<EOT;
4usage: config-enum enum [file ...]
5
6Returns the elements from an enum declaration.
7
8"Best effort": we're not building an entire C interpreter here!
9EOT
10
11use warnings;
12use strict;
13use Getopt::Std;
14
15my %opts;
16
17if (!getopts("", \%opts) || @ARGV < 1) {
18	print $usage;
19	exit 2;
20}
21
22my $enum = shift;
23
24my $in_enum = 0;
25
26while (<>) {
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
58exit 0;
59