summaryrefslogtreecommitdiffstats
path: root/src/util/gen.pl
blob: 9d2a3a1804beabbe0f69f8be5b0820858fc58b94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- perl -*-

# Crude template instantiation hack.
#
# The template named on the command line maps to a perl module t_$foo
# which defines certain methods including variable processing and
# output generation.  It can also suck in additional template modules
# for internal use.  One output file is generated, which typically
# contains structures and inline functions, and should be included by
# other files which will define, for example, the typedefname
# parameters supplied to this script.

# To do:
# Find a way to make dependency generation automatic.
# Make it less gross.

sub usage {
    print STDERR "usage: $0 TemplateName [-oOutputFile] PARM=value ...\n";
    print STDERR "  where acceptable PARM values depend on the template\n";
    exit(1);
}

my $orig_args = join(" ", @ARGV);
my $templatename = shift @ARGV || &usage;
my $outfile = shift @ARGV || &usage;
my $x;

eval "require t_$templatename;" || die;
eval "\$x = new t_$templatename;" || die;

sub getparms {
    my $arg;
    my $outfile;
    my %allowed_parms = ();

    foreach $arg (@ARGV) {
	my @words = split '=', $arg;
	if ($#words != 1) {
	    print STDERR "$0: $arg : #words = $#words\n";
	    &usage;
	}
	$x->setparm($words[0], $words[1]);
    }
}

sub generate {
    open OUTFILE, ">$outfile" || die;
    print OUTFILE "/*\n";
    print OUTFILE " * This file is generated, please don't edit it.\n";
    print OUTFILE " * script: $0\n";
    print OUTFILE " * args:   $orig_args\n";
    print OUTFILE " * The rest of this file is copied from a template, with\n";
    print OUTFILE " * substitutions.  See the template for copyright info.\n";
    print OUTFILE " */\n";
    $x->output(\*OUTFILE);
    close OUTFILE;
}

&getparms;
&generate;
exit (0);