4 # Information about the current enumeration
5 my $flags; # Is enumeration a bitmask?
6 my $seenbitshift; # Have we seen bitshift operators?
7 my $enum_prefix; # Prefix for this enumeration
8 my $enumname; # Name for this enumeration
9 my $enumshort; # $enumname without prefix
10 my $enumindex = 0; # Global enum counter
11 my $firstenum = 1; # Is this the first enumeration per file?
12 my @entries; # [ $name, $val ] for each entry
18 for $opt (split /\s*,\s*/, $opts) {
21 my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
22 defined $val or $val = 1;
23 push @opts, $key, $val;
29 my $file_name = shift;
30 my $looking_for_name = 0;
33 # read lines until we have no open comments
34 while (m@/\*([^*]|\*(?!/))*$@) {
36 defined ($new = <$file>) || die "Unmatched comment in $ARGV";
39 # strip comments w/o options
50 if ($looking_for_name) {
57 # Handle include files
58 if (/^\#include\s*<([^>]*)>/ ) {
60 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
62 if (parse_entries (\*NEWFILE, $NEWFILE)) {
69 if (/^\s*\}\s*(\w+)/) {
77 $looking_for_name = 1;
91 my ($name, $value, $options) = ($1,$2,$3);
93 if (!defined $flags && defined $value && $value =~ /<</) {
97 if (defined $options) {
98 my %options = parse_trigraph($options);
99 if (!defined $options{skip}) {
100 push @entries, [ $name, $options{nick} ];
103 push @entries, [ $name ];
105 } elsif (m@^\s*\#@) {
106 # ignore preprocessor directives
108 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
116 print STDERR "glib-mkenums version glib-@GLIB_VERSION@\n";
117 print STDERR "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
118 print STDERR "You may redistribute copies of glib-mkenums under the terms of\n";
119 print STDERR "the GNU General Public License which can be found in the\n";
120 print STDERR "GLib source package. Sources, examples and contact\n";
121 print STDERR "information are available at http://www.gtk.org\n";
125 print STDERR "Usage: glib-mkenums [options] [files...]\n";
126 print STDERR " --fhead <text> output file header\n";
127 print STDERR " --fprod <text> per input file production\n";
128 print STDERR " --ftail <text> output file trailer\n";
129 print STDERR " --eprod <text> per enum text (produced prior to value itarations)\n";
130 print STDERR " --vhead <text> value header, produced before iterating over enum values\n";
131 print STDERR " --vprod <text> value text, produced for each enum value\n";
132 print STDERR " --vtail <text> value tail, produced after iterating over enum values\n";
133 print STDERR " --comments <text> comment structure\n";
134 print STDERR " --template file template file\n";
135 print STDERR " -h, --help show this help message\n";
136 print STDERR " -v, --version print version informations\n";
137 print STDERR "Production text substitutions:\n";
138 print STDERR " \@EnumName\@ PrefixTheXEnum\n";
139 print STDERR " \@enum_name\@ prefix_the_xenum\n";
140 print STDERR " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
141 print STDERR " \@ENUMSHORT\@ THE_XENUM\n";
142 print STDERR " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
143 print STDERR " \@valuenick\@ the-xvalue\n";
144 print STDERR " \@type\@ either enum or flags\n";
145 print STDERR " \@Type\@ either Enum or Flags\n";
146 print STDERR " \@TYPE\@ either ENUM or FLAGS\n";
147 print STDERR " \@filename\@ name of current input file\n";
151 # production variables:
152 my $fhead = ""; # output file header
153 my $fprod = ""; # per input file production
154 my $ftail = ""; # output file trailer
155 my $eprod = ""; # per enum text (produced prior to value itarations)
156 my $vhead = ""; # value header, produced before iterating over enum values
157 my $vprod = ""; # value text, produced for each enum value
158 my $vtail = ""; # value tail, produced after iterating over enum values
160 my $comment_tmpl = "/* \@comment\@ */";
162 sub read_template_file {
164 my %tmpl = ('file-header', $fhead,
165 'file-production', $fprod,
167 'enumeration-production', $eprod,
168 'value-header', $vhead,
169 'value-production', $vprod,
170 'value-tail', $vtail,
171 'comment', $comment_tmpl);
173 open (FILE, $file) || die "Can't open $file: $!\n";
175 if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
176 if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
180 elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
185 die "Malformed template file $file\n";
188 if (!($in eq 'junk')) {
193 if (!($in eq 'junk')) {
194 die "Malformed template file $file\n";
196 $fhead = $tmpl{'file-header'};
197 $fprod = $tmpl{'file-production'};
198 $ftail = $tmpl{'file-tail'};
199 $eprod = $tmpl{'enumeration-production'};
200 $vhead = $tmpl{'value-header'};
201 $vprod = $tmpl{'value-production'};
202 $vtail = $tmpl{'value-tail'};
203 $comment_tmpl = $tmpl{'comment'};
206 if (!defined $ARGV[0]) {
209 while ($_ = $ARGV[0], /^-/) {
212 if (/^--template$/) { read_template_file (shift); }
213 elsif (/^--fhead$/) { $fhead = $fhead . shift }
214 elsif (/^--fprod$/) { $fprod = $fprod . shift }
215 elsif (/^--ftail$/) { $ftail = $ftail . shift }
216 elsif (/^--eprod$/) { $eprod = $eprod . shift }
217 elsif (/^--vhead$/) { $vhead = $vhead . shift }
218 elsif (/^--vprod$/) { $vprod = $vprod . shift }
219 elsif (/^--vtail$/) { $vtail = $vtail . shift }
220 elsif (/^--comments$/) { $comment_tmpl = shift }
221 elsif (/^--help$/ || /^-h$/) { usage; }
222 elsif (/^--version$/ || /^-v$/) { version; }
226 # put auto-generation comment
228 my $comment = $comment_tmpl;
229 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
230 print "\n" . $comment . "\n\n";
233 if (length($fhead)) {
236 $prod =~ s/\@filename\@/$ARGV[0]/g;
237 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
238 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
245 close (ARGV); # reset line numbering
246 $firstenum = 1; # Flag to print filename at next enum
249 # read lines until we have no open comments
250 while (m@/\*([^*]|\*(?!/))*$@) {
252 defined ($new = <>) || die "Unmatched comment in $ARGV";
255 # strip comments w/o options
260 if (m@^\s*typedef\s+enum\s*
267 my %options = parse_trigraph ($2);
268 next if defined $options{skip};
269 $enum_prefix = $options{prefix};
270 $flags = $options{flags};
272 $enum_prefix = undef;
275 # Didn't have trailing '{' look on next lines
287 # Now parse the entries
288 parse_entries (\*ARGV, $ARGV);
290 # figure out if this was a flags or enums enumeration
291 if (!defined $flags) {
292 $flags = $seenbitshift;
295 # Autogenerate a prefix
296 if (!defined $enum_prefix) {
299 if (!defined $nick) {
301 if (defined $enum_prefix) {
302 my $tmp = ~ ($name ^ $enum_prefix);
303 ($tmp) = $tmp =~ /(^\xff*)/;
304 $enum_prefix = $enum_prefix & $tmp;
306 $enum_prefix = $name;
310 if (!defined $enum_prefix) {
313 # Trim so that it ends in an underscore
314 $enum_prefix =~ s/_[^_]*$/_/;
317 # canonicalize user defined prefixes
318 $enum_prefix = uc($enum_prefix);
319 $enum_prefix =~ s/-/_/g;
320 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
323 for $entry (@entries) {
324 my ($name,$nick) = @{$entry};
325 if (!defined $nick) {
326 ($nick = $name) =~ s/^$enum_prefix//;
329 @{$entry} = ($name, $nick);
334 # Spit out the output
336 # enumname is e.g. GMatchType
337 $enspace = $enumname;
338 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
340 $enumshort = $enumname;
341 $enumshort =~ s/^[A-Z][a-z]*//;
342 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
343 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
344 $enumshort = uc($enumshort);
346 $enumlong = uc($enspace) . "_" . $enumshort;
347 $enumsym = lc($enspace) . "_" . lc($enumshort);
352 if (length($fprod)) {
355 $prod =~ s/\@filename\@/$ARGV/g;
356 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
357 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
363 if (length($eprod)) {
366 $prod =~ s/\@enum_name\@/$enumsym/g;
367 $prod =~ s/\@EnumName\@/$enumname/g;
368 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
369 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
370 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
371 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
372 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
373 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
374 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
379 if (length($vhead)) {
382 $prod =~ s/\@enum_name\@/$enumsym/g;
383 $prod =~ s/\@EnumName\@/$enumname/g;
384 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
385 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
386 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
387 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
388 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/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;
395 if (length($vprod)) {
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;
401 my ($name,$nick) = @{$_};
402 my $tmp_prod = $vprod;
404 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
405 $tmp_prod =~ s/\@valuenick\@/$nick/g;
406 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
407 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
408 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
414 if (length($vtail)) {
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;
432 if (length($ftail)) {
435 $prod =~ s/\@filename\@/$ARGV/g;
436 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
437 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
442 # put auto-generation comment
444 my $comment = $comment_tmpl;
445 $comment =~ s/\@comment\@/Generated data ends here/;
446 print "\n" . $comment . "\n\n";