1998-10-24 13:19:30 +08:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2006-12-10 05:33:38 +08:00
|
|
|
# GIMP - The GNU Image Manipulation Program
|
2003-07-03 08:47:26 +08:00
|
|
|
# Copyright (C) 1998-2003 Manish Singh <yosh@gimp.org>
|
1998-10-24 13:19:30 +08:00
|
|
|
|
2009-01-18 06:28:01 +08:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
1998-10-24 13:19:30 +08:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
1998-10-24 13:19:30 +08:00
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2018-07-12 05:27:07 +08:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1998-10-24 13:19:30 +08:00
|
|
|
|
1999-04-04 13:59:08 +08:00
|
|
|
require 5.004;
|
1998-10-24 13:19:30 +08:00
|
|
|
|
|
|
|
BEGIN {
|
2011-11-07 00:33:20 +08:00
|
|
|
$srcdir = $ENV{srcdir} || '.';
|
|
|
|
$destdir = $ENV{destdir} || '.';
|
|
|
|
$builddir = $ENV{builddir} || '.';
|
1998-10-24 13:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
use lib $srcdir;
|
|
|
|
|
1999-04-23 14:55:37 +08:00
|
|
|
BEGIN {
|
|
|
|
# Some important stuff
|
|
|
|
require 'pdb.pl';
|
|
|
|
require 'enums.pl';
|
|
|
|
require 'util.pl';
|
|
|
|
|
|
|
|
# What to do?
|
|
|
|
require 'groups.pl';
|
|
|
|
|
1999-04-28 15:03:35 +08:00
|
|
|
if ($ENV{PDBGEN_GROUPS}) {
|
|
|
|
@groups = split(/:/, $ENV{PDBGEN_GROUPS});
|
1999-04-23 14:55:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
# Stifle "used only once" warnings
|
|
|
|
$destdir = $destdir;
|
2011-11-07 00:33:20 +08:00
|
|
|
$builddir = $builddir;
|
1998-10-24 13:19:30 +08:00
|
|
|
%pdb = ();
|
|
|
|
|
|
|
|
# The actual parser (in a string so we can eval it in another namespace)
|
|
|
|
$evalcode = <<'CODE';
|
|
|
|
{
|
|
|
|
my $file = $main::file;
|
|
|
|
my $srcdir = $main::srcdir;
|
1999-04-04 13:59:08 +08:00
|
|
|
|
|
|
|
my $copyvars = sub {
|
|
|
|
my $dest = shift;
|
|
|
|
|
|
|
|
foreach (@_) {
|
1999-07-29 07:21:11 +08:00
|
|
|
if (eval "defined scalar $_") {
|
1999-04-04 13:59:08 +08:00
|
|
|
(my $var = $_) =~ s/^(\W)//;
|
1999-07-29 07:21:11 +08:00
|
|
|
for ($1) {
|
1999-04-04 13:59:08 +08:00
|
|
|
/\$/ && do { $$dest->{$var} = $$var ; last; };
|
|
|
|
/\@/ && do { $$dest->{$var} = [ @$var ]; last; };
|
|
|
|
/\%/ && do { $$dest->{$var} = { %$var }; last; };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
1998-10-24 13:19:30 +08:00
|
|
|
|
|
|
|
# Variables to evaluate and insert into the PDB structure
|
2004-05-14 03:59:11 +08:00
|
|
|
my @procvars = qw($name $group $blurb $help $author $copyright $date $since
|
2019-08-08 01:09:00 +08:00
|
|
|
$deprecated @inargs @outargs %invoke $canonical_name
|
|
|
|
$lib_private);
|
1998-10-24 13:19:30 +08:00
|
|
|
|
1999-04-04 13:59:08 +08:00
|
|
|
# These are attached to the group structure
|
2010-07-07 17:43:10 +08:00
|
|
|
my @groupvars = qw($desc $doc_title $doc_short_desc $doc_long_desc
|
2019-08-08 00:43:21 +08:00
|
|
|
$lib_private
|
2010-07-07 17:43:10 +08:00
|
|
|
@headers %extra);
|
1999-04-04 13:59:08 +08:00
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
# Hook some variables into the top-level namespace
|
|
|
|
*pdb = \%main::pdb;
|
|
|
|
*gen = \%main::gen;
|
|
|
|
*grp = \%main::grp;
|
|
|
|
|
|
|
|
# Hide our globals
|
|
|
|
my $safeeval = sub { local(%pdb, %gen, %grp); eval $_[0]; die $@ if $@ };
|
|
|
|
|
|
|
|
# Some standard shortcuts used by all def files
|
|
|
|
&$safeeval("do '$main::srcdir/stddefs.pdb'");
|
|
|
|
|
|
|
|
# Group properties
|
1999-04-04 13:59:08 +08:00
|
|
|
foreach (@groupvars) { eval "undef $_" }
|
1998-10-24 13:19:30 +08:00
|
|
|
|
|
|
|
# Load the file in and get the group info
|
2017-12-18 01:41:34 +08:00
|
|
|
&$safeeval("require '$main::srcdir/groups/$file.pdb'");
|
1998-10-24 13:19:30 +08:00
|
|
|
|
|
|
|
# Save these for later
|
1999-04-04 13:59:08 +08:00
|
|
|
&$copyvars(\$grp{$file}, @groupvars);
|
1998-10-24 13:19:30 +08:00
|
|
|
|
1999-03-18 07:08:08 +08:00
|
|
|
foreach $proc (@procs) {
|
1998-10-24 13:19:30 +08:00
|
|
|
# Reset all our PDB vars so previous defs don't interfere
|
|
|
|
foreach (@procvars) { eval "undef $_" }
|
|
|
|
|
|
|
|
# Get the info
|
|
|
|
&$safeeval("&$proc");
|
|
|
|
|
|
|
|
# Some derived fields
|
|
|
|
$name = $proc;
|
|
|
|
$group = $file;
|
|
|
|
|
2005-08-06 02:19:09 +08:00
|
|
|
($canonical_name = $name) =~ s/_/-/g;
|
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
# Load the info into %pdb, making copies of the data instead of refs
|
|
|
|
my $entry = {};
|
1999-04-04 13:59:08 +08:00
|
|
|
&$copyvars(\$entry, @procvars);
|
1998-10-24 13:19:30 +08:00
|
|
|
$pdb{$proc} = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find out what to do with these entries
|
1999-04-04 13:59:08 +08:00
|
|
|
while (my ($dest, $procs) = each %exports) { push @{$gen{$dest}}, @$procs }
|
1998-10-24 13:19:30 +08:00
|
|
|
}
|
|
|
|
CODE
|
|
|
|
|
|
|
|
# Slurp in the PDB defs
|
|
|
|
foreach $file (@groups) {
|
2017-12-18 01:41:34 +08:00
|
|
|
print "Processing $srcdir/groups/$file.pdb...\n";
|
1998-10-24 13:19:30 +08:00
|
|
|
eval "package Gimp::CodeGen::Safe::$file; $evalcode;";
|
|
|
|
die $@ if $@;
|
|
|
|
}
|
|
|
|
|
2013-09-14 09:12:26 +08:00
|
|
|
# Squash whitespace into just single spaces between words.
|
|
|
|
# Single new lines are considered as normal spaces, but n > 1 newlines are considered (n - 1) newlines.
|
2018-04-19 02:57:03 +08:00
|
|
|
# The slightly complicated suite of regexp is so that \n\s+\n is still considered a double newline.
|
2014-05-29 22:26:16 +08:00
|
|
|
sub trimspace { for (${$_[0]}) { s/(\S)[\ \t\r\f]*\n[\ \t\r\f]*(\S)/$1 $2/g; s/[\ \t\r\f]+/ /gs;
|
|
|
|
s/\n(([\ \t\r\f]*\n)+)/$1/g; s/[\ \t\r\f]*\n[\ \t\r\f]/\n/g ; s/^\s+//; s/\s+$//; } }
|
1998-10-24 13:19:30 +08:00
|
|
|
|
|
|
|
# Trim spaces and escape quotes C-style
|
|
|
|
sub nicetext {
|
|
|
|
my $val = shift;
|
1999-12-15 04:01:01 +08:00
|
|
|
if (defined $$val) {
|
|
|
|
&trimspace($val);
|
|
|
|
$$val =~ s/"/\\"/g;
|
|
|
|
}
|
1998-10-24 13:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Do the same for all the strings in the args, plus expand constraint text
|
|
|
|
sub niceargs {
|
|
|
|
my $args = shift;
|
|
|
|
foreach $arg (@$args) {
|
|
|
|
foreach (keys %$arg) {
|
|
|
|
&nicetext(\$arg->{$_});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Trim spaces from all the elements in a list
|
|
|
|
sub nicelist {
|
|
|
|
my $list = shift;
|
|
|
|
foreach (@$list) { &trimspace(\$_) }
|
|
|
|
}
|
|
|
|
|
1999-03-11 02:56:56 +08:00
|
|
|
# Add args for array lengths
|
|
|
|
|
|
|
|
sub arrayexpand {
|
|
|
|
my $args = shift;
|
|
|
|
my $newargs;
|
|
|
|
|
|
|
|
foreach (@$$args) {
|
2000-01-19 05:39:03 +08:00
|
|
|
if (exists $_->{array}) {
|
1999-03-11 02:56:56 +08:00
|
|
|
my $arg = $_->{array};
|
|
|
|
|
|
|
|
$arg->{name} = 'num_' . $_->{name} unless exists $arg->{name};
|
|
|
|
|
|
|
|
# We can't have negative lengths, but let them set a min number
|
|
|
|
unless (exists $arg->{type}) {
|
2010-06-06 01:26:06 +08:00
|
|
|
$arg->{type} = '0 <= int32';
|
1999-03-11 02:56:56 +08:00
|
|
|
}
|
|
|
|
elsif ($arg->{type} !~ /^\s*\d+\s*</) {
|
2010-06-06 01:26:06 +08:00
|
|
|
$arg->{type} = '0 <= ' . $arg->{type};
|
1999-03-11 02:56:56 +08:00
|
|
|
}
|
|
|
|
|
1999-12-26 15:54:39 +08:00
|
|
|
$arg->{void_ret} = 1 if exists $_->{void_ret};
|
|
|
|
|
1999-03-11 02:56:56 +08:00
|
|
|
$arg->{num} = 1;
|
|
|
|
|
|
|
|
push @$newargs, $arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
push @$newargs, $_;
|
|
|
|
}
|
|
|
|
|
|
|
|
$$args = $newargs;
|
|
|
|
}
|
|
|
|
|
2005-08-06 02:19:09 +08:00
|
|
|
sub canonicalargs {
|
|
|
|
my $args = shift;
|
|
|
|
foreach $arg (@$args) {
|
|
|
|
($arg->{canonical_name} = $arg->{name}) =~ s/_/-/g;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
# Post-process each pdb entry
|
|
|
|
while ((undef, $entry) = each %pdb) {
|
|
|
|
&nicetext(\$entry->{blurb});
|
|
|
|
&nicetext(\$entry->{help});
|
|
|
|
&nicetext(\$entry->{author});
|
|
|
|
&nicetext(\$entry->{copyright});
|
|
|
|
&nicetext(\$entry->{date});
|
1999-03-11 02:56:56 +08:00
|
|
|
|
1999-03-11 07:34:26 +08:00
|
|
|
foreach (qw(in out)) {
|
|
|
|
my $args = $_ . 'args';
|
|
|
|
if (exists $entry->{$args}) {
|
|
|
|
&arrayexpand(\$entry->{$args});
|
|
|
|
&niceargs($entry->{$args});
|
2005-08-06 02:19:09 +08:00
|
|
|
&canonicalargs($entry->{$args});
|
1999-03-11 07:34:26 +08:00
|
|
|
}
|
|
|
|
}
|
1999-03-11 02:56:56 +08:00
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
&nicelist($entry->{invoke}{headers}) if exists $entry->{invoke}{headers};
|
1999-03-11 02:56:56 +08:00
|
|
|
&nicelist($entry->{globals}) if exists $entry->{globals};
|
|
|
|
|
|
|
|
$entry->{invoke}{success} = 'TRUE' unless exists $entry->{invoke}{success};
|
1998-10-24 13:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Generate code from the modules
|
|
|
|
my $didstuff;
|
|
|
|
while (@ARGV) {
|
|
|
|
my $type = shift @ARGV;
|
1999-03-11 02:56:56 +08:00
|
|
|
|
1998-10-24 13:19:30 +08:00
|
|
|
print "\nProcessing $type...\n";
|
|
|
|
|
|
|
|
if (exists $gen{$type}) {
|
|
|
|
require "$type.pl";
|
|
|
|
&{"Gimp::CodeGen::${type}::generate"}($gen{$type});
|
|
|
|
print "done.\n";
|
|
|
|
$didstuff = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "nothing to do.\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print "\nNothing done at all.\n" unless $didstuff;
|