1#!/usr/local/bin/perl
2#
3#         $NetBSD: trrt2netbsd,v 1.4 1999/06/16 20:47:57 is Exp $
4#
5#  Perl script to convert a standard distribution directory for traceroute into
6#         a NetBSD source tree.
7#
8#  This is done as a script so that as each distribution is released,
9#         only changes from the previous one need to be dealt with as
10#         modifications to this script and related files.  This should
11#         reduce the cost of updating from a new release of traceroute by an
12#         order of magnitude (or more?)
13#
14#  This script requires two environment variables set:
15#         SRCDIR - traceroute source directory
16#         TARGETDIR - name of the high level directory to make
17#
18#  Written by Christos Zoulas Oct 2nd, 1997 for traceroute-1.4a5
19#
20
21$version = "1.4a5";
22
23# definitions ...
24
25@subdirs = ("usr.sbin/traceroute");
26
27@trrtf = ("ifaddrlist.c", "savestr.c", "traceroute.c");
28
29@trrthf = ("gnuc.h", "ifaddrlist.h", "savestr.h");
30
31@trrtmf = ("traceroute.8");
32@trrtdf = ("CHANGES", "README");
33@trrtaf = ("mean.awk", "median.awk");
34
35
36# sed edit list: file, sed-program
37%sedlist = ();
38
39#
40# Utility Subroutines
41#
42
43sub makedir {
44    system("mkdir -p @_");
45}
46
47# &fixrcs (fromfile, tofile);
48sub fixrcs
49{
50    my ($f, $t) = @_;
51    my @keywords = ("Author", "Date", "Header", "Id", "Locker", "Log",
52                        "Name", "RCSfile", "Revision", "Source", "State");
53    my $state = 0;
54    my $hdr = 0;
55
56    open(IFILE, "<$f") || die "Cannot open $f";
57    open(OFILE, ">$t") || die "Cannot create $t";
58
59    if ($t =~ /.*\.[0-9]/) {
60          print OFILE '.\\" $', 'NetBSD',  '$', "\n.\\\"", "\n";
61    }
62    elsif ($t =~ /.*\.[ch]/) {
63          print OFILE "/*\t", '$', 'NetBSD', '$', "\t*/\n\n";
64    }
65    elsif ($t =~ /.*\.[yl]/) {
66          $hdr = 1;
67    }
68    else {
69          print OFILE '$', 'NetBSD', '$', "\n";
70    }
71    while (<IFILE>) {
72          if ($hdr == 1) {
73              if (/%{/) {
74                    print OFILE "%{\n/*\t", '$', 'NetBSD', '$', "\t*/\n\n";
75                    $hdr = 0;
76                    next;
77              }
78          }
79          if ($state == 2) {
80              if (/#endif/) {
81                    print OFILE "#else\n__RCSID(", '"$', 'NetBSD', '$"',
82                        ");\n#endif\n";
83                    $state = 0;
84              }
85          }
86          if ($state == 1) {
87              print OFILE "#if 0\n";
88              $state = 2;
89          }
90          if (/#ifndef lint/) {
91              print OFILE "#include <sys/cdefs.h>\n";
92              $state = 1;
93          }
94          foreach $key (@keywords) {
95              s/\$$key\$/$key/g;
96              s/\$$key:(.*)\$/$key:$1/g;
97          }
98          print OFILE $_;
99    }
100    close(IFILE) || die "closing input file";
101    close(OFILE) || die "closing output file";
102}
103
104# &copyfiles (fromdir, todir, list of files);
105sub copyfiles {
106    local ($fdir, $tdir, @list) = @_;
107    local ($f);
108
109    foreach $f (@list) {
110          print "  $fdir/$f --> $tdir/$f\n";
111          &fixrcs("$fdir/$f", "$tdir/$f");
112    }
113}
114
115# &copyfile (fromfile, tofile);
116sub copyfile {
117    local ($f, $t) = @_;
118
119    print "  $f --> $t\n";
120    system ("cp $f $t");
121}
122
123sub uniq {
124    local (@inlist) = @_;
125    local (@outlist);
126
127    @outlist = ($inlist[0]);
128    for ( $i=1; $i < @inlist; $i++ ) {
129          if ($inlist[$i] ne $inlist[$i-1]) {
130              push (@outlist, $inlist[$i]);
131          }
132    }
133
134    @outlist;
135}
136
137sub dumpsrcs {
138    local (@names) = @_;
139    local ($count);
140
141    $count = 0;
142    while ($f = pop(@names)) {
143        print ODATA "$f ";
144          if ($count == 5 && @names > 0) {
145              print ODATA "\\\n";
146              $count = 0;
147          } else {
148              $count += 1;
149          }
150    }
151    if ($count != 0) {
152          print ODATA "\n";
153    }
154}
155
156#
157# Main program.
158#
159
160$srcdir = $ENV{'SRCDIR'};
161$targetdir = $ENV{'TARGETDIR'};
162$incdirs = "-I. -I$srcdir/config -I$srcdir";
163
164if (!$srcdir | !targetdir) {
165    die "You must define the environment variables SRCDIR and TARGETDIR.\n"
166}
167print "Making the NetBSD directory tree.\n";
168foreach $f (@subdirs) {
169    print "   -->$f\n";
170    makedir ("$targetdir/$f");
171}
172
173print "Populating the usr.sbin/traceroute directory.\n";
174&copyfiles ("$srcdir", "$targetdir/usr.sbin/traceroute", @trrtf, @trrthf, @trrtdf,
175              @trrtmf, @trrtaf);
176
177#
178# Build makefiles
179#
180
181$first = "True";
182while ($line = <DATA>) {
183    chop ($line);
184    if (substr($line,0,2) eq "%%") {
185          @cmd = split (/ /,$line);
186          if ($cmd[1] eq "file") {
187              print "Building $targetdir/$cmd[2]\n";
188              if ($first eq "") {
189                    close (ODATA);
190              } else {
191                    $first = "";
192              }
193              open (ODATA, ">$targetdir/$cmd[2]") ||
194                    die "Could not create $targetdir/$cmd[2]";
195          } elsif ($cmd[1] eq "awks") {
196              print "  Defining AWKS\n";
197              if ($first) {
198                    die "Data file must start with a %% file!";
199              }
200              print ODATA "AWKS=\t";
201              &dumpsrcs (@trrtaf);
202          } elsif ($cmd[1] eq "srcs") {
203              print "  Defining SRCS\n";
204              if ($first) {
205                    die "Data file must start with a %% file!";
206              }
207              print ODATA "SRCS=\t";
208              &dumpsrcs (@trrtf);
209          } elsif ($cmd[1] eq "man") {
210              print "  Defining MAN\n";
211              if ($first) {
212                    die "Data file must start with a %% file!";
213              }
214              print ODATA "MAN=\t";
215              &dumpsrcs (@trrtmf);
216          } elsif ($cmd[1] eq "version") {
217              print "  Defining VERSION\n";
218              print ODATA "char version[] = \"$version\";";
219          } elsif ($cmd[1] eq "NetBSD") {
220              if ($first) {
221                    die "Data section must start with a %% file!";
222              }
223              print ODATA "$cmd[2]      \$"."NetBSD".": \$  $cmd[3]\n";
224          }
225    } else {
226          if ($first) {
227              die "Data file must start with a %% file!";
228          }
229          print ODATA "$line\n";
230    }
231}
232close (ODATA);
233
234#
235# Sed transformations of files
236#
237foreach $n (keys(%sedlist)) {
238    print "Modifying $n\n";
239    system ("cd $targetdir; sed $sedlist{$n} $n > tmp;  mv -f tmp $n");
240}
241
242#
243# end of the script
244#
245
246# what follows is the data for makefiles and other special files
247# that need to be created.
248
249__END__
250%% file usr.sbin/traceroute/Makefile
251%% NetBSD #
252
253WARNS?=   1
254PROG=     traceroute
255%% man
256
257CPPFLAGS+=-DHAVE_MALLOC_H=1 -DHAVE_SYS_SELECT_H=1 -DHAVE_SYS_SOCKIO_H=1
258CPPFLAGS+=-DHAVE_STRERROR=1 -DHAVE_SETLINEBUF=1 -DHAVE_SOCKADDR_SA_LEN=1
259CPPFLAGS+=-DHAVE_RAW_OPTIONS=1
260
261BINOWN= root
262BINMODE=4555
263
264%% srcs
265SRCS+= version.c
266
267%% awks
268
269.include <bsd.prog.mk>
270%% file usr.sbin/traceroute/version.c
271%% NetBSD /* */
272%% version
273