#!/usr/bin/perl -s # # cvsweb - a CGI interface to the CVS tree. # # Note, this is under RCS control in /home/fenner: # $Header: /other/aer201/comp/cgi/RCS/cvsweb.cgi,v 1.4 2000/03/20 09:53:07 tim Exp $ # # Written by Bill Fenner . # # Copyright (C) 1996 # William C. Fenner. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the author nor the names of any co-contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY William C. Fenner AND CONTRIBUTORS ``AS IS'' # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL William C. Fenner, HIS BROTHER B1FF, OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # # To do: # - Parse the ,v yourself instead of using rlog output, since people # sometimes paste rlog output into commit entries, making it impossible # to parse. # - Figure out how to do directory-wide diff's (get named tags cheaply?) # # # Configuration section: # Set $cvsroot to the root of the CVS tree $cvsroot = '..'; # Set $rcsbinaries to the location of the RCS binaries, if they're # not in the web server's $PATH #$rcsbinaries = '/import/gnu-2.0/sparc-sun-sunos4.1/bin'; # The HTML title will be $title: /pathname $title = "'My CVS Tree'"; # The HTML to go inside the

tag $h1 = $title; # $intro is the HTML that is displayed along with the # top-level tree $intro = " This is a WWW interface to my CVS tree. You can browse the file hierarchy by picking directories (which have slashes after them, e.g. src/). If you pick a file, you will see the revision history for that file. Selecting a revision number will download that revision of the file. There is a link at each revision to display diffs between that revision and the previous one, and a form at the bottom of the page that allows you to display diffs between arbitrary revisions.

Please send any suggestions, comments, etc. to Bill Fenner <fenner\@freebsd.org> "; # # $shortinstr is the HTML displayed at the top of non-top-level # directory listings. $shortinstr = " Click on a directory to enter that directory. Click on a file to display its revision history and to get a chance to display diffs between revisions. "; # # $backicon is the icon to be used for the previous directory, if any $backicon = "/icons/back.gif"; # $diricon is the icon to be used for a directory, if any $diricon = "/icons/dir.gif"; # $texticon is the icon to be used for a text file, if any $texticon = "/icons/text.gif"; # # $tailhtml is the html for the bottom of the page $tailhtml ="

$ENV{SERVER_ADMIN}
"; ##### End configuration section require 'timelocal.pl'; require 'ctime.pl'; $cvswebversion = "1.0"; if (defined($rcsbinaries)) { $ENV{'PATH'} = $rcsbinaries . ":" . $ENV{'PATH'}; } $verbose = $v; ($where = $ENV{'PATH_INFO'}) =~ s|^/||; $where =~ s|/$||; $fullname = $cvsroot . '/' . $where; ($scriptname = $ENV{'SCRIPT_NAME'}) =~ s|^/?|/|; $scriptname =~ s|/$||; #$scriptname = "/~hoek/aer201/cgi/cvsweb.cgi"; $scriptname = "/cgi/cvsweb.cgi"; $scriptwhere = $scriptname . '/' . $where; $scriptwhere =~ s|/$||; ($fullname2 = $fullname) =~ s/([^\/]+)$/RCS\/$1/; if (-f "$fullname2,v") { $fullname = $fullname2; } if (!-d $cvsroot) { &fatal("500 Internal Error",'$CVSROOT not found!'); } if (-d $fullname) { # Something that would be nice to support, although I have no real # good idea of how, would be to get full directory diff's, using # symbolic names (revision numbers would be meaningless). # The problem is finding a list of symbolic names that is common # to all the files in the directory. # opendir(DIR, $fullname) || &fatal("404 Not Found","$where: $!"); opendir(DIR2, "$fullname/RCS"); @dir = (readdir(DIR), readdir(DIR2)); closedir(DIR); closedir(DIR2); print "Content-type: text/html\n\n"; print "${title}: /$where\n"; print "\n"; print "\n"; print "

$h1

\n
\n"; if ($where eq '') { print $intro; } else { print $shortinstr; } print "

Current directory: /$where\n"; print "


