6 # Information about the current enumeration
7 my $flags; # Is enumeration a bitmask?
8 my $option_underscore_name; # Overriden underscore variant of the enum name
9 # for example to fix the cases we don't get the
10 # mixed-case -> underscorized transform right.
11 my $option_lowercase_name; # DEPRECATED. A lower case name to use as part
12 # of the *_get_type() function, instead of the
13 # one that we guess. For instance, when an enum
14 # uses abnormal capitalization and we can not
15 # guess where to put the underscores.
16 my $seenbitshift; # Have we seen bitshift operators?
17 my $enum_prefix; # Prefix for this enumeration
18 my $enumname; # Name for this enumeration
19 my $enumshort; # $enumname without prefix
20 my $enumname_prefix; # prefix of $enumname
21 my $enumindex = 0; # Global enum counter
22 my $firstenum = 1; # Is this the first enumeration per file?
23 my @entries; # [ $name, $val ] for each entry
29 for $opt (split /\s*,\s*/, $opts) {
32 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
33 defined $val or $val = 1;
34 push @opts, $key, $val;
40 my $file_name = shift;
41 my $looking_for_name = 0;
44 # read lines until we have no open comments
45 while (m@/\*([^*]|\*(?!/))*$@) {
47 defined ($new = <$file>) || die "Unmatched comment in $ARGV";
50 # strip comments w/o options
61 if ($looking_for_name) {
68 # Handle include files
69 if (/^\#include\s*<([^>]*)>/ ) {
71 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
73 if (parse_entries (\*NEWFILE, $NEWFILE)) {
80 if (/^\s*\}\s*(\w+)/) {
88 $looking_for_name = 1;
95 \s*\w+\s*\(.*\)\s* # macro with multiple args
97 (?:[^,/]|/(?!\*))* # anything but a comma or comment
104 my ($name, $value, $options) = ($1,$2,$3);
106 if (!defined $flags && defined $value && $value =~ /<</) {
110 if (defined $options) {
111 my %options = parse_trigraph($options);
112 if (!defined $options{skip}) {
113 push @entries, [ $name, $options{nick} ];
116 push @entries, [ $name ];
118 } elsif (m@^\s*\#@) {
119 # ignore preprocessor directives
121 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
129 print "glib-mkenums version glib-@GLIB_VERSION@\n";
130 print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
131 print "You may redistribute copies of glib-mkenums under the terms of\n";
132 print "the GNU General Public License which can be found in the\n";
133 print "GLib source package. Sources, examples and contact\n";
134 print "information are available at http://www.gtk.org\n";
139 print " glib-mkenums [OPTION...] [FILES...]\n\n";
140 print "Help Options:\n";
141 print " -h, --help Show this help message\n\n";
142 print "Utility Options:\n";
143 print " --fhead <text> Output file header\n";
144 print " --fprod <text> Per input file production\n";
145 print " --ftail <text> Output file trailer\n";
146 print " --eprod <text> Per enum text (produced prior to value itarations)\n";
147 print " --vhead <text> Value header, produced before iterating over enum values\n";
148 print " --vprod <text> Value text, produced for each enum value\n";
149 print " --vtail <text> Value tail, produced after iterating over enum values\n";
150 print " --comments <text> Comment structure\n";
151 print " --template file Template file\n";
152 print " -v, --version Print version informations\n\n";
153 print "Production text substitutions:\n";
154 print " \@EnumName\@ PrefixTheXEnum\n";
155 print " \@enum_name\@ prefix_the_xenum\n";
156 print " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
157 print " \@ENUMSHORT\@ THE_XENUM\n";
158 print " \@ENUMPREFIX\@ PREFIX\n";
159 print " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
160 print " \@valuenick\@ the-xvalue\n";
161 print " \@type\@ either enum or flags\n";
162 print " \@Type\@ either Enum or Flags\n";
163 print " \@TYPE\@ either ENUM or FLAGS\n";
164 print " \@filename\@ name of current input file\n";
165 print " \@basename\@ base name of the current input file (Since: 2.22)\n";
169 # production variables:
170 my $fhead = ""; # output file header
171 my $fprod = ""; # per input file production
172 my $ftail = ""; # output file trailer
173 my $eprod = ""; # per enum text (produced prior to value itarations)
174 my $vhead = ""; # value header, produced before iterating over enum values
175 my $vprod = ""; # value text, produced for each enum value
176 my $vtail = ""; # value tail, produced after iterating over enum values
178 my $comment_tmpl = "/* \@comment\@ */";
180 sub read_template_file {
182 my %tmpl = ('file-header', $fhead,
183 'file-production', $fprod,
185 'enumeration-production', $eprod,
186 'value-header', $vhead,
187 'value-production', $vprod,
188 'value-tail', $vtail,
189 'comment', $comment_tmpl);
191 open (FILE, $file) || die "Can't open $file: $!\n";
193 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
194 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
198 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
202 die "Malformed template file $file\n";
205 if (!($in eq 'junk')) {
210 if (!($in eq 'junk')) {
211 die "Malformed template file $file\n";
213 $fhead = $tmpl{'file-header'};
214 $fprod = $tmpl{'file-production'};
215 $ftail = $tmpl{'file-tail'};
216 $eprod = $tmpl{'enumeration-production'};
217 $vhead = $tmpl{'value-header'};
218 $vprod = $tmpl{'value-production'};
219 $vtail = $tmpl{'value-tail'};
220 $comment_tmpl = $tmpl{'comment'};
223 if (!defined $ARGV[0]) {
226 while ($_=$ARGV[0],/^-/) {
229 if (/^--template$/) { read_template_file (shift); }
230 elsif (/^--fhead$/) { $fhead = $fhead . shift }
231 elsif (/^--fprod$/) { $fprod = $fprod . shift }
232 elsif (/^--ftail$/) { $ftail = $ftail . shift }
233 elsif (/^--eprod$/) { $eprod = $eprod . shift }
234 elsif (/^--vhead$/) { $vhead = $vhead . shift }
235 elsif (/^--vprod$/) { $vprod = $vprod . shift }
236 elsif (/^--vtail$/) { $vtail = $vtail . shift }
237 elsif (/^--comments$/) { $comment_tmpl = shift }
238 elsif (/^--help$/ || /^-h$/ || /^-\?$/) { usage; }
239 elsif (/^--version$/ || /^-v$/) { version; }
241 last if not defined($ARGV[0]);
244 # put auto-generation comment
246 my $comment = $comment_tmpl;
247 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
248 print "\n" . $comment . "\n\n";
251 if (length($fhead)) {
253 my $base = basename ($ARGV[0]);
255 $prod =~ s/\@filename\@/$ARGV[0]/g;
256 $prod =~ s/\@basename\@/$base/g;
257 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
258 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
266 close (ARGV); # reset line numbering
267 $firstenum = 1; # Flag to print filename at next enum
270 # read lines until we have no open comments
271 while (m@/\*([^*]|\*(?!/))*$@) {
273 defined ($new = <>) || die "Unmatched comment in $ARGV";
276 # strip comments w/o options
281 if (m@^\s*typedef\s+enum\s*
289 my %options = parse_trigraph ($2);
290 next if defined $options{skip};
291 $enum_prefix = $options{prefix};
292 $flags = $options{flags};
293 $option_lowercase_name = $options{lowercase_name};
294 $option_underscore_name = $options{underscore_name};
296 $enum_prefix = undef;
298 $option_lowercase_name = undef;
299 $option_underscore_name = undef;
301 if (defined $option_lowercase_name) {
302 if (defined $option_underscore_name) {
303 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
304 $option_lowercase_name = undef;
306 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
309 # Didn't have trailing '{' look on next lines
310 if (!defined $1 && !defined $4) {
321 # Now parse the entries
322 parse_entries (\*ARGV, $ARGV);
324 # figure out if this was a flags or enums enumeration
325 if (!defined $flags) {
326 $flags = $seenbitshift;
329 # Autogenerate a prefix
330 if (!defined $enum_prefix) {
333 if (!defined $nick) {
335 if (defined $enum_prefix) {
336 my $tmp = ~ ($name ^ $enum_prefix);
337 ($tmp) = $tmp =~ /(^\xff*)/;
338 $enum_prefix = $enum_prefix & $tmp;
340 $enum_prefix = $name;
344 if (!defined $enum_prefix) {
347 # Trim so that it ends in an underscore
348 $enum_prefix =~ s/_[^_]*$/_/;
351 # canonicalize user defined prefixes
352 $enum_prefix = uc($enum_prefix);
353 $enum_prefix =~ s/-/_/g;
354 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
357 for $entry (@entries) {
358 my ($name,$nick) = @{$entry};
359 if (!defined $nick) {
360 ($nick = $name) =~ s/^$enum_prefix//;
363 @{$entry} = ($name, $nick);
368 # Spit out the output
369 if (defined $option_underscore_name) {
370 $enumlong = uc $option_underscore_name;
371 $enumsym = lc $option_underscore_name;
372 $enumshort = $enumlong;
373 $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
375 $enumname_prefix = $enumlong;
376 $enumname_prefix =~ s/$enumshort$//;
378 # enumname is e.g. GMatchType
379 $enspace = $enumname;
380 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
382 $enumshort = $enumname;
383 $enumshort =~ s/^[A-Z][a-z]*//;
384 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
385 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
386 $enumshort = uc($enumshort);
388 $enumname_prefix = $enumname;
389 $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
390 $enumname_prefix = uc($enumname_prefix);
392 $enumlong = uc($enspace) . "_" . $enumshort;
393 $enumsym = lc($enspace) . "_" . lc($enumshort);
395 if (defined($option_lowercase_name)) {
396 $enumsym = $option_lowercase_name;
403 if (length($fprod)) {
405 my $base = basename ($ARGV);
407 $prod =~ s/\@filename\@/$ARGV/g;
408 $prod =~ s/\@basename\@/$base/g;
409 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
410 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
417 if (length($eprod)) {
420 $prod =~ s/\@enum_name\@/$enumsym/g;
421 $prod =~ s/\@EnumName\@/$enumname/g;
422 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
423 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
424 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
425 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
426 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
427 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
428 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
429 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
435 if (length($vhead)) {
438 $prod =~ s/\@enum_name\@/$enumsym/g;
439 $prod =~ s/\@EnumName\@/$enumname/g;
440 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
441 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
442 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
443 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
444 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
445 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
446 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
447 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
453 if (length($vprod)) {
456 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
457 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
459 my ($name,$nick) = @{$_};
460 my $tmp_prod = $prod;
462 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
463 $tmp_prod =~ s/\@valuenick\@/$nick/g;
464 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
465 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
466 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
473 if (length($vtail)) {
476 $prod =~ s/\@enum_name\@/$enumsym/g;
477 $prod =~ s/\@EnumName\@/$enumname/g;
478 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
479 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
480 $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
481 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
482 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
483 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
484 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
485 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
493 if (length($ftail)) {
495 my $base = basename ($ARGV);
497 $prod =~ s/\@filename\@/$ARGV/g;
498 $prod =~ s/\@basename\@/$base/g;
499 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
500 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
506 # put auto-generation comment
508 my $comment = $comment_tmpl;
509 $comment =~ s/\@comment\@/Generated data ends here/;
510 print "\n" . $comment . "\n\n";