| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Getopt::Std; | ||
| 5 | use IO::File; | ||
| 6 | |||
| 7 | sub usage { | ||
| 8 | print "usage: tvgen.pl [-p prefix]\n"; | ||
| 9 | } | ||
| 10 | |||
| 11 | my %opt; | ||
| 12 | getopts("tp:",\%opt) or &usage; | ||
| 13 | @ARGV==0 or &usage; | ||
| 14 | |||
| 15 | $opt{p} ||= 'MYPREFIX_'; | ||
| 16 | print "Using prefix '$opt{p}'...\n"; | ||
| 17 | |||
| 18 | my $dname = 'Tree/Fat'; | ||
| 19 | my $LIB; | ||
| 20 | for my $d (@INC) { | ||
| 21 | if (-f "$d/$dname/tv.sym") { | ||
| 22 | $LIB = "$d/$dname"; | ||
| 23 | last; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | die "tvgen.pl: cannot find tv template files in \@INC (".join(' ', @INC).")" | ||
| 27 | if !$LIB; | ||
| 28 | |||
| 29 | my %sym; { | ||
| 30 | my $sym = new IO::File; | ||
| 31 | $sym->open("$LIB/tv.sym") or die "open $LIB/tv.sym: $!"; | ||
| 32 | my @sym = <$sym>; | ||
| 33 | chop(@sym); | ||
| 34 | for my $s (@sym) { | ||
| 35 | if ($s =~ m/^ struct \s+ (\w+) $/x) { | ||
| 36 | $sym{'struct \s+ '.$1} = "struct $opt{p}$1"; | ||
| 37 | } else { | ||
| 38 | $sym{$s} = "$opt{p}$s"; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | $sym{'tv\.seek'} = "$opt{p}tvseek.h"; | ||
| 42 | $sym{'tv\.public'} = "$opt{p}tvpub.h"; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $conf_h = "./$opt{p}tv.tmpl"; | ||
| 46 | my %fmap = ( | ||
| 47 | "$LIB/tv.code" => "$opt{p}tv.c", | ||
| 48 | "$LIB/tv.seek" => "$opt{p}tvseek.h", | ||
| 49 | "$LIB/tv.private" => "$opt{p}tvpriv.h", | ||
| 50 | "$LIB/tv.public" => "$opt{p}tvpub.h", | ||
| 51 | $conf_h => "$opt{p}tv.h", | ||
| 52 | ); | ||
| 53 | for my $f (qw(tvcommon.c tvcommon.h)) { | ||
| 54 | if (!-e $f or (stat("$LIB/$f"))[9] > (stat($f))[9]) { | ||
| 55 | my $c = "cp -f $LIB/$f $f"; | ||
| 56 | print "$c\n"; | ||
| 57 | system($c)==0 or die "$c: $!"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | if (!-e 'tvcommon0.h') { | ||
| 61 | my $c = "cp -i $LIB/tvcommon0.h tvcommon0.h"; | ||
| 62 | print "$c\n"; | ||
| 63 | system($c); | ||
| 64 | chmod 0666, 'tvcommon0.h'; | ||
| 65 | } | ||
| 66 | if (!-e $conf_h) { | ||
| 67 | print "cp -i $LIB/tv.setup $conf_h\n"; | ||
| 68 | system("cp -i $LIB/tv.setup $conf_h")==0 or die "cp $LIB/tv.setup $conf_h: $!"; | ||
| 69 | chmod(0666, $conf_h); | ||
| 70 | } | ||
| 71 | |||
| 72 | my $fh = new IO::File; | ||
| 73 | my $to = new IO::File; | ||
| 74 | for my $f (sort keys %fmap) { | ||
| 75 | # my $stem = $f; | ||
| 76 | # $stem =~ s,^.*\/,,; | ||
| 77 | $fh->open("$f") or die "open $f: $!"; | ||
| 78 | $to->open(">".$fmap{$f}) or die "open >$fmap{$f}: $!"; | ||
| 79 | $to->print("/* Doing namespace management in -*-C-*- is very messy. */\n"); | ||
| 80 | $to->print("/* Generated by tvgen.pl at ".localtime()."! */\n"); | ||
| 81 | $to->print("/* CCov: off */\n") if !$opt{'t'}; | ||
| 82 | $to->print("/* !! DO NOT MODIFY THIS FILE !! */\n"); | ||
| 83 | if ($f =~ m/tv.code$/) { | ||
| 84 | $to->print(qq{\#include "$opt{p}tv.h"\n}); | ||
| 85 | $to->print("#define _TV_SNEAKY_ON_\n"); | ||
| 86 | $to->print(qq{\#include "$opt{p}tvpriv.h"\n}); | ||
| 87 | } | ||
| 88 | $to->print("/* Line numbers are relative to the original (unrenamed) file! */\n"); | ||
| 89 | $to->print(qq{\#line 1 "$f"\n}); | ||
| 90 | my $l = join('', $fh->getlines); | ||
| 91 | $l =~ s/ \/\* \s* CCov: .*? \*\/ //sxg if !$opt{'t'}; | ||
| 92 | while (my ($k,$v)=each %sym) { | ||
| 93 | $l =~ s/\b $k \b/$v/gx; | ||
| 94 | } | ||
| 95 | $to->print($l); | ||
| 96 | $fh->close; | ||
| 97 | $to->close; | ||
| 98 | print "$f -> $fmap{$f}\n"; | ||
| 99 | } | ||
| 100 | print "Done!\n"; |