[BACK]Return to cvsweb.cgi CVS log [TXT][DIR] Up to [local] / CVSROOT

Annotation of CVSROOT/cvsweb.cgi, Revision 1.4

1.1       maekawa     1: #!/usr/bin/perl -ws
                      2: #
                      3: # cvsweb - a CGI interface to CVS trees.
                      4: #
                      5: # Written in their spare time by
                      6: #             Bill Fenner      <fenner@freebsd.org>   (original work)
                      7: # extended by Henner Zeller    <zeller@think.de>,
                      8: #             Henrik Nordström <hno@hem.passagen.se>
                      9: #             Ken Coar         <coar@Apache.Org>
1.3       maekawa    10: #             Dick Balaska     <dick@buckosoft.com>
1.1       maekawa    11: #
                     12: # Based on:
                     13: # * Bill Fenners cvsweb.cgi revision 1.28 available from:
                     14: #   http://www.freebsd.org/cgi/cvsweb.cgi/www/en/cgi/cvsweb.cgi
                     15: #
                     16: # Copyright (c) 1996-1998 Bill Fenner
                     17: #           (c) 1998-1999 Henner Zeller
                     18: #          (c) 1999      Henrik Nordström
                     19: # All rights reserved.
                     20: #
                     21: # Redistribution and use in source and binary forms, with or without
                     22: # modification, are permitted provided that the following conditions
                     23: # are met:
                     24: # 1. Redistributions of source code must retain the above copyright
                     25: #    notice, this list of conditions and the following disclaimer.
                     26: # 2. Redistributions in binary form must reproduce the above copyright
                     27: #    notice, this list of conditions and the following disclaimer in the
                     28: #    documentation and/or other materials provided with the distribution.
                     29: #
                     30: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     31: # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     32: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     33: # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     34: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     35: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     36: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     37: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     38: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     39: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     40: # SUCH DAMAGE.
                     41: #
1.4     ! maekawa    42: # $Id: cvsweb.cgi,v 1.90 2000/07/17 20:02:20 hzeller Exp $
1.1       maekawa    43: #
                     44: ###
                     45:
                     46: use strict;
                     47:
                     48: use vars qw (
                     49:     $config $allow_version_select $verbose
                     50:     %CVSROOT %CVSROOTdescr %MIRRORS %DEFAULTVALUE %ICONS %MTYPES
                     51:     %alltags @tabcolors %fileinfo %tags @branchnames %nameprinted
                     52:     %symrev %revsym @allrevisions %date %author @revdisplayorder
                     53:     @revisions %state %difflines %log %branchpoint @revorder $prcgi
                     54:     $checkoutMagic $doCheckout $scriptname $scriptwhere
                     55:     $where $Browser $nofilelinks $maycompress @stickyvars
                     56:     %input $query $barequery $sortby $bydate $byrev $byauthor
                     57:     $bylog $byfile $hr_default $logsort $cvstree $cvsroot
                     58:     $mimetype $defaultTextPlain $defaultViewable $allow_compress
                     59:     $GZIPBIN $backicon $diricon $fileicon $fullname $newname
                     60:     $cvstreedefault $body_tag $logo $defaulttitle $address
                     61:     $backcolor $long_intro $short_instruction $shortLogLen
                     62:     $show_author $dirtable $tablepadding $columnHeaderColorDefault
                     63:     $columnHeaderColorSorted $hr_breakable $hr_funout $hr_ignwhite
                     64:     $hr_ignkeysubst $diffcolorHeading $diffcolorEmpty $diffcolorRemove
                     65:     $diffcolorChange $diffcolorAdd $diffcolorDarkChange $difffontface
                     66:     $difffontsize $inputTextSize $mime_types $allow_annotate
                     67:     $allow_markup $use_java_script $open_extern_window
                     68:     $extern_window_width $extern_window_height $edit_option_form
                     69:     $checkout_magic $show_subdir_lastmod $show_log_in_markup $v
                     70:     $navigationHeaderColor $tableBorderColor $markupLogColor
1.3       maekawa    71:     $tabstop $state $annTable $sel $curbranch @HideModules
                     72:     $module $use_descriptions %descriptions @mytz $dwhere $moddate
                     73:     $use_moddate
1.1       maekawa    74: );
                     75:
                     76: ##### Start of Configuration Area ########
                     77: # == EDIT this ==
                     78: # User configuration is stored in
1.2       maekawa    79: $config = $ENV{'CVSWEB_CONFIG'} || '/usr/local/etc/apache/cvsweb.conf';
1.1       maekawa    80:
                     81: # == Configuration defaults ==
                     82: # Defaults for configuration variables that shouldn't need
                     83: # to be configured..
                     84: $allow_version_select = 1;
                     85:
                     86: ##### End of Configuration Area   ########
                     87:
                     88: ######## Configuration variables #########
                     89: # These are defined to allow checking with perl -cw
                     90: %CVSROOT = %MIRRORS = %DEFAULTVALUE = %ICONS = %MTYPES =
                     91: %tags = %alltags = @tabcolors = ();
                     92: $cvstreedefault = $body_tag = $logo = $defaulttitle = $address =
                     93: $backcolor = $long_intro = $short_instruction = $shortLogLen =
                     94: $show_author = $dirtable = $tablepadding = $columnHeaderColorDefault =
                     95: $columnHeaderColorSorted = $hr_breakable = $hr_funout = $hr_ignwhite =
                     96: $hr_ignkeysubst = $diffcolorHeading = $diffcolorEmpty = $diffcolorRemove =
                     97: $diffcolorChange = $diffcolorAdd = $diffcolorDarkChange = $difffontface =
                     98: $difffontsize = $inputTextSize = $mime_types = $allow_annotate =
                     99: $allow_markup = $use_java_script = $open_extern_window =
                    100: $extern_window_width = $extern_window_height = $edit_option_form =
                    101: $checkout_magic = $show_subdir_lastmod = $show_log_in_markup = $v =
                    102: $navigationHeaderColor = $tableBorderColor = $markupLogColor =
