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;
33 # read lines until comment end is matched
34 while (m@/\*([^*]|\*[^/*])*\**$@x) {
36 defined ($new = <>) || die "Unmatched comment in $ARGV";
39 # strip comments w/o options
40 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
48 # print STDERR "xxx $_\n";
50 # Handle include files
51 if (/^\#include\s*<([^>]*)>/ ) {
53 open NEWFILE, $file or die "Cannot open include file $file: $!\n";
55 # read lines until comment end is matched
56 while (m@/\*([^*]|\*[^/*])*\**$@x) {
58 defined ($new = <>) || die "Unmatched comment in $file_name";
61 # strip comments w/o options
62 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
64 if (parse_entries (\*NEWFILE, $NEWFILE)) {
71 if (/^\s*\}\s*(\w+)/) {
87 my ($name, $value, $options) = ($1,$2,$3);
89 # print STDERR "xxx \"$name\" \"$value\" \"$otions\"\n";
91 if (!defined $flags && defined $value && $value =~ /<</) {
95 if (defined $options) {
96 my %options = parse_trigraph($options);
97 if (!defined $options{skip}) {
98 push @entries, [ $name, $options{nick} ];
101 push @entries, [ $name ];
103 } elsif (m@^\s*\#@) {
104 # ignore preprocessor directives
106 print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
114 print STDERR "glib-mkenums version glib-2.0\n"; # FIXME: autogen version?
115 print STDERR "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
116 print STDERR "You may redistribute copies of glib-mkenums under the terms of\n";
117 print STDERR "the GNU General Public License which can be found in the\n";
118 print STDERR "GLib source package. Sources, examples and contact\n";
119 print STDERR "information are available at http://www.gtk.org\n";
123 print STDERR "Usage: glib-mkenums [options] [files...]\n";
124 print STDERR " --fhead <text> output file header\n";
125 print STDERR " --fprod <text> per input file production\n";
126 print STDERR " --ftail <text> output file trailer\n";
127 print STDERR " --eprod <text> per enum text (produced prior to value itarations)\n";
128 print STDERR " --vhead <text> value header, produced before iterating over enum values\n";
129 print STDERR " --vprod <text> value text, produced for each enum value\n";
130 print STDERR " --vtail <text> value tail, produced after iterating over enum values\n";
131 print STDERR " --comments <text> comment structure\n";
132 print STDERR " -h, --help show this help message\n";
133 print STDERR " -v, --version print version informations\n";
134 print STDERR "Production text substitutions:\n";
135 print STDERR " \@EnumName\@ PrefixTheXEnum\n";
136 print STDERR " \@enum_name\@ prefix_the_xenum\n";
137 print STDERR " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
138 print STDERR " \@ENUMSHORT\@ THE_XENUM\n";
139 print STDERR " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
140 print STDERR " \@valuenick\@ the-xvalue\n";
141 print STDERR " \@type\@ either enum or flags\n";
142 print STDERR " \@Type\@ either Enum or Flags\n";
143 print STDERR " \@TYPE\@ either ENUM or FLAGS\n";
144 print STDERR " \@filename\@ name of current input file\n";
148 # production variables:
149 my $fhead = ""; # output file header
150 my $fprod = ""; # per input file production
151 my $ftail = ""; # output file trailer
152 my $eprod = ""; # per enum text (produced prior to value itarations)
153 my $vhead = ""; # value header, produced before iterating over enum values
154 my $vprod = ""; # value text, produced for each enum value
155 my $vtail = ""; # value tail, produced after iterating over enum values
157 my $comment_tmpl = "/* \@comment\@ */";
159 if (!defined $ARGV[0]) {
162 while ($_ = $ARGV[0], /^-/) {
165 if (/^--fhead$/) { $fhead = $fhead . shift }
166 elsif (/^--fprod$/) { $fprod = $fprod . shift }
167 elsif (/^--ftail$/) { $ftail = $ftail . shift }
168 elsif (/^--eprod$/) { $eprod = $eprod . shift }
169 elsif (/^--vhead$/) { $vhead = $vhead . shift }
170 elsif (/^--vprod$/) { $vprod = $vprod . shift }
171 elsif (/^--vtail$/) { $vtail = $vtail . shift }
172 elsif (/^--comments$/) { $comment_tmpl = shift }
173 elsif (/^--help$/ || /^-h$/) { usage; }
174 elsif (/^--version$/ || /^-v$/) { version; }
178 # put auto-generation comment
180 my $comment = $comment_tmpl;
181 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
182 print "\n" . $comment . "\n\n";
185 if (length($fhead)) {
188 $prod =~ s/\@filename\@/$ARGV/g;
189 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
190 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
197 close (ARGV); # reset line numbering
198 $firstenum = 1; # Flag to print filename at next enum
201 # read lines until comment end is matched
202 while (m@/\*([^*]|\*[^/*])*\**$@x) {
204 defined ($new = <>) || die "Unmatched comment in $ARGV";
207 # strip comments w/o options
208 s@/\*[^<]([^*]|\*[^/*])*\**\*/@@gx;
210 # print STDERR "xxx $_\n";
212 if (m@^\s*typedef\s+enum\s*
219 my %options = parse_trigraph ($2);
220 next if defined $options{skip};
221 $enum_prefix = $options{prefix};
222 $flags = $options{flags};
224 $enum_prefix = undef;
227 # Didn't have trailing '{' look on next lines
239 # Now parse the entries
240 parse_entries (\*ARGV, $ARGV);
242 # figure out if this was a flags or enums enumeration
243 if (!defined $flags) {
244 $flags = $seenbitshift;
247 # Autogenerate a prefix
248 if (!defined $enum_prefix) {
251 if (!defined $nick) {
253 if (defined $enum_prefix) {
254 my $tmp = ~ ($name ^ $enum_prefix);
255 ($tmp) = $tmp =~ /(^\xff*)/;
256 $enum_prefix = $enum_prefix & $tmp;
258 $enum_prefix = $name;
262 if (!defined $enum_prefix) {
265 # Trim so that it ends in an underscore
266 $enum_prefix =~ s/_[^_]*$/_/;
269 # canonicalize user defined prefixes
270 $enum_prefix = uc($enum_prefix);
271 $enum_prefix =~ s/-/_/g;
272 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
275 for $entry (@entries) {
276 my ($name,$nick) = @{$entry};
277 if (!defined $nick) {
278 ($nick = $name) =~ s/^$enum_prefix//;
281 @{$entry} = ($name, $nick);
286 # Spit out the output
288 # enumname is e.g. GMatchType
289 $enspace = $enumname;
290 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
292 $enumshort = $enumname;
293 $enumshort =~ s/^[A-Z][a-z]*//;
294 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
295 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
296 $enumshort = uc($enumshort);
298 $enumlong = uc($enspace) . "_" . $enumshort;
299 $enumsym = lc($enspace) . "_" . lc($enumshort);
304 if (length($fprod)) {
307 $prod =~ s/\@filename\@/$ARGV/g;
308 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
309 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
315 if (length($eprod)) {
318 $prod =~ s/\@enum_name\@/$enumsym/g;
319 $prod =~ s/\@EnumName\@/$enumname/g;
320 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
321 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
322 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
323 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
324 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
325 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
326 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
331 if (length($vhead)) {
334 $prod =~ s/\@enum_name\@/$enumsym/g;
335 $prod =~ s/\@EnumName\@/$enumname/g;
336 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
337 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
338 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
339 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
340 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
341 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
342 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
347 if (length($vprod)) {
350 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
351 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
353 my ($name,$nick) = @{$_};
354 my $tmp_prod = $vprod;
356 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
357 $tmp_prod =~ s/\@valuenick\@/$nick/g;
358 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
359 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
360 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
366 if (length($vtail)) {
369 $prod =~ s/\@enum_name\@/$enumsym/g;
370 $prod =~ s/\@EnumName\@/$enumname/g;
371 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
372 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
373 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
374 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
375 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
376 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
377 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
384 if (length($ftail)) {
387 $prod =~ s/\@filename\@/$ARGV/g;
388 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
389 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
394 # put auto-generation comment
396 my $comment = $comment_tmpl;
397 $comment =~ s/\@comment\@/Generated data ends here/;
398 print "\n" . $comment . "\n\n";