aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/w3mman/hlink.cgi
diff options
context:
space:
mode:
authorTatsuya Kinoshita <tats@vega.ocn.ne.jp>2011-05-04 07:05:14 +0000
committerTatsuya Kinoshita <tats@vega.ocn.ne.jp>2011-05-04 07:05:14 +0000
commit72f72d64a422d6628c4796f5c0bf2e508f134214 (patch)
tree0c9ea90cc53310832c977265521fb44db24a515e /scripts/w3mman/hlink.cgi
parentAdding upstream version 0.3 (diff)
downloadw3m-1ef51c20e0ea7b35c8c9cad6f9d9bc1e16664cc5.tar.gz
w3m-1ef51c20e0ea7b35c8c9cad6f9d9bc1e16664cc5.zip
Adding upstream version 0.5.1upstream/0.5.1
Diffstat (limited to 'scripts/w3mman/hlink.cgi')
-rw-r--r--scripts/w3mman/hlink.cgi97
1 files changed, 97 insertions, 0 deletions
diff --git a/scripts/w3mman/hlink.cgi b/scripts/w3mman/hlink.cgi
new file mode 100644
index 0000000..a92ac1d
--- /dev/null
+++ b/scripts/w3mman/hlink.cgi
@@ -0,0 +1,97 @@
+#!/usr/local/bin/perl
+
+$SCRIPT_NAME = $ENV{'SCRIPT_NAME'} || $0;
+$CGI = "file://$SCRIPT_NAME?";
+
+if ($ENV{'QUERY_STRING'}) {
+ $file = $ENV{'QUERY_STRING'};
+} else {
+ $file = $ARGV[0];
+}
+$file = &cleanup($file);
+
+if (-d $file) {
+ print <<EOF;
+Location: file:$file
+EOF
+ exit;
+}
+if (! open(FILE, "< $file")) {
+ $file = &html_quote($file);
+ $_ = "$file: " . &html_quote($!);
+ print <<EOF;
+Content-Type: text/html
+
+<head><title>$file</title></head>
+<b>$_</b>
+EOF
+ exit 1;
+}
+
+$file = &html_quote($file);
+($dir = $file) =~ s@[^/]*$@@;
+
+print <<EOF;
+Content-Type: text/html
+
+<head><title>$file</title></head>
+<pre>
+EOF
+while (<FILE>) {
+ $_ = &html_quote($_);
+
+ s/^(\#\s*include\s+)(\&quot;.*\&quot;|\&lt\;.*\&gt\;)/$1 . &header_ref($2)/ge;
+
+ print;
+}
+close(FILE);
+print "</pre>\n";
+
+sub header_ref {
+ local($_) = @_;
+ local($d);
+
+ if (s/^\&quot;//) {
+ s/\&quot;$//;
+ return "&quot;<a href=\"$CGI$dir$_\">$_</a>&quot;";
+ }
+ s/^\&lt\;//;
+ s/\&gt\;$//;
+
+ for $d (
+ "/usr/include",
+ "/usr/local/include",
+ "/usr/X11R6/include",
+ "/usr/X11/include",
+ "/usr/X/include",
+ "/usr/include/X11"
+ ) {
+ -f "$d/$_" && return "&lt;<a href=\"$CGI$d/$_\">$_</a>&gt;";
+ }
+ return $_;
+}
+
+
+sub html_quote {
+ local($_) = @_;
+ local(%QUOTE) = (
+ '<', '&lt;',
+ '>', '&gt;',
+ '&', '&amp;',
+ '"', '&quot;',
+ );
+ s/[<>&"]/$QUOTE{$&}/g;
+ return $_;
+}
+
+sub cleanup {
+ local($_) = @_;
+
+ s@//+@/@g;
+ s@/\./@/@g;
+ while(m@/\.\./@) {
+ s@^/(\.\./)+@/@;
+ s@/[^/]+/\.\./@/@;
+ }
+ return $_;
+}