1.3       maekawa   103: $tabstop = $use_moddate = $moddate = undef;
1.1       maekawa   104:
                    105: ##### End of configuration variables #####
                    106:
                    107: use Time::Local;
                    108: use IPC::Open2;
                    109:
                    110: $verbose = $v;
                    111: $checkoutMagic = "~checkout~";
                    112: $where = defined($ENV{'PATH_INFO'}) ? $ENV{'PATH_INFO'} : "";
                    113: $doCheckout = ($where =~ /^\/$checkoutMagic/);
                    114: $where =~ s|^/($checkoutMagic)?||;
                    115: $where =~ s|/+$||;
                    116: ($scriptname = $ENV{'SCRIPT_NAME'}) =~ s|^/?|/|;
                    117: $scriptname =~ s|/+$||;
                    118: if ($where) {
                    119:     $scriptwhere = $scriptname . '/' . urlencode($where);
                    120: }
                    121: else {
                    122:     $scriptwhere = $scriptname;
                    123: }
                    124: $scriptwhere =~ s|/+$||;
                    125:
                    126: # in lynx, it it very annoying to have two links
                    127: # per file, so disable the link at the icon
                    128: # in this case:
                    129: $Browser = $ENV{'HTTP_USER_AGENT'};
                    130: $nofilelinks = ($Browser =~ m'^Lynx/');
                    131:
                    132: # newer browsers accept gzip content encoding
                    133: # and state this in a header
                    134: # (netscape did always but didn't state it)
                    135: # It has been reported that these
                    136: #  braindamaged MS-Internet Exploders claim that they
                    137: # accept gzip .. but don't in fact and
                    138: # display garbage then :-/
                    139: # Turn off gzip if running under mod_perl. piping does
                    140: # not work as expected inside the server. One can probably
                    141: # achieve the same result using Apache::GZIPFilter.
                    142: $maycompress =(($ENV{'HTTP_ACCEPT_ENCODING'} =~ m|gzip|
                    143:                || $Browser =~ m%^Mozilla/3%)
                    144:               && ($Browser !~ m/MSIE/)
                    145:               && !defined($ENV{'MOD_PERL'}));
                    146:
                    147: # put here the variables we need in order
                    148: # to hold our state - they will be added (with
                    149: # their current value) to any link/query string
                    150: # you construct
                    151: @stickyvars = ('cvsroot','hideattic','sortby','logsort','f','only_with_tag');
                    152:
                    153: if (-f $config) {
                    154:     do "$config";
                    155: }
                    156: else {
                    157:    &fatal("500 Internal Error",
                    158:          'Configuration not found.  Set the variable <code>$config</code> '
                    159:           . 'in cvsweb.cgi, or the environment variable '
                    160:           . '<code>CVSWEB_CONFIG</code>, to your <b>cvsweb.conf</b> '
                    161:           . 'configuration file first.');
                    162: }
                    163:
                    164: undef %input;
                    165: if ($query = $ENV{'QUERY_STRING'}) {
                    166:     foreach (split(/&/, $query)) {
                    167:        s/%(..)/sprintf("%c", hex($1))/ge;      # unquote %-quoted
                    168:        if (/(\S+)=(.*)/) {
                    169:            $input{$1} = $2 if ($2 ne "");
                    170:        }
                    171:        else {
                    172:            $input{$_}++;
                    173:        }
                    174:     }
                    175: }
                    176:
                    177: # For backwards compability, set only_with_tag to only_on_branch if set.
                    178: $input{only_with_tag} = $input{only_on_branch}
                    179:     if (defined($input{only_on_branch}));
                    180:
                    181: foreach (keys %DEFAULTVALUE)
                    182: {
                    183:     # replace not given parameters with the default parameters
                    184:     if (!defined($input{$_}) || $input{$_} eq "") {
                    185:        # Empty Checkboxes in forms return -- nothing. So we define a helper
                    186:        # variable in these forms (copt) which indicates that we just set
                    187:        # parameters with a checkbox
                    188:        if (!defined($input{"copt"})) {
                    189:            # 'copt' isn't defined --> empty input is not the result
                    190:            # of empty input checkbox --> set default
                    191:            $input{$_} = $DEFAULTVALUE{$_} if (defined($DEFAULTVALUE{$_}));
                    192:        }
                    193:        else {
                    194:            # 'copt' is defined -> the result of empty input checkbox
                    195:            # -> set to zero (disable) if default is a boolean (0|1).
                    196:            $input{$_} = 0
                    197:                if (defined($DEFAULTVALUE{$_})
                    198:                    && ($DEFAULTVALUE{$_} eq "0" || $DEFAULTVALUE{$_} eq "1"));
                    199:        }
                    200:     }
                    201: }
                    202:
                    203: $barequery = "";
                    204: foreach (@stickyvars) {
                    205:     # construct a query string with the sticky non default parameters set
1.4     ! maekawa   206:     if (defined($input{$_}) && $input{$_} ne "" && defined($DEFAULTVALUE{$_}) && $input{$_} ne $DEFAULTVALUE{$_}) {
1.1       maekawa   207:        if ($barequery) {
1.3       maekawa   208:            $barequery = $barequery . "&amp;";
1.1       maekawa   209:        }
                    210:        my $thisval = urlencode($_) . "=" . urlencode($input{$_});
                    211:        $barequery .= $thisval;
                    212:     }
                    213: }
                    214: # is there any query ?
                    215: if ($barequery) {
                    216:     $query = "?$barequery";
1.3       maekawa   217:     $barequery = "&amp;" . $barequery;
1.1       maekawa   218: }
                    219: else {
                    220:     $query = "";
                    221: }
                    222:
                    223: # get actual parameters
                    224: $sortby = $input{"sortby"};
                    225: $bydate = 0;
                    226: $byrev = 0;
                    227: $byauthor = 0;
                    228: $bylog = 0;
                    229: $byfile = 0;
                    230: if ($sortby eq "date") {
                    231:     $bydate = 1;
                    232: }
                    233: elsif ($sortby eq "rev") {
                    234:     $byrev = 1;
                    235: }
                    236: elsif ($sortby eq "author") {
                    237:     $byauthor = 1;
                    238: }
                    239: elsif ($sortby eq "log") {
                    240:     $bylog = 1;
                    241: }
                    242: else {
                    243:     $byfile = 1;
                    244: }
                    245:
                    246: $hr_default = $input{'f'} eq 'h';
                    247:
                    248: $logsort = $input{"logsort"};
                    249:
                    250:
                    251: ## Default CVS-Tree
                    252: if (!defined($CVSROOT{$cvstreedefault})) {
                    253:    &fatal("500 Internal Error",
                    254:          "<code>\$cvstreedefault</code> points to a repository "
                    255:          . "not defined in <code>%CVSROOT</code> "
                    256:          . "(edit your configuration file $config)");
                    257: }
                    258: $cvstree = $cvstreedefault;
                    259: $cvsroot = $CVSROOT{"$cvstree"};
                    260:
                    261: # alternate CVS-Tree, configured in cvsweb.conf
                    262: if ($input{'cvsroot'}) {
                    263:     if ($CVSROOT{$input{'cvsroot'}}) {
                    264:        $cvstree = $input{'cvsroot'};
                    265:        $cvsroot = $CVSROOT{"$cvstree"};
                    266:     }
                    267: }
                    268:
                    269: # create icons out of description
                    270: foreach my $k (keys %ICONS) {
                    271:     no strict 'refs';
                    272:     my ($itxt,$ipath,$iwidth,$iheight) = @{$ICONS{$k}};
                    273:     if ($ipath) {
                    274:        $ {"${k}icon"} = "<IMG SRC=\"$ipath\" ALT=\"$itxt\" BORDER=\"0\" WIDTH=\"$iwidth\" HEIGHT=\"$iheight\">";
                    275:     }
                    276:     else {
                    277:        $ {"${k}icon"} = $itxt;
                    278:     }
                    279: }
                    280:
                    281: # Do some special configuration for cvstrees
                    282: do "$config-$cvstree" if (-f "$config-$cvstree");
                    283:
                    284: $fullname = $cvsroot . '/' . $where;
                    285: $mimetype = &getMimeTypeFromSuffix ($fullname);
                    286: $defaultTextPlain = ($mimetype eq "text/plain");
                    287: $defaultViewable = $allow_markup && viewable($mimetype);
                    288:
                    289: # search for GZIP if compression allowed
                    290: # We've to find out if the GZIP-binary exists .. otherwise
                    291: # ge get an Internal Server Error if we try to pipe the
                    292: # output through the nonexistent gzip ..
                    293: # any more elegant ways to prevent this are welcome!
                    294: if ($allow_compress && $maycompress) {
                    295:     foreach (split(/:/, $ENV{PATH})) {
                    296:        if (-x "$_/gzip") {
                    297:            $GZIPBIN = "$_/gzip";
                    298:            last;
                    299:        }
                    300:     }
                    301: }
                    302:
                    303: if (-d $fullname) {
                    304:     #
                    305:     # ensure, that directories always end with (exactly) one '/'
                    306:     # to allow relative URL's. If they're not, make a redirect.
                    307:     ##
                    308:     my $pathinfo = defined($ENV{'PATH_INFO'}) ? $ENV{'PATH_INFO'} : "";
                    309:     if (!($pathinfo =~ m|/$|) || ($pathinfo =~ m |/{2,}$|)) {
                    310:        redirect ($scriptwhere . '/' . $query);
                    311:     }
                    312:     else {
                    313:        $where .= '/';
                    314:        $scriptwhere .= '/';
                    315:     }
                    316: }
                    317:
                    318: if (!-d $cvsroot) {
                    319:     &fatal("500 Internal Error",'$CVSROOT not found!<P>The server on which the CVS tree lives is probably down.  Please try again in a few minutes.');
                    320: }
                    321:
                    322: #
                    323: # See if the module is in our forbidden list.
                    324: #
                    325: $where =~ m:([^/]*):;
                    326: $module = $1;
                    327: if ($module && &forbidden_module($module)) {
                    328:     &fatal("403 Forbidden", "Access to $where forbidden.");
                    329: }
                    330: ##############################
                    331: # View a directory
                    332: ###############################
                    333: elsif (-d $fullname) {
                    334:        my $dh = do {local(*DH);};
                    335:        opendir($dh, $fullname) || &fatal("404 Not Found","$where: $!");
                    336:        my @dir = readdir($dh);
                    337:        closedir($dh);
                    338:        my @subLevelFiles = findLastModifiedSubdirs(@dir)
                    339:            if ($show_subdir_lastmod);
                    340:        getDirLogs($cvsroot,$where,@subLevelFiles);
                    341:
                    342:        if ($where eq '/') {
                    343:            html_header("$defaulttitle");
                    344:            print $long_intro;
                    345:        }
                    346:        else {
                    347:            html_header("$where");
                    348:            print $short_instruction;
                    349:        }
1.3       maekawa   350:
                    351:        my $descriptions;
                    352:        if (($use_descriptions) && open (DESC, "<$cvsroot/CVSROOT/descriptions")) {
                    353:            while (<DESC>) {
                    354:                chomp;
                    355:                my ($dir,$description) = /(\S+)\s+(.*)/;
                    356:                $descriptions{$dir} = $description;
                    357:            }
                    358:        }
                    359:
                    360:        print "<P><a name=\"dirlist\"></a>\n";
1.1       maekawa   361:        # give direct access to dirs
                    362:        if ($where eq '/') {
                    363:            chooseMirror();
                    364:            chooseCVSRoot();
                    365:        }
                    366:        else {
                    367:            print "<p>Current directory: <b>", &clickablePath($where,0), "</b>\n";
                    368:
                    369:            print "<P>Current tag: <B>", $input{only_with_tag}, "</b>\n" if
                    370:                $input{only_with_tag};
                    371:
                    372:        }
                    373:
                    374:
1.3       maekawa   375:        print "<HR NOSHADE>\n";
1.1       maekawa   376:        # Using <MENU> in this manner violates the HTML2.0 spec but
                    377:        # provides the results that I want in most browsers.  Another
                    378:        # case of layout spooging up HTML.
                    379:
                    380:        my $infocols = 0;
                    381:        if ($dirtable) {
                    382:            if (defined($tableBorderColor)) {
                    383:                # Can't this be done by defining the border for the inner table?
                    384:                print "<table border=0 cellpadding=0 width=\"100%\"><tr><td bgcolor=\"$tableBorderColor\">";
                    385:            }
                    386:            print "<table  width=\"100%\" border=0 cellspacing=1 cellpadding=$tablepadding>\n";
                    387:            $infocols++;
1.3       maekawa   388:            print "<tr><th align=left bgcolor=\"" . (($byfile) ?
1.1       maekawa   389:                                                   $columnHeaderColorSorted :
1.3       maekawa   390:                                                   $columnHeaderColorDefault) . "\">";
1.1       maekawa   391:            print "<a href=\"./" . &toggleQuery("sortby","file") .
                    392:                "#dirlist\">" if (!$byfile);
                    393:            print "File";
                    394:            print "</a>" if (!$byfile);
                    395:            print "</th>";
                    396:            # do not display the other column-headers, if we do not have any files
                    397:            # with revision information:
                    398:            if (scalar(%fileinfo)) {
                    399:                $infocols++;
1.3       maekawa   400:                print "<th align=left bgcolor=\"" . (($byrev) ?
1.1       maekawa   401:                                                   $columnHeaderColorSorted :
1.3       maekawa   402:                                                   $columnHeaderColorDefault) . "\">";
1.1       maekawa   403:                print "<a href=\"./" . &toggleQuery ("sortby","rev") .
                    404:                    "#dirlist\">" if (!$byrev);
                    405:                print "Rev.";
                    406:                print "</a>" if (!$byrev);
                    407:                print "</th>";
                    408:                $infocols++;
1.3       maekawa   409:                print "<th align=left bgcolor=\"" . (($bydate) ?
1.1       maekawa   410:                                                   $columnHeaderColorSorted :
1.3       maekawa   411:                                                   $columnHeaderColorDefault) . "\">";
1.1       maekawa   412:                print "<a href=\"./" . &toggleQuery ("sortby","date") .
                    413:                    "#dirlist\">" if (!$bydate);
                    414:                print "Age";
                    415:                print "</a>" if (!$bydate);
                    416:                print "</th>";
                    417:                if ($show_author) {
                    418:                    $infocols++;
1.3       maekawa   419:                    print "<th align=left bgcolor=\"" . (($byauthor) ?
1.1       maekawa   420:                                                   $columnHeaderColorSorted :
1.3       maekawa   421:                                                   $columnHeaderColorDefault) . "\">";
1.1       maekawa   422:                    print "<a href=\"./" . &toggleQuery ("sortby","author") .
                    423:                            "#dirlist\">" if (!$byauthor);
                    424:                    print "Author";
                    425:                    print "</a>" if (!$byauthor);
                    426:                    print "</th>";
                    427:                }
                    428:                $infocols++;
1.3       maekawa   429:                print "<th align=left bgcolor=\"" . (($bylog) ?
1.1       maekawa   430:                                               $columnHeaderColorSorted :
1.3       maekawa   431:                                               $columnHeaderColorDefault) . "\">";
1.1       maekawa   432:                print "<a href=\"./", toggleQuery("sortby","log"), "#dirlist\">" if (!$bylog);
                    433:                print "Last log entry";
                    434:                print "</a>" if (!$bylog);
                    435:                print "</th>";
                    436:            }
1.3       maekawa   437:            elsif ($use_descriptions) {
                    438:                print "<th align=left bgcolor=\"". $columnHeaderColorDefault . "\">";
                    439:                print "Description";
                    440:                $infocols++;
                    441:            }
1.1       maekawa   442:            print "</tr>\n";
                    443:        }
                    444:        else {
                    445:            print "<menu>\n";
                    446:        }
                    447:        my $dirrow = 0;
                    448:
                    449:        my $i;
                    450:        lookingforattic:
                    451:        for ($i = 0; $i <= $#dir; $i++) {
                    452:                if ($dir[$i] eq "Attic") {
                    453:                    last lookingforattic;
                    454:                }
                    455:        }
                    456:        if (!$input{'hideattic'} && ($i <= $#dir) &&
                    457:            opendir($dh, $fullname . "/Attic")) {
                    458:            splice(@dir, $i, 1,
                    459:                        grep((s|^|Attic/|,!m|/\.|), readdir($dh)));
                    460:            closedir($dh);
                    461:        }
                    462:
                    463:        my $hideAtticToggleLink = "<a href=\"./" .
                    464:                &toggleQuery ("hideattic") .
                    465:                "#dirlist\">[Hide]</a>" if (!$input{'hideattic'});
                    466:
                    467:        # Sort without the Attic/ pathname.
                    468:        # place directories first
                    469:
                    470:        my $attic;
                    471:        my $url;
                    472:        my $fileurl;
                    473:        my $filesexists;
                    474:        my $filesfound;
                    475:
                    476:        foreach (sort { &fileSortCmp } @dir) {
                    477:            if ($_ eq '.') {
                    478:                next;
                    479:            }
                    480:            # ignore CVS lock and stale NFS files
                    481:            next if (/^#cvs\.|^,|^\.nfs/);
                    482:
                    483:            # Check whether to show the CVSROOT path
                    484:            next if ($input{'hidecvsroot'} && ($_ eq 'CVSROOT'));
                    485:
                    486:            # Check whether the module is in the restricted list
                    487:            next if ($_ && &forbidden_module($_));
                    488:
                    489:            # Ignore non-readable files
                    490:            next if ($input{'hidenonreadable'} && !(-r "$fullname/$_"));
                    491:
                    492:            if (s|^Attic/||) {
                    493:                $attic  = " (in the Attic)&nbsp;" . $hideAtticToggleLink;
                    494:            }
                    495:            else {
                    496:                $attic = "";
                    497:            }
                    498:
                    499:            if ($_ eq '..' || -d "$fullname/$_") {
                    500:                next if ($_ eq '..' && $where eq '/');
                    501:                my ($rev,$date,$log,$author,$filename) = @{$fileinfo{$_}}
                    502:                    if (defined($fileinfo{$_}));
                    503:                print "<tr bgcolor=\"" . @tabcolors[$dirrow%2] . "\"><td>" if ($dirtable);
                    504:                if ($_ eq '..') {
                    505:                    $url = "../" . $query;
                    506:                    if ($nofilelinks) {
                    507:                        print $backicon;
                    508:                    }
                    509:                    else {
                    510:                        print &link($backicon,$url);
                    511:                    }
                    512:                    print " ", &link("Previous Directory",$url);
                    513:                }
                    514:                else {
                    515:                    $url = urlencode($_) . '/' . $query;
1.3       maekawa   516:                    print "<A NAME=\"$_\"></A>";
1.1       maekawa   517:                    if ($nofilelinks) {
                    518:                        print $diricon;
                    519:                    }
                    520:                    else {
                    521:                        print &link($diricon,$url);
                    522:                    }
                    523:                    print " ", &link($_ . "/", $url), $attic;
                    524:                    if ($_ eq "Attic") {
                    525:                        print "&nbsp; <a href=\"./" .
                    526:                            &toggleQuery ("hideattic") .
                    527:                                "#dirlist\">[Don't hide]</a>";
                    528:                    }
                    529:                }
                    530:                # Show last change in dir
                    531:                if ($filename) {
1.3       maekawa   532:                    print "</td><td>&nbsp;</td><td>&nbsp;" if ($dirtable);
1.1       maekawa   533:                    if ($date) {
                    534:                        print " <i>" . readableTime(time() - $date,0) . "</i>";
                    535:                    }
                    536:                    if ($show_author) {
                    537:                        print "</td><td>&nbsp;" if ($dirtable);
                    538:                        print $author;
                    539:                    }
                    540:                    print "</td><td>&nbsp;" if ($dirtable);
                    541:                    $filename =~ s%^[^/]+/%%;
                    542:                    print "$filename/$rev";
                    543:                    print "<BR>" if ($dirtable);
                    544:                    if ($log) {
                    545:                        print "&nbsp;<font size=-1>"
                    546:                            . &htmlify(substr($log,0,$shortLogLen));
                    547:                        if (length $log > 80) {
                    548:                            print "...";
                    549:                        }
                    550:                        print "</font>";
                    551:                    }
                    552:                }
                    553:                else {
1.3       maekawa   554:                    my ($dwhere) = ($where ne "/" ? $where : "") . $_;
                    555:                    if ($use_descriptions && defined $descriptions{$dwhere}) {
                    556:                        print "<TD COLSPAN=" . ($infocols-1) . ">&nbsp;" if $dirtable;
                    557:                        print $descriptions{$dwhere};
                    558:                    } elsif ($dirtable && $infocols > 1) {
                    559:                        # close the row with the appropriate number of
                    560:                        # columns, so that the vertical seperators are visible
1.1       maekawa   561:                        my($cols) = $infocols;
                    562:                        while ($cols > 1) {
1.3       maekawa   563:                            print "</td><td>&nbsp;";
1.1       maekawa   564:                            $cols--;
                    565:                        }
                    566:                    }
                    567:                }
                    568:                if ($dirtable) {
                    569:                    print "</td></tr>\n";
                    570:                }
                    571:                else {
                    572:                    print "<br>\n";
                    573:                }
                    574:                $dirrow++;
                    575:            }
                    576:            elsif (s/,v$//) {
                    577:                $fileurl = ($attic ? "Attic/" : "") . urlencode($_);
                    578:                $url = $fileurl . $query;
                    579:                my $rev = '';
                    580:                my $date = '';
                    581:                my $log = '';
                    582:                my $author = '';
                    583:                $filesexists++;
                    584:                next if (!defined($fileinfo{$_}));
                    585:                ($rev,$date,$log,$author) = @{$fileinfo{$_}};
                    586:                $filesfound++;
                    587:                print "<tr bgcolor=\"" . @tabcolors[$dirrow%2] . "\"><td>" if ($dirtable);
1.3       maekawa   588:                print "<A NAME=\"$_\"></A>";
1.1       maekawa   589:                if ($nofilelinks) {
                    590:                    print $fileicon;
                    591:                }
                    592:                else {
                    593:                    print &link($fileicon,$url);
                    594:                }
                    595:                print " ", &link($_, $url), $attic;
                    596:                print "</td><td>&nbsp;" if ($dirtable);
                    597:                download_link($fileurl,
                    598:                        $rev, $rev,
                    599:                        $defaultViewable ? "text/x-cvsweb-markup" : undef);
                    600:                print "</td><td>&nbsp;" if ($dirtable);
                    601:                if ($date) {
                    602:                    print " <i>" . readableTime(time() - $date,0) . "</i>";
                    603:                }
                    604:                if ($show_author) {
                    605:                    print "</td><td>&nbsp;" if ($dirtable);
                    606:                    print $author;
                    607:                }
                    608:                print "</td><td>&nbsp;" if ($dirtable);
                    609:                if ($log) {
                    610:                    print " <font size=-1>" . &htmlify(substr($log,0,$shortLogLen));
                    611:                    if (length $log > 80) {
                    612:                        print "...";
                    613:                    }
                    614:                    print "</font>";
                    615:                }
                    616:                print "</td>" if ($dirtable);
                    617:                print (($dirtable) ? "</tr>" : "<br>");
                    618:                $dirrow++;
                    619:            }
                    620:            print "\n";
                    621:        }
                    622:        if ($dirtable && defined($tableBorderColor)) {
                    623:            print "</td></tr></table>";
                    624:        }
                    625:        print "". ($dirtable == 1) ? "</table>" : "</menu>" . "\n";
                    626:
                    627:        if ($filesexists && !$filesfound) {
                    628:            print "<P><B>NOTE:</B> There are $filesexists files, but none matches the current tag ($input{only_with_tag})\n";
                    629:        }
                    630:        if ($input{only_with_tag} && (!%tags || !$tags{$input{only_with_tag}})) {
                    631:            %tags = %alltags
                    632:        }
                    633:        if (scalar %tags
                    634:            || $input{only_with_tag}
                    635:            || $edit_option_form
                    636:            || defined($input{"options"})) {
                    637:            print "<hr size=1 NOSHADE>";
                    638:        }
                    639:
                    640:        if (scalar %tags || $input{only_with_tag}) {
                    641:            print "<FORM METHOD=\"GET\" ACTION=\"./\">\n";
                    642:            foreach my $var (@stickyvars) {
                    643:                print "<INPUT TYPE=HIDDEN NAME=\"$var\" VALUE=\"$input{$var}\">\n"
                    644:                    if (defined($input{$var})
                    645:                        && $input{$var} ne $DEFAULTVALUE{$var}
                    646:                        && $input{$var} ne ""
                    647:                        && $var ne "only_with_tag");
                    648:            }
                    649:            print "Show only files with tag:\n";
                    650:            print "<SELECT NAME=only_with_tag";
                    651:            print " onchange=\"submit()\"" if ($use_java_script);
                    652:            print ">";
                    653:            print "<OPTION VALUE=\"\">All tags / default branch\n";
                    654:            foreach my $tag (reverse sort { lc $a cmp lc $b } keys %tags) {
                    655:                print "<OPTION",defined($input{only_with_tag}) &&
                    656:                       $input{only_with_tag} eq $tag ? " SELECTED":"",
                    657:                       ">$tag\n";
                    658:            }
                    659:            print "</SELECT>\n";
                    660:            print "<INPUT TYPE=SUBMIT VALUE=\"Go\">\n";
                    661:            print "</FORM>\n";
                    662:        }
                    663:        my $formwhere = $scriptwhere;
                    664:        $formwhere =~ s|Attic/?$|| if ($input{'hideattic'});
                    665:
                    666:        if ($edit_option_form || defined($input{"options"})) {
                    667:            print "<FORM METHOD=\"GET\" ACTION=\"${formwhere}\">\n";
                    668:            print "<INPUT TYPE=HIDDEN NAME=\"copt\" VALUE=\"1\">\n";
                    669:            if ($cvstree ne $cvstreedefault) {
                    670:                print "<INPUT TYPE=HIDDEN NAME=\"cvsroot\" VALUE=\"$cvstree\">\n";
                    671:            }
                    672:            print "<center><table cellpadding=0 cellspacing=0>";
                    673:            print "<tr bgcolor=\"$columnHeaderColorDefault\"><th colspan=2>Preferences</th></tr>";
                    674:            print "<tr><td>Sort files by <SELECT name=\"sortby\">";
                    675:            print "<OPTION VALUE=\"\">File";
                    676:            print "<OPTION",$bydate ? " SELECTED" : ""," VALUE=date>Age";
                    677:            print "<OPTION",$byauthor ? " SELECTED" : ""," VALUE=author>Author"
                    678:                if ($show_author);
                    679:            print "<OPTION",$byrev ? " SELECTED" : ""," VALUE=rev>Revision";
                    680:            print "<OPTION",$bylog ? " SELECTED" : ""," VALUE=log>Log message";
                    681:            print "</SELECT></td>";
                    682:            print "<td>revisions by: \n";
                    683:            print "<SELECT NAME=logsort>\n";
                    684:            print "<OPTION VALUE=cvs",$logsort eq "cvs" ? " SELECTED" : "", ">Not sorted";
                    685:            print "<OPTION VALUE=date",$logsort eq "date" ? " SELECTED" : "", ">Commit date";
                    686:            print "<OPTION VALUE=rev",$logsort eq "rev" ? " SELECTED" : "", ">Revision";
                    687:            print "</SELECT></td></tr>";
                    688:            print "<tr><td>Diff format: ";
                    689:            printDiffSelect();
                    690:            print "</td>";
                    691:            print "<td>Show Attic files: ";
                    692:            print "<INPUT NAME=hideattic TYPE=CHECKBOX", $input{'hideattic'}?" CHECKED":"",
                    693:            "></td></tr>\n";
                    694:            print "<tr><td align=center colspan=2><input type=submit value=\"Change Options\">";
                    695:            print "</td></tr></table></center></FORM>\n";
                    696:        }
                    697:        print &html_footer;
                    698:        print "</BODY></HTML>\n";
                    699:     }
                    700:
                    701: ###############################
                    702: # View Files
                    703: ###############################
                    704:     elsif (-f $fullname . ',v') {
                    705:        if (defined($input{'rev'}) || $doCheckout) {
                    706:            &doCheckout($fullname, $input{'rev'});
                    707:            exit;
                    708:        }
                    709:        if (defined($input{'annotate'}) && $allow_annotate) {
                    710:            &doAnnotate($input{'annotate'});
                    711:            exit;
                    712:        }
                    713:        if (defined($input{'r1'}) && defined($input{'r2'})) {
                    714:            &doDiff($fullname, $input{'r1'}, $input{'tr1'},
                    715:                    $input{'r2'}, $input{'tr2'}, $input{'f'});
                    716:            exit;
                    717:        }
                    718:        print("going to dolog($fullname)\n") if ($verbose);
                    719:        &doLog($fullname);
                    720: ##############################
                    721: # View Diff
                    722: ##############################
                    723:     }
                    724:     elsif ($fullname =~ s/\.diff$// && -f $fullname . ",v" &&
                    725:           $input{'r1'} && $input{'r2'}) {
                    726:
                    727:        # $where-diff-removal if 'cvs rdiff' is used
                    728:        # .. but 'cvs rdiff'doesn't support some options
                    729:        # rcsdiff does (-w and -p), so it is disabled
                    730:        # $where =~ s/\.diff$//;
                    731:
                    732:        # Allow diffs using the ".diff" extension
                    733:        # so that browsers that default to the URL
                    734:        # for a save filename don't save diff's as
                    735:        # e.g. foo.c
                    736:        &doDiff($fullname, $input{'r1'}, $input{'tr1'},
                    737:                $input{'r2'}, $input{'tr2'}, $input{'f'});
                    738:        exit;
                    739:     }
                    740:     elsif (($newname = $fullname) =~ s|/([^/]+)$|/Attic/$1| &&
                    741:           -f $newname . ",v") {
                    742:        # The file has been removed and is in the Attic.
                    743:        # Send a redirect pointing to the file in the Attic.
                    744:        (my $newplace = $scriptwhere) =~ s|/([^/]+)$|/Attic/$1|;
                    745:        &redirect($newplace);
                    746:        exit;
                    747:     }
                    748:     elsif (0 && (my @files = &safeglob($fullname . ",v"))) {
                    749:        http_header("text/plain");
                    750:        print "You matched the following files:\n";
                    751:        print join("\n", @files);
                    752:        # Find the tags from each file
                    753:        # Display a form offering diffs between said tags
                    754:     }
                    755:     else {
                    756:        my $fh = do {local(*FH);};
                    757:        my ($xtra, $module);
                    758:        # Assume it's a module name with a potential path following it.
                    759:        $xtra = $& if (($module = $where) =~ s|/.*||);
                    760:        # Is there an indexed version of modules?
                    761:        if (open($fh, "$cvsroot/CVSROOT/modules")) {
                    762:            while (<$fh>) {
                    763:                if (/^(\S+)\s+(\S+)/o && $module eq $1
                    764:                    && -d "${cvsroot}/$2" && $module ne $2) {
                    765:                    &redirect($scriptname . '/' . $2 . $xtra);
                    766:                }
                    767:            }
                    768:        }
                    769:        &fatal("404 Not Found","$where: no such file or directory");
                    770:     }
                    771: ## End MAIN
                    772:
                    773: sub printDiffSelect {
                    774:     my ($use_java_script) = @_;
                    775:     $use_java_script = 0 if (!defined($use_java_script));
                    776:     my ($f) = $input{'f'};
                    777:     print "<SELECT NAME=\"f\"";
                    778:     print " onchange=\"submit()\"" if ($use_java_script);
                    779:     print ">\n";
                    780:     print "<OPTION VALUE=h",$f eq "h" ? " SELECTED" : "", ">Colored Diff";
                    781:     print "<OPTION VALUE=H",$f eq "H" ? " SELECTED" : "", ">Long Colored Diff";
                    782:     print "<OPTION VALUE=u",$f eq "u" ? " SELECTED" : "", ">Unidiff";
                    783:     print "<OPTION VALUE=c",$f eq "c" ? " SELECTED" : "", ">Context Diff";
                    784:     print "<OPTION VALUE=s",$f eq "s" ? " SELECTED" : "", ">Side by Side";
                    785:     print "</SELECT>";
                    786: }
                    787:
                    788: sub findLastModifiedSubdirs {
                    789:     my (@dirs) = @_;
                    790:     my ($dirname, @files);
                    791:
                    792:     foreach $dirname (@dirs) {
                    793:        next if ($dirname eq ".");
                    794:        next if ($dirname eq "..");
                    795:        my ($dir) = "$fullname/$dirname";
                    796:        next if (!-d $dir);
                    797:
                    798:        my ($lastmod) = undef;
                    799:        my ($lastmodtime) = undef;
                    800:        my $dh = do {local(*DH);};
                    801:
                    802:        opendir($dh,$dir) || next;
                    803:        my (@filenames) = readdir($dh);
                    804:        closedir($dh);
                    805:
                    806:        foreach my $filename (@filenames) {
                    807:            $filename = "$dirname/$filename";
                    808:            my ($file) = "$fullname/$filename";
                    809:            next if ($filename !~ /,v$/ || !-f $file);
                    810:            $filename =~ s/,v$//;
                    811:            my $modtime = -M $file;
                    812:            if (!defined($lastmod) || $modtime < $lastmodtime) {
                    813:                $lastmod = $filename;
                    814:                $lastmodtime = $modtime;
                    815:            }
                    816:        }
                    817:        push(@files, $lastmod) if (defined($lastmod));
                    818:     }
                    819:     return @files;
                    820: }
                    821:
                    822: sub htmlify {
                    823:        my($string, $pr) = @_;
                    824:
                    825:        # Special Characters; RFC 1866
                    826:        $string =~ s/&/&amp;/g;
                    827:        $string =~ s/\"/&quot;/g;
                    828:        $string =~ s/</&lt;/g;
                    829:        $string =~ s/>/&gt;/g;
                    830:
                    831:        # get URL's as link ..
1.4     ! maekawa   832:        $string =~ s§(http|ftp|https)(://[-a-zA-Z0-9%.~:_/]+)([?&]([-a-zA-Z0-9%.~:_]+)=([-a-zA-Z0-9%.~:_])+)*§<A HREF="$1$2$3">$1$2$3</A>§;
1.1       maekawa   833:        # get e-mails as link
                    834:        $string =~ s§([-a-zA-Z0-9_.]+@([-a-zA-Z0-9]+\.)+[A-Za-z]{2,4})§<A HREF="mailto:$1">$1</A>§;
                    835:
                    836:        # get #PR as link ..
                    837:        if ($pr && defined($prcgi)) {
                    838:            $string =~ s!\b((pr[:#]?\s*#?)|((bin|conf|docs|gnu|i386|kern|misc|ports)\/))(\d+)\b!<A HREF="$prcgi?pr=$5">$&</A>!ig;
                    839:        }
                    840:
                    841:        return $string;
                    842: }
                    843:
                    844: sub spacedHtmlText {
                    845:        my($string, $pr) = @_;
                    846:
                    847:        # Cut trailing spaces
                    848:        s/\s+$//;
                    849:
                    850:        # Expand tabs
                    851:        $string =~ s/\t+/' ' x (length($&) * $tabstop - length($`) % $tabstop)/e
                    852:            if (defined($tabstop));
                    853:
                    854:        # replace <tab> and <space> (§ is to protect us from htmlify)
                    855:        # gzip can make excellent use of this repeating pattern :-)
                    856:        $string =~ s/§/§%/g; #protect our & substitute
                    857:        if ($hr_breakable) {
                    858:            # make every other space 'breakable'
                    859:            $string =~ s/       / §nbsp; §nbsp; §nbsp; §nbsp;/g;    # <tab>
                    860:            $string =~ s/  / §nbsp;/g;                              # 2 * <space>
                    861:            # leave single space as it is
                    862:        }
                    863:        else {
                    864:            $string =~ s/       /§nbsp;§nbsp;§nbsp;§nbsp;§nbsp;§nbsp;§nbsp;§nbsp;/g;
                    865:            $string =~ s/ /§nbsp;/g;
                    866:        }
                    867:
                    868:        $string = htmlify($string);
                    869:
                    870:        # unescape
                    871:        $string =~ s/§([^%])/&$1/g;
                    872:        $string =~ s/§%/§/g;
                    873:
                    874:        return $string;
                    875: }
                    876:
                    877: sub link {
                    878:        my($name, $where) = @_;
                    879:
                    880:        return "<A HREF=\"$where\">$name</A>\n";
                    881: }
                    882:
                    883: sub revcmp {
                    884:        my($rev1, $rev2) = @_;
                    885:        my(@r1) = split(/\./, $rev1);
                    886:        my(@r2) = split(/\./, $rev2);
                    887:        my($a,$b);
                    888:
                    889:        while (($a = shift(@r1)) && ($b = shift(@r2))) {
                    890:            if ($a != $b) {
                    891:                return $a <=> $b;
                    892:            }
                    893:        }
                    894:        if (@r1) { return 1; }
                    895:        if (@r2) { return -1; }
                    896:        return 0;
                    897: }
                    898:
                    899: sub fatal {
                    900:        my($errcode, $errmsg) = @_;
                    901:        if (defined($ENV{'MOD_PERL'})) {
                    902:                Apache->request->status((split(/ /, $errcode))[0]);
                    903:        }
                    904:        else {
                    905:                print "Status: $errcode\n";
                    906:        }
                    907:        html_header("Error");
                    908:        print "Error: $errmsg\n";
                    909:        print &html_footer;
                    910:        exit(1);
                    911: }
                    912:
                    913: sub redirect {
                    914:        my($url) = @_;
                    915:        if (defined($ENV{'MOD_PERL'})) {
                    916:                Apache->request->status(301);
                    917:                Apache->request->header_out(Location => $url);
                    918:        }
                    919:        else {
                    920:                print "Status: 301 Moved\n";
                    921:                print "Location: $url\n";
                    922:        }
                    923:        html_header("Moved");
                    924:        print "This document is located <A HREF=$url>here</A>.\n";
                    925:        print &html_footer;
                    926:        exit(1);
                    927: }
                    928:
                    929: sub safeglob {
                    930:        my ($filename) = @_;
                    931:        my ($dirname);
                    932:        my (@results);
                    933:        my $dh = do {local(*DH);};
                    934:
                    935:        ($dirname = $filename) =~ s|/[^/]+$||;
                    936:        $filename =~ s|.*/||;
                    937:
                    938:        if (opendir($dh, $dirname)) {
                    939:                my $glob = $filename;
                    940:                my $t;
                    941:        #       transform filename from glob to regex.  Deal with:
                    942:        #       [, {, ?, * as glob chars
                    943:        #       make sure to escape all other regex chars
                    944:                $glob =~ s/([\.\(\)\|\+])/\\$1/g;
                    945:                $glob =~ s/\*/.*/g;
                    946:                $glob =~ s/\?/./g;
                    947:                $glob =~ s/{([^}]+)}/($t = $1) =~ s-,-|-g; "($t)"/eg;
                    948:                foreach (readdir($dh)) {
                    949:                        if (/^${glob}$/) {
                    950:                                push(@results, $dirname . "/" .$_);
                    951:                        }
                    952:                }
                    953:        }
                    954:
                    955:        @results;
                    956: }
                    957:
                    958: sub getMimeTypeFromSuffix {
                    959:     my ($fullname) = @_;
                    960:     my ($mimetype, $suffix);
                    961:     my $fh = do {local(*FH);};
                    962:
                    963:     ($suffix = $fullname) =~ s/^.*\.([^.]*)$/$1/;
                    964:     $mimetype = $MTYPES{$suffix};
                    965:     $mimetype = $MTYPES{'*'} if (!$mimetype);
                    966:
                    967:     if (!$mimetype && -f $mime_types) {
                    968:        # okey, this is something special - search the
                    969:        # mime.types database
                    970:        open ($fh, "<$mime_types");
                    971:        while (<$fh>) {
                    972:            if ($_ =~ /^\s*(\S+\/\S+).*\b$suffix\b/) {
                    973:                $mimetype = $1;
                    974:                last;
                    975:            }
                    976:        }
                    977:        close ($fh);
                    978:     }
                    979:
                    980: # okey, didn't find anything useful ..
                    981:     if (!($mimetype =~ /\S\/\S/)) {
                    982:        $mimetype = "text/plain";
                    983:     }
                    984:     return $mimetype;
                    985: }
                    986:
                    987: ###############################
                    988: # show Annotation
                    989: ###############################
                    990: sub doAnnotate ($$) {
                    991:     my ($rev) = @_;
                    992:     my ($pid);
                    993:     my ($pathname, $filename);
                    994:     my $reader = do {local(*FH);};
                    995:     my $writer = do {local(*FH);};
                    996:
                    997:     # make sure the revisions a wellformed, for security
                    998:     # reasons ..
                    999:     if (!($rev =~ /^[\d\.]+$/)) {
                   1000:        &fatal("404 Not Found",
                   1001:                "Malformed query \"$ENV{'QUERY_STRING'}\"");
                   1002:     }
                   1003:
                   1004:     ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1005:     ($filename = $where) =~ s/^.*\///;
                   1006:
                   1007:     http_header();
                   1008:
                   1009:     navigateHeader ($scriptwhere,$pathname,$filename,$rev, "annotate");
                   1010:     print "<h3 align=center>Annotation of $pathname$filename, Revision $rev</h3>\n";
                   1011:
                   1012:     # this seems to be necessary
                   1013:     $| = 1; $| = 0; # Flush
                   1014:
                   1015:     # this annotate version is based on the
                   1016:     # cvs annotate-demo Perl script by Cyclic Software
                   1017:     # It was written by Cyclic Software, http://www.cyclic.com/, and is in
                   1018:     # the public domain.
                   1019:     # we could abandon the use of rlog, rcsdiff and co using
                   1020:     # the cvsserver in a similiar way one day (..after rewrite)
                   1021:     $pid = open2($reader, $writer, "cvs server") || fatal ("500 Internal Error",
                   1022:                                                               "Fatal Error - unable to open cvs for annotation");
                   1023:
                   1024:     # OK, first send the request to the server.  A simplified example is:
                   1025:     #     Root /home/kingdon/zwork/cvsroot
                   1026:     #     Argument foo/xx
                   1027:     #     Directory foo
                   1028:     #     /home/kingdon/zwork/cvsroot/foo
                   1029:     #     Directory .
                   1030:     #     /home/kingdon/zwork/cvsroot
                   1031:     #     annotate
                   1032:     # although as you can see there are a few more details.
                   1033:
                   1034:     print $writer "Root $cvsroot\n";
                   1035:     print $writer "Valid-responses ok error Valid-requests Checked-in Updated Merged Removed M E\n";
                   1036:     # Don't worry about sending valid-requests, the server just needs to
                   1037:     # support "annotate" and if it doesn't, there isn't anything to be done.
                   1038:     print $writer "UseUnchanged\n";
                   1039:     print $writer "Argument -r\n";
                   1040:     print $writer "Argument $rev\n";
                   1041:     print $writer "Argument $where\n";
                   1042:
                   1043:     # The protocol requires us to fully fake a working directory (at
                   1044:     # least to the point of including the directories down to the one
                   1045:     # containing the file in question).
                   1046:     # So if $where is "dir/sdir/file", then @dirs will be ("dir","sdir","file")
                   1047:     my @dirs = split (/\//, $where);
                   1048:     my $path = "";
                   1049:     foreach (@dirs) {
                   1050:        if ($path eq "") {
                   1051:            # In our example, $_ is "dir".
                   1052:            $path = $_;
                   1053:        }
                   1054:        else {
                   1055:            print $writer "Directory " . $path . "\n";
                   1056:            print $writer "$cvsroot/" . $path ."\n";
                   1057:            # In our example, $_ is "sdir" and $path becomes "dir/sdir"
                   1058:            # And the next time, "file" and "dir/sdir/file" (which then gets
                   1059:            # ignored, because we don't need to send Directory for the file).
                   1060:             $path = $path . "/" . $_;
                   1061:        }
                   1062:     }
                   1063:     # And the last "Directory" before "annotate" is the top level.
                   1064:     print $writer "Directory .\n";
                   1065:     print $writer "$cvsroot\n";
                   1066:
                   1067:     print $writer "annotate\n";
                   1068:     # OK, we've sent our command to the server.  Thing to do is to
                   1069:     # close the writer side and get all the responses.  If "cvs server"
                   1070:     # were nicer about buffering, then we could just leave it open, I think.
                   1071:     close ($writer) || die "cannot close: $!";
                   1072:
                   1073:     # Ready to get the responses from the server.
                   1074:     # For example:
                   1075:     #     E Annotations for foo/xx
                   1076:     #     E ***************
                   1077:     #     M 1.3          (kingdon  06-Sep-97): hello
                   1078:     #     ok
                   1079:     my ($lineNr) = 0;
                   1080:     my ($oldLrev, $oldLusr) = ("", "");
                   1081:     my ($revprint, $usrprint);
                   1082:     if ($annTable) {
                   1083:        print "<table border=0 cellspacing=0 cellpadding=0>\n";
                   1084:     }
                   1085:     else {
                   1086:        print "<pre>";
                   1087:     }
                   1088:     while (<$reader>) {
                   1089:        my @words = split;
                   1090:        # Adding one is for the (single) space which follows $words[0].
                   1091:        my $rest = substr ($_, length ($words[0]) + 1);
                   1092:        if ($words[0] eq "E") {
                   1093:            next;
                   1094:        }
                   1095:        elsif ($words[0] eq "M") {
                   1096:            $lineNr++;
                   1097:            my $lrev = substr ($_, 2, 13);
                   1098:            my $lusr = substr ($_, 16,  9);
                   1099:            my $line = substr ($_, 36);
                   1100:            # we should parse the date here ..
                   1101:            if ($lrev eq $oldLrev) {
                   1102:                $revprint = "             ";
                   1103:            }
                   1104:            else {
                   1105:                $revprint = $lrev; $oldLusr = "";
                   1106:            }
                   1107:            if ($lusr eq $oldLusr) {
                   1108:                $usrprint = "         ";
                   1109:            }
                   1110:            else {
                   1111:                $usrprint = $lusr;
                   1112:            }
                   1113:            $oldLrev = $lrev;
                   1114:            $oldLusr = $lusr;
                   1115:            # is there a less timeconsuming way to strip spaces ?
                   1116:            ($lrev = $lrev) =~ s/\s+//g;
                   1117:            my $isCurrentRev = ("$rev" eq "$lrev");
                   1118:
                   1119:            print "<b>" if ($isCurrentRev);
                   1120:            printf ("%8s%s%8s %4d:", $revprint, ($isCurrentRev ? "|" : " "), $usrprint, $lineNr);
                   1121:            print spacedHtmlText($line);
                   1122:            print "</b>" if ($isCurrentRev);
                   1123:        }
                   1124:        elsif ($words[0] eq "ok") {
                   1125:            # We could complain about any text received after this, like the
                   1126:            # CVS command line client.  But for simplicity, we don't.
                   1127:        }
                   1128:        elsif ($words[0] eq "error") {
                   1129:            fatal ("500 Internal Error", "Error occured during annotate: <b>$_</b>");
                   1130:        }
                   1131:     }
                   1132:     if ($annTable) {
                   1133:        print "</table>";
                   1134:     }
                   1135:     else {
                   1136:        print "</pre>";
                   1137:     }
                   1138:     close ($reader) || warn "cannot close: $!";
                   1139:     wait;
                   1140: }
                   1141:
                   1142: ###############################
                   1143: # make Checkout
                   1144: ###############################
                   1145: sub doCheckout {
                   1146:     my ($fullname, $rev) = @_;
                   1147:     my ($mimetype,$revopt);
                   1148:     my $fh = do {local(*FH);};
                   1149:
                   1150:     # make sure the revisions a wellformed, for security
                   1151:     # reasons ..
                   1152:     if (defined($rev) && !($rev =~ /^[\d\.]+$/)) {
                   1153:        &fatal("404 Not Found",
                   1154:                "Malformed query \"$ENV{'QUERY_STRING'}\"");
                   1155:     }
                   1156:
                   1157:     # get mimetype
                   1158:     if (defined($input{"content-type"}) && ($input{"content-type"} =~ /\S\/\S/)) {
                   1159:        $mimetype = $input{"content-type"}
                   1160:     }
                   1161:     else {
                   1162:        $mimetype = &getMimeTypeFromSuffix($fullname);
                   1163:     }
                   1164:
                   1165:     if (defined($rev)) {
1.3       maekawa  1166:        $revopt = "-r$rev";
                   1167:        if ($use_moddate) {
                   1168:            readLog($fullname,$rev);
                   1169:            $moddate=$date{$rev};
                   1170:        }
1.1       maekawa  1171:     }
                   1172:     else {
1.4     ! maekawa  1173:        $revopt = "-rHEAD";
1.3       maekawa  1174:        if ($use_moddate) {
                   1175:            readLog($fullname);
                   1176:            $moddate=$date{$symrev{HEAD}};
                   1177:        }
1.1       maekawa  1178:     }
                   1179:
                   1180:     ### just for the record:
                   1181:     ### 'cvs co' seems to have a bug regarding single checkout of
                   1182:     ### directories/files having spaces in it;
                   1183:     ### this is an issue that should be resolved on cvs's side
1.3       maekawa  1184:     #
                   1185:     # Safely for a child process to read from.
                   1186:     if (! open($fh, "-|")) { # child
                   1187:       open(STDERR, ">&STDOUT"); # Redirect stderr to stdout
1.4     ! maekawa  1188:       exec("cvs", "-d", "$cvsroot", "co", "-p", "$revopt", "$where");
1.3       maekawa  1189:     }
1.1       maekawa  1190: #===================================================================
                   1191: #Checking out squid/src/ftp.c
                   1192: #RCS:  /usr/src/CVS/squid/src/ftp.c,v
                   1193: #VERS: 1.1.1.28.6.2
                   1194: #***************
                   1195:
                   1196:     # Parse CVS header
                   1197:     my ($revision, $filename, $cvsheader);
                   1198:     while(<$fh>) {
                   1199:        last if (/^\*\*\*\*/);
                   1200:        $revision = $1 if (/^VERS: (.*)$/);
1.4     ! maekawa  1201:        if (/^Checking out (.*)$/) {
        !          1202:                $filename = $1;
        !          1203:                $filename =~ s/^\.\/*//;
        !          1204:        }
1.1       maekawa  1205:        $cvsheader .= $_;
                   1206:     }
                   1207:     if ($filename ne $where) {
                   1208:        &fatal("500 Internal Error",
                   1209:               "Unexpected output from cvs co: $cvsheader"
                   1210:               . "<p><b>Check whether the directory $cvsroot/CVSROOT exists "
                   1211:               . "and the script has write-access to the CVSROOT/history "
                   1212:               . "file if it exists."
                   1213:               . "<br>The script needs to place lock files in the "
                   1214:               . "directory the file is in as well.</b>");
                   1215:     }
                   1216:     $| = 1;
                   1217:
                   1218:     if ($mimetype eq "text/x-cvsweb-markup") {
                   1219:        &cvswebMarkup($fh,$fullname,$revision);
                   1220:     }
                   1221:     else {
                   1222:        http_header($mimetype);
                   1223:        print <$fh>;
                   1224:     }
                   1225:     close($fh);
                   1226: }
                   1227:
                   1228: sub cvswebMarkup {
                   1229:     my ($filehandle,$fullname,$revision) = @_;
                   1230:     my ($pathname, $filename);
                   1231:
                   1232:     ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1233:     ($filename = $where) =~ s/^.*\///;
                   1234:     my ($fileurl) = urlencode($filename);
                   1235:
                   1236:     http_header();
                   1237:
                   1238:     navigateHeader ($scriptwhere, $pathname, $filename, $revision, "view");
                   1239:     print "<HR noshade>";
                   1240:     print "<table width=\"100%\"><tr><td bgcolor=\"$markupLogColor\">";
1.3       maekawa  1241:     print "File: ", &clickablePath($where, 1);
1.1       maekawa  1242:     print "&nbsp;";
                   1243:     &download_link(urlencode($fileurl), $revision, "(download)");
                   1244:     if (!$defaultTextPlain) {
                   1245:        print "&nbsp;";
                   1246:        &download_link(urlencode($fileurl), $revision, "(as text)",
                   1247:               "text/plain");
                   1248:     }
                   1249:     print "<BR>\n";
                   1250:     if ($show_log_in_markup) {
                   1251:        readLog($fullname); #,$revision);
                   1252:        printLog($revision,0);
                   1253:     }
                   1254:     else {
                   1255:        print "Version: <B>$revision</B><BR>\n";
                   1256:        print "Tag: <B>", $input{only_with_tag}, "</b><br>\n" if
                   1257:            $input{only_with_tag};
                   1258:     }
                   1259:     print "</td></tr></table>";
                   1260:     my @content = <$filehandle>;
                   1261:     my $url = download_url($fileurl, $revision, $mimetype);
                   1262:     print "<HR noshade>";
                   1263:     if ($mimetype =~ /^image/) {
                   1264:        print "<IMG SRC=\"$url$barequery\"><BR>";
                   1265:     }
                   1266:     else {
                   1267:        print "<PRE>";
                   1268:        foreach (@content) {
1.3       maekawa  1269:            print spacedHtmlText($_);
1.1       maekawa  1270:        }
                   1271:        print "</PRE>";
                   1272:     }
                   1273: }
                   1274:
                   1275: sub viewable($) {
                   1276:     my ($mimetype) = @_;
                   1277:
                   1278:     $mimetype =~ m%^text/% ||
                   1279:     $mimetype =~ m%^image/% ||
                   1280:     0;
                   1281: }
                   1282:
                   1283: ###############################
                   1284: # Show Colored Diff
                   1285: ###############################
                   1286: sub doDiff {
                   1287:        my($fullname, $r1, $tr1, $r2, $tr2, $f) = @_;
                   1288:         my $fh = do {local(*FH);};
1.3       maekawa  1289:        my ($rev1, $rev2, $sym1, $sym2, @difftype, $diffname, $f1, $f2);
1.1       maekawa  1290:
                   1291:        if ($r1 =~ /([^:]+)(:(.+))?/) {
                   1292:            $rev1 = $1;
                   1293:            $sym1 = $3;
                   1294:        }
                   1295:        if ($r1 eq 'text') {
                   1296:            $rev1 = $tr1;
                   1297:            $sym1 = "";
                   1298:        }
                   1299:        if ($r2 =~ /([^:]+)(:(.+))?/) {
                   1300:            $rev2 = $1;
                   1301:            $sym2 = $3;
                   1302:        }
                   1303:        if ($r2 eq 'text') {
                   1304:            $rev2 = $tr2;
                   1305:            $sym2 = "";
                   1306:        }
                   1307:        # make sure the revisions a wellformed, for security
                   1308:        # reasons ..
                   1309:        if (!($rev1 =~ /^[\d\.]+$/) || !($rev2 =~ /^[\d\.]+$/)) {
                   1310:            &fatal("404 Not Found",
                   1311:                    "Malformed query \"$ENV{'QUERY_STRING'}\"");
                   1312:        }
                   1313: #
                   1314: # rev1 and rev2 are now both numeric revisions.
                   1315: # Thus we do a DWIM here and swap them if rev1 is after rev2.
                   1316: # XXX should we warn about the fact that we do this?
                   1317:        if (&revcmp($rev1,$rev2) > 0) {
                   1318:            my ($tmp1, $tmp2) = ($rev1, $sym1);
                   1319:            ($rev1, $sym1) = ($rev2, $sym2);
                   1320:            ($rev2, $sym2) = ($tmp1, $tmp2);
                   1321:        }
                   1322:        my $human_readable = 0;
                   1323:        if ($f eq 'c') {
1.3       maekawa  1324:            @difftype = qw{-c};
1.1       maekawa  1325:            $diffname = "Context diff";
                   1326:        }
                   1327:        elsif ($f eq 's') {
1.3       maekawa  1328:            @difftype = qw{--side-by-side --width=164};
1.1       maekawa  1329:            $diffname = "Side by Side";
                   1330:        }
                   1331:        elsif ($f eq 'H') {
                   1332:            $human_readable = 1;
1.3       maekawa  1333:            @difftype = qw{--unified=15};
1.1       maekawa  1334:            $diffname = "Long Human readable";
                   1335:        }
                   1336:        elsif ($f eq 'h') {
1.3       maekawa  1337:            @difftype =qw{-u};
1.1       maekawa  1338:            $human_readable = 1;
                   1339:            $diffname = "Human readable";
                   1340:        }
                   1341:        elsif ($f eq 'u') {
1.3       maekawa  1342:            @difftype = qw{-u};
1.1       maekawa  1343:            $diffname = "Unidiff";
                   1344:        }
                   1345:        else {
                   1346:            fatal ("400 Bad arguments", "Diff format $f not understood");
                   1347:        }
                   1348:
                   1349:        # apply special options
                   1350:        if ($human_readable) {
                   1351:            if ($hr_funout) {
1.3       maekawa  1352:                push @difftype, '-p';
1.1       maekawa  1353:            }
                   1354:            if ($hr_ignwhite) {
1.3       maekawa  1355:                push @difftype, '-w';
1.1       maekawa  1356:            }
                   1357:            if ($hr_ignkeysubst) {
1.3       maekawa  1358:                push @difftype, '-kk';
1.1       maekawa  1359:            }
                   1360:        }
1.3       maekawa  1361:        if (! open($fh, "-|")) { # child
                   1362:                open(STDERR, ">&STDOUT"); # Redirect stderr to stdout
                   1363:                exec("rcsdiff",@difftype,"-r$rev1","-r$rev2",$fullname);
                   1364:        }
1.1       maekawa  1365:        if ($human_readable) {
                   1366:            http_header();
                   1367:            &human_readable_diff($fh, $rev2);
                   1368:            exit;
                   1369:        }
                   1370:        else {
                   1371:            http_header("text/plain");
                   1372:        }
                   1373: #
                   1374: #===================================================================
                   1375: #RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
                   1376: #retrieving revision 1.16
                   1377: #retrieving revision 1.17
                   1378: #diff -c -r1.16 -r1.17
                   1379: #*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   1380: #--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
                   1381: #
                   1382: # Ideas:
                   1383: # - nuke the stderr output if it's what we expect it to be
                   1384: # - Add "no differences found" if the diff command supplied no output.
                   1385: #
                   1386: #*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   1387: #--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
                   1388: # (bogus example, but...)
                   1389: #
1.3       maekawa  1390:        if (grep { $_ eq '-u'} @difftype) {
1.1       maekawa  1391:            $f1 = '---';
                   1392:            $f2 = '\+\+\+';
                   1393:        }
                   1394:        else {
                   1395:            $f1 = '\*\*\*';
                   1396:            $f2 = '---';
                   1397:        }
                   1398:        while (<$fh>) {
                   1399:            if (m|^$f1 $cvsroot|o) {
                   1400:                s|$cvsroot/||o;
                   1401:                if ($sym1) {
                   1402:                    chop;
                   1403:                    $_ .= " " . $sym1 . "\n";
                   1404:                }
                   1405:            }
                   1406:            elsif (m|^$f2 $cvsroot|o) {
                   1407:                s|$cvsroot/||o;
                   1408:                if ($sym2) {
                   1409:                    chop;
                   1410:                    $_ .= " " . $sym2 . "\n";
                   1411:                }
                   1412:            }
                   1413:            print $_;
                   1414:        }
                   1415:        close($fh);
                   1416: }
                   1417:
                   1418: ###############################
                   1419: # Show Logs ..
                   1420: ###############################
                   1421: sub getDirLogs {
                   1422:     my ($cvsroot,$dirname,@otherFiles) = @_;
                   1423:     my ($state,$otherFiles,$tag, $file, $date, $branchpoint, $branch, $log);
                   1424:     my ($rev, $revision, $revwanted, $filename, $head, $author);
                   1425:
                   1426:     $tag = $input{only_with_tag};
                   1427:
                   1428:     my ($DirName) = "$cvsroot/$where";
                   1429:     my (@files, @filetags);
                   1430:     my $fh = do {local(*FH);};
                   1431:
                   1432:     push(@files, &safeglob("$DirName/*,v"));
                   1433:     push(@files, &safeglob("$DirName/Attic/*,v")) if (!$input{'hideattic'});
                   1434:     foreach $file (@otherFiles) {
                   1435:        push(@files, "$DirName/$file");
                   1436:     }
                   1437:
                   1438:     # just execute rlog if there are any files
                   1439:     if ($#files < 0) {
                   1440:        return;
                   1441:     }
                   1442:
                   1443:     if ($tag) {
                   1444:        #can't use -r<tag> as - is allowed in tagnames, but misinterpreated by rlog..
1.3       maekawa  1445:        if (! open($fh, "-|")) {
                   1446:                close(STDERR); # rlog may complain; ignore.
                   1447:                exec("rlog",@files);
                   1448:        }
1.1       maekawa  1449:     }
                   1450:     else {
1.3       maekawa  1451:        my $kidpid = open($fh, "-|");
                   1452:        if (! $kidpid) {
                   1453:                close(STDERR); # rlog may complain; ignore.
                   1454:                exec("rlog","-r",@files);
                   1455:        }
1.1       maekawa  1456:     }
                   1457:     $state = "start";
                   1458:     while (<$fh>) {
                   1459:        if ($state eq "start") {
                   1460:            #Next file. Initialize file variables
                   1461:            $rev = undef;
                   1462:            $revwanted = undef;
                   1463:            $branch = undef;
                   1464:            $branchpoint = undef;
                   1465:            $filename = undef;
                   1466:            $log = undef;
                   1467:            $revision = undef;
                   1468:            $branch = undef;
                   1469:            %symrev = ();
                   1470:            @filetags = ();
                   1471:            #jump to head state
                   1472:            $state = "head";
                   1473:        }
                   1474:        print "$state:$_" if ($verbose);
                   1475: again:
                   1476:        if ($state eq "head") {
                   1477:            #$rcsfile = $1 if (/^RCS file: (.+)$/); #not used (yet)
                   1478:            $filename = $1 if (/^Working file: (.+)$/);
                   1479:            $head = $1 if (/^head: (.+)$/);
                   1480:            $branch = $1 if (/^branch: (.+)$/);
                   1481:        }
                   1482:        if ($state eq "head" && /^symbolic names/) {
                   1483:            $state = "tags";
                   1484:            ($branch = $head) =~ s/\.\d+$// if (!defined($branch));
                   1485:            $branch =~ s/(\.?)(\d+)$/${1}0.$2/;
                   1486:            $symrev{MAIN} = $branch;
                   1487:            $symrev{HEAD} = $branch;
                   1488:            $alltags{MAIN} = 1;
                   1489:            $alltags{HEAD} = 1;
                   1490:            push (@filetags, "MAIN", "HEAD");
                   1491:            next;
                   1492:        }
                   1493:        if ($state eq "tags" &&
                   1494:                            /^\s+(.+):\s+([\d\.]+)\s+$/) {
                   1495:            push (@filetags, $1);
                   1496:            $symrev{$1} = $2;
                   1497:            $alltags{$1} = 1;
                   1498:            next;
                   1499:        }
                   1500:        if ($state eq "tags" && /^\S/) {
                   1501:            if (defined($tag) && (defined($symrev{$tag}) || $tag eq "HEAD")) {
                   1502:                $revwanted = $tag eq "HEAD" ? $symrev{"MAIN"} : $symrev{$tag};
                   1503:                ($branch = $revwanted) =~ s/\b0\.//;
                   1504:                ($branchpoint = $branch) =~ s/\.?\d+$//;
                   1505:                $revwanted = undef if ($revwanted ne $branch);
                   1506:            }
                   1507:            elsif (defined($tag) && $tag ne "HEAD") {
                   1508:                print "Tag not found, skip this file" if ($verbose);
                   1509:                $state = "skip";
                   1510:                next;
                   1511:            }
                   1512:            foreach my $tagfound (@filetags) {
                   1513:                $tags{$tagfound} = 1;
                   1514:            }
                   1515:            $state = "head";
                   1516:            goto again;
                   1517:        }
                   1518:        if ($state eq "head" && /^----------------------------$/) {
                   1519:            $state = "log";
                   1520:            $rev = undef;
                   1521:            $date = undef;
                   1522:            $log = "";
                   1523:            # Try to reconstruct the relative filename if RCS spits out a full path
1.3       maekawa  1524:            $filename =~ s%^\Q$DirName\E/%%;
1.1       maekawa  1525:            next;
                   1526:        }
                   1527:        if ($state eq "log") {
                   1528:            if (/^----------------------------$/
                   1529:                || /^=============================/) {
                   1530:                # End of a log entry.
                   1531:                my $revbranch;
                   1532:                ($revbranch = $rev) =~ s/\.\d+$//;
                   1533:                print "$filename $rev Wanted: $revwanted "
                   1534:                    . "Revbranch: $revbranch Branch: $branch "
                   1535:                    . "Branchpoint: $branchpoint\n" if ($verbose);
                   1536:                if (!defined($revwanted) && defined($branch)
                   1537:                    && $branch eq $revbranch || !defined($tag)) {
                   1538:                    print "File revision $rev found for branch $branch\n"
                   1539:                        if ($verbose);
                   1540:                    $revwanted = $rev;
                   1541:                }
                   1542:                if (defined($revwanted) ? $rev eq $revwanted :
                   1543:                    defined($branchpoint) ? $rev eq $branchpoint :
                   1544:                    0 && ($rev eq $head)) { # Don't think head is needed here..
                   1545:                    print "File info $rev found for $filename\n" if ($verbose);
                   1546:                    my @finfo = ($rev,$date,$log,$author,$filename);
                   1547:                    my ($name);
                   1548:                    ($name = $filename) =~ s%/.*%%;
                   1549:                    $fileinfo{$name} = [ @finfo ];
                   1550:                    $state = "done" if ($rev eq $revwanted);
                   1551:                }
                   1552:                $rev = undef;
                   1553:                $date = undef;
                   1554:                $log = "";
                   1555:            }
                   1556:            elsif (!defined($date) && m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);|) {
                   1557:                my $yr = $1;
                   1558:                # damn 2-digit year routines :-)
                   1559:                if ($yr > 100) {
                   1560:                    $yr -= 1900;
                   1561:                }
                   1562:                $date = &Time::Local::timegm($6,$5,$4,$3,$2 - 1,$yr);
                   1563:                ($author) = /author: ([^;]+)/;
                   1564:                $state = "log";
                   1565:                $log = '';
                   1566:                next;
                   1567:            }
                   1568:            elsif (!defined($rev) && m/^revision (.*)$/) {
                   1569:                $rev = $1;
                   1570:                next;
                   1571:            }
                   1572:            else {
                   1573:                $log = $log . $_;
                   1574:            }
                   1575:        }
                   1576:        if (/^===============/) {
                   1577:            $state = "start";
                   1578:            next;
                   1579:        }
                   1580:     }
                   1581:     if ($. == 0) {
                   1582:        fatal("500 Internal Error",
1.3       maekawa  1583:              "Failed to spawn GNU rlog on <em>'".join(", ", @files)."'</em><p>did you set the <b>\$ENV{PATH}</b> in your configuration file correctly ?");
1.1       maekawa  1584:     }
                   1585:     close($fh);
                   1586: }
                   1587:
                   1588: sub readLog {
                   1589:        my($fullname,$revision) = @_;
                   1590:        my ($symnames, $head, $rev, $br, $brp, $branch, $branchrev);
                   1591:        my $fh = do {local(*FH);};
                   1592:
                   1593:        if (defined($revision)) {
                   1594:            $revision = "-r$revision";
                   1595:        }
                   1596:        else {
                   1597:            $revision = "";
                   1598:        }
                   1599:
                   1600:        undef %symrev;
                   1601:        undef %revsym;
                   1602:        undef @allrevisions;
                   1603:        undef %date;
                   1604:        undef %author;
                   1605:        undef %state;
                   1606:        undef %difflines;
                   1607:        undef %log;
                   1608:
                   1609:        print("Going to rlog '$fullname'\n") if ($verbose);
1.3       maekawa  1610:        if (! open($fh, "-|")) { # child
                   1611:                if ($revision ne '') {
                   1612:                        exec("rlog",$revision,$fullname);
                   1613:                }
                   1614:                else {
                   1615:                        exec("rlog",$fullname);
                   1616:                }
                   1617:        }
1.1       maekawa  1618:        while (<$fh>) {
                   1619:            print if ($verbose);
                   1620:            if ($symnames) {
                   1621:                if (/^\s+([^:]+):\s+([\d\.]+)/) {
                   1622:                    $symrev{$1} = $2;
                   1623:                }
                   1624:                else {
                   1625:                    $symnames = 0;
                   1626:                }
                   1627:            }
                   1628:            elsif (/^head:\s+([\d\.]+)/) {
                   1629:                $head = $1;
                   1630:            }
                   1631:            elsif (/^branch:\s+([\d\.]+)/) {
                   1632:                $curbranch = $1;
                   1633:            }
                   1634:            elsif (/^symbolic names/) {
                   1635:                $symnames = 1;
                   1636:            }
                   1637:            elsif (/^-----/) {
                   1638:                last;
                   1639:            }
                   1640:        }
                   1641:        ($curbranch = $head) =~ s/\.\d+$// if (!defined($curbranch));
                   1642:
                   1643: # each log entry is of the form:
                   1644: # ----------------------------
                   1645: # revision 3.7.1.1
                   1646: # date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
                   1647: # log info
                   1648: # ----------------------------
                   1649:        logentry:
                   1650:        while (!/^=========/) {
                   1651:            $_ = <$fh>;
                   1652:            last logentry if (!defined($_));    # EOF
                   1653:            print "R:", $_ if ($verbose);
                   1654:            if (/^revision ([\d\.]+)/) {
                   1655:                $rev = $1;
                   1656:                unshift(@allrevisions,$rev);
                   1657:            }
                   1658:            elsif (/^========/ || /^----------------------------$/) {
                   1659:                next logentry;
                   1660:            }
                   1661:            else {
                   1662:                # The rlog output is syntactically ambiguous.  We must
                   1663:                # have guessed wrong about where the end of the last log
                   1664:                # message was.
                   1665:                # Since this is likely to happen when people put rlog output
                   1666:                # in their commit messages, don't even bother keeping
                   1667:                # these lines since we don't know what revision they go with
                   1668:                # any more.
                   1669:                next logentry;
                   1670: #              &fatal("500 Internal Error","Error parsing RCS output: $_");
                   1671:            }
                   1672:            $_ = <$fh>;
                   1673:            print "D:", $_ if ($verbose);
                   1674:            if (m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);\s+state:\s+(\S+);\s+(lines:\s+([0-9\s+-]+))?|) {
                   1675:                my $yr = $1;
                   1676:                 # damn 2-digit year routines :-)
                   1677:                 if ($yr > 100) {
                   1678:                     $yr -= 1900;
                   1679:                 }
                   1680:                $date{$rev} = &Time::Local::timegm($6,$5,$4,$3,$2 - 1,$yr);
                   1681:                $author{$rev} = $7;
                   1682:                $state{$rev} = $8;
                   1683:                $difflines{$rev} = $10;
                   1684:            }
                   1685:            else {
                   1686:                &fatal("500 Internal Error", "Error parsing RCS output: $_");
                   1687:            }
                   1688:            line:
                   1689:            while (<$fh>) {
                   1690:                print "L:", $_ if ($verbose);
                   1691:                next line if (/^branches:\s/);
                   1692:                last line if (/^----------------------------$/ || /^=========/);
                   1693:                $log{$rev} .= $_;
                   1694:            }
                   1695:            print "E:", $_ if ($verbose);
                   1696:        }
                   1697:        close($fh);
                   1698:        print "Done reading RCS file\n" if ($verbose);
                   1699:
                   1700:        @revorder = reverse sort {revcmp($a,$b)} @allrevisions;
                   1701:        print "Done sorting revisions",join(" ",@revorder),"\n" if ($verbose);
                   1702:
                   1703: #
                   1704: # HEAD is an artificial tag which is simply the highest tag number on the main
                   1705: # branch, unless there is a branch tag in the RCS file in which case it's the
                   1706: # highest revision on that branch.  Find it by looking through @revorder; it
                   1707: # is the first commit listed on the appropriate branch.
                   1708: # This is not neccesary the same revision as marked as head in the RCS file.
                   1709:        my $headrev = $curbranch || "1";
                   1710:        ($symrev{"MAIN"} = $headrev) =~ s/(\.?)(\d+)$/${1}0.$2/;
                   1711:        revision:
                   1712:        foreach $rev (@revorder) {
                   1713:            if ($rev =~ /^(\S*)\.\d+$/ && $headrev eq $1) {
                   1714:                $symrev{"HEAD"} = $rev;
                   1715:                last revision;
                   1716:            }
                   1717:        }
                   1718:        ($symrev{"HEAD"} = $headrev) =~ s/\.\d+$//
                   1719:             if (!defined($symrev{"HEAD"}));
                   1720:        print "Done finding HEAD\n" if ($verbose);
                   1721: #
                   1722: # Now that we know all of the revision numbers, we can associate
                   1723: # absolute revision numbers with all of the symbolic names, and
                   1724: # pass them to the form so that the same association doesn't have
                   1725: # to be built then.
                   1726: #
                   1727:        undef @branchnames;
                   1728:        undef %branchpoint;
                   1729:        undef $sel;
                   1730:
                   1731:        foreach (reverse sort keys %symrev) {
                   1732:            $rev = $symrev{$_};
                   1733:            if ($rev =~ /^((.*)\.)?\b0\.(\d+)$/) {
                   1734:                push(@branchnames, $_);
                   1735:                #
                   1736:                # A revision number of A.B.0.D really translates into
                   1737:                # "the highest current revision on branch A.B.D".
                   1738:                #
                   1739:                # If there is no branch A.B.D, then it translates into
                   1740:                # the head A.B .
                   1741:                #
                   1742:                # This reasoning also applies to the main branch A.B,
                   1743:                # with the branch number 0.A, with the exception that
                   1744:                # it has no head to translate to if there is nothing on
                   1745:                # the branch, but I guess this can never happen?
                   1746:                # (the code below gracefully forgets about the branch
                   1747:                # if it should happen)
                   1748:                #
                   1749:                $head = defined($2) ? $2 : "";
                   1750:                $branch = $3;
                   1751:                $branchrev = $head . ($head ne "" ? "." : "") . $branch;
                   1752:                my $regex;
                   1753:                ($regex = $branchrev) =~ s/\./\\./g;
                   1754:                $rev = $head;
                   1755:
                   1756:                revision:
                   1757:                foreach my $r (@revorder) {
                   1758:                    if ($r =~ /^${regex}\b/) {
                   1759:                        $rev = $branchrev;
                   1760:                        last revision;
                   1761:                    }
                   1762:                }
                   1763:                next if ($rev eq "");
                   1764:                if ($rev ne $head && $head ne "") {
                   1765:                    $branchpoint{$head} .= ", " if ($branchpoint{$head});
                   1766:                    $branchpoint{$head} .= $_;
                   1767:                }
                   1768:            }
                   1769:            $revsym{$rev} .= ", " if ($revsym{$rev});
                   1770:            $revsym{$rev} .= $_;
                   1771:            $sel .= "<OPTION VALUE=\"${rev}:${_}\">$_\n";
                   1772:        }
                   1773:        print "Done associating revisions with branches\n" if ($verbose);
                   1774:
                   1775:        my ($onlyonbranch, $onlybranchpoint);
                   1776:        if ($onlyonbranch = $input{'only_with_tag'}) {
                   1777:            $onlyonbranch = $symrev{$onlyonbranch};
                   1778:            if ($onlyonbranch =~ s/\b0\.//) {
                   1779:                ($onlybranchpoint = $onlyonbranch) =~ s/\.\d+$//;
                   1780:            }
                   1781:             else {
                   1782:                $onlybranchpoint = $onlyonbranch;
                   1783:            }
                   1784:            if (!defined($onlyonbranch) || $onlybranchpoint eq "") {
                   1785:                fatal("404 Tag not found","Tag $input{'only_with_tag'} not defined");
                   1786:            }
                   1787:        }
                   1788:
                   1789:        undef @revisions;
                   1790:
                   1791:        foreach (@allrevisions) {
                   1792:            ($br = $_) =~ s/\.\d+$//;
                   1793:            ($brp = $br) =~ s/\.\d+$//;
                   1794:            next if ($onlyonbranch && $br ne $onlyonbranch &&
                   1795:                                        $_ ne $onlybranchpoint);
                   1796:            unshift(@revisions,$_);
                   1797:        }
                   1798:
                   1799:        if ($logsort eq "date") {
                   1800:            # Sort the revisions in commit order an secondary sort on revision
                   1801:            # (secondary sort needed for imported sources, or the first main
                   1802:            # revision gets before the same revision on the 1.1.1 branch)
                   1803:            @revdisplayorder = sort {$date{$b} <=> $date{$a} || -revcmp($a, $b)} @revisions;
                   1804:        }
                   1805:         elsif ($logsort eq "rev") {
                   1806:            # Sort the revisions in revision order, highest first
                   1807:            @revdisplayorder = reverse sort {revcmp($a,$b)} @revisions;
                   1808:        }
                   1809:         else {
                   1810:            # No sorting. Present in the same order as rlog / cvs log
                   1811:            @revdisplayorder = @revisions;
                   1812:        }
                   1813:
                   1814: }
                   1815:
                   1816: sub printLog($;$) {
                   1817:        my ($link, $br, $brp);
                   1818:        ($_,$link) = @_;
                   1819:        ($br = $_) =~ s/\.\d+$//;
                   1820:        ($brp = $br) =~ s/\.?\d+$//;
                   1821:        my ($isDead, $prev);
                   1822:
                   1823:        $link = 1 if (!defined($link));
                   1824:        $isDead = ($state{$_} eq "dead");
                   1825:
                   1826:        if ($link && !$isDead) {
                   1827:            my ($filename);
                   1828:            ($filename = $where) =~ s/^.*\///;
                   1829:            my ($fileurl) = urlencode($filename);
                   1830:            print "<a NAME=\"rev$_\"></a>";
                   1831:            if (defined($revsym{$_})) {
                   1832:                foreach my $sym (split(", ", $revsym{$_})) {
                   1833:                    print "<a NAME=\"$sym\"></a>";
                   1834:                }
                   1835:            }
                   1836:            if (defined($revsym{$br}) && $revsym{$br} && !defined($nameprinted{$br})) {
                   1837:                foreach my $sym (split(", ", $revsym{$br})) {
                   1838:                    print "<a NAME=\"$sym\"></a>";
                   1839:                }
                   1840:                $nameprinted{$br} = 1;
                   1841:            }
                   1842:            print "\n Revision ";
                   1843:            &download_link($fileurl, $_, $_,
                   1844:                $defaultViewable ? "text/x-cvsweb-markup" : undef);
                   1845:            if ($defaultViewable) {
                   1846:                print " / ";
                   1847:                &download_link($fileurl, $_, "(download)", $mimetype);
                   1848:            }
                   1849:            if (not $defaultTextPlain) {
                   1850:                print " / ";
                   1851:                &download_link($fileurl, $_, "(as text)",
                   1852:                           "text/plain");
                   1853:            }
                   1854:            if (!$defaultViewable) {
                   1855:                print " / ";
                   1856:                &download_link($fileurl, $_, "(view)", "text/x-cvsweb-markup");
                   1857:            }
                   1858:            if ($allow_annotate) {
                   1859:                print " - <a href=\"" . $scriptname . "/" . urlencode($where) . "?annotate=$_$barequery\">";
                   1860:                print "annotate</a>";
                   1861:            }
                   1862:            # Plus a select link if enabled, and this version isn't selected
                   1863:            if ($allow_version_select) {
                   1864:                if ((!defined($input{"r1"}) || $input{"r1"} ne $_)) {
                   1865:                    print " - <A HREF=\"${scriptwhere}?r1=$_$barequery" .
                   1866:                        "\">[select for diffs]</A>\n";
                   1867:                }
                   1868:                else {
                   1869:                    print " - <b>[selected]</b>";
                   1870:                }
                   1871:            }
                   1872:        }
                   1873:        else {
                   1874:            print "Revision <B>$_</B>";
                   1875:        }
                   1876:        if (/^1\.1\.1\.\d+$/) {
                   1877:            print " <i>(vendor branch)</i>";
                   1878:        }
1.3       maekawa  1879:        if (defined @mytz) {
                   1880:            my ($est) = $mytz[(localtime($date{$_}))[8]];
                   1881:            print ", <i>" . scalar localtime($date{$_}) . " $est</i> (";
                   1882:        } else {
                   1883:            print ", <i>" . scalar gmtime($date{$_}) . " UTC</i> (";
                   1884:        }
1.1       maekawa  1885:        print readableTime(time() - $date{$_},1) . " ago)";
                   1886:        print " by ";
                   1887:        print "<i>" . $author{$_} . "</i>\n";
                   1888:        print "<BR>Branch: <b>",$link?link_tags($revsym{$br}):$revsym{$br},"</b>\n"
                   1889:            if ($revsym{$br});
                   1890:        print "<BR>CVS Tags: <b>",$link?link_tags($revsym{$_}):$revsym{$_},"</b>"
                   1891:            if ($revsym{$_});
                   1892:        print "<BR>Branch point for: <b>",$link?link_tags($branchpoint{$_}):$branchpoint{$_},"</b>\n"
                   1893:            if ($branchpoint{$_});
                   1894:        # Find the previous revision
                   1895:        my @prevrev = split(/\./, $_);
                   1896:        do {
                   1897:            if (--$prevrev[$#prevrev] <= 0) {
                   1898:                # If it was X.Y.Z.1, just make it X.Y
                   1899:                pop(@prevrev);
                   1900:                pop(@prevrev);
                   1901:            }
                   1902:            $prev = join(".", @prevrev);
                   1903:        } until (defined($date{$prev}) || $prev eq "");
                   1904:        if ($prev ne "") {
                   1905:            if ($difflines{$_}) {
                   1906:                print "<BR>Changes since <b>$prev: $difflines{$_} lines</b>";
                   1907:            }
                   1908:        }
                   1909:        if ($isDead) {
                   1910:            print "<BR><B><I>FILE REMOVED</I></B>\n";
                   1911:        }
                   1912:        elsif ($link) {
                   1913:            my %diffrev = ();
                   1914:            $diffrev{$_} = 1;
                   1915:            $diffrev{""} = 1;
                   1916:            print "<BR>Diff";
                   1917:            #
                   1918:            # Offer diff to previous revision
                   1919:            if ($prev) {
                   1920:                $diffrev{$prev} = 1;
                   1921:                print " to previous <A HREF=\"${scriptwhere}.diff?r1=$prev";
1.3       maekawa  1922:                print "&amp;r2=$_" . $barequery . "\">$prev</A>\n";
1.1       maekawa  1923:                if (!$hr_default) { # offer a human readable version if not default
                   1924:                    print "(<A HREF=\"${scriptwhere}.diff?r1=$prev";
1.3       maekawa  1925:                    print "&amp;r2=$_" . $barequery . "&amp;f=h\">colored</A>)\n";
1.1       maekawa  1926:                }
                   1927:            }
                   1928:            #
                   1929:            # Plus, if it's on a branch, and it's not a vendor branch,
                   1930:            # offer a diff with the branch point.
                   1931:            if ($revsym{$brp} && !/^1\.1\.1\.\d+$/ && !defined($diffrev{$brp})) {
                   1932:                print " to branchpoint <A HREF=\"${scriptwhere}.diff?r1=$brp";
1.3       maekawa  1933:                print "&amp;r2=$_" . $barequery . "\">$brp</A>\n";
1.1       maekawa  1934:                if (!$hr_default) { # offer a human readable version if not default
                   1935:                print "(<A HREF=\"${scriptwhere}.diff?r1=$brp";
1.3       maekawa  1936:                print "&amp;r2=$_" . $barequery . "&amp;f=h\">colored</A>)\n";
1.1       maekawa  1937:                }
                   1938:            }
                   1939:            #
                   1940:            # Plus, if it's on a branch, and it's not a vendor branch,
                   1941:            # offer to diff with the next revision of the higher branch.
                   1942:            # (e.g. change gets committed and then brought
                   1943:            # over to -stable)
                   1944:            if (/^\d+\.\d+\.\d+/ && !/^1\.1\.1\.\d+$/) {
                   1945:                my ($i,$nextmain);
                   1946:                for ($i = 0; $i < $#revorder && $revorder[$i] ne $_; $i++){}
                   1947:                my (@tmp2) = split(/\./, $_);
                   1948:                for ($nextmain = ""; $i > 0; $i--) {
                   1949:                    my ($next) = $revorder[$i-1];
                   1950:                    my (@tmp1) = split(/\./, $next);
                   1951:                    if ($#tmp1 < $#tmp2) {
                   1952:                        $nextmain = $next;
                   1953:                        last;
                   1954:                    }
                   1955:                    # Only the highest version on a branch should have
                   1956:                    # a diff for the "next main".
                   1957:                    last if (join(".",@tmp1[0..$#tmp1-1])
                   1958:                             eq join(".",@tmp2[0..$#tmp1-1]));
                   1959:                }
                   1960:                if (!defined($diffrev{$nextmain})) {
                   1961:                    $diffrev{$nextmain} = 1;
                   1962:                    print " next main <A HREF=\"${scriptwhere}.diff?r1=$nextmain";
1.3       maekawa  1963:                    print "&amp;r2=$_" . $barequery .
1.1       maekawa  1964:                        "\">$nextmain</A>\n";
                   1965:                    if (!$hr_default) { # offer a human readable version if not default
                   1966:                        print "(<A HREF=\"${scriptwhere}.diff?r1=$nextmain";
1.3       maekawa  1967:                        print "&amp;r2=$_" . $barequery .
                   1968:                            "&amp;f=h\">colored</A>)\n";
1.1       maekawa  1969:                    }
                   1970:                }
                   1971:            }
                   1972:            # Plus if user has selected only r1, then present a link
                   1973:            # to make a diff to that revision
                   1974:            if (defined($input{"r1"}) && !defined($diffrev{$input{"r1"}})) {
                   1975:                $diffrev{$input{"r1"}} = 1;
                   1976:                print " to selected <A HREF=\"${scriptwhere}.diff?"
1.3       maekawa  1977:                        . "r1=$input{'r1'}&amp;r2=$_" . $barequery
1.1       maekawa  1978:                        . "\">$input{'r1'}</A>\n";
                   1979:                if (!$hr_default) { # offer a human readable version if not default
                   1980:                    print "(<A HREF=\"${scriptwhere}.diff?r1=$input{'r1'}";
1.3       maekawa  1981:                    print "&amp;r2=$_" . $barequery .
                   1982:                        "&amp;f=h\">colored</A>)\n";
1.1       maekawa  1983:
                   1984:                }
                   1985:            }
                   1986:        }
                   1987:        print "<PRE>\n";
                   1988:        print &htmlify($log{$_}, 1);
                   1989:        print "</PRE>\n";
                   1990: }
                   1991:
                   1992: sub doLog {
                   1993:        my($fullname) = @_;
                   1994:        my ($diffrev, $upwhere, $filename, $backurl);
                   1995:
                   1996:        readLog($fullname);
                   1997:
                   1998:         html_header("CVS log for $where");
                   1999:        ($upwhere = $where) =~ s|(Attic/)?[^/]+$||;
                   2000:         ($filename = $where) =~ s|^.*/||;
                   2001:         $backurl = $scriptname . "/" . urlencode($upwhere) . $query;
                   2002:        print &link($backicon, "$backurl#$filename"),
                   2003:               " <b>Up to ", &clickablePath($upwhere, 1), "</b><p>\n";
                   2004:        print "<A HREF=\"#diff\">Request diff between arbitrary revisions</A>\n";
                   2005:        print "<HR NOSHADE>\n";
                   2006:        if ($curbranch) {
                   2007:            print "Default branch: ";
                   2008:            print ($revsym{$curbranch} || $curbranch);
                   2009:        }
                   2010:        else {
                   2011:            print "No default branch";
                   2012:        }
                   2013:        print "<BR>\n";
                   2014:        if ($input{only_with_tag}) {
                   2015:            print "Current tag: $input{only_with_tag}<BR>\n";
                   2016:        }
                   2017:
                   2018:        undef %nameprinted;
                   2019:
                   2020:        for (my $i = 0; $i <= $#revdisplayorder; $i++) {
                   2021:            print "<HR size=1 NOSHADE>";
                   2022:            printLog($revdisplayorder[$i]);
                   2023:        }
                   2024:
1.3       maekawa  2025:         print "<HR NOSHADE>";
1.1       maekawa  2026:        print "<A NAME=diff>\n";
                   2027:        print "This form allows you to request diff's between any two\n";
                   2028:        print "revisions of a file.  You may select a symbolic revision\n";
                   2029:        print "name using the selection box or you may type in a numeric\n";
                   2030:        print "name using the type-in text box.\n";
                   2031:        print "</A><P>\n";
                   2032:        print "<FORM METHOD=\"GET\" ACTION=\"${scriptwhere}.diff\" NAME=\"diff_select\">\n";
                   2033:         foreach (@stickyvars) {
                   2034:            print "<INPUT TYPE=HIDDEN NAME=\"$_\" VALUE=\"$input{$_}\">\n"
                   2035:                if (defined($input{$_})
                   2036:                    && ($input{$_} ne $DEFAULTVALUE{$_} && $input{$_} ne ""));
                   2037:        }
                   2038:        print "Diffs between \n";
                   2039:        print "<SELECT NAME=\"r1\">\n";
                   2040:        print "<OPTION VALUE=\"text\" SELECTED>Use Text Field\n";
                   2041:        print $sel;
                   2042:        print "</SELECT>\n";
                   2043:        $diffrev = $revdisplayorder[$#revdisplayorder];
                   2044:        $diffrev = $input{"r1"} if (defined($input{"r1"}));
                   2045:        print "<INPUT TYPE=\"TEXT\" SIZE=\"$inputTextSize\" NAME=\"tr1\" VALUE=\"$diffrev\" onChange='document.diff_select.r1.selectedIndex=0'>\n";
                   2046:        print " and \n";
                   2047:        print "<SELECT NAME=\"r2\">\n";
                   2048:        print "<OPTION VALUE=\"text\" SELECTED>Use Text Field\n";
                   2049:        print $sel;
                   2050:        print "</SELECT>\n";
                   2051:        $diffrev = $revdisplayorder[0];
                   2052:        $diffrev = $input{"r2"} if (defined($input{"r2"}));
                   2053:        print "<INPUT TYPE=\"TEXT\" SIZE=\"$inputTextSize\" NAME=\"tr2\" VALUE=\"$diffrev\" onChange='docuement.diff_select.r2.selectedIndex=0'>\n";
                   2054:         print "<BR>Type of Diff should be a&nbsp;";
                   2055:        printDiffSelect();
                   2056:        print "<INPUT TYPE=SUBMIT VALUE=\"  Get Diffs  \">\n";
                   2057:        print "</FORM>\n";
                   2058:        print "<HR noshade>\n";
                   2059:         if (@branchnames) {
1.3       maekawa  2060:            print "<A name=branch></A>\n";
1.1       maekawa  2061:            print "<FORM METHOD=\"GET\" ACTION=\"$scriptwhere\">\n";
                   2062:            foreach (@stickyvars) {
                   2063:                next if ($_ eq "only_with_tag");
                   2064:                next if ($_ eq "logsort");
                   2065:                print "<INPUT TYPE=HIDDEN NAME=\"$_\" VALUE=\"$input{$_}\">\n"
                   2066:                    if (defined($input{$_}) && $input{$_} ne $DEFAULTVALUE{$_}
                   2067:                        && $input{$_} ne "");
                   2068:            }
                   2069:            print "View only Branch: \n";
                   2070:            print "<SELECT NAME=\"only_with_tag\"";
                   2071:            print " onchange=\"submit()\"" if ($use_java_script);
                   2072:            print ">\n";
                   2073:            print "<OPTION VALUE=\"\"";
                   2074:            print " SELECTED" if (defined($input{"only_with_tag"}) &&
                   2075:                $input{"only_with_tag"} eq "");
                   2076:            print ">Show all branches\n";
                   2077:            foreach (reverse sort @branchnames) {
                   2078:                print "<OPTION";
                   2079:                print " SELECTED" if (defined($input{"only_with_tag"})
                   2080:                        && $input{"only_with_tag"} eq $_);
                   2081:                print ">${_}\n";
                   2082:            }
                   2083:            print "</SELECT>\n";
                   2084:            print "<INPUT TYPE=SUBMIT VALUE=\"  View Branch  \">\n";
                   2085:            print "</FORM>\n";
                   2086:        }
1.3       maekawa  2087:        print "<A name=logsort></A>\n";
1.1       maekawa  2088:        print "<FORM METHOD=\"GET\" ACTION=\"$scriptwhere\">\n";
                   2089:        foreach (@stickyvars) {
                   2090:            next if ($_ eq "only_with_tag");
                   2091:            next if ($_ eq "logsort");
                   2092:            print "<INPUT TYPE=HIDDEN NAME=\"$_\" VALUE=\"$input{$_}\">\n"
                   2093:                if (defined($input{$_}) && $input{$_} ne $DEFAULTVALUE{$_}
                   2094:                    && $input{$_} ne "");
                   2095:        }
                   2096:        print "Sort log by: \n";
                   2097:        print "<SELECT NAME=\"logsort\"";
                   2098:        print " onchange=\"submit()\"" if ($use_java_script);
                   2099:        print ">\n";
                   2100:        print "<OPTION VALUE=cvs",$logsort eq "cvs" ? " SELECTED" : "", ">Not sorted";
                   2101:        print "<OPTION VALUE=date",$logsort eq "date" ? " SELECTED" : "", ">Commit date";
                   2102:        print "<OPTION VALUE=rev",$logsort eq "rev" ? " SELECTED" : "", ">Revision";
                   2103:        print "</SELECT>\n";
                   2104:        print "<INPUT TYPE=SUBMIT VALUE=\"  Sort  \">\n";
                   2105:        print "</FORM>\n";
                   2106:         print &html_footer;
                   2107:        print "</BODY></HTML>\n";
                   2108: }
                   2109:
                   2110: sub flush_diff_rows ($$$$)
                   2111: {
                   2112:     my $j;
                   2113:     my ($leftColRef,$rightColRef,$leftRow,$rightRow) = @_;
                   2114:     if ($state eq "PreChangeRemove") {          # we just got remove-lines before
                   2115:       for ($j = 0 ; $j < $leftRow; $j++) {
                   2116:           print  "<tr><td bgcolor=\"$diffcolorRemove\">@$leftColRef[$j]</td>";
                   2117:           print  "<td bgcolor=\"$diffcolorEmpty\">&nbsp;</td></tr>\n";
                   2118:       }
                   2119:     }
                   2120:     elsif ($state eq "PreChange") {             # state eq "PreChange"
                   2121:       # we got removes with subsequent adds
                   2122:       for ($j = 0; $j < $leftRow || $j < $rightRow ; $j++) {  # dump out both cols
                   2123:           print  "<tr>";
                   2124:           if ($j < $leftRow) {
                   2125:              print  "<td bgcolor=\"$diffcolorChange\">@$leftColRef[$j]</td>";
                   2126:          }
                   2127:           else {
                   2128:              print  "<td bgcolor=\"$diffcolorDarkChange\">&nbsp;</td>";
                   2129:          }
                   2130:           if ($j < $rightRow) {
                   2131:              print  "<td bgcolor=\"$diffcolorChange\">@$rightColRef[$j]</td>";
                   2132:          }
                   2133:           else {
                   2134:              print  "<td bgcolor=\"$diffcolorDarkChange\">&nbsp;</td>";
                   2135:          }
                   2136:           print  "</tr>\n";
                   2137:       }
                   2138:     }
                   2139: }
                   2140:
                   2141: ##
                   2142: # Function to generate Human readable diff-files
                   2143: # human_readable_diff(String revision_to_return_to);
                   2144: ##
                   2145: sub human_readable_diff($){
                   2146:   my ($i,$difftxt, $where_nd, $filename, $pathname, $scriptwhere_nd);
                   2147:   my ($fh, $rev) = @_;
                   2148:   my ($date1, $date2, $r1d, $r2d, $r1r, $r2r, $rev1, $rev2, $sym1, $sym2);
                   2149:   my (@rightCol, @leftCol);
                   2150:
                   2151:   ($where_nd = $where) =~ s/.diff$//;
                   2152:   ($filename = $where_nd) =~ s/^.*\///;
                   2153:   ($pathname = $where_nd) =~ s/(Attic\/)?[^\/]*$//;
                   2154:   ($scriptwhere_nd = $scriptwhere) =~ s/.diff$//;
                   2155:
                   2156:   navigateHeader ($scriptwhere_nd, $pathname, $filename, $rev, "diff");
                   2157:
                   2158:   # Read header to pick up read revision and date, if possible
                   2159:   while (<$fh>) {
                   2160:       ($r1d,$r1r) = /\t(.*)\t(.*)$/ if (/^--- /);
                   2161:       ($r2d,$r2r) = /\t(.*)\t(.*)$/ if (/^\+\+\+ /);
                   2162:       last if (/^\+\+\+ /);
                   2163:   }
                   2164:   if (defined($r1r) && $r1r =~ /^(\d+\.)+\d+$/) {
                   2165:     $rev1 = $r1r;
                   2166:     $date1 = $r1d;
                   2167:   }
                   2168:   if (defined($r2r) && $r2r =~ /^(\d+\.)+\d+$/) {
                   2169:     $rev2 = $r2r;
                   2170:     $date2 = $r2d;
                   2171:   }
                   2172:
                   2173:   print "<h3 align=center>Diff for /$where_nd between version $rev1 and $rev2</h3>\n";
                   2174:
1.3       maekawa  2175:   print "<table border=0 cellspacing=0 cellpadding=0 width=\"100%\">\n";
                   2176:   print "<tr bgcolor=\"#ffffff\">\n";
1.1       maekawa  2177:   print "<th width=\"50%\" valign=TOP>";
                   2178:   print "version $rev1";
                   2179:   print ", $date1" if (defined($date1));
                   2180:   print "<br>Tag: $sym1\n" if ($sym1);
                   2181:   print "</th>\n";
                   2182:   print "<th width=\"50%\" valign=TOP>";
                   2183:   print "version $rev2";
                   2184:   print ", $date2" if (defined($date2));
                   2185:   print "<br>Tag: $sym2\n" if ($sym1);
                   2186:   print "</th>\n";
                   2187:
                   2188:   my $fs = "<font face=\"$difffontface\" size=\"$difffontsize\">";
                   2189:   my $fe = "</font>";
                   2190:
                   2191:   my $leftRow = 0;
                   2192:   my $rightRow = 0;
                   2193:   my ($oldline, $newline, $funname, $diffcode, $rest);
                   2194:
                   2195:   # Process diff text
                   2196:   # The diffrows are could make excellent use of
                   2197:   # cascading style sheets because we've to set the
                   2198:   # font and color for each row. anyone ...?
                   2199:   ####
                   2200:   while (<$fh>) {
                   2201:       $difftxt = $_;
                   2202:
                   2203:       if ($difftxt =~ /^@@/) {
                   2204:          ($oldline,$newline,$funname) = $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/;
                   2205:           print  "<tr bgcolor=\"$diffcolorHeading\"><td width=\"50%\">";
1.3       maekawa  2206:          print  "<table width=\"100%\" border=1 cellpadding=5><tr><td><b>Line $oldline</b>";
1.1       maekawa  2207:          print  "&nbsp;<font size=-1>$funname</font></td></tr></table>";
                   2208:           print  "</td><td width=\"50%\">";
1.3       maekawa  2209:          print  "<table width=\"100%\" border=1 cellpadding=5><tr><td><b>Line $newline</b>";
1.1       maekawa  2210:          print  "&nbsp;<font size=-1>$funname</font></td></tr></table>";
1.3       maekawa  2211:          print  "</td>\n";
1.1       maekawa  2212:          $state = "dump";
                   2213:          $leftRow = 0;
                   2214:          $rightRow = 0;
                   2215:       }
                   2216:       else {
                   2217:          ($diffcode,$rest) = $difftxt =~ /^([-+ ])(.*)/;
                   2218:          $_ = spacedHtmlText ($rest);
                   2219:
                   2220:          # Add fontface, size
                   2221:          $_ = "$fs&nbsp;$_$fe";
                   2222:
                   2223:          #########
                   2224:          # little state machine to parse unified-diff output (Hen, zeller@think.de)
                   2225:          # in order to get some nice 'ediff'-mode output
                   2226:          # states:
                   2227:          #  "dump"             - just dump the value
                   2228:          #  "PreChangeRemove"  - we began with '-' .. so this could be the start of a 'change' area or just remove
                   2229:          #  "PreChange"        - okey, we got several '-' lines and moved to '+' lines -> this is a change block
                   2230:          ##########
                   2231:
                   2232:          if ($diffcode eq '+') {
                   2233:              if ($state eq "dump") {  # 'change' never begins with '+': just dump out value
                   2234:                  print  "<tr><td bgcolor=\"$diffcolorEmpty\">&nbsp;</td><td bgcolor=\"$diffcolorAdd\">$_</td></tr>\n";
                   2235:              }
                   2236:              else {                   # we got minus before
                   2237:                  $state = "PreChange";
                   2238:                  $rightCol[$rightRow++] = $_;
                   2239:              }
                   2240:          }
                   2241:          elsif ($diffcode eq '-') {
                   2242:              $state = "PreChangeRemove";
                   2243:              $leftCol[$leftRow++] = $_;
                   2244:         }
                   2245:         else {  # empty diffcode
                   2246:             flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   2247:              print  "<tr><td>$_</td><td>$_</td></tr>\n";
                   2248:              $state = "dump";
                   2249:              $leftRow = 0;
                   2250:              $rightRow = 0;
                   2251:          }
                   2252:       }
                   2253:   }
                   2254:   flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   2255:
                   2256:   # state is empty if we didn't have any change
                   2257:   if (!$state) {
                   2258:       print "<tr><td colspan=2>&nbsp;</td></tr>";
                   2259:       print "<tr bgcolor=\"$diffcolorEmpty\" >";
                   2260:       print "<td colspan=2 align=center><b>- No viewable Change -</b></td></tr>";
                   2261:   }
                   2262:   print  "</table>";
                   2263:   close($fh);
                   2264:
1.3       maekawa  2265:   print "<br><hr noshade width=\"100%\">\n";
1.1       maekawa  2266:
                   2267:   print "<table border=0>";
                   2268:
                   2269:   print "<tr><td>";
                   2270:   # print legend
                   2271:   print "<table border=1><tr><td>";
                   2272:   print  "Legend:<br><table border=0 cellspacing=0 cellpadding=1>\n";
                   2273:   print  "<tr><td align=center bgcolor=\"$diffcolorRemove\">Removed from v.$rev1</td><td bgcolor=\"$diffcolorEmpty\">&nbsp;</td></tr>";
                   2274:   print  "<tr bgcolor=\"$diffcolorChange\"><td align=center colspan=2>changed lines</td></tr>";
                   2275:   print  "<tr><td bgcolor=\"$diffcolorEmpty\">&nbsp;</td><td align=center bgcolor=\"$diffcolorAdd\">Added in v.$rev2</td></tr>";
                   2276:   print  "</table></td></tr></table>\n";
                   2277:
                   2278:   print "<td>";
                   2279:   # Print format selector
                   2280:   print "<FORM METHOD=\"GET\" ACTION=\"${scriptwhere}\">\n";
                   2281:   foreach my $var (keys %input) {
                   2282:     next if ($var eq "f");
                   2283:     next if (defined($DEFAULTVALUE{$var})
                   2284:             && $DEFAULTVALUE{$var} eq $input{$var});
                   2285:     print "<INPUT TYPE=HIDDEN NAME=\"",urlencode($var),"\" VALUE=\"",
                   2286:            urlencode($input{$var}),"\">\n";
                   2287:   }
                   2288:   printDiffSelect($use_java_script);
                   2289:   print "<INPUT TYPE=SUBMIT VALUE=\"Show\">\n";
                   2290:   print "</FORM>\n";
                   2291:   print "</td>";
                   2292:
                   2293:   print "</tr></table>";
                   2294: }
                   2295:
                   2296: sub navigateHeader ($$$$$) {
                   2297:     my ($swhere,$path,$filename,$rev,$title) = @_;
                   2298:     $swhere = "" if ($swhere eq $scriptwhere);
                   2299:     $swhere = urlencode($filename) if ($swhere eq "");
1.3       maekawa  2300:     print "<\!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
1.1       maekawa  2301:     print "<HTML>\n<HEAD>\n";
1.4     ! maekawa  2302:     print '<!-- hennerik CVSweb $Revision: 1.90 $ -->';
1.1       maekawa  2303:     print "\n<TITLE>$path$filename - $title - $rev</TITLE></HEAD>\n";
                   2304:     print  "<BODY BGCOLOR=\"$backcolor\">\n";
                   2305:     print "<table width=\"100%\" border=0 cellspacing=0 cellpadding=1 bgcolor=\"$navigationHeaderColor\">";
                   2306:     print "<tr valign=bottom><td>";
                   2307:     print  "<a href=\"$swhere$query#rev$rev\">$backicon";
                   2308:     print "</a> <b>Return to ", &link("$filename","$swhere$query#rev$rev")," CVS log";
                   2309:     print "</b> $fileicon</td>";
                   2310:
                   2311:     print "<td align=right>$diricon <b>Up to ", &clickablePath($path, 1), "</b></td>";
                   2312:     print "</tr></table>";
                   2313: }
                   2314:
                   2315: sub plural_write ($$)
                   2316: {
                   2317:     my ($num,$text) = @_;
                   2318:     if ($num != 1) {
                   2319:        $text = $text . "s";
                   2320:     }
                   2321:     if ($num > 0) {
                   2322:        return $num . " " . $text;
                   2323:     }
                   2324:     else {
                   2325:        return "";
                   2326:     }
                   2327: }
                   2328:
                   2329: ##
                   2330: # print readable timestamp in terms of
                   2331: # '..time ago'
                   2332: # H. Zeller <zeller@think.de>
                   2333: ##
                   2334: sub readableTime ($$)
                   2335: {
                   2336:     my ($i, $break, $retval);
                   2337:     my ($secs,$long) = @_;
                   2338:
                   2339:     # this function works correct for time >= 2 seconds
                   2340:     if ($secs < 2) {
                   2341:        return "very little time";
                   2342:     }
                   2343:
                   2344:     my %desc = (1 , 'second',
                   2345:                   60, 'minute',
                   2346:                   3600, 'hour',
                   2347:                   86400, 'day',
                   2348:                   604800, 'week',
                   2349:                   2628000, 'month',
                   2350:                   31536000, 'year');
                   2351:     my @breaks = sort {$a <=> $b} keys %desc;
                   2352:     $i = 0;
                   2353:     while ($i <= $#breaks && $secs >= 2 * $breaks[$i]) {
                   2354:        $i++;
                   2355:     }
                   2356:     $i--;
                   2357:     $break = $breaks[$i];
                   2358:     $retval = plural_write(int ($secs / $break), $desc{"$break"});
                   2359:
                   2360:     if ($long == 1 && $i > 0) {
                   2361:        my $rest = $secs % $break;
                   2362:        $i--;
                   2363:        $break = $breaks[$i];
                   2364:        my $resttime = plural_write(int ($rest / $break),
                   2365:                                $desc{"$break"});
                   2366:        if ($resttime) {
                   2367:            $retval = $retval . ", " . $resttime;
                   2368:        }
                   2369:     }
                   2370:
                   2371:     return $retval;
                   2372: }
                   2373:
                   2374: ##
                   2375: # clickablePath(String pathname, boolean last_item_clickable)
                   2376: #
                   2377: # returns a html-ified path whereas each directory is a link for
                   2378: # faster navigation. last_item_clickable controls whether the
                   2379: # basename (last directory/file) is a link as well
                   2380: ##
                   2381: sub clickablePath($$) {
                   2382:     my ($pathname,$clickLast) = @_;
                   2383:     my $retval = '';
                   2384:
                   2385:     if ($pathname eq '/') {
                   2386:        # this should never happen - chooseCVSRoot() is
                   2387:        # intended to do this
                   2388:        $retval = "[$cvstree]";
                   2389:     }
                   2390:     else {
                   2391:        $retval = $retval . " <a href=\"${scriptname}/${query}#dirlist\">[$cvstree]</a>";
                   2392:        my $wherepath = '';
                   2393:        my ($lastslash) = $pathname =~ m|/$|;
                   2394:        foreach (split(/\//, $pathname)) {
                   2395:            $retval = $retval . " / ";
                   2396:            $wherepath = $wherepath . '/' . $_;
                   2397:            my ($last) = "$wherepath/" eq "/$pathname"
                   2398:                || "$wherepath" eq "/$pathname";
                   2399:            if ($clickLast || !$last) {
                   2400:                $retval = $retval . "<a href=\"${scriptname}"
                   2401:                    . urlencode($wherepath)
                   2402:                    . (!$last || $lastslash ? '/' : '')
                   2403:                    . ${query}
                   2404:                    . (!$last || $lastslash ? "#dirlist" : "")
                   2405:                    . "\">$_</a>";
                   2406:            }
                   2407:            else { # do not make a link to the current dir
                   2408:                $retval = $retval .  $_;
                   2409:            }
                   2410:        }
                   2411:     }
                   2412:     return $retval;
                   2413: }
                   2414:
                   2415: sub chooseCVSRoot() {
                   2416:     my @foo;
                   2417:     foreach (sort keys %CVSROOT) {
                   2418:        if (-d $CVSROOT{$_}) {
                   2419:            push(@foo, $_);
                   2420:        }
                   2421:     }
                   2422:     if (@foo > 1) {
                   2423:        my ($k);
                   2424:        print "<form method=\"GET\" action=\"${scriptwhere}\">\n";
                   2425:        foreach $k (keys %input) {
                   2426:            print "<input type=hidden NAME=$k VALUE=$input{$k}>\n"
                   2427:                if ($input{$k}) && ($k ne "cvsroot");
                   2428:        }
                   2429:        # Form-Elements look wierd in Netscape if the background
                   2430:        # isn't gray and the form elements are not placed
                   2431:        # within a table ...
                   2432:        print "<table><tr>";
                   2433:        print "<td>CVS Root:</td>";
                   2434:        print "<td>\n<select name=\"cvsroot\"";
                   2435:        print " onchange=\"submit()\"" if ($use_java_script);
                   2436:        print ">\n";
                   2437:        foreach $k (@foo) {
                   2438:            print "<option value=\"$k\"";
                   2439:            print " selected" if ("$k" eq "$cvstree");
                   2440:            print ">" . ($CVSROOTdescr{"$k"} ? $CVSROOTdescr{"$k"} :
                   2441:                        $k). "</option>\n";
                   2442:        }
                   2443:        print "</select>\n</td>";
                   2444:        print "<td><input type=submit value=\"Go\"></td>";
                   2445:        print "</tr></table></form>";
                   2446:     }
                   2447:     else {
                   2448:        # no choice ..
                   2449:        print "CVS Root: <b>[$cvstree]</b>";
                   2450:     }
                   2451: }
                   2452:
                   2453: sub chooseMirror() {
                   2454:     my ($mirror,$moremirrors);
                   2455:     $moremirrors = 0;
                   2456:     # This code comes from the original BSD-cvsweb
                   2457:     # and may not be useful for your site; If you don't
                   2458:     # set %MIRRORS this won't show up, anyway
                   2459:     #
                   2460:     # Should perhaps exlude the current site somehow..
                   2461:     if (keys %MIRRORS) {
                   2462:        print "\nThis cvsweb is mirrored in:\n";
                   2463:        foreach $mirror (keys %MIRRORS) {
                   2464:            print ", " if ($moremirrors);
                   2465:            print qq(<a href="$MIRRORS{$mirror}">$mirror</A>\n);
                   2466:            $moremirrors = 1;
                   2467:        }
                   2468:        print "<p>\n";
                   2469:     }
                   2470: }
                   2471:
                   2472: sub fileSortCmp {
                   2473:     my ($comp) = 0;
                   2474:     my ($c,$d,$af,$bf);
                   2475:
                   2476:     ($af = $a) =~ s/,v$//;
                   2477:     ($bf = $b) =~ s/,v$//;
                   2478:     my ($rev1,$date1,$log1,$author1,$filename1) = @{$fileinfo{$af}}
                   2479:         if (defined($fileinfo{$af}));
                   2480:     my ($rev2,$date2,$log2,$author2,$filename2) = @{$fileinfo{$bf}}
                   2481:         if (defined($fileinfo{$bf}));
                   2482:
                   2483:     if (defined($filename1) && defined($filename2) && $af eq $filename1 && $bf eq $filename2) {
                   2484:        # Two files
                   2485:        $comp = -revcmp($rev1, $rev2) if ($byrev && $rev1 && $rev2);
                   2486:        $comp = ($date2 <=> $date1) if ($bydate && $date1 && $date2);
                   2487:        $comp = ($log1 cmp $log2) if ($bylog && $log1 && $log2);
                   2488:        $comp = ($author1 cmp $author2) if ($byauthor && $author1 && $author2);
                   2489:     }
                   2490:     if ($comp == 0) {
                   2491:        # Directories first, then sorted on name if no other sort critera
                   2492:        # available.
                   2493:        my $ad = ((-d "$fullname/$a")?"D":"F");
                   2494:        my $bd = ((-d "$fullname/$b")?"D":"F");
                   2495:        ($c=$a) =~ s|.*/||;
                   2496:        ($d=$b) =~ s|.*/||;
                   2497:        $comp = ("$ad$c" cmp "$bd$d");
                   2498:     }
                   2499:     return $comp;
                   2500: }
                   2501:
                   2502: # make A url for downloading
                   2503: sub download_url {
                   2504:     my ($url,$revision,$mimetype) = @_;
                   2505:
                   2506:     $revision =~ s/\b0\.//;
                   2507:
                   2508:     if (defined($checkout_magic)
                   2509:        && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup")) {
                   2510:        my ($path);
                   2511:        ($path = $where) =~ s|/[^/]*$|/|;
                   2512:        $url = "$scriptname/$checkoutMagic/${path}$url";
                   2513:     }
                   2514:     $url .= "?rev=$revision";
1.3       maekawa  2515:     $url .= "&amp;content-type=$mimetype" if (defined($mimetype));
1.1       maekawa  2516:
                   2517:     return $url;
                   2518: }
                   2519:
                   2520: # Presents a link to download the
                   2521: # selected revision
                   2522: sub download_link {
                   2523:     my ($url,$revision,$textlink,$mimetype) = @_;
                   2524:     my ($fullurl) = download_url($url,$revision,$mimetype);
                   2525:     my ($paren) = $textlink =~ /^\(/;
                   2526:     $textlink =~ s/^\(// if ($paren);
                   2527:     $textlink =~ s/\)$// if ($paren);
                   2528:     print "(" if ($paren);
                   2529:     print "<A HREF=\"$fullurl";
                   2530:     print $barequery;
                   2531:     print "\"";
                   2532:     if ($open_extern_window && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup")) {
                   2533:        print " target=\"cvs_checkout\"";
                   2534:        # we should have
                   2535:        #   'if (document.cvswin==null) document.cvswin=window.open(...'
                   2536:        # in order to allow the user to resize the window; otherwise
                   2537:        # the user may resize the window, but on next checkout - zap -
                   2538:        # its original (configured s. cvsweb.conf) size is back again
                   2539:        # .. annoying (if $extern_window_(width|height) is defined)
                   2540:        # but this if (..) solution is far from perfect
                   2541:        # what we need to do as well is
                   2542:        # 1) save cvswin in an invisible frame that always exists
                   2543:        #    (document.cvswin will be void on next load)
                   2544:        # 2) on close of the cvs_checkout - window set the cvswin
                   2545:        #    variable to 'null' again - so that it will be
                   2546:        #    reopenend with the configured size
                   2547:        # anyone a JavaScript programmer ?
                   2548:        # .. so here without if (..):
                   2549:        # currently, the best way is to comment out the size parameters
                   2550:        # ($extern_window...) in cvsweb.conf.
                   2551:        if ($use_java_script) {
                   2552:            print " onClick=\"window.open('$fullurl','cvs_checkout',";
                   2553:            print "'resizeable,scrollbars";
                   2554:            print ",status,toolbar" if (defined($mimetype)
                   2555:                && $mimetype eq "text/html");
                   2556:            print ",width=$extern_window_width" if (defined($extern_window_width));
                   2557:            print ",height=$extern_window_height" if (defined($extern_window_height));
                   2558:            print"');\"";
                   2559:        }
                   2560:     }
                   2561:     print "><b>$textlink</b></A>";
                   2562:     print ")" if ($paren);
                   2563: }
                   2564:
                   2565: # Returns a Query string with the
                   2566: # specified parameter toggled
                   2567: sub toggleQuery($$) {
                   2568:     my ($toggle,$value) = @_;
                   2569:     my ($newquery,$var);
                   2570:     my (%vars);
                   2571:     %vars = %input;
                   2572:     if (defined($value)) {
                   2573:        $vars{$toggle} = $value;
                   2574:     }
                   2575:     else {
                   2576:        $vars{$toggle} = $vars{$toggle} ? 0 : 1;
                   2577:     }
                   2578:     # Build a new query of non-default paramenters
                   2579:     $newquery = "";
                   2580:     foreach $var (@stickyvars) {
                   2581:        my ($value) = defined($vars{$var}) ? $vars{$var} : "";
                   2582:        my ($default) = defined($DEFAULTVALUE{$var}) ? $DEFAULTVALUE{$var} : "";
                   2583:        if ($value ne $default) {
1.3       maekawa  2584:            $newquery .= "&amp;" if ($newquery ne "");
1.1       maekawa  2585:            $newquery .= urlencode($var) . "=" . urlencode($value);
                   2586:        }
                   2587:     }
                   2588:     if ($newquery) {
                   2589:        return '?' . $newquery;
                   2590:     }
                   2591:     return "";
                   2592: }
                   2593:
                   2594: sub urlencode {
                   2595:     my ($in) = @_;
                   2596:     my ($out);
                   2597:     ($out = $in) =~ s/([\000-+{-\377])/sprintf("%%%02x", ord($1))/ge;
                   2598:     return $out;
                   2599: }
                   2600:
                   2601: sub http_header {
                   2602:     my $content_type = shift || "text/html";
                   2603:     my $is_mod_perl = defined($ENV{'MOD_PERL'});
1.3       maekawa  2604:     if (defined($moddate)) {
                   2605:        if ($is_mod_perl) {
                   2606:            Apache->request->header_out(Last_modified => scalar gmtime($moddate) . " GMT");
                   2607:        }
                   2608:        else {
                   2609:            print "Last-Modified: " . scalar gmtime($moddate) . " GMT\n";
                   2610:        }
                   2611:     }
1.1       maekawa  2612:     if ($is_mod_perl) {
                   2613:        Apache->request->content_type($content_type);
                   2614:     }
                   2615:     else {
                   2616:            print "Content-type: $content_type\n";
                   2617:     }
                   2618:     if ($allow_compress && $maycompress) {
                   2619:        my $fh = do {local(*FH);};
                   2620:        if (defined($GZIPBIN) && open($fh, "|$GZIPBIN -1 -c")) {
                   2621:            if ($is_mod_perl) {
                   2622:                    Apache->request->content_encoding("x-gzip");
                   2623:                    Apache->request->header_out(Vary => "Accept-Encoding");
                   2624:                    Apache->request->send_http_header;
                   2625:            }
                   2626:            else {
                   2627:                    print "Content-encoding: x-gzip\n";
                   2628:                    print "Vary: Accept-Encoding\n";  #RFC 2068, 14.43
                   2629:                    print "\n"; # Close headers
                   2630:            }
                   2631:            $| = 1; $| = 0; # Flush header output
                   2632:            select ($fh);
                   2633: #          print "<!-- gzipped -->" if ($content_type eq "text/html");
                   2634:        }
                   2635:        else {
                   2636:            if ($is_mod_perl) {
                   2637:                    Apache->request->send_http_header;
                   2638:            }
                   2639:            else {
                   2640:                    print "\n"; # Close headers
                   2641:            }
                   2642:            print "<font size=-1>Unable to find gzip binary in the \$PATH to compress output</font><br>";
                   2643:        }
                   2644:     }
                   2645:     else {
                   2646:            if ($is_mod_perl) {
                   2647:                    Apache->request->send_http_header;
                   2648:            }
                   2649:            else {
                   2650:                    print "\n"; # Close headers
                   2651:            }
                   2652:     }
                   2653: }
                   2654:
                   2655: sub html_header($) {
                   2656:     my ($title) = @_;
1.4     ! maekawa  2657:     my $version = '$Revision: 1.90 $';
1.1       maekawa  2658:     http_header();
                   2659:     print <<EOH;
                   2660: <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"
                   2661:  "http://www.w3.org/TR/REC-html40/loose.dtd">
                   2662: <html>
                   2663: <title>$title</title>
1.3       maekawa  2664: <!-- hennerik CVSweb $version -->
1.1       maekawa  2665: </head>
                   2666: $body_tag
                   2667: $logo <h1 align="center">$title</h1>
                   2668: EOH
                   2669: }
                   2670:
                   2671: sub html_footer {
                   2672:     return "<hr noshade><address>$address</address>\n";
                   2673: }
                   2674:
                   2675: sub link_tags
                   2676: {
                   2677:     my ($tags) = @_;
                   2678:     my ($ret) = "";
                   2679:     my ($fileurl,$filename);
                   2680:
                   2681:     ($filename = $where) =~ s/^.*\///;
                   2682:     $fileurl = urlencode($filename);
                   2683:
                   2684:     foreach my $sym (split(", ", $tags)) {
                   2685:        $ret .= ",\n" if ($ret ne "");
                   2686:        $ret .= "<A HREF=\"$fileurl"
                   2687:                . toggleQuery('only_with_tag',$sym) . "\">$sym</A>";
                   2688:     }
                   2689:     return $ret."\n";
                   2690: }
                   2691:
                   2692: #
                   2693: # See if a module is listed in the config file's @HideModule list.
                   2694: #
                   2695: sub forbidden_module {
                   2696:     my($module) = @_;
1.3       maekawa  2697:
                   2698:     for (my $i=0; $i < @HideModules; $i++) {
                   2699:        return 1 if $module eq $HideModules[$i];
                   2700:     }
1.1       maekawa  2701:
1.3       maekawa  2702:     return 0;
1.1       maekawa  2703: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>