[BACK]Return to cvsweb.cgi CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / cvsweb

Annotation of cvsweb/cvsweb.cgi, Revision 3.20

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

CVSweb