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-2.0\n"; # FIXME: autogen version?
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 " -h, --help show this help message\n";
135 print STDERR " -v, --version print version informations\n";
136 print STDERR "Production text substitutions:\n";
137 print STDERR " \@EnumName\@ PrefixTheXEnum\n";
138 print STDERR " \@enum_name\@ prefix_the_xenum\n";
139 print STDERR " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
140 print STDERR " \@ENUMSHORT\@ THE_XENUM\n";
141 print STDERR " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
142 print STDERR " \@valuenick\@ the-xvalue\n";
143 print STDERR " \@type\@ either enum or flags\n";
144 print STDERR " \@Type\@ either Enum or Flags\n";
145 print STDERR " \@TYPE\@ either ENUM or FLAGS\n";
146 print STDERR " \@filename\@ name of current input file\n";
150 # production variables:
151 my $fhead = ""; # output file header
152 my $fprod = ""; # per input file production
153 my $ftail = ""; # output file trailer
154 my $eprod = ""; # per enum text (produced prior to value itarations)
155 my $vhead = ""; # value header, produced before iterating over enum values
156 my $vprod = ""; # value text, produced for each enum value
157 my $vtail = ""; # value tail, produced after iterating over enum values
159 my $comment_tmpl = "/* \@comment\@ */";
161 if (!defined $ARGV[0]) {
164 while ($_ = $ARGV[0], /^-/) {
167 if (/^--fhead$/) { $fhead = $fhead . shift }
168 elsif (/^--fprod$/) { $fprod = $fprod . shift }
169 elsif (/^--ftail$/) { $ftail = $ftail . shift }
170 elsif (/^--eprod$/) { $eprod = $eprod . shift }
171 elsif (/^--vhead$/) { $vhead = $vhead . shift }
172 elsif (/^--vprod$/) { $vprod = $vprod . shift }
173 elsif (/^--vtail$/) { $vtail = $vtail . shift }
174 elsif (/^--comments$/) { $comment_tmpl = shift }
175 elsif (/^--help$/ || /^-h$/) { usage; }
176 elsif (/^--version$/ || /^-v$/) { version; }
180 # put auto-generation comment
182 my $comment = $comment_tmpl;
183 $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
184 print "\n" . $comment . "\n\n";
187 if (length($fhead)) {
190 $prod =~ s/\@filename\@/$ARGV/g;
191 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
192 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
199 close (ARGV); # reset line numbering
200 $firstenum = 1; # Flag to print filename at next enum
203 # read lines until we have no open comments
204 while (m@/\*([^*]|\*(?!/))*$@) {
206 defined ($new = <>) || die "Unmatched comment in $ARGV";
209 # strip comments w/o options
214 if (m@^\s*typedef\s+enum\s*
221 my %options = parse_trigraph ($2);
222 next if defined $options{skip};
223 $enum_prefix = $options{prefix};
224 $flags = $options{flags};
226 $enum_prefix = undef;
229 # Didn't have trailing '{' look on next lines
241 # Now parse the entries
242 parse_entries (\*ARGV, $ARGV);
244 # figure out if this was a flags or enums enumeration
245 if (!defined $flags) {
246 $flags = $seenbitshift;
249 # Autogenerate a prefix
250 if (!defined $enum_prefix) {
253 if (!defined $nick) {
255 if (defined $enum_prefix) {
256 my $tmp = ~ ($name ^ $enum_prefix);
257 ($tmp) = $tmp =~ /(^\xff*)/;
258 $enum_prefix = $enum_prefix & $tmp;
260 $enum_prefix = $name;
264 if (!defined $enum_prefix) {
267 # Trim so that it ends in an underscore
268 $enum_prefix =~ s/_[^_]*$/_/;
271 # canonicalize user defined prefixes
272 $enum_prefix = uc($enum_prefix);
273 $enum_prefix =~ s/-/_/g;
274 $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
277 for $entry (@entries) {
278 my ($name,$nick) = @{$entry};
279 if (!defined $nick) {
280 ($nick = $name) =~ s/^$enum_prefix//;
283 @{$entry} = ($name, $nick);
288 # Spit out the output
290 # enumname is e.g. GMatchType
291 $enspace = $enumname;
292 $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
294 $enumshort = $enumname;
295 $enumshort =~ s/^[A-Z][a-z]*//;
296 $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
297 $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
298 $enumshort = uc($enumshort);
300 $enumlong = uc($enspace) . "_" . $enumshort;
301 $enumsym = lc($enspace) . "_" . lc($enumshort);
306 if (length($fprod)) {
309 $prod =~ s/\@filename\@/$ARGV/g;
310 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
311 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
317 if (length($eprod)) {
320 $prod =~ s/\@enum_name\@/$enumsym/g;
321 $prod =~ s/\@EnumName\@/$enumname/g;
322 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
323 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
324 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
325 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
326 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
327 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
328 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
333 if (length($vhead)) {
336 $prod =~ s/\@enum_name\@/$enumsym/g;
337 $prod =~ s/\@EnumName\@/$enumname/g;
338 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
339 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
340 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
341 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
342 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
343 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
344 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
349 if (length($vprod)) {
352 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
353 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
355 my ($name,$nick) = @{$_};
356 my $tmp_prod = $vprod;
358 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
359 $tmp_prod =~ s/\@valuenick\@/$nick/g;
360 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
361 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
362 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
368 if (length($vtail)) {
371 $prod =~ s/\@enum_name\@/$enumsym/g;
372 $prod =~ s/\@EnumName\@/$enumname/g;
373 $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
374 $prod =~ s/\@ENUMNAME\@/$enumlong/g;
375 if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
376 if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
377 if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
378 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
379 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
386 if (length($ftail)) {
389 $prod =~ s/\@filename\@/$ARGV/g;
390 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
391 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
396 # put auto-generation comment
398 my $comment = $comment_tmpl;
399 $comment =~ s/\@comment\@/Generated data ends here/;
400 print "\n" . $comment . "\n\n";