8 # Information about the current enumeration
9 my $flags; # Is enumeration a bitmask?
10 my $option_underscore_name; # Overriden underscore variant of the enum name
11 # for example to fix the cases we don't get the
12 # mixed-case -> underscorized transform right.
13 my $option_lowercase_name; # DEPRECATED. A lower case name to use as part
14 # of the *_get_type() function, instead of the
15 # one that we guess. For instance, when an enum
16 # uses abnormal capitalization and we can not
17 # guess where to put the underscores.
18 my $seenbitshift; # Have we seen bitshift operators?
19 my $enum_prefix; # Prefix for this enumeration
20 my $enumname; # Name for this enumeration
21 my $enumshort; # $enumname without prefix
22 my $enumname_prefix; # prefix of $enumname
23 my $enumindex = 0; # Global enum counter
24 my $firstenum = 1; # Is this the first enumeration per file?
25 my @entries; # [ $name, $val ] for each entry
26 my $sandbox = Safe->new; # sandbox for safe evaluation of expressions
32 for $opt (split /\s*,\s*/, $opts) {
35 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
36 defined $val or $val = 1;
37 push @opts, $key, $val;
43 my $file_name = shift;
44 my $looking_for_name = 0;
47 # read lines until we have no open comments
48 while (m@/\*([^*]|\*(?!/))*$@) {
50 defined ($new = <$file>) || die "Unmatched comment in $ARGV";
53 # strip comments w/o options
64 if ($looking_for_name) {
71 # Handle include files
72 if (/^\#include\s*<([^>]*)>/ ) {
74 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
76 if (parse_entries (\*NEWFILE, $NEWFILE)) {
83 if (/^\s*\}\s*(\w+)/) {
91 $looking_for_name = 1;
98 \s*\w+\s*\(.*\)\s* # macro with multiple args
100 (?:[^,/]|/(?!\*))* # anything but a comma or comment
107 my ($name, $value, $options) = ($1,$2,$3);
109 if (!defined $flags && defined $value && $value =~ /<</) {
113 if (defined $options) {
114 my %options = parse_trigraph($options);
115 if (!defined $options{skip}) {
116 push @entries, [ $name, $value, $options{nick} ];
119 push @entries, [ $name, $value ];
121 } elsif (m@^\s*\#@) {
122 # ignore preprocessor directives
124 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
132 print "glib-mkenums version glib-@GLIB_VERSION@\n";
133 print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
134 print "You may redistribute copies of glib-mkenums under the terms of\n";
135 print "the GNU General Public License which can be found in the\n";
136 print "GLib source package. Sources, examples and contact\n";
137 print "information are available at http://www.gtk.org\n";
142 print " glib-mkenums [OPTION...] [FILES...]\n\n";
143 print "Help Options:\n";
144 print " -h, --help Show this help message\n\n";
145 print "Utility Options:\n";
146 print " --fhead <text> Output file header\n";
147 print " --fprod <text> Per input file production\n";
148 print " --ftail <text> Output file trailer\n";
149 print " --eprod <text> Per enum text (produced prior to value itarations)\n";
150 print " --vhead <text> Value header, produced before iterating over enum values\n";
151 print " --vprod <text> Value text, produced for each enum value\n";
152 print " --vtail <text> Value tail, produced after iterating over enum values\n";
153 print " --comments <text> Comment structure\n";
154 print " --template file Template file\n";
155 print " -v, --version Print version informations\n\n";
156 print "Production text substitutions:\n";
157 print " \@EnumName\@ PrefixTheXEnum\n";
158 print " \@enum_name\@ prefix_the_xenum\n";
159 print " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
160 print " \@ENUMSHORT\@ THE_XENUM\n";
161 print " \@ENUMPREFIX\@ PREFIX\n";
162 print " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
163 print " \@valuenick\@ the-xvalue\n";
164 print " \@valuenum\@ the integer value (limited support, Since: 2.26)\n";
165 print " \@type\@ either enum or flags\n";
166 print " \@Type\@ either Enum or Flags\n";
167 print " \@TYPE\@ either ENUM or FLAGS\n";
168 print " \@filename\@ name of current input file\n";
169 print " \@basename\@ base name of the current input file (Since: 2.22)\n";
173 # production variables:
174 my $fhead = ""; # output file header
175 my $fprod = ""; # per input file production
176 my $ftail = ""; # output file trailer
177 my $eprod = ""; # per enum text (produced prior to value itarations)
178 my $vhead = ""; # value header, produced before iterating over enum values
179 my $vprod = ""; # value text, produced for each enum value
180 my $vtail = ""; # value tail, produced after iterating over enum values
181 my $comment_tmpl = ""; # comment template
183 sub read_template_file {
185 my %tmpl = ('file-header', $fhead,
186 'file-production', $fprod,
188 'enumeration-production', $eprod,
189 'value-header', $vhead,
190 'value-production', $vprod,
191 'value-tail', $vtail,
192 'comment', $comment_tmpl);
194 open (FILE, $file) || die "Can't open $file: $!\n";
196 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
197 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
201 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
205 die "Malformed template file $file\n";
208 if (!($in eq 'junk')) {
213 if (!($in eq 'junk')) {
214 die "Malformed template file $file\n";
216 $fhead = $tmpl{'file-header'};
217 $fprod = $tmpl{'file-production'};
218 $ftail = $tmpl{'file-tail'};
219 $eprod = $tmpl{'enumeration-production'};
220 $vhead = $tmpl{'value-header'};
221 $vprod = $tmpl{'value-production'};
222 $vtail = $tmpl{'value-tail'};
223 $comment_tmpl = $tmpl{'comment'};
225 # default to C-style comments
226 $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
229 if (!defined $ARGV[0]) {
232 while ($_=$ARGV[0],/^-/) {
235 if (/^--template$/) { read_template_file (shift); }
236 elsif (/^--fhead$/) { $fhead = $fhead . shift }
237 elsif (/^--fprod$/) { $fprod = $fprod . shift }
238 elsif (/^--ftail$/) { $ftail = $ftail . shift }
239 elsif (/^--eprod$/) { $eprod = $eprod . shift }
240 elsif (/^--vhead$/) { $vhead = $vhead . shift }
241 elsif (/^--vprod$/) { $vprod = $vprod . shift }
242 elsif (/^--vtail$/) { $vtail = $vtail . shift }
243 elsif (/^--comments$/) { $comment_tmpl = shift }
244 elsif (/^--help$/ || /^-h$/ || /^-\?$/) { usage; }
245 elsif (/^--version$/ || /^-v$/) { version; }
247 last if not defined($ARGV[0]);
250 # put auto-generation comment
252 my $comment = $comment_tmpl;
253 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
254 print "\n" . $comment . "\n\n";
257 if (length($fhead)) {
259 my $base = basename ($ARGV[0]);
261 $prod =~ s/\@filename\@/$ARGV[0]/g;
262 $prod =~ s/\@basename\@/$base/g;
263 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
264 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
272 close (ARGV); # reset line numbering
273 $firstenum = 1; # Flag to print filename at next enum
276 # read lines until we have no open comments
277 while (m@/\*([^*]|\*(?!/))*$@) {
279 defined ($new = <>) || die "Unmatched comment in $ARGV";
282 # strip comments w/o options
287 if (m@^\s*typedef\s+enum\s*
295 my %options = parse_trigraph ($2);
296 next if defined $options{skip};
297 $enum_prefix = $options{prefix};
298 $flags = $options{flags};
299 $option_lowercase_name = $options{lowercase_name};
300 $option_underscore_name = $options{underscore_name};
302 $enum_prefix = undef;
304 $option_lowercase_name = undef;
305 $option_underscore_name = undef;
307 if (defined $option_lowercase_name) {
308 if (defined $option_underscore_name) {
309 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
310 $option_lowercase_name = undef;
312 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
315 # Didn't have trailing '{' look on next lines
316 if (!defined $1 && !defined $4) {
327 # Now parse the entries
328 parse_entries (\*ARGV, $ARGV);
330 # figure out if this was a flags or enums enumeration
331 if (!defined $flags) {
332 $flags = $seenbitshift;
335 # Autogenerate a prefix
336 if (!defined $enum_prefix) {
339 if (!defined $nick) {
341 if (defined $enum_prefix) {
342 my $tmp = ~ ($name ^ $enum_prefix);
343 ($tmp) = $tmp =~ /(^\xff*)/;
344 $enum_prefix = $enum_prefix & $tmp;
346 $enum_prefix = $name;
350 if (!defined $enum_prefix) {
353 # Trim so that it ends in an underscore
354 $enum_prefix =~ s/_[^_]*$/_/;
357 # canonicalize user defined prefixes
358 $enum_prefix = uc($enum_prefix);
359 $enum_prefix =~ s/-/_/g;
360 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
363 for $entry (@entries) {
364 my ($name,$num,$nick) = @{$entry};
365 if (!defined $nick) {
366 ($nick = $name) =~ s/^$enum_prefix//;
369 @{$entry} = ($name, $num, $nick);
374 # Spit out the output
375 if (defined $option_underscore_name) {
376 $enumlong = uc $option_underscore_name;
377 $enumsym = lc $option_underscore_name;
378 $enumshort = $enumlong;
379 $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
381 $enumname_prefix = $enumlong;
382 $enumname_prefix =~ s/$enumshort$//;
384 # enumname is e.g. GMatchType
385 $enspace = $enumname;
386 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
388 $enumshort = $enumname;
389 $enumshort =~ s/^[A-Z][a-z]*//;
390 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
391 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
392 $enumshort = uc($enumshort);
394 $enumname_prefix = $enumname;
395 $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
396 $enumname_prefix = uc($enumname_prefix);
398 $enumlong = uc($enspace) . "_" . $enumshort;
399 $enumsym = lc($enspace) . "_" . lc($enumshort);
401 if (defined($option_lowercase_name)) {
402 $enumsym = $option_lowercase_name;
409 if (length($fprod)) {
411 my $base = basename ($ARGV);
413 $prod =~ s/\@filename\@/$ARGV/g;
414 $prod =~ s/\@basename\@/$base/g;
415 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
416 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
423 if (length($eprod)) {
426 $prod =~ s/\@enum_name\@/$enumsym/g;
427 $prod =~ s/\@EnumName\@/$enumname/g;
428 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
429 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
430 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
431 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
432 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
433 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
434 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
435 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
441 if (length($vhead)) {
444 $prod =~ s/\@enum_name\@/$enumsym/g;
445 $prod =~ s/\@EnumName\@/$enumname/g;
446 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
447 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
448 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
449 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
450 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
451 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
452 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
453 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
459 if (length($vprod)) {
463 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
464 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
466 my ($name,$num,$nick) = @{$_};
467 my $tmp_prod = $prod;
469 if ($prod =~ /\@valuenum\@/) {
470 # only attempt to eval the value if it is requested
471 # this prevents us from throwing errors otherwise
473 # use sandboxed perl evaluation as a reasonable
474 # approximation to C constant folding
475 $num = $sandbox->reval ($num);
477 # make sure it parsed to an integer
478 if (!defined $num or $num !~ /^-?\d+$/) {
479 die "Unable to parse enum value '$num'";
485 $tmp_prod =~ s/\@valuenum\@/$num/g;
486 $next_num = $num + 1;
489 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
490 $tmp_prod =~ s/\@valuenick\@/$nick/g;
491 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
492 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
493 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
500 if (length($vtail)) {
503 $prod =~ s/\@enum_name\@/$enumsym/g;
504 $prod =~ s/\@EnumName\@/$enumname/g;
505 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
506 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
507 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
508 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
509 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
510 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
511 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
512 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
520 if (length($ftail)) {
522 my $base = basename ($ARGV);
524 $prod =~ s/\@filename\@/$ARGV/g;
525 $prod =~ s/\@basename\@/$base/g;
526 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
527 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
533 # put auto-generation comment
535 my $comment = $comment_tmpl;
536 $comment =~ s/\@comment\@/Generated data ends here/;
537 print "\n" . $comment . "\n\n";