4 # Information about the current enumeration
5 my $flags; # Is enumeration a bitmask?
6 my $seenbitshift; # Have we seen bitshift operators?
7 my $enum_prefix; # Prefix for this enumeration
8 my $enumname; # Name for this enumeration
9 my $enumshort; # $enumname without prefix
10 my $enumindex = 0; # Global enum counter
11 my $firstenum = 1; # Is this the first enumeration per file?
12 my @entries; # [ $name, $val ] for each entry
18 for $opt (split /\s*,\s*/, $opts) {
21 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
22 defined $val or $val = 1;
23 push @opts, $key, $val;
29 my $file_name = shift;
30 my $looking_for_name = 0;
34 # read lines until comment end is matched
35 while (m@/\*([^*]|\*[^/*])*\**$@x) {
37 defined ($new = <>) || die "Unmatched comment in $ARGV";
40 # strip comments w/o options
41 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
49 # print STDERR "xxx $_\n";
51 if ($looking_for_name) {
58 # Handle include files
59 if (/^\#include\s*<([^>]*)>/ ) {
61 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
63 # read lines until comment end is matched
64 while (m@/\*([^*]|\*[^/*])*\**$@x) {
66 defined ($new = <>) || die "Unmatched comment in $file_name";
69 # strip comments w/o options
70 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
72 if (parse_entries (\*NEWFILE, $NEWFILE)) {
79 if (/^\s*\}\s*(\w+)/) {
87 $looking_for_name = 1;
101 my ($name, $value, $options) = ($1,$2,$3);
103 # print STDERR "xxx \"$name\" \"$value\" \"$otions\"\n";
105 if (!defined $flags && defined $value && $value =~ /<</) {
109 if (defined $options) {
110 my %options = parse_trigraph($options);
111 if (!defined $options{skip}) {
112 push @entries, [ $name, $options{nick} ];
115 push @entries, [ $name ];
117 } elsif (m@^\s*\#@) {
118 # ignore preprocessor directives
120 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
128 print STDERR "glib-mkenums version glib-2.0\n"; # FIXME: autogen version?
129 print STDERR "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
130 print STDERR "You may redistribute copies of glib-mkenums under the terms of\n";
131 print STDERR "the GNU General Public License which can be found in the\n";
132 print STDERR "GLib source package. Sources, examples and contact\n";
133 print STDERR "information are available at http://www.gtk.org\n";
137 print STDERR "Usage: glib-mkenums [options] [files...]\n";
138 print STDERR " --fhead <text> output file header\n";
139 print STDERR " --fprod <text> per input file production\n";
140 print STDERR " --ftail <text> output file trailer\n";
141 print STDERR " --eprod <text> per enum text (produced prior to value itarations)\n";
142 print STDERR " --vhead <text> value header, produced before iterating over enum values\n";
143 print STDERR " --vprod <text> value text, produced for each enum value\n";
144 print STDERR " --vtail <text> value tail, produced after iterating over enum values\n";
145 print STDERR " --comments <text> comment structure\n";
146 print STDERR " -h, --help show this help message\n";
147 print STDERR " -v, --version print version informations\n";
148 print STDERR "Production text substitutions:\n";
149 print STDERR " \@EnumName\@ PrefixTheXEnum\n";
150 print STDERR " \@enum_name\@ prefix_the_xenum\n";
151 print STDERR " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
152 print STDERR " \@ENUMSHORT\@ THE_XENUM\n";
153 print STDERR " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
154 print STDERR " \@valuenick\@ the-xvalue\n";
155 print STDERR " \@type\@ either enum or flags\n";
156 print STDERR " \@Type\@ either Enum or Flags\n";
157 print STDERR " \@TYPE\@ either ENUM or FLAGS\n";
158 print STDERR " \@filename\@ name of current input file\n";
162 # production variables:
163 my $fhead = ""; # output file header
164 my $fprod = ""; # per input file production
165 my $ftail = ""; # output file trailer
166 my $eprod = ""; # per enum text (produced prior to value itarations)
167 my $vhead = ""; # value header, produced before iterating over enum values
168 my $vprod = ""; # value text, produced for each enum value
169 my $vtail = ""; # value tail, produced after iterating over enum values
171 my $comment_tmpl = "/* \@comment\@ */";
173 if (!defined $ARGV[0]) {
176 while ($_ = $ARGV[0], /^-/) {
179 if (/^--fhead$/) { $fhead = $fhead . shift }
180 elsif (/^--fprod$/) { $fprod = $fprod . shift }
181 elsif (/^--ftail$/) { $ftail = $ftail . shift }
182 elsif (/^--eprod$/) { $eprod = $eprod . shift }
183 elsif (/^--vhead$/) { $vhead = $vhead . shift }
184 elsif (/^--vprod$/) { $vprod = $vprod . shift }
185 elsif (/^--vtail$/) { $vtail = $vtail . shift }
186 elsif (/^--comments$/) { $comment_tmpl = shift }
187 elsif (/^--help$/ || /^-h$/) { usage; }
188 elsif (/^--version$/ || /^-v$/) { version; }
192 # put auto-generation comment
194 my $comment = $comment_tmpl;
195 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
196 print "\n" . $comment . "\n\n";
199 if (length($fhead)) {
202 $prod =~ s/\@filename\@/$ARGV/g;
203 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
204 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
211 close (ARGV); # reset line numbering
212 $firstenum = 1; # Flag to print filename at next enum
215 # read lines until comment end is matched
216 while (m@/\*([^*]|\*[^/*])*\**$@x) {
218 defined ($new = <>) || die "Unmatched comment in $ARGV";
221 # strip comments w/o options
222 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
224 # print STDERR "xxx $_\n";
226 if (m@^\s*typedef\s+enum\s*
233 my %options = parse_trigraph ($2);
234 next if defined $options{skip};
235 $enum_prefix = $options{prefix};
236 $flags = $options{flags};
238 $enum_prefix = undef;
241 # Didn't have trailing '{' look on next lines
253 # Now parse the entries
254 parse_entries (\*ARGV, $ARGV);
256 # figure out if this was a flags or enums enumeration
257 if (!defined $flags) {
258 $flags = $seenbitshift;
261 # Autogenerate a prefix
262 if (!defined $enum_prefix) {
265 if (!defined $nick) {
267 if (defined $enum_prefix) {
268 my $tmp = ~ ($name ^ $enum_prefix);
269 ($tmp) = $tmp =~ /(^\xff*)/;
270 $enum_prefix = $enum_prefix & $tmp;
272 $enum_prefix = $name;
276 if (!defined $enum_prefix) {
279 # Trim so that it ends in an underscore
280 $enum_prefix =~ s/_[^_]*$/_/;
283 # canonicalize user defined prefixes
284 $enum_prefix = uc($enum_prefix);
285 $enum_prefix =~ s/-/_/g;
286 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
289 for $entry (@entries) {
290 my ($name,$nick) = @{$entry};
291 if (!defined $nick) {
292 ($nick = $name) =~ s/^$enum_prefix//;
295 @{$entry} = ($name, $nick);
300 # Spit out the output
302 # enumname is e.g. GMatchType
303 $enspace = $enumname;
304 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
306 $enumshort = $enumname;
307 $enumshort =~ s/^[A-Z][a-z]*//;
308 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
309 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
310 $enumshort = uc($enumshort);
312 $enumlong = uc($enspace) . "_" . $enumshort;
313 $enumsym = lc($enspace) . "_" . lc($enumshort);
318 if (length($fprod)) {
321 $prod =~ s/\@filename\@/$ARGV/g;
322 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
323 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
329 if (length($eprod)) {
332 $prod =~ s/\@enum_name\@/$enumsym/g;
333 $prod =~ s/\@EnumName\@/$enumname/g;
334 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
335 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
336 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
337 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
338 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
339 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
340 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
345 if (length($vhead)) {
348 $prod =~ s/\@enum_name\@/$enumsym/g;
349 $prod =~ s/\@EnumName\@/$enumname/g;
350 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
351 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
352 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
353 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
354 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
355 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
356 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
361 if (length($vprod)) {
364 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
365 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
367 my ($name,$nick) = @{$_};
368 my $tmp_prod = $vprod;
370 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
371 $tmp_prod =~ s/\@valuenick\@/$nick/g;
372 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
373 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
374 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
380 if (length($vtail)) {
383 $prod =~ s/\@enum_name\@/$enumsym/g;
384 $prod =~ s/\@EnumName\@/$enumname/g;
385 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
386 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
387 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
388 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
389 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
390 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
391 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
398 if (length($ftail)) {
401 $prod =~ s/\@filename\@/$ARGV/g;
402 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
403 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
408 # put auto-generation comment
410 my $comment = $comment_tmpl;
411 $comment =~ s/\@comment\@/Generated data ends here/;
412 print "\n" . $comment . "\n\n";