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 " --identifier-prefix <text> Identifier prefix\n";
147 print " --symbol-prefix <text> Symbol prefix\n";
148 print " --fhead <text> Output file header\n";
149 print " --fprod <text> Per input file production\n";
150 print " --ftail <text> Output file trailer\n";
151 print " --eprod <text> Per enum text (produced prior to value itarations)\n";
152 print " --vhead <text> Value header, produced before iterating over enum values\n";
153 print " --vprod <text> Value text, produced for each enum value\n";
154 print " --vtail <text> Value tail, produced after iterating over enum values\n";
155 print " --comments <text> Comment structure\n";
156 print " --template file Template file\n";
157 print " -v, --version Print version informations\n\n";
158 print "Production text substitutions:\n";
159 print " \@EnumName\@ PrefixTheXEnum\n";
160 print " \@enum_name\@ prefix_the_xenum\n";
161 print " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
162 print " \@ENUMSHORT\@ THE_XENUM\n";
163 print " \@ENUMPREFIX\@ PREFIX\n";
164 print " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
165 print " \@valuenick\@ the-xvalue\n";
166 print " \@valuenum\@ the integer value (limited support, Since: 2.26)\n";
167 print " \@type\@ either enum or flags\n";
168 print " \@Type\@ either Enum or Flags\n";
169 print " \@TYPE\@ either ENUM or FLAGS\n";
170 print " \@filename\@ name of current input file\n";
171 print " \@basename\@ base name of the current input file (Since: 2.22)\n";
175 # production variables:
176 my $idprefix = ""; # "G", "Gtk", etc
177 my $symprefix = ""; # "g", "gtk", etc, if not just lc($idprefix)
178 my $fhead = ""; # output file header
179 my $fprod = ""; # per input file production
180 my $ftail = ""; # output file trailer
181 my $eprod = ""; # per enum text (produced prior to value itarations)
182 my $vhead = ""; # value header, produced before iterating over enum values
183 my $vprod = ""; # value text, produced for each enum value
184 my $vtail = ""; # value tail, produced after iterating over enum values
185 my $comment_tmpl = ""; # comment template
187 sub read_template_file {
189 my %tmpl = ('file-header', $fhead,
190 'file-production', $fprod,
192 'enumeration-production', $eprod,
193 'value-header', $vhead,
194 'value-production', $vprod,
195 'value-tail', $vtail,
196 'comment', $comment_tmpl);
198 open (FILE, $file) || die "Can't open $file: $!\n";
200 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
201 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
205 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
209 die "Malformed template file $file\n";
212 if (!($in eq 'junk')) {
217 if (!($in eq 'junk')) {
218 die "Malformed template file $file\n";
220 $fhead = $tmpl{'file-header'};
221 $fprod = $tmpl{'file-production'};
222 $ftail = $tmpl{'file-tail'};
223 $eprod = $tmpl{'enumeration-production'};
224 $vhead = $tmpl{'value-header'};
225 $vprod = $tmpl{'value-production'};
226 $vtail = $tmpl{'value-tail'};
227 $comment_tmpl = $tmpl{'comment'};
229 # default to C-style comments
230 $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
233 if (!defined $ARGV[0]) {
236 while ($_=$ARGV[0],/^-/) {
239 if (/^--template$/) { read_template_file (shift); }
240 elsif (/^--identifier-prefix$/) { $idprefix = shift }
241 elsif (/^--symbol-prefix$/) { $symprefix = shift }
242 elsif (/^--fhead$/) { $fhead = $fhead . shift }
243 elsif (/^--fprod$/) { $fprod = $fprod . shift }
244 elsif (/^--ftail$/) { $ftail = $ftail . shift }
245 elsif (/^--eprod$/) { $eprod = $eprod . shift }
246 elsif (/^--vhead$/) { $vhead = $vhead . shift }
247 elsif (/^--vprod$/) { $vprod = $vprod . shift }
248 elsif (/^--vtail$/) { $vtail = $vtail . shift }
249 elsif (/^--comments$/) { $comment_tmpl = shift }
250 elsif (/^--help$/ || /^-h$/ || /^-\?$/) { usage; }
251 elsif (/^--version$/ || /^-v$/) { version; }
253 last if not defined($ARGV[0]);
256 # put auto-generation comment
258 my $comment = $comment_tmpl;
259 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
260 print "\n" . $comment . "\n\n";
263 if (length($fhead)) {
265 my $base = basename ($ARGV[0]);
267 $prod =~ s/\@filename\@/$ARGV[0]/g;
268 $prod =~ s/\@basename\@/$base/g;
269 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
270 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
278 close (ARGV); # reset line numbering
279 $firstenum = 1; # Flag to print filename at next enum
282 # read lines until we have no open comments
283 while (m@/\*([^*]|\*(?!/))*$@) {
285 defined ($new = <>) || die "Unmatched comment in $ARGV";
288 # strip comments w/o options
293 # ignore forward declarations
294 next if /^\s*typedef\s+enum.*;/;
296 if (m@^\s*typedef\s+enum\s*
304 my %options = parse_trigraph ($2);
305 next if defined $options{skip};
306 $enum_prefix = $options{prefix};
307 $flags = $options{flags};
308 $option_lowercase_name = $options{lowercase_name};
309 $option_underscore_name = $options{underscore_name};
311 $enum_prefix = undef;
313 $option_lowercase_name = undef;
314 $option_underscore_name = undef;
316 if (defined $option_lowercase_name) {
317 if (defined $option_underscore_name) {
318 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
319 $option_lowercase_name = undef;
321 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
324 # Didn't have trailing '{' look on next lines
325 if (!defined $1 && !defined $4) {
328 die "Hit end of file while parsing enum in $ARGV";
339 # Now parse the entries
340 parse_entries (\*ARGV, $ARGV);
342 # figure out if this was a flags or enums enumeration
343 if (!defined $flags) {
344 $flags = $seenbitshift;
347 # Autogenerate a prefix
348 if (!defined $enum_prefix) {
351 if (!defined $nick) {
353 if (defined $enum_prefix) {
354 my $tmp = ~ ($name ^ $enum_prefix);
355 ($tmp) = $tmp =~ /(^\xff*)/;
356 $enum_prefix = $enum_prefix & $tmp;
358 $enum_prefix = $name;
362 if (!defined $enum_prefix) {
365 # Trim so that it ends in an underscore
366 $enum_prefix =~ s/_[^_]*$/_/;
369 # canonicalize user defined prefixes
370 $enum_prefix = uc($enum_prefix);
371 $enum_prefix =~ s/-/_/g;
372 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
375 for $entry (@entries) {
376 my ($name,$num,$nick) = @{$entry};
377 if (!defined $nick) {
378 ($nick = $name) =~ s/^$enum_prefix//;
381 @{$entry} = ($name, $num, $nick);
386 # Spit out the output
387 if (defined $option_underscore_name) {
388 $enumlong = uc $option_underscore_name;
389 $enumsym = lc $option_underscore_name;
390 $enumshort = $enumlong;
391 $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
393 $enumname_prefix = $enumlong;
394 $enumname_prefix =~ s/_$enumshort$//;
395 } elsif (!$symprefix && !$idprefix) {
396 # enumname is e.g. GMatchType
397 $enspace = $enumname;
398 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
400 $enumshort = $enumname;
401 $enumshort =~ s/^[A-Z][a-z]*//;
402 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
403 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
404 $enumshort = uc($enumshort);
406 $enumname_prefix = $enumname;
407 $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
408 $enumname_prefix = uc($enumname_prefix);
410 $enumlong = uc($enspace) . "_" . $enumshort;
411 $enumsym = lc($enspace) . "_" . lc($enumshort);
413 if (defined($option_lowercase_name)) {
414 $enumsym = $option_lowercase_name;
417 $enumshort = $enumname;
419 $enumshort =~ s/^${idprefix}//;
421 $enumshort =~ s/^[A-Z][a-z]*//;
423 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
424 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
425 $enumshort = uc($enumshort);
427 $enumname_prefix = $symprefix && uc($symprefix) || uc($idprefix);
429 $enumlong = $enumname_prefix . "_" . $enumshort;
430 $enumsym = lc($enumlong);
436 if (length($fprod)) {
438 my $base = basename ($ARGV);
440 $prod =~ s/\@filename\@/$ARGV/g;
441 $prod =~ s/\@basename\@/$base/g;
442 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
443 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
450 if (length($eprod)) {
453 $prod =~ s/\@enum_name\@/$enumsym/g;
454 $prod =~ s/\@EnumName\@/$enumname/g;
455 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
456 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
457 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
458 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
459 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
460 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
461 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
462 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
468 if (length($vhead)) {
471 $prod =~ s/\@enum_name\@/$enumsym/g;
472 $prod =~ s/\@EnumName\@/$enumname/g;
473 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
474 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
475 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
476 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
477 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
478 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
479 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
480 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
486 if (length($vprod)) {
490 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
491 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
493 my ($name,$num,$nick) = @{$_};
494 my $tmp_prod = $prod;
496 if ($prod =~ /\@valuenum\@/) {
497 # only attempt to eval the value if it is requested
498 # this prevents us from throwing errors otherwise
500 # use sandboxed perl evaluation as a reasonable
501 # approximation to C constant folding
502 $num = $sandbox->reval ($num);
504 # make sure it parsed to an integer
505 if (!defined $num or $num !~ /^-?\d+$/) {
506 die "Unable to parse enum value '$num'";
512 $tmp_prod =~ s/\@valuenum\@/$num/g;
513 $next_num = $num + 1;
516 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
517 $tmp_prod =~ s/\@valuenick\@/$nick/g;
518 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
519 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
520 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
527 if (length($vtail)) {
530 $prod =~ s/\@enum_name\@/$enumsym/g;
531 $prod =~ s/\@EnumName\@/$enumname/g;
532 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
533 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
534 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
535 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
536 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
537 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
538 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
539 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
547 if (length($ftail)) {
549 my $base = basename ($ARGV);
551 $prod =~ s/\@filename\@/$ARGV/g;
552 $prod =~ s/\@basename\@/$base/g;
553 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
554 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
560 # put auto-generation comment
562 my $comment = $comment_tmpl;
563 $comment =~ s/\@comment\@/Generated data ends here/;
564 print "\n" . $comment . "\n\n";