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

Annotation of cvsweb/cvsweb.cgi, Revision 3.16

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

CVSweb