Fix various bugs and excessive stack usage that crept in the conversion
[platform/upstream/glib.git] / gobject / glib-mkenums.in
1 #!@PERL_PATH@ -w
2
3 # glib-mkenums.pl 
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
13
14 sub parse_trigraph {
15     my $opts = shift;
16     my @opts;
17
18     for $opt (split /\s*,\s*/, $opts) {
19         $opt =~ s/^\s*//;
20         $opt =~ s/\s*$//;
21         my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
22         defined $val or $val = 1;
23         push @opts, $key, $val;
24     }
25     @opts;
26 }
27 sub parse_entries {
28     my $file = shift;
29     my $file_name = shift;
30     my $looking_for_name = 0;
31     
32     while (<$file>) {
33         # read lines until we have no open comments
34         while (m@/\*([^*]|\*(?!/))*$@) {
35             my $new;
36             defined ($new = <$file>) || die "Unmatched comment in $ARGV";
37             $_ .= $new;
38         }
39         # strip comments w/o options
40         s@/\*(?!<)
41             ([^*]+|\*(?!/))*
42            \*/@@gx;
43         
44         # strip newlines
45         s@\n@ @;
46         
47         # skip empty lines
48         next if m@^\s*$@;
49         
50         if ($looking_for_name) {
51             if (/^\s*(\w+)/) {
52                 $enumname = $1;
53                 return 1;
54             }
55         }
56         
57         # Handle include files
58         if (/^\#include\s*<([^>]*)>/ ) {
59             my $file= "../$1";
60             open NEWFILE, $file or die "Cannot open include file $file: $!\n";
61             
62             if (parse_entries (\*NEWFILE, $NEWFILE)) {
63                 return 1;
64             } else {
65                 next;
66             }
67         }
68         
69         if (/^\s*\}\s*(\w+)/) {
70             $enumname = $1;
71             $enumindex++;
72             return 1;
73         }
74         
75         if (/^\s*\}/) {
76             $enumindex++;
77             $looking_for_name = 1;
78             next;
79         }
80
81         if (m@^\s*
82               (\w+)\s*                   # name
83               (?:=(                      # value
84                    (?:[^,/]|/(?!\*))*
85                   ))?,?\s*
86               (?:/\*<                    # options
87                 (([^*]|\*(?!/))*)
88                >\s*\*/)?,?
89               \s*$
90              @x) {
91             my ($name, $value, $options) = ($1,$2,$3);
92
93             if (!defined $flags && defined $value && $value =~ /<</) {
94                 $seenbitshift = 1;
95             }
96
97             if (defined $options) {
98                 my %options = parse_trigraph($options);
99                 if (!defined $options{skip}) {
100                     push @entries, [ $name, $options{nick} ];
101                 }
102             } else {
103                 push @entries, [ $name ];
104             }
105         } elsif (m@^\s*\#@) {
106             # ignore preprocessor directives
107         } else {
108             print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
109         }
110     }
111
112     return 0;
113 }
114
115 sub version {
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";
122     exit 0;
123 }
124 sub usage {
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";
147     exit 0;
148 }
149
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
158 # other options
159 my $comment_tmpl = "/* \@comment\@ */";
160
161 if (!defined $ARGV[0]) {
162     usage;
163 }
164 while ($_ = $ARGV[0], /^-/) {
165     shift;
166     last if /^--$/;
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; }
177     else { usage; }
178 }
179
180 # put auto-generation comment
181 {
182     my $comment = $comment_tmpl;
183     $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
184     print "\n" . $comment . "\n\n";
185 }
186
187 if (length($fhead)) {
188     my $prod = $fhead;
189
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;
193                 
194     print "$prod\n";
195 }
196
197 while (<>) {
198     if (eof) {
199         close (ARGV);           # reset line numbering
200         $firstenum = 1;         # Flag to print filename at next enum
201     }
202
203     # read lines until we have no open comments
204     while (m@/\*([^*]|\*(?!/))*$@) {
205         my $new;
206         defined ($new = <>) || die "Unmatched comment in $ARGV";
207         $_ .= $new;
208     }
209     # strip comments w/o options
210     s@/\*(?!<)
211        ([^*]+|\*(?!/))*
212        \*/@@gx;
213         
214     if (m@^\s*typedef\s+enum\s*
215            ({)?\s*
216            (?:/\*<
217              (([^*]|\*(?!/))*)
218             >\s*\*/)?
219          @x) {
220         if (defined $2) {
221             my %options = parse_trigraph ($2);
222             next if defined $options{skip};
223             $enum_prefix = $options{prefix};
224             $flags = $options{flags};
225         } else {
226             $enum_prefix = undef;
227             $flags = undef;
228         }
229         # Didn't have trailing '{' look on next lines
230         if (!defined $1) {
231             while (<>) {
232                 if (s/^\s*\{//) {
233                     last;
234                 }
235             }
236         }
237
238         $seenbitshift = 0;
239         @entries = ();
240
241         # Now parse the entries
242         parse_entries (\*ARGV, $ARGV);
243
244         # figure out if this was a flags or enums enumeration
245         if (!defined $flags) {
246             $flags = $seenbitshift;
247         }
248
249         # Autogenerate a prefix
250         if (!defined $enum_prefix) {
251             for (@entries) {
252                 my $nick = $_->[1];
253                 if (!defined $nick) {
254                     my $name = $_->[0];
255                     if (defined $enum_prefix) {
256                         my $tmp = ~ ($name ^ $enum_prefix);
257                         ($tmp) = $tmp =~ /(^\xff*)/;
258                         $enum_prefix = $enum_prefix & $tmp;
259                     } else {
260                         $enum_prefix = $name;
261                     }
262                 }
263             }
264             if (!defined $enum_prefix) {
265                 $enum_prefix = "";
266             } else {
267                 # Trim so that it ends in an underscore
268                 $enum_prefix =~ s/_[^_]*$/_/;
269             }
270         } else {
271             # canonicalize user defined prefixes
272             $enum_prefix = uc($enum_prefix);
273             $enum_prefix =~ s/-/_/g;
274             $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
275         }
276         
277         for $entry (@entries) {
278             my ($name,$nick) = @{$entry};
279             if (!defined $nick) {
280                 ($nick = $name) =~ s/^$enum_prefix//;
281                 $nick =~ tr/_/-/;
282                 $nick = lc($nick);
283                 @{$entry} = ($name, $nick);
284             }
285         }
286         
287
288         # Spit out the output
289         
290         # enumname is e.g. GMatchType
291         $enspace = $enumname;
292         $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
293         
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);
299
300         $enumlong = uc($enspace) . "_" . $enumshort;
301         $enumsym = lc($enspace) . "_" . lc($enumshort);
302
303         if ($firstenum) {
304             $firstenum = 0;
305             
306             if (length($fprod)) {
307                 my $prod = $fprod;
308
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;
312                 
313                 print "$prod\n";
314             }
315         }
316         
317         if (length($eprod)) {
318             my $prod = $eprod;
319
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;
329
330             print "$prod\n";
331         }
332
333         if (length($vhead)) {
334             my $prod = $vhead;
335
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;
345             
346             print "$prod\n";
347         }
348
349         if (length($vprod)) {
350             my $prod = $vprod;
351             
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;
354             for (@entries) {
355                 my ($name,$nick) = @{$_};
356                 my $tmp_prod = $vprod;
357
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; }
363
364                 print "$tmp_prod\n";
365             }
366         }
367
368         if (length($vtail)) {
369             my $prod = $vtail;
370
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;
380             
381             print "$prod\n";
382         }
383     }
384 }
385
386 if (length($ftail)) {
387     my $prod = $ftail;
388
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;
392                 
393     print "$prod\n";
394 }
395
396 # put auto-generation comment
397 {
398     my $comment = $comment_tmpl;
399     $comment =~ s/\@comment\@/Generated data ends here/;
400     print "\n" . $comment . "\n\n";
401 }