\n"; # Using in this manner violates the HTML2.0 spec but # provides the results that I want in most browsers. Another # case of layout desires spooging up HTML. print "\n"; foreach (sort @dir) { if ($_ eq '.') { next; } if ($_ eq '..') { next if ($where eq ''); ($updir = $scriptwhere) =~ s|[^/]+$||; if (defined($backicon)) { print "\"[UP"; } else { print "[UP ]"; } print " ", &link("Previous Directory",$updir), "
"; } elsif (-d $fullname . "/" . $_) { if (defined($diricon)) { print "\"[DIR]\""; } else { print "[DIR]"; } print " ", &link($_ . "/", $scriptwhere . '/' . $_ . '/'), "
"; } elsif (s/,v$//) { if (defined($texticon)) { print "\"[TXT]\""; } else { print "[TXT]"; } print " ", &link($_, $scriptwhere . '/' . $_), "
"; } elsif (! -f "${fullname}/RCS/$_,v") { print "[TXT] ", &link($_, $scriptwhere . '/' . $_), " (no RCS control file)
"; } } print "
\n"; print "
Created by cvsweb $cvswebversion

\n"; print $tailhtml,"\n"; print "\n"; } elsif (-f "$fullname,v") { if ($_ = $ENV{'QUERY_STRING'}) { s/%(..)/sprintf("%c", hex($1))/ge; # unquote %-quoted if (/rev=([\d\.]+)/) { $rev = $1; open(RCS, "co -p$rev '$fullname,v' 2>&1 |") || &fail("500 Internal Error", "Couldn't co: $!"); # /home/ncvs/src/sys/netinet/igmp.c,v --> standard output # revision 1.1.1.2 # /* $_ = ; if (/^$fullname,v\s+-->\s+standard output\s*$/o) { # As expected } else { &fatal("500 Internal Error", "Unexpected output from co: $_"); } $_ = ; if (/^revision\s+$rev\s*$/) { # As expected } else { &fatal("500 Internal Error", "Unexpected output from co: $_"); } $| = 1; print "Content-type: text/plain\n"; print "Content-encoding: x-gzip\n\n"; open(GZIP, "|gzip -1 -c"); # need lightweight compression print GZIP ; close(GZIP); close(RCS); exit; } if (/r1=([^&:]+)(:([^&]+))?/) { $rev1 = $1; $sym1 = $3; } if ($rev1 eq 'text') { if (/tr1=([^&]+)/) { $rev1 = $1; } } if (/r2=([^&:]+)(:([^&]+))?/) { $rev2 = $1; $sym2 = $3; } if ($rev2 eq 'text') { if (/tr2=([^&]+)/) { $rev2 = $1; } } if (!($rev1 =~ /^[\d\.]+$/) || !($rev2 =~ /^[\d\.]+$/)) { &fatal("404 Not Found", "Malformed query \"$ENV{'QUERY_STRING'}\""); } # # rev1 and rev2 are now both numeric revisions. # Thus we do a DWIM here and swap them if rev1 is after rev2. # XXX should we warn about the fact that we do this? if (&revcmp($rev1,$rev2) > 0) { ($tmp1, $tmp2) = ($rev1, $sym1); ($rev1, $sym1) = ($rev2, $sym2); ($rev2, $sym2) = ($tmp1, $tmp2); } # $difftype = "-u"; $diffname = "Unidiff"; if (/f=([^&]+)/) { if ($1 eq 'c') { $difftype = '-c'; $diffname = "Context diff"; } } # XXX should this just be text/plain # or should it have an HTML header and then a
	    print "Content-type: text/plain\n\n";
	    open(RCSDIFF, "rcsdiff $difftype -r$rev1 -r$rev2 '$fullname,v' 2>&1 |") ||
		&fail("500 Internal Error", "Couldn't rcsdiff: $!");
