[Date Index][Thread Index]
[Date Prev][Date Next][Thread Prev][Thread Next]
Re: Recursive #include problem
- From: Denis Barbier <nospam@thanx>
- Date: Tue, 28 Dec 1999 13:34:04 +0100 (CET)
On Tue, 14 Dec 1999, Gilles Lamiral wrote:
> Hello,
>
> Consider this problem:
>
> O) make directories top/dir1/dir2/
>
> 1) make a empty file named top/ancestors
>
> 2) make a file named top/dir1/ancestors with the content:
> #include '../ancestors'
>
> 3) make a file named top/dir1/dir2/ancestors with the content:
> #include '../ancestors'
>
> 4) go into the top/dir1/dir2/ directory and type:
> $ wml ancestors
>
> wml goes in deep recursion in the first phase. It should not. It
> should if the include directive were #include './ancestors'.
> Recursion is not well treated. The phase 1 is just like the cpp job.
[...]
Phase 1 is more than the cpp job, because prolog filters may be applied.
A patch is attached. It will go under CVS after some checks.
--
Denis Barbier
WML Maintainer
--- wml_backend/p1_ipp/ipp.src 1999/09/21 15:31:52 1.21
+++ wml_backend/p1_ipp/ipp.src 1999/12/28 12:07:02
@@ -134,6 +134,7 @@
#
%INCLUDES = ();
+@DIR = ();
$outbuf = '';
sub setargs {
@@ -186,21 +187,6 @@
return $file;
}
-sub CanonPath {
- my ($path) = @_;
-
- $pathL = '';
- while ($path ne $pathL) {
- $pathL = $path;
- $path =~ s|//|/|g;
- $path =~ s|/\./|/|g;
- $path =~ s|/\.$|/|g;
- $path =~ s|^\./(.)|$1|g;
- $path =~ s|([^/.][^/.]*)/\.\.||;
- }
- return $path;
-}
-
sub PatternProcess {
my ($mode, $delimiter, $dirname, $pattern, $ext, $level, $noid, %arg) = @_;
my ($dir, $out, $test, @ls);
@@ -219,7 +205,7 @@
closedir DIR;
foreach (@ls) {
next if (m|/\.+$| or m|^\.+$|);
- $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", $level, $noid, %arg);
+ $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", '', $level, $noid, %arg);
}
}
}
@@ -230,7 +216,7 @@
closedir DIR;
foreach (@ls) {
next if (m|/\.+$| or m|^\.+$|);
- $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", $level, $noid, %arg);
+ $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", '', $level, $noid, %arg);
}
}
}
@@ -242,7 +228,7 @@
closedir DIR;
foreach (@ls) {
next if (m|/\.+$| or m|^\.+$|);
- $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", $level, $noid, %arg);
+ $out .= &ProcessFile($mode, $delimiter, "$dirname/$_$ext", '', $level, $noid, %arg);
}
}
}
@@ -250,7 +236,7 @@
}
sub ProcessFile {
- my ($mode, $delimiter, $file, $level, $noid, %arg) = @_;
+ my ($mode, $delimiter, $file, $dir, $level, $noid, %arg) = @_;
my ($in, $found, $line, $incfile, $type, %argO, $out, $store);
#
@@ -320,16 +306,16 @@
#
# stop if file was still included some time before
#
+ $file = File::PathConvert::realpath($file);
if (not $noid) {
- $id = &CanonPath($file);
if ($mode eq 'use') {
- return '' if (exists $INCLUDES{$id});
+ return '' if (exists $INCLUDES{$file});
}
if ($delimiter eq '<') {
- $INCLUDES{$id} = 1;
+ $INCLUDES{$file} = 1;
}
else {
- $INCLUDES{$id} = 2;
+ $INCLUDES{$file} = 2;
}
}
#
@@ -340,13 +326,23 @@
#
# process the file
#
- $in = new IO::File;
&verbose($level, "|");
&verbose($level, "+-- $file");
+
+ # stores current working directory and change directory
+ push(@DIR, Cwd::cwd);
+ if ($dir eq '') {
+ $dir = File::PathConvert::realpath($file);
+ $dir =~ s|/+[^/]*$||;
+ }
+ chdir $dir || die "Unable to chdir";
+
+ $in = new IO::File;
$in->open("<$file");
$line = 0;
$out = '';
$store = '';
+
while ($l = <$in>) {
$line++;
@@ -404,7 +400,7 @@
}
# now recurse down
- $out .= &ProcessFile($cmd, $type, $incfile, $level+1, 0, %arg);
+ $out .= &ProcessFile($cmd, $type, $incfile, '', $level+1, 0, %arg);
# reset arguments
%arg = %argO;
@@ -442,6 +438,10 @@
$out .= $store;
$in->close();
+ # go back to previous working directory
+ $dir = pop(@DIR);
+ chdir $dir || die "Unable to chdir";
+
return $out;
}
@@ -492,7 +492,7 @@
}
}
$tmp->close();
-$outbuf .= &ProcessFile('include', "'", $tmpfile, 0, 1, %arg);
+$outbuf .= &ProcessFile('include', "'", $tmpfile, Cwd::cwd, 0, 1, %arg);
unlink($tmpfile);
#
@@ -528,7 +528,7 @@
}
# process file via IPP filter
- $outbuf .= &ProcessFile('include', "'", $tmpfile, 0, 1, %arg);
+ $outbuf .= &ProcessFile('include', "'", $tmpfile, $arg{"WML_SRC_DIRNAME"}, 0, 1, %arg);
# cleanup
unlink($tmpfile);
--- wml_frontend/wml.src 1999/11/30 17:02:59 1.36
+++ wml_frontend/wml.src 1999/12/28 12:02:22
@@ -904,12 +904,16 @@
$src_realname = $gen_realname;
}
else {
- $src_dirname = $cwd;
- $src_filename = $src;
- $src_filename =~ s|^.*/+||;
- $src_basename = $src_filename;
- $src_basename =~ s|(\.[a-zA-Z0-9]+)$||;
- $src_suffix = $1;
+ if ($src =~ m|/|) {
+ ($path = $src) =~ s|/+[^/]*$||;
+ $src_dirname = File::PathConvert::realpath("$path");
+ } else {
+ $src_dirname = $cwd;
+ }
+ ($src_filename = $src) =~ s|^.*/+||;
+ ($src_basename = $src_filename) =~ s|(\.[a-zA-Z0-9]+)$||;
+ $src_suffix = $1;
+
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($src);
$src_ctime = &ctime($mtime);
@@ -1110,26 +1114,9 @@
$tmp[2] = "$tmpdir/wml.$$.tmp3" . $src_suffix;
$tmp[3] = "$tmpdir/wml.$$.tmp4" . $src_suffix;
-if (not $src_istmp) {
- $fpin = new IO::File;
- $fpin->open("<$src");
- $fpout = new IO::File;
- $fpout->open(">$tmp[0]");
- while (<$fpin>) {
- $fpout->print($_);
- }
- $fpout->close;
- $fpin->close;
-
- $from = $tmp[0];
- $to = $tmp[1];
- $cnt = 1;
-}
-else {
- $from = $src;
- $to = $tmp[0];
- $cnt = 0;
-}
+$from = $src;
+$to = $tmp[0];
+$cnt = 0;
$out_istmp = 0;
if ($out eq '') {