-
Notifications
You must be signed in to change notification settings - Fork 791
Expand file tree
/
Copy pathhipp
More file actions
executable file
·79 lines (72 loc) · 1.64 KB
/
hipp
File metadata and controls
executable file
·79 lines (72 loc) · 1.64 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/local/bin/perl
# Html Include Processor:
#
# search for tag <include src=file ...>
# and replace it by the content of "file"
#
# Arnaud Le Hors - lehors@w3.org
# $Id: hipp,v 1.5 2006-10-09 18:55:52 ihickson Exp $
# init search path
$paths[0] = ".";
# parse include flags and augment search path
while ($_ = $ARGV[0], /^-I(.*)/) {
$paths[@paths] = $1;
shift;
}
$PROGNAME = substr($0, rindex($0, "/") + 1);
if ($_ = $ARGV[0], /^-h|^-\?/) {
die "Usage: $PROGNAME [-Idir1 -Idir2 ...] [src] [output]\n";
}
if ($ARGV[0]) {
$file = $ARGV[0];
shift;
} else {
$file = "-";
}
if ($ARGV[0]) {
$output = $ARGV[0];
} else {
$output = "-";
}
# regexp
$wd = "[^ \t\n>]+"; # word
$qwd = "\"([^\"]*)\""; # quoted word
$inctag = "<!--[ \t]*#include[ \t]*src=(?:$qwd|($wd)).*?-->([ \t]*\n)?";
# open file using the given search path
sub openfile {
if ($_[0] eq "-") {
open(INPUT, "-");
return INPUT;
}
foreach $p (@paths) {
$path = "$p/$_[0]";
if (open(INPUT, $path)) {
return INPUT;
}
}
return ();
}
# recursive sub routine extending include tags
# args: file exitflag
sub processf {
# copy file in memory
if (!openfile($_[0])) {
print STDERR "$PROGNAME Error: Cannot open file: $_[0]\n";
if ($_[1] == 1) {
exit 1;
} else {
return "";
}
}
my $buf = <INPUT>;
close(INPUT);
# then substitute include tags by the content of file they reference
# only $1 or $2 will actually be non null
$buf =~ s/$inctag/processf("$1$2",0)/sgieo;
return $buf;
}
undef $/; # read files as single blocks
$buf = processf($file,1);
open(OUTPUT, "> $output");
print OUTPUT $buf;
close(OUTPUT);