#
#===================================================================
#RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
#retrieving revision 1.16
#retrieving revision 1.17
#diff -c -r1.16 -r1.17
#*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
#--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
#
# Ideas:
# - nuke the stderr output if it's what we expect it to be
# - Add "no differences found" if the diff command supplied no output.
#
#*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
#--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
# (bogus example, but...)
#
	    if ($difftype eq '-u') {
		$f1 = '---';
		$f2 = '\+\+\+';
	    } else {
		$f1 = '\*\*\*';
		$f2 = '---';
	    }
	    while () {
		if (m|^$f1 $cvsroot|o) {
		    s|$cvsroot/||o;
		    if ($sym1) {
			chop;
			$_ .= " " . $sym1 . "\n";
		    }
		} elsif (m|^$f2 $cvsroot|o) {
		    s|$cvsroot/||o;
		    if ($sym2) {
			chop;
			$_ .= " " . $sym2 . "\n";
		    }
		}
		print $_;
	    }
	    close(RCSDIFF);
	    exit;
	}
	open(RCS, "rlog '$fullname,v'|") || &fatal("500 Internal Error",
						"Failed to spawn rlog");
	while () {
	    print if ($verbose);
	    if ($symnames) {
		if (/^\s+([^:]+):\s+([\d\.]+)/) {
		    $symrev{$1} = $2;
		    if ($revsym{$2}) {
			$revsym{$2} .= ", ";
		    }
		    $revsym{$2} .= $1;
		} else {
		    $symnames = 0;
		}
	    } elsif (/^symbolic names/) {
		$symnames = 1;
	    } elsif (/^-----/) {
		last;
	    }
	}
# each log entry is of the form:
# ----------------------------
# revision 3.7.1.1
# date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
# log info
# ----------------------------
	logentry:
	while (!/^=========/) {
	    $_ = ;
	    print "R:", $_ if ($verbose);
	    if (/^revision ([\d\.]+)/) {
		$rev = $1;
	    } elsif (/^========/ || /^----------------------------$/) {
		next logentry;
	    } else {
		&fatal("500 Internal Error","Error parsing RCS output: $_");
	    }
	    $_ = ;
	    print "D:", $_ if ($verbose);
	    if (m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);|) {
		$yr = $1;
		# damn 2-digit year routines
		if ($yr > 100) {
		    $yr -= 1900;
		}
		$date{$rev} = &timelocal($6,$5,$4,$3,$2 - 1,$yr);
		$author{$rev} = $7;
	    } else {
		&fatal("500 Internal Error", "Error parsing RCS output: $_");
	    }
	    line:
	    while () {
		print "L:", $_ if ($verbose);
		next line if (/^branches:\s/);
		last line if (/^----------------------------$/ || /^=========/);
		$log{$rev} .= $_;
	    }
	    print "E:", $_ if ($verbose);
	}
	close(RCS);
	print "Done reading RCS file\n" if ($verbose);
#
# Sort the revisions into commit-date order.
	@revorder = sort {$date{$b} <=> $date{$a}} keys %date;
	print "Done sorting revisions\n" if ($verbose);
#
# HEAD is an artificial tag which is simply the highest tag number on the main
# branch.  Find it by looking through @revorder; it should at least
# be near the beginning (In fact, it *should* be the first commit listed on
# the main branch.)
	revision:
	for ($i = 0; $i <= $#revorder; $i++) {
	    if ($revorder[$i] =~ /^\d+\.\d+$/) {
		if ($revsym{$revorder[$i]}) {
		    $revsym{$revorder[$i]} .= ", ";
		}
		$revsym{$revorder[$i]} .= "HEAD";
		$symrev{"HEAD"} = $revorder[$i];
		last revision;
	    }
	}
	print "Done finding HEAD\n" if ($verbose);
#
# Now that we know all of the revision numbers, we can associate
# absolute revision numbers with all of the symbolic names, and
# pass them to the form so that the same association doesn't have
# to be built then.
#
# should make this a case-insensitive sort
	foreach (sort keys %symrev) {
	    $rev = $symrev{$_};
	    if ($rev =~ /^(\d+(\.\d+)+)\.0\.(\d+)$/) {
		#
		# A revision number of A.B.0.D really translates into
		# "the highest current revision on branch A.B.D".
		#
		# If there is no branch A.B.D, then it translates into
		# the head A.B .
		#
		# This is pure speculation.
		#
		$head = $1;
		$branch = $3;
		$regex = $head . "." . $branch;
		$regex =~ s/\./\./g;
		#             <
		#           \____/
		$rev = $head;

		revision:
		foreach $r (@revorder) {
		    if ($r =~ /^${regex}/) {
			$rev = $head . "." . $branch;
			last revision;
		    }
		}
		$revsym{$rev} .= ", " if ($revsym{$rev});
		$revsym{$rev} .= $_;
	    }
	    $sel .= "