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