summaryrefslogtreecommitdiffstats
path: root/src/util/ktemplate.pm
diff options
context:
space:
mode:
authorKen Raeburn <raeburn@mit.edu>2007-07-04 04:06:54 +0000
committerKen Raeburn <raeburn@mit.edu>2007-07-04 04:06:54 +0000
commit1016ef14acf2135c05fec0b8e06d0eb1c0434c35 (patch)
tree206085079d775cd4c22a1ecfc74b9a4fa4d70454 /src/util/ktemplate.pm
parent059e4efd1675acabc65237fb8ae1622389a5c846 (diff)
downloadkrb5-1016ef14acf2135c05fec0b8e06d0eb1c0434c35.tar.gz
krb5-1016ef14acf2135c05fec0b8e06d0eb1c0434c35.tar.xz
krb5-1016ef14acf2135c05fec0b8e06d0eb1c0434c35.zip
Perl code for generating "map" routines from a common template with
supplied type info. * ktemplate.pm: Code for parsing a command line and writing out a supplied template with substitutions. * gen-map.pl: Parameter info and template for "map" type. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@19669 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/util/ktemplate.pm')
-rw-r--r--src/util/ktemplate.pm67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/util/ktemplate.pm b/src/util/ktemplate.pm
new file mode 100644
index 000000000..f5f9ab7db
--- /dev/null
+++ b/src/util/ktemplate.pm
@@ -0,0 +1,67 @@
+# -*- perl -*-
+
+sub usage {
+ print STDERR "usage: $0 -oOutputFile PARM=value ...\n";
+ print STDERR " where acceptable PARM values are:\n";
+ print STDERR "\t", join(' ', @parms), "\n";
+ exit(1);
+}
+
+%parm = ();
+sub run {
+ my $arg;
+ my $outfile;
+ my %allowed_parms = ();
+
+ foreach $arg (@parms) { $allowed_parms{$arg} = 1; }
+
+ foreach $arg (@ARGV) {
+ if ($arg =~ /^-o./) {
+ if (defined $outfile) {
+ die "$0: Output file specified multiple times\n";
+ }
+ $outfile = substr($arg, 2);
+ } else {
+ my @words = split '=', $arg;
+ if ($#words != 1) {
+ print STDERR "$0: $arg : #words = $#words\n";
+ &usage;
+ }
+ if (!defined $allowed_parms{$words[0]}) {
+ print STDERR "$0: Unknown parameter $words[0]\n";
+ &usage;
+ }
+ $parm{$words[0]} = $words[1];
+ }
+ }
+ my $p;
+ my $subst = "";
+ #print "Keys defined: ", join(' ', keys %parm), "\n";
+ foreach $p (@parms) {
+ if (!defined $parm{$p}) {
+ die "$0: No value supplied for parameter $p\n";
+ }
+ # XXX More careful quoting of supplied value!
+ $subst .= "\t\$a =~ s|<$p>|$parm{$p}|go;\n";
+ }
+ $subst = "sub do_substitution {\n"
+ . "\tmy(\$a) = \@_;\n"
+ . $subst
+ . "\treturn \$a;\n"
+ . "}\n"
+ . "1;";
+ eval $subst || die;
+ if (defined $outfile) {
+ open OUTFILE, ">$outfile" || die;
+ } else {
+ print STDERR "$0: No output file specified.\n";
+ &usage;
+ }
+ while (<DATA>) {
+ print OUTFILE &do_substitution($_);
+ }
+ close OUTFILE;
+ exit (0);
+}
+
+1;