4 # Information about the current enumeration
5 my $flags; # Is enumeration a bitmask?
6 my $option_underscore_name; # Overriden underscore variant of the enum name
7 # for example to fix the cases we don't get the
8 # mixed-case -> underscorized transform right.
9 my $option_lowercase_name; # DEPRECATED. A lower case name to use as part
10 # of the *_get_type() function, instead of the
11 # one that we guess. For instance, when an enum
12 # uses abnormal capitalization and we can not
13 # guess where to put the underscores.
14 my $seenbitshift; # Have we seen bitshift operators?
15 my $enum_prefix; # Prefix for this enumeration
16 my $enumname; # Name for this enumeration
17 my $enumshort; # $enumname without prefix
18 my $enumindex = 0; # Global enum counter
19 my $firstenum = 1; # Is this the first enumeration per file?
20 my @entries; # [ $name, $val ] for each entry
26 for $opt (split /\s*,\s*/, $opts) {
29 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
30 defined $val or $val = 1;
31 push @opts, $key, $val;
37 my $file_name = shift;
38 my $looking_for_name = 0;
41 # read lines until we have no open comments
42 while (m@/\*([^*]|\*(?!/))*$@) {
44 defined ($new = <$file>) || die "Unmatched comment in $ARGV";
47 # strip comments w/o options
58 if ($looking_for_name) {
65 # Handle include files
66 if (/^\#include\s*<([^>]*)>/ ) {
68 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
70 if (parse_entries (\*NEWFILE, $NEWFILE)) {
77 if (/^\s*\}\s*(\w+)/) {
85 $looking_for_name = 1;
92 \s*\w+\s*\(.*\)\s* # macro with multiple args
94 (?:[^,/]|/(?!\*))* # anything but a comma or comment
101 my ($name, $value, $options) = ($1,$2,$3);
103 if (!defined $flags && defined $value && $value =~ /<</) {
107 if (defined $options) {
108 my %options = parse_trigraph($options);
109 if (!defined $options{skip}) {
110 push @entries, [ $name, $options{nick} ];
113 push @entries, [ $name ];
115 } elsif (m@^\s*\#@) {
116 # ignore preprocessor directives
118 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
126 print "glib-mkenums version glib-@GLIB_VERSION@\n";
127 print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
128 print "You may redistribute copies of glib-mkenums under the terms of\n";
129 print "the GNU General Public License which can be found in the\n";
130 print "GLib source package. Sources, examples and contact\n";
131 print "information are available at http://www.gtk.org\n";
135 print "Usage: glib-mkenums [options] [files...]\n";
136 print " --fhead <text> output file header\n";
137 print " --fprod <text> per input file production\n";
138 print " --ftail <text> output file trailer\n";
139 print " --eprod <text> per enum text (produced prior to value itarations)\n";
140 print " --vhead <text> value header, produced before iterating over enum values\n";
141 print " --vprod <text> value text, produced for each enum value\n";
142 print " --vtail <text> value tail, produced after iterating over enum values\n";
143 print " --comments <text> comment structure\n";
144 print " --template file template file\n";
145 print " -h, --help show this help message\n";
146 print " -v, --version print version informations\n";
147 print "Production text substitutions:\n";
148 print " \@EnumName\@ PrefixTheXEnum\n";
149 print " \@enum_name\@ prefix_the_xenum\n";
150 print " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
151 print " \@ENUMSHORT\@ THE_XENUM\n";
152 print " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
153 print " \@valuenick\@ the-xvalue\n";
154 print " \@type\@ either enum or flags\n";
155 print " \@Type\@ either Enum or Flags\n";
156 print " \@TYPE\@ either ENUM or FLAGS\n";
157 print " \@filename\@ name of current input file\n";
161 # production variables:
162 my $fhead = ""; # output file header
163 my $fprod = ""; # per input file production
164 my $ftail = ""; # output file trailer
165 my $eprod = ""; # per enum text (produced prior to value itarations)
166 my $vhead = ""; # value header, produced before iterating over enum values
167 my $vprod = ""; # value text, produced for each enum value
168 my $vtail = ""; # value tail, produced after iterating over enum values
170 my $comment_tmpl = "/* \@comment\@ */";
172 sub read_template_file {
174 my %tmpl = ('file-header', $fhead,
175 'file-production', $fprod,
177 'enumeration-production', $eprod,
178 'value-header', $vhead,
179 'value-production', $vprod,
180 'value-tail', $vtail,
181 'comment', $comment_tmpl);
183 open (FILE, $file) || die "Can't open $file: $!\n";
185 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
186 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
190 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
194 die "Malformed template file $file\n";
197 if (!($in eq 'junk')) {
202 if (!($in eq 'junk')) {
203 die "Malformed template file $file\n";
205 $fhead = $tmpl{'file-header'};
206 $fprod = $tmpl{'file-production'};
207 $ftail = $tmpl{'file-tail'};
208 $eprod = $tmpl{'enumeration-production'};
209 $vhead = $tmpl{'value-header'};
210 $vprod = $tmpl{'value-production'};
211 $vtail = $tmpl{'value-tail'};
212 $comment_tmpl = $tmpl{'comment'};
215 if (!defined $ARGV[0]) {
218 while ($_ = $ARGV[0], /^-/) {
221 if (/^--template$/) { read_template_file (shift); }
222 elsif (/^--fhead$/) { $fhead = $fhead . shift }
223 elsif (/^--fprod$/) { $fprod = $fprod . shift }
224 elsif (/^--ftail$/) { $ftail = $ftail . shift }
225 elsif (/^--eprod$/) { $eprod = $eprod . shift }
226 elsif (/^--vhead$/) { $vhead = $vhead . shift }
227 elsif (/^--vprod$/) { $vprod = $vprod . shift }
228 elsif (/^--vtail$/) { $vtail = $vtail . shift }
229 elsif (/^--comments$/) { $comment_tmpl = shift }
230 elsif (/^--help$/ || /^-h$/) { usage; }
231 elsif (/^--version$/ || /^-v$/) { version; }
235 # put auto-generation comment
237 my $comment = $comment_tmpl;
238 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
239 print "\n" . $comment . "\n\n";
242 if (length($fhead)) {
245 $prod =~ s/\@filename\@/$ARGV[0]/g;
246 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
247 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
255 close (ARGV); # reset line numbering
256 $firstenum = 1; # Flag to print filename at next enum
259 # read lines until we have no open comments
260 while (m@/\*([^*]|\*(?!/))*$@) {
262 defined ($new = <>) || die "Unmatched comment in $ARGV";
265 # strip comments w/o options
270 if (m@^\s*typedef\s+enum\s*
278 my %options = parse_trigraph ($2);
279 next if defined $options{skip};
280 $enum_prefix = $options{prefix};
281 $flags = $options{flags};
282 $option_lowercase_name = $options{lowercase_name};
283 $option_underscore_name = $options{underscore_name};
285 $enum_prefix = undef;
287 $option_lowercase_name = undef;
288 $option_underscore_name = undef;
290 if (defined $option_lowercase_name) {
291 if (defined $option_underscore_name) {
292 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
293 $option_lowercase_name = undef;
295 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
298 # Didn't have trailing '{' look on next lines
299 if (!defined $1 && !defined $4) {
310 # Now parse the entries
311 parse_entries (\*ARGV, $ARGV);
313 # figure out if this was a flags or enums enumeration
314 if (!defined $flags) {
315 $flags = $seenbitshift;
318 # Autogenerate a prefix
319 if (!defined $enum_prefix) {
322 if (!defined $nick) {
324 if (defined $enum_prefix) {
325 my $tmp = ~ ($name ^ $enum_prefix);
326 ($tmp) = $tmp =~ /(^\xff*)/;
327 $enum_prefix = $enum_prefix & $tmp;
329 $enum_prefix = $name;
333 if (!defined $enum_prefix) {
336 # Trim so that it ends in an underscore
337 $enum_prefix =~ s/_[^_]*$/_/;
340 # canonicalize user defined prefixes
341 $enum_prefix = uc($enum_prefix);
342 $enum_prefix =~ s/-/_/g;
343 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
346 for $entry (@entries) {
347 my ($name,$nick) = @{$entry};
348 if (!defined $nick) {
349 ($nick = $name) =~ s/^$enum_prefix//;
352 @{$entry} = ($name, $nick);
357 # Spit out the output
358 if (defined $option_underscore_name) {
359 $enumlong = uc $option_underscore_name;
360 $enumsym = lc $option_underscore_name;
361 $enumshort = $enumlong;
362 $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
364 # enumname is e.g. GMatchType
365 $enspace = $enumname;
366 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
368 $enumshort = $enumname;
369 $enumshort =~ s/^[A-Z][a-z]*//;
370 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
371 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
372 $enumshort = uc($enumshort);
374 $enumlong = uc($enspace) . "_" . $enumshort;
375 $enumsym = lc($enspace) . "_" . lc($enumshort);
377 if (defined($option_lowercase_name)) {
378 $enumsym = $option_lowercase_name;
385 if (length($fprod)) {
388 $prod =~ s/\@filename\@/$ARGV/g;
389 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
390 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
397 if (length($eprod)) {
400 $prod =~ s/\@enum_name\@/$enumsym/g;
401 $prod =~ s/\@EnumName\@/$enumname/g;
402 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
403 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
404 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
405 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
406 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
407 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
408 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
414 if (length($vhead)) {
417 $prod =~ s/\@enum_name\@/$enumsym/g;
418 $prod =~ s/\@EnumName\@/$enumname/g;
419 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
420 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
421 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
422 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
423 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
424 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
425 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
431 if (length($vprod)) {
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;
437 my ($name,$nick) = @{$_};
438 my $tmp_prod = $prod;
440 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
441 $tmp_prod =~ s/\@valuenick\@/$nick/g;
442 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
443 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
444 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
451 if (length($vtail)) {
454 $prod =~ s/\@enum_name\@/$enumsym/g;
455 $prod =~ s/\@EnumName\@/$enumname/g;
456 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
457 $prod =~ s/\@ENUMNAME\@/$enumlong/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;
470 if (length($ftail)) {
473 $prod =~ s/\@filename\@/$ARGV/g;
474 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
475 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
481 # put auto-generation comment
483 my $comment = $comment_tmpl;
484 $comment =~ s/\@comment\@/Generated data ends here/;
485 print "\n" . $comment . "\n\n";