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

Annotation of cvsweb/cvsweb.cgi, Revision 3.26

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

CVSweb