[kdbus] Do not set body message if signature field is empty
[platform/upstream/glib.git] / gobject / glib-mkenums.in
index 1e40622..b776e1f 100755 (executable)
@@ -1,6 +1,8 @@
-#!@PERL_PATH@ -w
+#! @PERL_PATH@
 
+use warnings;
 use File::Basename;
+use Safe;
 
 # glib-mkenums.pl 
 # Information about the current enumeration
@@ -21,6 +23,7 @@ my $enumname_prefix;          # prefix of $enumname
 my $enumindex = 0;             # Global enum counter
 my $firstenum = 1;             # Is this the first enumeration per file?
 my @entries;                   # [ $name, $val ] for each entry
+my $sandbox = Safe->new;        # sandbox for safe evaluation of expressions
 
 sub parse_trigraph {
     my $opts = shift;
@@ -110,10 +113,10 @@ sub parse_entries {
            if (defined $options) {
                my %options = parse_trigraph($options);
                if (!defined $options{skip}) {
-                   push @entries, [ $name, $options{nick} ];
+                   push @entries, [ $name, $value, $options{nick} ];
                }
            } else {
-               push @entries, [ $name ];
+               push @entries, [ $name, $value ];
            }
        } elsif (m@^\s*\#@) {
            # ignore preprocessor directives
@@ -140,16 +143,18 @@ sub usage {
     print "Help Options:\n";
     print "  -h, --help            Show this help message\n\n";
     print "Utility Options:\n";
-    print "  --fhead <text>        Output file header\n";
-    print "  --fprod <text>        Per input file production\n";
-    print "  --ftail <text>        Output file trailer\n";
-    print "  --eprod <text>        Per enum text (produced prior to value itarations)\n";
-    print "  --vhead <text>        Value header, produced before iterating over enum values\n";
-    print "  --vprod <text>        Value text, produced for each enum value\n";
-    print "  --vtail <text>        Value tail, produced after iterating over enum values\n";
-    print "  --comments <text>     Comment structure\n";
-    print "  --template file       Template file\n";
-    print "  -v, --version         Print version informations\n\n";
+    print "  --identifier-prefix <text>   Identifier prefix\n";
+    print "  --symbol-prefix <text>       Symbol prefix\n";
+    print "  --fhead <text>               Output file header\n";
+    print "  --fprod <text>               Per input file production\n";
+    print "  --ftail <text>               Output file trailer\n";
+    print "  --eprod <text>               Per enum text (produced prior to value itarations)\n";
+    print "  --vhead <text>               Value header, produced before iterating over enum values\n";
+    print "  --vprod <text>               Value text, produced for each enum value\n";
+    print "  --vtail <text>               Value tail, produced after iterating over enum values\n";
+    print "  --comments <text>            Comment structure\n";
+    print "  --template file              Template file\n";
+    print "  -v, --version                Print version informations\n\n";
     print "Production text substitutions:\n";
     print "  \@EnumName\@            PrefixTheXEnum\n";
     print "  \@enum_name\@           prefix_the_xenum\n";
@@ -158,6 +163,7 @@ sub usage {
     print "  \@ENUMPREFIX\@          PREFIX\n";
     print "  \@VALUENAME\@           PREFIX_THE_XVALUE\n";
     print "  \@valuenick\@           the-xvalue\n";
+    print "  \@valuenum\@            the integer value (limited support, Since: 2.26)\n";
     print "  \@type\@                either enum or flags\n";
     print "  \@Type\@                either Enum or Flags\n";
     print "  \@TYPE\@                either ENUM or FLAGS\n";
@@ -167,6 +173,8 @@ sub usage {
 }
 
 # production variables:
+my $idprefix = "";    # "G", "Gtk", etc
+my $symprefix = "";   # "g", "gtk", etc, if not just lc($idprefix)
 my $fhead = "";   # output file header
 my $fprod = "";   # per input file production
 my $ftail = "";   # output file trailer
@@ -174,8 +182,7 @@ my $eprod = "";   # per enum text (produced prior to value itarations)
 my $vhead = "";   # value header, produced before iterating over enum values
 my $vprod = "";   # value text, produced for each enum value
 my $vtail = "";   # value tail, produced after iterating over enum values
-# other options
-my $comment_tmpl = "/* \@comment\@ */";
+my $comment_tmpl = "";   # comment template
 
 sub read_template_file {
   my ($file) = @_;
@@ -218,6 +225,9 @@ sub read_template_file {
   $vprod = $tmpl{'value-production'};
   $vtail = $tmpl{'value-tail'};
   $comment_tmpl = $tmpl{'comment'};
+
+  # default to C-style comments
+  $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
 }
 
 if (!defined $ARGV[0]) {
@@ -227,6 +237,8 @@ while ($_=$ARGV[0],/^-/) {
     shift;
     last if /^--$/;
     if (/^--template$/)                      { read_template_file (shift); }
+    elsif (/^--identifier-prefix$/)          { $idprefix = shift }
+    elsif (/^--symbol-prefix$/)              { $symprefix = shift }
     elsif (/^--fhead$/)                      { $fhead = $fhead . shift }
     elsif (/^--fprod$/)                      { $fprod = $fprod . shift }
     elsif (/^--ftail$/)                      { $ftail = $ftail . shift }
@@ -235,7 +247,7 @@ while ($_=$ARGV[0],/^-/) {
     elsif (/^--vprod$/)                      { $vprod = $vprod . shift }
     elsif (/^--vtail$/)                      { $vtail = $vtail . shift }
     elsif (/^--comments$/)                   { $comment_tmpl = shift }
-    elsif (/^--help$/ || /^-h$/ || /^-h$/)   { usage; }
+    elsif (/^--help$/ || /^-h$/ || /^-\?$/)  { usage; }
     elsif (/^--version$/ || /^-v$/)          { version; }
     else { usage; }
     last if not defined($ARGV[0]);
@@ -278,6 +290,9 @@ while (<>) {
        ([^*]+|\*(?!/))*
        \*/@@gx;
        
+    # ignore forward declarations
+    next if /^\s*typedef\s+enum.*;/;
+
     if (m@^\s*typedef\s+enum\s*
            ({)?\s*
            (?:/\*<
@@ -309,6 +324,9 @@ while (<>) {
        # Didn't have trailing '{' look on next lines
        if (!defined $1 && !defined $4) {
            while (<>) {
+               if (eof) {
+                   die "Hit end of file while parsing enum in $ARGV";
+               }
                if (s/^\s*\{//) {
                    last;
                }
@@ -329,7 +347,7 @@ while (<>) {
        # Autogenerate a prefix
        if (!defined $enum_prefix) {
            for (@entries) {
-               my $nick = $_->[1];
+               my $nick = $_->[2];
                if (!defined $nick) {
                    my $name = $_->[0];
                    if (defined $enum_prefix) {
@@ -355,12 +373,12 @@ while (<>) {
        }
        
        for $entry (@entries) {
-           my ($name,$nick) = @{$entry};
+           my ($name,$num,$nick) = @{$entry};
             if (!defined $nick) {
                ($nick = $name) =~ s/^$enum_prefix//;
                $nick =~ tr/_/-/;
                $nick = lc($nick);
-               @{$entry} = ($name, $nick);
+               @{$entry} = ($name, $num, $nick);
             }
        }
        
@@ -373,8 +391,8 @@ while (<>) {
            $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
 
            $enumname_prefix = $enumlong;
-           $enumname_prefix =~ s/$enumshort$//;
-       } else {
+           $enumname_prefix =~ s/_$enumshort$//;
+       } elsif (!$symprefix && !$idprefix) {
            # enumname is e.g. GMatchType
            $enspace = $enumname;
            $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
@@ -395,6 +413,21 @@ while (<>) {
            if (defined($option_lowercase_name)) {
                $enumsym = $option_lowercase_name;
            }
+       } else {
+           $enumshort = $enumname;
+           if ($idprefix) {
+               $enumshort =~ s/^${idprefix}//;
+           } else {
+               $enumshort =~ s/^[A-Z][a-z]*//;
+           }
+           $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
+           $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
+           $enumshort = uc($enumshort);
+
+           $enumname_prefix = $symprefix && uc($symprefix) || uc($idprefix);
+
+           $enumlong = $enumname_prefix . "_" . $enumshort;
+           $enumsym = lc($enumlong);
        }
 
        if ($firstenum) {
@@ -452,13 +485,34 @@ while (<>) {
 
        if (length($vprod)) {
            my $prod = $vprod;
+            my $next_num = 0;
            
            $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
            $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
            for (@entries) {
-               my ($name,$nick) = @{$_};
+               my ($name,$num,$nick) = @{$_};
                my $tmp_prod = $prod;
 
+                if ($prod =~ /\@valuenum\@/) {
+                    # only attempt to eval the value if it is requested
+                    # this prevents us from throwing errors otherwise
+                    if (defined $num) {
+                        # use sandboxed perl evaluation as a reasonable
+                        # approximation to C constant folding
+                        $num = $sandbox->reval ($num);
+
+                        # make sure it parsed to an integer
+                        if (!defined $num or $num !~ /^-?\d+$/) {
+                            die "Unable to parse enum value '$num'";
+                        }
+                    } else {
+                        $num = $next_num;
+                    }
+
+                    $tmp_prod =~ s/\@valuenum\@/$num/g;
+                    $next_num = $num + 1;
+                }
+
                $tmp_prod =~ s/\@VALUENAME\@/$name/g;
                $tmp_prod =~ s/\@valuenick\@/$nick/g;
                if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }