4 # Information about the current enumeration
5 my $flags; # Is enumeration a bitmask?
6 my $option_lowercase_name; # A lower case name to use as part of the *_get_type() function, instead of the one that we guess.
7 # For instance, when an enum uses abnormal capitalization and we can not guess where to put the underscores.
8 my $seenbitshift; # Have we seen bitshift operators?
9 my $enum_prefix; # Prefix for this enumeration
10 my $enumname; # Name for this enumeration
11 my $enumshort; # $enumname without prefix
12 my $enumindex = 0; # Global enum counter
13 my $firstenum = 1; # Is this the first enumeration per file?
14 my @entries; # [ $name, $val ] for each entry
20 for $opt (split /\s*,\s*/, $opts) {
23 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
24 defined $val or $val = 1;
25 push @opts, $key, $val;
31 my $file_name = shift;
32 my $looking_for_name = 0;
35 # read lines until we have no open comments
36 while (m@/\*([^*]|\*(?!/))*$@) {
38 defined ($new = <$file>) || die "Unmatched comment in $ARGV";
41 # strip comments w/o options
52 if ($looking_for_name) {
59 # Handle include files
60 if (/^\#include\s*<([^>]*)>/ ) {
62 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
64 if (parse_entries (\*NEWFILE, $NEWFILE)) {
71 if (/^\s*\}\s*(\w+)/) {
79 $looking_for_name = 1;
93 my ($name, $value, $options) = ($1,$2,$3);
95 if (!defined $flags && defined $value && $value =~ /<</) {
99 if (defined $options) {
100 my %options = parse_trigraph($options);
101 if (!defined $options{skip}) {
102 push @entries, [ $name, $options{nick} ];
105 push @entries, [ $name ];
107 } elsif (m@^\s*\#@) {
108 # ignore preprocessor directives
110 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
118 print STDERR "glib-mkenums version glib-@GLIB_VERSION@\n";
119 print STDERR "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
120 print STDERR "You may redistribute copies of glib-mkenums under the terms of\n";
121 print STDERR "the GNU General Public License which can be found in the\n";
122 print STDERR "GLib source package. Sources, examples and contact\n";
123 print STDERR "information are available at http://www.gtk.org\n";
127 print STDERR "Usage: glib-mkenums [options] [files...]\n";
128 print STDERR " --fhead <text> output file header\n";
129 print STDERR " --fprod <text> per input file production\n";
130 print STDERR " --ftail <text> output file trailer\n";
131 print STDERR " --eprod <text> per enum text (produced prior to value itarations)\n";
132 print STDERR " --vhead <text> value header, produced before iterating over enum values\n";
133 print STDERR " --vprod <text> value text, produced for each enum value\n";
134 print STDERR " --vtail <text> value tail, produced after iterating over enum values\n";
135 print STDERR " --comments <text> comment structure\n";
136 print STDERR " --template file template file\n";
137 print STDERR " -h, --help show this help message\n";
138 print STDERR " -v, --version print version informations\n";
139 print STDERR "Production text substitutions:\n";
140 print STDERR " \@EnumName\@ PrefixTheXEnum\n";
141 print STDERR " \@enum_name\@ prefix_the_xenum\n";
142 print STDERR " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
143 print STDERR " \@ENUMSHORT\@ THE_XENUM\n";
144 print STDERR " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
145 print STDERR " \@valuenick\@ the-xvalue\n";
146 print STDERR " \@type\@ either enum or flags\n";
147 print STDERR " \@Type\@ either Enum or Flags\n";
148 print STDERR " \@TYPE\@ either ENUM or FLAGS\n";
149 print STDERR " \@filename\@ name of current input file\n";
153 # production variables:
154 my $fhead = ""; # output file header
155 my $fprod = ""; # per input file production
156 my $ftail = ""; # output file trailer
157 my $eprod = ""; # per enum text (produced prior to value itarations)
158 my $vhead = ""; # value header, produced before iterating over enum values
159 my $vprod = ""; # value text, produced for each enum value
160 my $vtail = ""; # value tail, produced after iterating over enum values
162 my $comment_tmpl = "/* \@comment\@ */";
164 sub read_template_file {
166 my %tmpl = ('file-header', $fhead,
167 'file-production', $fprod,
169 'enumeration-production', $eprod,
170 'value-header', $vhead,
171 'value-production', $vprod,
172 'value-tail', $vtail,
173 'comment', $comment_tmpl);
175 open (FILE, $file) || die "Can't open $file: $!\n";
177 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
178 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
182 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
187 die "Malformed template file $file\n";
190 if (!($in eq 'junk')) {
195 if (!($in eq 'junk')) {
196 die "Malformed template file $file\n";
198 $fhead = $tmpl{'file-header'};
199 $fprod = $tmpl{'file-production'};
200 $ftail = $tmpl{'file-tail'};
201 $eprod = $tmpl{'enumeration-production'};
202 $vhead = $tmpl{'value-header'};
203 $vprod = $tmpl{'value-production'};
204 $vtail = $tmpl{'value-tail'};
205 $comment_tmpl = $tmpl{'comment'};
208 if (!defined $ARGV[0]) {
211 while ($_ = $ARGV[0], /^-/) {
214 if (/^--template$/) { read_template_file (shift); }
215 elsif (/^--fhead$/) { $fhead = $fhead . shift }
216 elsif (/^--fprod$/) { $fprod = $fprod . shift }
217 elsif (/^--ftail$/) { $ftail = $ftail . shift }
218 elsif (/^--eprod$/) { $eprod = $eprod . shift }
219 elsif (/^--vhead$/) { $vhead = $vhead . shift }
220 elsif (/^--vprod$/) { $vprod = $vprod . shift }
221 elsif (/^--vtail$/) { $vtail = $vtail . shift }
222 elsif (/^--comments$/) { $comment_tmpl = shift }
223 elsif (/^--help$/ || /^-h$/) { usage; }
224 elsif (/^--version$/ || /^-v$/) { version; }
228 # put auto-generation comment
230 my $comment = $comment_tmpl;
231 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
232 print "\n" . $comment . "\n\n";
235 if (length($fhead)) {
238 $prod =~ s/\@filename\@/$ARGV[0]/g;
239 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
240 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
247 close (ARGV); # reset line numbering
248 $firstenum = 1; # Flag to print filename at next enum
251 # read lines until we have no open comments
252 while (m@/\*([^*]|\*(?!/))*$@) {
254 defined ($new = <>) || die "Unmatched comment in $ARGV";
257 # strip comments w/o options
262 if (m@^\s*typedef\s+enum\s*
269 my %options = parse_trigraph ($2);
270 next if defined $options{skip};
271 $enum_prefix = $options{prefix};
272 $flags = $options{flags};
273 $option_lowercase_name = $options{lowercase_name};
275 $enum_prefix = undef;
277 $option_lowercase_name = undef;
279 # Didn't have trailing '{' look on next lines
291 # Now parse the entries
292 parse_entries (\*ARGV, $ARGV);
294 # figure out if this was a flags or enums enumeration
295 if (!defined $flags) {
296 $flags = $seenbitshift;
299 # Autogenerate a prefix
300 if (!defined $enum_prefix) {
303 if (!defined $nick) {
305 if (defined $enum_prefix) {
306 my $tmp = ~ ($name ^ $enum_prefix);
307 ($tmp) = $tmp =~ /(^\xff*)/;
308 $enum_prefix = $enum_prefix & $tmp;
310 $enum_prefix = $name;
314 if (!defined $enum_prefix) {
317 # Trim so that it ends in an underscore
318 $enum_prefix =~ s/_[^_]*$/_/;
321 # canonicalize user defined prefixes
322 $enum_prefix = uc($enum_prefix);
323 $enum_prefix =~ s/-/_/g;
324 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
327 for $entry (@entries) {
328 my ($name,$nick) = @{$entry};
329 if (!defined $nick) {
330 ($nick = $name) =~ s/^$enum_prefix//;
333 @{$entry} = ($name, $nick);
338 # Spit out the output
340 # enumname is e.g. GMatchType
341 $enspace = $enumname;
342 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
344 $enumshort = $enumname;
345 $enumshort =~ s/^[A-Z][a-z]*//;
346 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
347 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
348 $enumshort = uc($enumshort);
350 $enumlong = uc($enspace) . "_" . $enumshort;
351 $enumsym = lc($enspace) . "_" . lc($enumshort);
353 #The options might override the lower case name if it could not be generated correctly:
354 if (defined($option_lowercase_name)) {
355 $enumsym = $option_lowercase_name;
361 if (length($fprod)) {
364 $prod =~ s/\@filename\@/$ARGV/g;
365 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
366 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
372 if (length($eprod)) {
375 $prod =~ s/\@enum_name\@/$enumsym/g;
376 $prod =~ s/\@EnumName\@/$enumname/g;
377 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
378 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
379 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
380 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
381 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
382 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
383 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
388 if (length($vhead)) {
391 $prod =~ s/\@enum_name\@/$enumsym/g;
392 $prod =~ s/\@EnumName\@/$enumname/g;
393 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
394 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
395 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
396 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
397 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
398 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
399 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
404 if (length($vprod)) {
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;
410 my ($name,$nick) = @{$_};
411 my $tmp_prod = $vprod;
413 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
414 $tmp_prod =~ s/\@valuenick\@/$nick/g;
415 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
416 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
417 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
423 if (length($vtail)) {
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 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/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 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
434 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
441 if (length($ftail)) {
444 $prod =~ s/\@filename\@/$ARGV/g;
445 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
446 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
451 # put auto-generation comment
453 my $comment = $comment_tmpl;
454 $comment =~ s/\@comment\@/Generated data ends here/;
455 print "\n" . $comment . "\n\n";