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

Annotation of cvsweb/cvsweb.cgi, Revision 3.31

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

CVSweb