glib-mkenums.in: fix @ENUMPREFIX@ with /*< underscore_name=... >*/
[platform/upstream/glib.git] / gobject / glib-mkenums.in
1 #! @PERL_PATH@
2
3 use warnings;
4 use File::Basename;
5 use Safe;
6
7 # glib-mkenums.pl 
8 # Information about the current enumeration
9 my $flags;                      # Is enumeration a bitmask?
10 my $option_underscore_name;     # Overriden underscore variant of the enum name
11                                 # for example to fix the cases we don't get the
12                                 # mixed-case -> underscorized transform right.
13 my $option_lowercase_name;      # DEPRECATED.  A lower case name to use as part
14                                 # of the *_get_type() function, instead of the
15                                 # one that we guess. For instance, when an enum
16                                 # uses abnormal capitalization and we can not
17                                 # guess where to put the underscores.
18 my $seenbitshift;               # Have we seen bitshift operators?
19 my $enum_prefix;                # Prefix for this enumeration
20 my $enumname;                   # Name for this enumeration
21 my $enumshort;                  # $enumname without prefix
22 my $enumname_prefix;            # prefix of $enumname
23 my $enumindex = 0;              # Global enum counter
24 my $firstenum = 1;              # Is this the first enumeration per file?
25 my @entries;                    # [ $name, $val ] for each entry
26 my $sandbox = Safe->new;        # sandbox for safe evaluation of expressions
27
28 sub parse_trigraph {
29     my $opts = shift;
30     my @opts;
31
32     for $opt (split /\s*,\s*/, $opts) {
33         $opt =~ s/^\s*//;
34         $opt =~ s/\s*$//;
35         my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
36         defined $val or $val = 1;
37         push @opts, $key, $val;
38     }
39     @opts;
40 }
41 sub parse_entries {
42     my $file = shift;
43     my $file_name = shift;
44     my $looking_for_name = 0;
45     
46     while (<$file>) {
47         # read lines until we have no open comments
48         while (m@/\*([^*]|\*(?!/))*$@) {
49             my $new;
50             defined ($new = <$file>) || die "Unmatched comment in $ARGV";
51             $_ .= $new;
52         }
53         # strip comments w/o options
54         s@/\*(?!<)
55             ([^*]+|\*(?!/))*
56            \*/@@gx;
57         
58         # strip newlines
59         s@\n@ @;
60         
61         # skip empty lines
62         next if m@^\s*$@;
63         
64         if ($looking_for_name) {
65             if (/^\s*(\w+)/) {
66                 $enumname = $1;
67                 return 1;
68             }
69         }
70         
71         # Handle include files
72         if (/^\#include\s*<([^>]*)>/ ) {
73             my $file= "../$1";
74             open NEWFILE, $file or die "Cannot open include file $file: $!\n";
75             
76             if (parse_entries (\*NEWFILE, $NEWFILE)) {
77                 return 1;
78             } else {
79                 next;
80             }
81         }
82         
83         if (/^\s*\}\s*(\w+)/) {
84             $enumname = $1;
85             $enumindex++;
86             return 1;
87         }
88         
89         if (/^\s*\}/) {
90             $enumindex++;
91             $looking_for_name = 1;
92             next;
93         }
94
95         if (m@^\s*
96               (\w+)\s*                   # name
97               (?:=(                      # value
98                    \s*\w+\s*\(.*\)\s*       # macro with multiple args
99                    |                        # OR
100                    (?:[^,/]|/(?!\*))*       # anything but a comma or comment
101                   ))?,?\s*
102               (?:/\*<                    # options
103                 (([^*]|\*(?!/))*)
104                >\s*\*/)?,?
105               \s*$
106              @x) {
107             my ($name, $value, $options) = ($1,$2,$3);
108
109             if (!defined $flags && defined $value && $value =~ /<</) {
110                 $seenbitshift = 1;
111             }
112
113             if (defined $options) {
114                 my %options = parse_trigraph($options);
115                 if (!defined $options{skip}) {
116                     push @entries, [ $name, $value, $options{nick} ];
117                 }
118             } else {
119                 push @entries, [ $name, $value ];
120             }
121         } elsif (m@^\s*\#@) {
122             # ignore preprocessor directives
123         } else {
124             print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
125         }
126     }
127
128     return 0;
129 }
130
131 sub version {
132     print "glib-mkenums version glib-@GLIB_VERSION@\n";
133     print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
134     print "You may redistribute copies of glib-mkenums under the terms of\n";
135     print "the GNU General Public License which can be found in the\n";
136     print "GLib source package. Sources, examples and contact\n";
137     print "information are available at http://www.gtk.org\n";
138     exit 0;
139 }
140 sub usage {
141     print "Usage:\n";
142     print "  glib-mkenums [OPTION...] [FILES...]\n\n";
143     print "Help Options:\n";
144     print "  -h, --help            Show this help message\n\n";
145     print "Utility Options:\n";
146     print "  --fhead <text>        Output file header\n";
147     print "  --fprod <text>        Per input file production\n";
148     print "  --ftail <text>        Output file trailer\n";
149     print "  --eprod <text>        Per enum text (produced prior to value itarations)\n";
150     print "  --vhead <text>        Value header, produced before iterating over enum values\n";
151     print "  --vprod <text>        Value text, produced for each enum value\n";
152     print "  --vtail <text>        Value tail, produced after iterating over enum values\n";
153     print "  --comments <text>     Comment structure\n";
154     print "  --template file       Template file\n";
155     print "  -v, --version         Print version informations\n\n";
156     print "Production text substitutions:\n";
157     print "  \@EnumName\@            PrefixTheXEnum\n";
158     print "  \@enum_name\@           prefix_the_xenum\n";
159     print "  \@ENUMNAME\@            PREFIX_THE_XENUM\n";
160     print "  \@ENUMSHORT\@           THE_XENUM\n";
161     print "  \@ENUMPREFIX\@          PREFIX\n";
162     print "  \@VALUENAME\@           PREFIX_THE_XVALUE\n";
163     print "  \@valuenick\@           the-xvalue\n";
164     print "  \@valuenum\@            the integer value (limited support, Since: 2.26)\n";
165     print "  \@type\@                either enum or flags\n";
166     print "  \@Type\@                either Enum or Flags\n";
167     print "  \@TYPE\@                either ENUM or FLAGS\n";
168     print "  \@filename\@            name of current input file\n";
169     print "  \@basename\@            base name of the current input file (Since: 2.22)\n";
170     exit 0;
171 }
172
173 # production variables:
174 my $fhead = "";   # output file header
175 my $fprod = "";   # per input file production
176 my $ftail = "";   # output file trailer
177 my $eprod = "";   # per enum text (produced prior to value itarations)
178 my $vhead = "";   # value header, produced before iterating over enum values
179 my $vprod = "";   # value text, produced for each enum value
180 my $vtail = "";   # value tail, produced after iterating over enum values
181 my $comment_tmpl = "";   # comment template
182
183 sub read_template_file {
184   my ($file) = @_;
185   my %tmpl = ('file-header', $fhead, 
186               'file-production', $fprod, 
187               'file-tail', $ftail, 
188               'enumeration-production', $eprod,
189               'value-header', $vhead,
190               'value-production', $vprod,
191               'value-tail', $vtail,
192               'comment', $comment_tmpl);
193   my $in = 'junk';
194   open (FILE, $file) || die "Can't open $file: $!\n";
195   while (<FILE>) {
196     if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
197       if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
198         $in = $2;
199         next;
200       }
201       elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
202         $in = 'junk';
203         next;
204       } else {
205           die "Malformed template file $file\n";
206       }
207     }
208     if (!($in eq 'junk')) {
209         $tmpl{$in} .= $_;
210     }
211   }
212   close (FILE);
213   if (!($in eq 'junk')) {
214       die "Malformed template file $file\n";
215   }
216   $fhead = $tmpl{'file-header'};
217   $fprod = $tmpl{'file-production'};
218   $ftail = $tmpl{'file-tail'};
219   $eprod = $tmpl{'enumeration-production'};
220   $vhead = $tmpl{'value-header'};
221   $vprod = $tmpl{'value-production'};
222   $vtail = $tmpl{'value-tail'};
223   $comment_tmpl = $tmpl{'comment'};
224
225   # default to C-style comments
226   $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
227 }
228
229 if (!defined $ARGV[0]) {
230     usage;
231 }
232 while ($_=$ARGV[0],/^-/) {
233     shift;
234     last if /^--$/;
235     if (/^--template$/)                      { read_template_file (shift); }
236     elsif (/^--fhead$/)                      { $fhead = $fhead . shift }
237     elsif (/^--fprod$/)                      { $fprod = $fprod . shift }
238     elsif (/^--ftail$/)                      { $ftail = $ftail . shift }
239     elsif (/^--eprod$/)                      { $eprod = $eprod . shift }
240     elsif (/^--vhead$/)                      { $vhead = $vhead . shift }
241     elsif (/^--vprod$/)                      { $vprod = $vprod . shift }
242     elsif (/^--vtail$/)                      { $vtail = $vtail . shift }
243     elsif (/^--comments$/)                   { $comment_tmpl = shift }
244     elsif (/^--help$/ || /^-h$/ || /^-\?$/)  { usage; }
245     elsif (/^--version$/ || /^-v$/)          { version; }
246     else { usage; }
247     last if not defined($ARGV[0]);
248 }
249
250 # put auto-generation comment
251 {
252     my $comment = $comment_tmpl;
253     $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
254     print "\n" . $comment . "\n\n";
255 }
256
257 if (length($fhead)) {
258     my $prod = $fhead;
259     my $base = basename ($ARGV[0]);
260
261     $prod =~ s/\@filename\@/$ARGV[0]/g;
262     $prod =~ s/\@basename\@/$base/g;
263     $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
264     $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
265     chomp ($prod);
266                 
267     print "$prod\n";
268 }
269
270 while (<>) {
271     if (eof) {
272         close (ARGV);           # reset line numbering
273         $firstenum = 1;         # Flag to print filename at next enum
274     }
275
276     # read lines until we have no open comments
277     while (m@/\*([^*]|\*(?!/))*$@) {
278         my $new;
279         defined ($new = <>) || die "Unmatched comment in $ARGV";
280         $_ .= $new;
281     }
282     # strip comments w/o options
283     s@/\*(?!<)
284        ([^*]+|\*(?!/))*
285        \*/@@gx;
286         
287     if (m@^\s*typedef\s+enum\s*
288            ({)?\s*
289            (?:/\*<
290              (([^*]|\*(?!/))*)
291             >\s*\*/)?
292            \s*({)?
293          @x) {
294         if (defined $2) {
295             my %options = parse_trigraph ($2);
296             next if defined $options{skip};
297             $enum_prefix = $options{prefix};
298             $flags = $options{flags};
299             $option_lowercase_name = $options{lowercase_name};
300             $option_underscore_name = $options{underscore_name};
301         } else {
302             $enum_prefix = undef;
303             $flags = undef;
304             $option_lowercase_name = undef;
305             $option_underscore_name = undef;
306         }
307         if (defined $option_lowercase_name) {
308             if (defined $option_underscore_name) {
309                 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
310                 $option_lowercase_name = undef;
311             } else {
312                 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
313             }
314         }
315         # Didn't have trailing '{' look on next lines
316         if (!defined $1 && !defined $4) {
317             while (<>) {
318                 if (s/^\s*\{//) {
319                     last;
320                 }
321             }
322         }
323
324         $seenbitshift = 0;
325         @entries = ();
326
327         # Now parse the entries
328         parse_entries (\*ARGV, $ARGV);
329
330         # figure out if this was a flags or enums enumeration
331         if (!defined $flags) {
332             $flags = $seenbitshift;
333         }
334
335         # Autogenerate a prefix
336         if (!defined $enum_prefix) {
337             for (@entries) {
338                 my $nick = $_->[2];
339                 if (!defined $nick) {
340                     my $name = $_->[0];
341                     if (defined $enum_prefix) {
342                         my $tmp = ~ ($name ^ $enum_prefix);
343                         ($tmp) = $tmp =~ /(^\xff*)/;
344                         $enum_prefix = $enum_prefix & $tmp;
345                     } else {
346                         $enum_prefix = $name;
347                     }
348                 }
349             }
350             if (!defined $enum_prefix) {
351                 $enum_prefix = "";
352             } else {
353                 # Trim so that it ends in an underscore
354                 $enum_prefix =~ s/_[^_]*$/_/;
355             }
356         } else {
357             # canonicalize user defined prefixes
358             $enum_prefix = uc($enum_prefix);
359             $enum_prefix =~ s/-/_/g;
360             $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
361         }
362         
363         for $entry (@entries) {
364             my ($name,$num,$nick) = @{$entry};
365             if (!defined $nick) {
366                 ($nick = $name) =~ s/^$enum_prefix//;
367                 $nick =~ tr/_/-/;
368                 $nick = lc($nick);
369                 @{$entry} = ($name, $num, $nick);
370             }
371         }
372         
373
374         # Spit out the output
375         if (defined $option_underscore_name) {
376             $enumlong = uc $option_underscore_name;
377             $enumsym = lc $option_underscore_name;
378             $enumshort = $enumlong;
379             $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
380
381             $enumname_prefix = $enumlong;
382             $enumname_prefix =~ s/_$enumshort$//;
383         } else {
384             # enumname is e.g. GMatchType
385             $enspace = $enumname;
386             $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
387
388             $enumshort = $enumname;
389             $enumshort =~ s/^[A-Z][a-z]*//;
390             $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
391             $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
392             $enumshort = uc($enumshort);
393
394             $enumname_prefix = $enumname;
395             $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
396             $enumname_prefix = uc($enumname_prefix);
397
398             $enumlong = uc($enspace) . "_" . $enumshort;
399             $enumsym = lc($enspace) . "_" . lc($enumshort);
400
401             if (defined($option_lowercase_name)) {
402                 $enumsym = $option_lowercase_name;
403             }
404         }
405
406         if ($firstenum) {
407             $firstenum = 0;
408             
409             if (length($fprod)) {
410                 my $prod = $fprod;
411                 my $base = basename ($ARGV);
412
413                 $prod =~ s/\@filename\@/$ARGV/g;
414                 $prod =~ s/\@basename\@/$base/g;
415                 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
416                 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
417                 chomp ($prod);
418                 
419                 print "$prod\n";
420             }
421         }
422         
423         if (length($eprod)) {
424             my $prod = $eprod;
425
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             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/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             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
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;
436             chomp ($prod);
437
438             print "$prod\n";
439         }
440
441         if (length($vhead)) {
442             my $prod = $vhead;
443
444             $prod =~ s/\@enum_name\@/$enumsym/g;
445             $prod =~ s/\@EnumName\@/$enumname/g;
446             $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
447             $prod =~ s/\@ENUMNAME\@/$enumlong/g;
448             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
449             if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
450             if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
451             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
452             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
453             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
454             chomp ($prod);
455             
456             print "$prod\n";
457         }
458
459         if (length($vprod)) {
460             my $prod = $vprod;
461             my $next_num = 0;
462             
463             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
464             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
465             for (@entries) {
466                 my ($name,$num,$nick) = @{$_};
467                 my $tmp_prod = $prod;
468
469                 if ($prod =~ /\@valuenum\@/) {
470                     # only attempt to eval the value if it is requested
471                     # this prevents us from throwing errors otherwise
472                     if (defined $num) {
473                         # use sandboxed perl evaluation as a reasonable
474                         # approximation to C constant folding
475                         $num = $sandbox->reval ($num);
476
477                         # make sure it parsed to an integer
478                         if (!defined $num or $num !~ /^-?\d+$/) {
479                             die "Unable to parse enum value '$num'";
480                         }
481                     } else {
482                         $num = $next_num;
483                     }
484
485                     $tmp_prod =~ s/\@valuenum\@/$num/g;
486                     $next_num = $num + 1;
487                 }
488
489                 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
490                 $tmp_prod =~ s/\@valuenick\@/$nick/g;
491                 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
492                 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
493                 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
494                 chomp ($tmp_prod);
495
496                 print "$tmp_prod\n";
497             }
498         }
499
500         if (length($vtail)) {
501             my $prod = $vtail;
502
503             $prod =~ s/\@enum_name\@/$enumsym/g;
504             $prod =~ s/\@EnumName\@/$enumname/g;
505             $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
506             $prod =~ s/\@ENUMNAME\@/$enumlong/g;
507             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
508             if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
509             if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
510             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
511             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
512             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
513             chomp ($prod);
514             
515             print "$prod\n";
516         }
517     }
518 }
519
520 if (length($ftail)) {
521     my $prod = $ftail;
522     my $base = basename ($ARGV);
523
524     $prod =~ s/\@filename\@/$ARGV/g;
525     $prod =~ s/\@basename\@/$base/g;
526     $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
527     $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
528     chomp ($prod);
529                 
530     print "$prod\n";
531 }
532
533 # put auto-generation comment
534 {
535     my $comment = $comment_tmpl;
536     $comment =~ s/\@comment\@/Generated data ends here/;
537     print "\n" . $comment . "\n\n";
538 }