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

Annotation of cvsweb/cvsweb.cgi, Revision 3.113

3.55      knu         1: #!/usr/bin/perl -wT
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.113   ! scop        6: #             Bill Fenner          <fenner@FreeBSD.org>   (original work)
        !             7: # extended by Henner Zeller        <zeller@think.de>,
        !             8: #             Henrik Nordstrom     <hno@hem.passagen.se>
        !             9: #             Ken Coar             <coar@Apache.Org>
        !            10: #             Dick Balaska         <dick@buckosoft.com>
        !            11: #             Akinori MUSHA        <knu@FreeBSD.org>
        !            12: #             Jens-Uwe Mager       <jum@helios.de>
        !            13: #             Ville Skyttä         <scop@FreeBSD.org>
        !            14: #             Vassilii Khachaturov <vassilii@tarunz.org>
3.1       knu        15: #
                     16: # Based on:
                     17: # * Bill Fenners cvsweb.cgi revision 1.28 available from:
3.5       knu        18: #   http://www.FreeBSD.org/cgi/cvsweb.cgi/www/en/cgi/cvsweb.cgi
1.1       jfieber    19: #
1.21      wosch      20: # Copyright (c) 1996-1998 Bill Fenner
3.1       knu        21: #           (c) 1998-1999 Henner Zeller
3.109     scop       22: #           (c) 1999      Henrik Nordstrom
                     23: #           (c) 2000-2002 Akinori MUSHA
                     24: #           (c) 2002      Ville Skyttä
1.21      wosch      25: # All rights reserved.
                     26: #
                     27: # Redistribution and use in source and binary forms, with or without
                     28: # modification, are permitted provided that the following conditions
                     29: # are met:
                     30: # 1. Redistributions of source code must retain the above copyright
                     31: #    notice, this list of conditions and the following disclaimer.
                     32: # 2. Redistributions in binary form must reproduce the above copyright
                     33: #    notice, this list of conditions and the following disclaimer in the
                     34: #    documentation and/or other materials provided with the distribution.
                     35: #
                     36: # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     37: # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     38: # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     39: # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     40: # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     41: # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     42: # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     43: # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     44: # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     45: # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     46: # SUCH DAMAGE.
                     47: #
3.87      knu        48: # $FreeBSD$
3.82      knu        49: # $Id: cvsweb.cgi,v 1.112 2001/07/24 13:03:16 hzeller Exp $
3.85      knu        50: # $Idaemons: /home/cvs/cvsweb/cvsweb.cgi,v 3.84 2001/10/07 20:50:10 knu Exp $
1.21      wosch      51: #
3.1       knu        52: ###
                     53:
3.55      knu        54: require 5.000;
                     55:
3.1       knu        56: use strict;
1.21      wosch      57:
3.1       knu        58: use vars qw (
3.64      knu        59:     $cvsweb_revision
3.60      knu        60:     $mydir $uname $config $allow_version_select $verbose
3.56      knu        61:     @CVSrepositories @CVSROOT %CVSROOT %CVSROOTdescr
                     62:     %MIRRORS %DEFAULTVALUE %ICONS %MTYPES
3.37      knu        63:     @DIFFTYPES %DIFFTYPES @LOGSORTKEYS %LOGSORTKEYS
3.1       knu        64:     %alltags @tabcolors %fileinfo %tags @branchnames %nameprinted
                     65:     %symrev %revsym @allrevisions %date %author @revdisplayorder
3.17      knu        66:     @revisions %state %difflines %log %branchpoint @revorder
3.54      knu        67:     $prcgi @prcategories $re_prcategories $prkeyword $re_prkeyword $mancgi
3.1       knu        68:     $checkoutMagic $doCheckout $scriptname $scriptwhere
3.99      knu        69:     $where $pathinfo $Browser $nofilelinks $maycompress
                     70:     @stickyvars @unsafevars
3.3       knu        71:     %funcline_regexp $is_mod_perl
3.34      knu        72:     $is_links $is_lynx $is_w3m $is_msie $is_mozilla3 $is_textbased
3.1       knu        73:     %input $query $barequery $sortby $bydate $byrev $byauthor
3.37      knu        74:     $bylog $byfile $defaultDiffType $logsort $cvstree $cvsroot
3.86      knu        75:     $mimetype $charset $output_filter $defaultTextPlain $defaultViewable
3.58      knu        76:     $command_path %CMD $allow_compress
                     77:     $backicon $diricon $fileicon
3.40      knu        78:     $fullname $newname $cvstreedefault
                     79:     $body_tag $body_tag_for_src $logo $defaulttitle $address
3.26      knu        80:     $long_intro $short_instruction $shortLogLen
3.1       knu        81:     $show_author $dirtable $tablepadding $columnHeaderColorDefault
3.3       knu        82:     $columnHeaderColorSorted $hr_breakable $showfunc $hr_ignwhite
3.1       knu        83:     $hr_ignkeysubst $diffcolorHeading $diffcolorEmpty $diffcolorRemove
                     84:     $diffcolorChange $diffcolorAdd $diffcolorDarkChange $difffontface
3.101     knu        85:     $difffontsize $inputTextSize $mime_types
                     86:     $allow_annotate $allow_markup
                     87:     $allow_log_extra $allow_dir_extra $allow_source_extra
                     88:     $use_java_script $open_extern_window
3.1       knu        89:     $extern_window_width $extern_window_height $edit_option_form
3.51      knu        90:     $show_subdir_lastmod $show_log_in_markup $preformat_in_markup $v
3.1       knu        91:     $navigationHeaderColor $tableBorderColor $markupLogColor
3.81      knu        92:     $tabstop $state $annTable $sel $curbranch @HideModules @ForbiddenFiles
3.1       knu        93:     $module $use_descriptions %descriptions @mytz $dwhere $moddate
3.61      knu        94:     $use_moddate $has_zlib $gzip_open
3.77      knu        95:     $allow_tar @tar_options @gzip_options @zip_options @cvs_options
3.32      knu        96:     $LOG_FILESEPARATOR $LOG_REVSEPARATOR
3.106     scop       97:     $tmpdir $HTML_DOCTYPE $HTML_META
3.1       knu        98: );
                     99:
3.12      knu       100: sub printDiffSelect($);
3.37      knu       101: sub printDiffLinks($$);
                    102: sub printLogSortSelect($);
3.12      knu       103: sub findLastModifiedSubdirs(@);
3.36      knu       104: sub htmlify_sub(&$);
3.12      knu       105: sub htmlify($;$);
3.20      knu       106: sub spacedHtmlText($;$);
3.12      knu       107: sub link($$);
                    108: sub revcmp($$);
3.103     knu       109: sub fatal($$@);
3.12      knu       110: sub redirect($);
                    111: sub safeglob($);
3.58      knu       112: sub search_path($);
3.12      knu       113: sub getMimeTypeFromSuffix($);
3.24      knu       114: sub head($;$);
                    115: sub scan_directives(@);
3.86      knu       116: sub openOutputFilter();
3.12      knu       117: sub doAnnotate($$);
                    118: sub doCheckout($$);
                    119: sub cvswebMarkup($$$);
                    120: sub viewable($);
                    121: sub doDiff($$$$$$);
                    122: sub getDirLogs($$@);
                    123: sub readLog($;$);
                    124: sub printLog($;$);
                    125: sub doLog($);
                    126: sub flush_diff_rows($$$$);
                    127: sub human_readable_diff($);
                    128: sub navigateHeader($$$$$);
                    129: sub plural_write($$);
                    130: sub readableTime($$);
                    131: sub clickablePath($$);
                    132: sub chooseCVSRoot();
                    133: sub chooseMirror();
                    134: sub fileSortCmp();
                    135: sub download_url($$;$);
                    136: sub download_link($$$;$);
                    137: sub toggleQuery($$);
                    138: sub urlencode($);
3.35      knu       139: sub htmlquote($);
3.36      knu       140: sub htmlunquote($);
3.48      knu       141: sub hrefquote($);
3.12      knu       142: sub http_header(;$);
                    143: sub html_header($);
                    144: sub html_footer();
                    145: sub link_tags($);
3.81      knu       146: sub forbidden_file($);
3.12      knu       147: sub forbidden_module($);
                    148:
3.1       knu       149: ##### Start of Configuration Area ########
3.59      knu       150: delete $ENV{PATH};
                    151:
3.112     scop      152: $cvsweb_revision = '2.0.4';
3.64      knu       153:
3.100     knu       154: use File::Basename ();
3.11      knu       155:
3.100     knu       156: ($mydir) = (File::Basename::dirname($0) =~ /(.*)/);    # untaint
3.58      knu       157:
3.12      knu       158: # == EDIT this ==
3.28      knu       159: # Locations to search for user configuration, in order:
3.80      knu       160: for ("$mydir/cvsweb.conf", '/usr/local/etc/cvsweb/cvsweb.conf') {
                    161:        if (defined($_) && -r $_) {
                    162:                $config = $_;
                    163:                last;
                    164:        }
3.11      knu       165: }
3.1       knu       166:
                    167: # == Configuration defaults ==
                    168: # Defaults for configuration variables that shouldn't need
                    169: # to be configured..
                    170: $allow_version_select = 1;
3.101     knu       171: $allow_log_extra = 1;
3.1       knu       172:
                    173: ##### End of Configuration Area   ########
                    174:
                    175: ######## Configuration variables #########
                    176: # These are defined to allow checking with perl -cw
3.80      knu       177: @CVSrepositories = @CVSROOT = %CVSROOT = %MIRRORS = %DEFAULTVALUE = %ICONS =
                    178:     %MTYPES = %tags = %alltags = @tabcolors = %fileinfo = ();
                    179: $cvstreedefault = $body_tag = $body_tag_for_src = $logo = $defaulttitle =
                    180:     $address = $long_intro = $short_instruction = $shortLogLen = $show_author =
                    181:     $dirtable = $tablepadding = $columnHeaderColorDefault =
                    182:     $columnHeaderColorSorted = $hr_breakable = $showfunc = $hr_ignwhite =
                    183:     $hr_ignkeysubst = $diffcolorHeading = $diffcolorEmpty = $diffcolorRemove =
                    184:     $diffcolorChange = $diffcolorAdd  = $diffcolorDarkChange = $difffontface   =
                    185:     $difffontsize    = $inputTextSize = $mime_types          = $allow_annotate =
                    186:     $allow_markup        = $use_java_script      = $open_extern_window =
                    187:     $extern_window_width = $extern_window_height = $edit_option_form   =
                    188:     $show_subdir_lastmod = $show_log_in_markup = $v = $navigationHeaderColor =
                    189:     $tableBorderColor = $markupLogColor = $tabstop = $use_moddate = $moddate =
3.106     scop      190:     $gzip_open = $HTML_DOCTYPE = $HTML_META = undef;
3.80      knu       191: $tmpdir = defined($ENV{TMPDIR}) ? $ENV{TMPDIR} : "/var/tmp";
1.21      wosch     192:
3.32      knu       193: $LOG_FILESEPARATOR = q/^={77}$/;
3.80      knu       194: $LOG_REVSEPARATOR  = q/^-{28}$/;
3.32      knu       195:
3.37      knu       196: @DIFFTYPES = qw(h H u c s);
                    197: @DIFFTYPES{@DIFFTYPES} = (
3.80      knu       198:        {
                    199:                'descr'   => 'colored',
                    200:                'opts'    => ['-u'],
                    201:                'colored' => 1,
                    202:        },
                    203:        {
                    204:                'descr'   => 'long colored',
                    205:                'opts'    => ['--unified=15'],
                    206:                'colored' => 1,
                    207:        },
                    208:        {
                    209:                'descr'   => 'unified',
                    210:                'opts'    => ['-u'],
                    211:                'colored' => 0,
                    212:        },
                    213:        {
                    214:                'descr'   => 'context',
                    215:                'opts'    => ['-c'],
                    216:                'colored' => 0,
                    217:        },
                    218:        {
                    219:                'descr'   => 'side by side',
                    220:                'opts'    => ['--side-by-side', '--width=164'],
                    221:                'colored' => 0,
                    222:        },
                    223: );
3.37      knu       224:
                    225: @LOGSORTKEYS = qw(cvs date rev);
                    226: @LOGSORTKEYS{@LOGSORTKEYS} = (
3.80      knu       227:        {
                    228:                'descr' => 'Not sorted',
                    229:        },
                    230:        {
                    231:                'descr' => 'Commit date',
                    232:        },
                    233:        {
                    234:                'descr' => 'Revision',
                    235:        },
                    236: );
3.37      knu       237:
3.100     knu       238: $HTML_DOCTYPE =
                    239:   '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
                    240:
3.106     scop      241: $HTML_META = <<EOM;
                    242: <meta name="robots" content="nofollow">
3.109     scop      243: <meta name="generator" content="FreeBSD-CVSweb $cvsweb_revision">
3.106     scop      244: <meta http-equiv="Content-Script-Type" content="text/javascript">
                    245: <meta http-equiv="Content-Style-Type" content="text/css">
                    246: EOM
                    247:
3.1       knu       248: ##### End of configuration variables #####
                    249:
3.100     knu       250: use Time::Local ();
                    251: use IPC::Open2 qw(open2);
1.1       jfieber   252:
3.23      knu       253: # Check if the zlib C library interface is installed, and if yes
                    254: # we can avoid using the extra gzip process.
3.80      knu       255: eval { require Compress::Zlib; };
3.23      knu       256: $has_zlib = !$@;
                    257:
3.80      knu       258: $verbose       = $v;
3.1       knu       259: $checkoutMagic = "~checkout~";
3.80      knu       260: $pathinfo      = defined($ENV{PATH_INFO}) ? $ENV{PATH_INFO} : '';
                    261: $where         = $pathinfo;
3.63      knu       262: $doCheckout = ($where =~ m|^/$checkoutMagic/|);
                    263: $where =~ s|^/$checkoutMagic/|/|;
                    264: $where =~ s|^/||;
3.3       knu       265: $scriptname = defined($ENV{SCRIPT_NAME}) ? $ENV{SCRIPT_NAME} : '';
3.63      knu       266: $scriptname =~ s|^/*|/|;
                    267:
3.64      knu       268: # Let's workaround thttpd's stupidity..
3.63      knu       269: if ($scriptname =~ m|/$|) {
3.80      knu       270:        $pathinfo .= '/';
                    271:        my $re = quotemeta $pathinfo;
                    272:        $scriptname =~ s/$re$//;
3.63      knu       273: }
                    274:
3.3       knu       275: $scriptwhere = $scriptname;
3.63      knu       276: $scriptwhere .= '/' . urlencode($where);
                    277: $where = '/' if ($where eq '');
3.3       knu       278:
                    279: $is_mod_perl = defined($ENV{MOD_PERL});
3.1       knu       280:
                    281: # in lynx, it it very annoying to have two links
                    282: # per file, so disable the link at the icon
                    283: # in this case:
3.44      knu       284: $Browser = $ENV{HTTP_USER_AGENT} || '';
3.80      knu       285: $is_links    = ($Browser =~ m`^Links `);
                    286: $is_lynx     = ($Browser =~ m`^Lynx/`i);
                    287: $is_w3m      = ($Browser =~ m`^w3m/`i);
                    288: $is_msie     = ($Browser =~ m`MSIE`);
3.5       knu       289: $is_mozilla3 = ($Browser =~ m`^Mozilla/[3-9]`);
3.3       knu       290:
3.34      knu       291: $is_textbased = ($is_links || $is_lynx || $is_w3m);
3.5       knu       292:
                    293: $nofilelinks = $is_textbased;
3.1       knu       294:
                    295: # newer browsers accept gzip content encoding
                    296: # and state this in a header
                    297: # (netscape did always but didn't state it)
                    298: # It has been reported that these
                    299: #  braindamaged MS-Internet Exploders claim that they
                    300: # accept gzip .. but don't in fact and
                    301: # display garbage then :-/
3.23      knu       302: # Turn off gzip if running under mod_perl and no zlib is available,
                    303: # piping does not work as expected inside the server.
3.80      knu       304: $maycompress =
                    305:     (((defined($ENV{HTTP_ACCEPT_ENCODING})
                    306:     && $ENV{HTTP_ACCEPT_ENCODING} =~ m`gzip`) || $is_mozilla3) && !$is_msie
                    307:     && !($is_mod_perl && !$has_zlib));
3.1       knu       308:
                    309: # put here the variables we need in order
                    310: # to hold our state - they will be added (with
3.12      knu       311: # their current value) to any link/query string
3.1       knu       312: # you construct
3.6       knu       313: @stickyvars = qw(cvsroot hideattic sortby logsort f only_with_tag);
3.97      knu       314: @unsafevars = qw(logsort only_with_tag r1 r2 rev sortby tr1 tr2);
1.1       jfieber   315:
3.1       knu       316: if (-f $config) {
3.103     knu       317:        do "$config" or fatal("500 Internal Error",
                    318:                              'Error in loading configuration file: %s<br><br>%s<br>',
                    319:                              $config, $@);
3.27      knu       320: } else {
3.103     knu       321:        fatal("500 Internal Error",
                    322:              'Configuration not found.  Set the variable <code>$config</code> in cvsweb.cgi to your <b>cvsweb.conf</b> configuration file first.'
                    323:             );
3.1       knu       324: }
                    325:
                    326: undef %input;
3.3       knu       327: $query = $ENV{QUERY_STRING};
                    328:
3.12      knu       329: if (defined($query) && $query ne '') {
3.80      knu       330:        foreach (split (/&/, $query)) {
                    331:                y/+/ /;
                    332:                s/%(..)/sprintf("%c", hex($1))/ge;    # unquote %-quoted
                    333:                if (/(\S+)=(.*)/) {
                    334:                        $input{$1} = $2 if ($2 ne "");
                    335:                } else {
                    336:                        $input{$_}++;
                    337:                }
3.1       knu       338:        }
1.7       fenner    339: }
1.10      wosch     340:
3.12      knu       341: # For backwards compability, set only_with_tag to only_on_branch if set.
3.1       knu       342: $input{only_with_tag} = $input{only_on_branch}
                    343:     if (defined($input{only_on_branch}));
3.97      knu       344:
                    345: # Prevent cross-site scripting
                    346: foreach (@unsafevars) {
3.103     knu       347:        if (defined($input{$_}) && $input{$_} =~ /[^\w\-.]/) {
                    348:                fatal("500 Internal Error",
                    349:                      'Malformed query (%s=%s)',
                    350:                      $_, $input{$_});
                    351:        }
3.97      knu       352: }
                    353:
                    354: if (defined($input{"content-type"})) {
                    355:        fatal("500 Internal Error", "Unsupported content-type")
                    356:            if ($input{"content-type"} !~ /^[-0-9A-Za-z]+\/[-0-9A-Za-z]+$/);
                    357: }
1.10      wosch     358:
3.1       knu       359: $DEFAULTVALUE{'cvsroot'} = $cvstreedefault;
1.10      wosch     360:
3.80      knu       361: foreach (keys %DEFAULTVALUE) {
                    362:
                    363:        # replace not given parameters with the default parameters
                    364:        if (!defined($input{$_}) || $input{$_} eq "") {
                    365:
                    366:                # Empty Checkboxes in forms return -- nothing. So we define a helper
                    367:                # variable in these forms (copt) which indicates that we just set
                    368:                # parameters with a checkbox
                    369:                if (!defined($input{"copt"})) {
                    370:
                    371:                        # 'copt' isn't defined --> empty input is not the result
                    372:                        # of empty input checkbox --> set default
                    373:                        $input{$_} = $DEFAULTVALUE{$_}
                    374:                            if (defined($DEFAULTVALUE{$_}));
                    375:                } else {
                    376:
                    377:                        # 'copt' is defined -> the result of empty input checkbox
                    378:                        # -> set to zero (disable) if default is a boolean (0|1).
                    379:                        $input{$_} = 0
                    380:                            if (defined($DEFAULTVALUE{$_})
                    381:                            && ($DEFAULTVALUE{$_} eq "0"
                    382:                            || $DEFAULTVALUE{$_} eq "1"));
                    383:                }
3.1       knu       384:        }
1.10      wosch     385: }
3.12      knu       386:
3.1       knu       387: $barequery = "";
3.31      knu       388: my @barequery;
3.1       knu       389: foreach (@stickyvars) {
3.80      knu       390:
                    391:        # construct a query string with the sticky non default parameters set
                    392:        if (defined($input{$_}) && $input{$_} ne ''
                    393:            && !(defined($DEFAULTVALUE{$_}) && $input{$_} eq $DEFAULTVALUE{$_}))
                    394:        {
                    395:                push @barequery,
                    396:                    join ('=', urlencode($_), urlencode($input{$_}));
                    397:        }
3.1       knu       398: }
3.80      knu       399:
3.1       knu       400: # is there any query ?
3.31      knu       401: if (@barequery) {
3.80      knu       402:        $barequery = join ('&', @barequery);
                    403:        $query     = "?$barequery";
                    404:        $barequery = "&$barequery";
                    405: } else {
                    406:        $query = "";
3.1       knu       407: }
3.31      knu       408: undef @barequery;
3.1       knu       409:
3.57      knu       410: if (defined($input{path})) {
3.80      knu       411:        redirect("$scriptname/$input{path}$query");
3.57      knu       412: }
                    413:
3.1       knu       414: # get actual parameters
3.80      knu       415: $sortby   = $input{"sortby"};
                    416: $bydate   = 0;
                    417: $byrev    = 0;
3.1       knu       418: $byauthor = 0;
3.80      knu       419: $bylog    = 0;
                    420: $byfile   = 0;
3.1       knu       421: if ($sortby eq "date") {
3.80      knu       422:        $bydate = 1;
                    423: } elsif ($sortby eq "rev") {
                    424:        $byrev = 1;
                    425: } elsif ($sortby eq "author") {
                    426:        $byauthor = 1;
                    427: } elsif ($sortby eq "log") {
                    428:        $bylog = 1;
                    429: } else {
                    430:        $byfile = 1;
3.1       knu       431: }
                    432:
3.37      knu       433: $defaultDiffType = $input{'f'};
3.1       knu       434:
3.3       knu       435: $logsort = $input{'logsort'};
1.10      wosch     436:
3.95      knu       437: {
                    438:        my @tmp = @CVSrepositories;
                    439:        my @pair;
3.56      knu       440:
3.95      knu       441:        while (@pair = splice(@tmp, 0, 2)) {
                    442:                my ($key,   $val)     = @pair;
                    443:                my ($descr, $cvsroot) = @$val;
                    444:
                    445:                next if !-d $cvsroot;
                    446:
                    447:                $CVSROOTdescr{$key} = $descr;
                    448:                $CVSROOT{$key}      = $cvsroot;
                    449:                push @CVSROOT, $key;
                    450:        }
3.56      knu       451: }
1.12      fenner    452:
3.1       knu       453: ## Default CVS-Tree
                    454: if (!defined($CVSROOT{$cvstreedefault})) {
3.103     knu       455:        fatal("500 Internal Error",
                    456:              '<code>$cvstreedefault</code> points to a repository (%s) not defined in <code>%%CVSROOT</code> (edit your configuration file %s)',
                    457:              $cvstreedefault, $config);
3.1       knu       458: }
                    459:
                    460: # alternate CVS-Tree, configured in cvsweb.conf
                    461: if ($input{'cvsroot'} && $CVSROOT{$input{'cvsroot'}}) {
3.80      knu       462:        $cvstree = $input{'cvsroot'};
3.1       knu       463: } else {
3.80      knu       464:        $cvstree = $cvstreedefault;
1.10      wosch     465: }
                    466:
3.1       knu       467: $cvsroot = $CVSROOT{$cvstree};
1.10      wosch     468:
3.1       knu       469: # create icons out of description
3.32      knu       470: my $k;
3.31      knu       471: foreach $k (keys %ICONS) {
3.80      knu       472:        no strict 'refs';
                    473:        my ($itxt, $ipath, $iwidth, $iheight) = @{$ICONS{$k}};
                    474:        if ($ipath) {
                    475:                ${"${k}icon"} =
                    476:                    sprintf(
3.89      knu       477:                        '<img src="%s" alt="%s" border="0" width="%d" height="%d">',
3.80      knu       478:                        hrefquote($ipath), htmlquote($itxt), $iwidth, $iheight)
                    479:        } else {
                    480:                ${"${k}icon"} = $itxt;
                    481:        }
1.24      wosch     482: }
3.31      knu       483: undef $k;
1.24      wosch     484:
3.27      knu       485: my $config_cvstree = "$config-$cvstree";
                    486:
3.1       knu       487: # Do some special configuration for cvstrees
3.27      knu       488: if (-f $config_cvstree) {
3.103     knu       489:        do "$config_cvstree" or
                    490:            fatal("500 Internal Error",
                    491:                  'Error in loading configuration file: %s<br><br>%s<br>',
                    492:                  $config_cvstree, $@);
3.27      knu       493: }
3.31      knu       494: undef $config_cvstree;
3.1       knu       495:
3.80      knu       496: $re_prcategories = '(?:' . join ('|', @prcategories) . ')' if @prcategories;
3.54      knu       497: $re_prkeyword = quotemeta($prkeyword) if defined($prkeyword);
3.17      knu       498: $prcgi .= '%s' if defined($prcgi) && $prcgi !~ /%s/;
1.24      wosch     499:
3.80      knu       500: $fullname         = "$cvsroot/$where";
                    501: $mimetype         = &getMimeTypeFromSuffix($fullname);
3.1       knu       502: $defaultTextPlain = ($mimetype eq "text/plain");
3.80      knu       503: $defaultViewable  = $allow_markup && viewable($mimetype);
3.1       knu       504:
3.63      knu       505: my $rewrite = 0;
                    506:
                    507: if ($pathinfo =~ m|//|) {
3.80      knu       508:        $pathinfo =~ y|/|/|s;
                    509:        $rewrite = 1;
3.63      knu       510: }
                    511:
                    512: if (-d $fullname && $pathinfo !~ m|/$|) {
3.80      knu       513:        $pathinfo .= '/';
                    514:        $rewrite = 1;
3.63      knu       515: }
                    516:
                    517: if (!-d $fullname && $pathinfo =~ m|/$|) {
3.80      knu       518:        chop $pathinfo;
                    519:        $rewrite = 1;
3.63      knu       520: }
                    521:
                    522: if ($rewrite) {
3.80      knu       523:        redirect($scriptname . urlencode($pathinfo) . $query);
3.1       knu       524: }
                    525:
3.63      knu       526: undef $rewrite;
                    527:
3.1       knu       528: if (!-d $cvsroot) {
3.103     knu       529:        fatal("500 Internal Error",
                    530:              '$CVSROOT not found!<p>The server on which the CVS tree lives is probably down.  Please try again in a few minutes.');
3.1       knu       531: }
                    532:
                    533: #
                    534: # See if the module is in our forbidden list.
                    535: #
                    536: $where =~ m:([^/]*):;
                    537: $module = $1;
                    538: if ($module && &forbidden_module($module)) {
3.103     knu       539:        fatal("403 Forbidden",
                    540:              'Access to %s forbidden.',
                    541:              $where);
3.1       knu       542: }
3.46      knu       543:
                    544: #
                    545: # Handle tarball downloads before any headers are output.
                    546: #
                    547: if ($input{tarball}) {
3.103     knu       548:        fatal("403 Forbidden",
                    549:              'Downloading tarballs is prohibited.')
3.80      knu       550:            unless $allow_tar;
                    551:        my ($module) = ($where =~ m,^/?(.*),);    # untaint
                    552:        $module =~ s,/([^/]*)$,,;
                    553:        my ($ext)     = ($1      =~ /(\.tar\.gz|\.zip)$/);
                    554:        my ($basedir) = ($module =~ m,([^/]+)$,);
                    555:
                    556:        if ($basedir eq '' || $module eq '') {
3.103     knu       557:                fatal("500 Internal Error",
                    558:                      'You cannot download the top level directory.');
3.80      knu       559:        }
                    560:
3.88      knu       561:        my $tmpexportdir = "$tmpdir/.cvsweb.$$." . int(time);
3.80      knu       562:
3.88      knu       563:        mkdir($tmpexportdir, 0700)
3.103     knu       564:            or fatal("500 Internal Error",
                    565:                     'Unable to make temporary directory: %s',
                    566:                     $!);
3.80      knu       567:
                    568:        my @fatal;
                    569:
                    570:        my $tag =
                    571:            (exists $input{only_with_tag} && length $input{only_with_tag}) ?
                    572:            $input{only_with_tag} : "HEAD";
                    573:
3.88      knu       574:        if ($tag eq 'MAIN') {
                    575:            $tag = 'HEAD';
                    576:        }
                    577:
3.80      knu       578:        if (system $CMD{cvs}, @cvs_options, '-Qd', $cvsroot, 'export', '-r',
3.88      knu       579:            $tag, '-d', "$tmpexportdir/$basedir", $module)
3.80      knu       580:        {
3.103     knu       581:                @fatal = ("500 Internal Error",
                    582:                          'cvs co failure: %s: %s',
                    583:                          $!, $module);
3.80      knu       584:        } else {
                    585:                $| = 1;    # Essential to get the buffering right.
3.46      knu       586:
3.80      knu       587:                if ($ext eq '.tar.gz') {
3.89      knu       588:                        print "Content-Type: application/x-gzip\r\n\r\n";
3.46      knu       589:
3.80      knu       590:                        system
3.88      knu       591:                            "$CMD{tar} @tar_options -cf - -C $tmpexportdir $basedir | $CMD{gzip} @gzip_options -c"
3.80      knu       592:                            and @fatal =
3.103     knu       593:                                ("500 Internal Error",
                    594:                                 'tar zc failure: %s: %s',
                    595:                             $!, $basedir);
3.80      knu       596:                } elsif ($ext eq '.zip' && $CMD{zip}) {
3.89      knu       597:                        print "Content-Type: application/zip\r\n\r\n";
3.80      knu       598:
                    599:                        system
3.88      knu       600:                            "cd $tmpexportdir && $CMD{zip} @zip_options -r - $basedir"
3.80      knu       601:                            and @fatal =
3.103     knu       602:                                ("500 Internal Error",
                    603:                                 'zip failure: %s: %s',
                    604:                                 $!, $basedir);
3.80      knu       605:                } else {
                    606:                        @fatal =
3.103     knu       607:                            ("500 Internal Error",
                    608:                             'unsupported file type');
3.80      knu       609:                }
3.77      knu       610:        }
3.46      knu       611:
3.88      knu       612:        system $CMD{rm}, '-rf', $tmpexportdir if -d $tmpexportdir;
3.46      knu       613:
3.80      knu       614:        &fatal(@fatal) if @fatal;
3.46      knu       615:
3.80      knu       616:        exit;
3.46      knu       617: }
                    618:
3.1       knu       619: ##############################
                    620: # View a directory
                    621: ###############################
3.46      knu       622: if (-d $fullname) {
3.80      knu       623:        my $dh = do { local (*DH); };
3.103     knu       624:        opendir($dh, $fullname) or fatal("404 Not Found",
                    625:                                         '%s: %s',
                    626:                                         $where, $!);
3.1       knu       627:        my @dir = readdir($dh);
                    628:        closedir($dh);
                    629:        my @subLevelFiles = findLastModifiedSubdirs(@dir)
                    630:            if ($show_subdir_lastmod);
3.80      knu       631:        getDirLogs($cvsroot, $where, @subLevelFiles);
3.1       knu       632:
                    633:        if ($where eq '/') {
3.80      knu       634:                html_header($defaulttitle);
                    635:                $long_intro =~ s/!!CVSROOTdescr!!/$CVSROOTdescr{$cvstree}/g;
                    636:                print $long_intro;
                    637:        } else {
                    638:                html_header($where);
                    639:                print $short_instruction;
3.1       knu       640:        }
                    641:
3.107     scop      642:        if ($use_descriptions && open(DESC, "<$cvsroot/CVSROOT/descriptions"))
3.80      knu       643:        {
                    644:                while (<DESC>) {
                    645:                        chomp;
                    646:                        my ($dir, $description) = /(\S+)\s+(.*)/;
                    647:                        $descriptions{$dir} = $description;
                    648:                }
3.107     scop      649:                close(DESC);
3.1       knu       650:        }
                    651:
3.89      knu       652:        print "<p><a name=\"dirlist\"></a></p>\n";
3.80      knu       653:
3.1       knu       654:        # give direct access to dirs
                    655:        if ($where eq '/') {
3.80      knu       656:                chooseMirror ();
                    657:                chooseCVSRoot ();
                    658:        } else {
                    659:                print "<p>Current directory: <b>", &clickablePath($where, 0),
3.89      knu       660:                    "</b></p>\n";
3.1       knu       661:
3.89      knu       662:                print "<p>Current tag: <b>", $input{only_with_tag},"</b></p>\n"
3.80      knu       663:                    if $input{only_with_tag};
1.16      wosch     664:
1.1       jfieber   665:        }
3.12      knu       666:
3.89      knu       667:        print "<hr noshade>\n";
3.1       knu       668:
3.89      knu       669:        # Using <menu> in this manner violates the HTML2.0 spec but
1.1       jfieber   670:        # provides the results that I want in most browsers.  Another
                    671:        # case of layout spooging up HTML.
3.12      knu       672:
3.1       knu       673:        my $infocols = 0;
                    674:        if ($dirtable) {
3.100     knu       675:                print "<table style=\"border-width: 0";
                    676:                print "; background-color: $tableBorderColor"
                    677:                    if (defined $tableBorderColor);
                    678:                print "\" width=\"100%\" cellspacing=\"1\" cellpadding=\"$tablepadding\">\n";
3.1       knu       679:                $infocols++;
3.100     knu       680:                printf "<tr>\n<th style=\"text-align: left; background-color: %s\">",
3.80      knu       681:                    $byfile ? $columnHeaderColorSorted :
                    682:                    $columnHeaderColorDefault;
                    683:
                    684:                if ($byfile) {
                    685:                        print 'File';
3.35      knu       686:                } else {
3.80      knu       687:                        print &link(
                    688:                                'File',
                    689:                                sprintf(
                    690:                                        './%s#dirlist',
                    691:                                        &toggleQuery("sortby", "file")
                    692:                                )
                    693:                        );
3.35      knu       694:                }
3.100     knu       695:                print "</th>\n";
3.80      knu       696:
                    697:                # do not display the other column-headers, if we do not have any files
                    698:                # with revision information:
                    699:                if (scalar(%fileinfo)) {
                    700:                        $infocols++;
3.100     knu       701:                        printf '<th style="text-align: left; background-color: %s">',
3.80      knu       702:                            $byrev ? $columnHeaderColorSorted :
                    703:                            $columnHeaderColorDefault;
                    704:
                    705:                        if ($byrev) {
                    706:                                print 'Rev.';
                    707:                        } else {
                    708:                                print &link(
                    709:                                        'Rev.',
                    710:                                        sprintf(
                    711:                                                './%s#dirlist',
                    712:                                                &toggleQuery("sortby", "rev")
                    713:                                        )
                    714:                                );
                    715:                        }
3.100     knu       716:                        print "</th>\n";
3.80      knu       717:                        $infocols++;
3.100     knu       718:                        printf '<th style="text-align: left; background-color: %s">',
3.80      knu       719:                            $bydate ? $columnHeaderColorSorted :
                    720:                            $columnHeaderColorDefault;
                    721:
                    722:                        if ($bydate) {
                    723:                                print 'Age';
                    724:                        } else {
                    725:                                print &link(
                    726:                                        'Age',
                    727:                                        sprintf(
                    728:                                                './%s#dirlist',
                    729:                                                &toggleQuery("sortby", "date")
                    730:                                        )
                    731:                                );
                    732:                        }
3.100     knu       733:                        print "</th>\n";
3.80      knu       734:
                    735:                        if ($show_author) {
                    736:                                $infocols++;
3.100     knu       737:                                printf '<th style="text-align: left; background-color: %s">',
3.80      knu       738:                                    $byauthor ? $columnHeaderColorSorted :
                    739:                                    $columnHeaderColorDefault;
                    740:
                    741:                                if ($byauthor) {
                    742:                                        print 'Author';
                    743:                                } else {
                    744:                                        print &link(
                    745:                                                'Author',
                    746:                                                sprintf(
                    747:                                                        './%s#dirlist',
                    748:                                                        &toggleQuery(
                    749:                                                                "sortby",
                    750:                                                                "author"
                    751:                                                        )
                    752:                                                )
                    753:                                        );
                    754:                                }
3.100     knu       755:                                print "</th>\n";
3.80      knu       756:                        }
                    757:                        $infocols++;
3.100     knu       758:                        printf '<th style="text-align: left; background-color: %s">',
3.80      knu       759:                            $bylog ? $columnHeaderColorSorted :
                    760:                            $columnHeaderColorDefault;
                    761:
                    762:                        if ($bylog) {
                    763:                                print 'Last log entry';
                    764:                        } else {
                    765:                                print &link(
                    766:                                        'Last log entry',
                    767:                                        sprintf(
                    768:                                                './%s#dirlist',
                    769:                                                &toggleQuery("sortby", "log")
                    770:                                        )
                    771:                                );
                    772:                        }
3.100     knu       773:                        print "</th>\n";
3.80      knu       774:                } elsif ($use_descriptions) {
3.100     knu       775:                        printf '<th style="text-align: left; background-color: s">',
3.80      knu       776:                            $columnHeaderColorDefault;
3.100     knu       777:                        print "Description</th>\n";
3.80      knu       778:                        $infocols++;
3.1       knu       779:                }
3.80      knu       780:                print "</tr>\n";
                    781:        } else {
                    782:                print "<menu>\n";
3.1       knu       783:        }
                    784:        my $dirrow = 0;
3.12      knu       785:
3.1       knu       786:        my $i;
1.9       fenner    787:        lookingforattic:
3.80      knu       788:        for ($i = 0 ; $i <= $#dir ; $i++) {
1.9       fenner    789:                if ($dir[$i] eq "Attic") {
3.80      knu       790:                        last lookingforattic;
1.9       fenner    791:                }
                    792:        }
3.80      knu       793:
                    794:        if (!$input{'hideattic'} && ($i <= $#dir)
                    795:            && opendir($dh, $fullname . "/Attic"))
                    796:        {
                    797:                splice(@dir, $i, 1, grep((s|^|Attic/|, !m|/\.|), readdir($dh)));
                    798:                closedir($dh);
1.9       fenner    799:        }
3.1       knu       800:
3.80      knu       801:        my $hideAtticToggleLink =
                    802:            $input{'hideattic'} ? '' :
                    803:            &link('[Hide]', sprintf('./%s#dirlist', &toggleQuery("hideattic")));
3.1       knu       804:
1.9       fenner    805:        # Sort without the Attic/ pathname.
3.1       knu       806:        # place directories first
                    807:
                    808:        my $attic;
                    809:        my $url;
                    810:        my $fileurl;
                    811:        my $filesexists;
                    812:        my $filesfound;
                    813:
                    814:        foreach (sort { &fileSortCmp } @dir) {
3.80      knu       815:                if ($_ eq '.') {
                    816:                        next;
                    817:                }
                    818:
                    819:                # ignore CVS lock and stale NFS files
                    820:                next if (/^#cvs\.|^,|^\.nfs/);
                    821:
                    822:                # Check whether to show the CVSROOT path
                    823:                next if ($input{'hidecvsroot'} && ($_ eq 'CVSROOT'));
                    824:
                    825:                # Check whether the module is in the restricted list
                    826:                next if ($_ && &forbidden_module($_));
                    827:
                    828:                # Ignore non-readable files
                    829:                next if ($input{'hidenonreadable'} && !(-r "$fullname/$_"));
3.1       knu       830:
3.80      knu       831:                if (s|^Attic/||) {
                    832:                        $attic = " (in the Attic)&nbsp;" . $hideAtticToggleLink;
                    833:                } else {
                    834:                        $attic = "";
                    835:                }
3.1       knu       836:
3.80      knu       837:                if ($_ eq '..' || -d "$fullname/$_") {
                    838:                        next if ($_ eq '..' && $where eq '/');
                    839:                        my ($rev, $date, $log, $author, $filename);
                    840:                        ($rev, $date, $log, $author, $filename) =
                    841:                            @{$fileinfo{$_}}
                    842:                            if (defined($fileinfo{$_}));
3.100     knu       843:                        printf "<tr style=\"background-color: %s\">\n<td>",
                    844:                             $tabcolors[$dirrow % 2] if $dirtable;
3.80      knu       845:
                    846:                        if ($_ eq '..') {
                    847:                                $url = "../$query";
                    848:                                if ($nofilelinks) {
                    849:                                        print $backicon;
                    850:                                } else {
                    851:                                        print &link($backicon, $url);
                    852:                                }
3.100     knu       853:                                print '&nbsp;', &link("Parent Directory", $url);
3.80      knu       854:                        } else {
                    855:                                $url = './' . urlencode($_) . "/$query";
3.89      knu       856:                                print "<a name=\"$_\"></a>";
3.80      knu       857:
                    858:                                if ($nofilelinks) {
                    859:                                        print $diricon;
                    860:                                } else {
                    861:                                        print &link($diricon, $url);
                    862:                                }
3.100     knu       863:                                print '&nbsp;', &link("$_/", $url), $attic;
3.80      knu       864:
                    865:                                if ($_ eq "Attic") {
                    866:                                        print "&nbsp; ";
                    867:                                        print &link(
                    868:                                                "[Don't hide]",
                    869:                                                sprintf(
                    870:                                                        './%s#dirlist',
                    871:                                                        &toggleQuery(
                    872:                                                                "hideattic")
                    873:                                                )
                    874:                                        );
                    875:                                }
                    876:                        }
3.1       knu       877:
3.80      knu       878:                        # Show last change in dir
                    879:                        if ($filename) {
3.100     knu       880:                                print "</td>\n<td>&nbsp;</td>\n<td>&nbsp;"
3.80      knu       881:                                    if ($dirtable);
                    882:                                if ($date) {
                    883:                                        print " <i>",
                    884:                                            readableTime(time() - $date, 0),
                    885:                                            "</i>";
                    886:                                }
                    887:
                    888:                                if ($show_author) {
3.100     knu       889:                                        print "</td>\n<td>&nbsp;" if ($dirtable);
3.80      knu       890:                                        print $author;
                    891:                                }
3.100     knu       892:                                print "</td>\n<td>&nbsp;" if ($dirtable);
3.80      knu       893:                                $filename =~ s%^[^/]+/%%;
                    894:                                print "$filename/$rev";
3.89      knu       895:                                print "<br>" if ($dirtable);
3.80      knu       896:
                    897:                                if ($log) {
3.100     knu       898:                                        print "&nbsp;<span style=\"font-size: smaller\">",
3.89      knu       899:                                          &htmlify(
3.101     knu       900:                                                substr($log, 0, $shortLogLen), $allow_dir_extra);
3.80      knu       901:                                        if (length $log > 80) {
                    902:                                                print "...";
                    903:                                        }
3.100     knu       904:                                        print "</span>";
3.80      knu       905:                                }
                    906:                        } else {
                    907:                                my ($dwhere) =
                    908:                                    ($where ne "/" ? $where : "") . $_;
                    909:
                    910:                                if ($use_descriptions
                    911:                                    && defined $descriptions{$dwhere})
                    912:                                {
3.89      knu       913:                                        print "<td colspan=\"",($infocols - 1),
                    914:                                            "\">&nbsp;"
3.80      knu       915:                                            if $dirtable;
                    916:                                        print $descriptions{$dwhere};
                    917:                                } elsif ($dirtable && $infocols > 1) {
                    918:
                    919:                                        # close the row with the appropriate number of
                    920:                                        # columns, so that the vertical seperators are visible
                    921:                                        my ($cols) = $infocols;
                    922:                                        while ($cols > 1) {
3.100     knu       923:                                                print "</td>\n<td>&nbsp;";
3.80      knu       924:                                                $cols--;
                    925:                                        }
                    926:                                }
                    927:                        }
1.18      wosch     928:
3.80      knu       929:                        if ($dirtable) {
3.100     knu       930:                                print "</td>\n</tr>\n";
3.80      knu       931:                        } else {
                    932:                                print "<br>\n";
                    933:                        }
                    934:                        $dirrow++;
                    935:                } elsif (s/,v$//) {
                    936:                        $fileurl = ($attic ? "Attic/" : "") . urlencode($_);
                    937:                        $url = './' . $fileurl . $query;
                    938:                        my $rev    = '';
                    939:                        my $date   = '';
                    940:                        my $log    = '';
                    941:                        my $author = '';
                    942:                        $filesexists++;
                    943:                        next if (!defined($fileinfo{$_}));
                    944:                        ($rev, $date, $log, $author) = @{$fileinfo{$_}};
                    945:                        $filesfound++;
3.100     knu       946:                        printf "<tr style=\"background-color: %s\">\n<td>",
                    947:                            $tabcolors[$dirrow % 2] if $dirtable;
3.89      knu       948:                        print "<a name=\"$_\"></a>";
3.80      knu       949:
                    950:                        if ($nofilelinks) {
                    951:                                print $fileicon;
                    952:                        } else {
                    953:                                print &link($fileicon, $url);
                    954:                        }
3.100     knu       955:                        print '&nbsp;', &link($_, $url), $attic;
                    956:                        print "</td>\n<td>&nbsp;" if ($dirtable);
3.80      knu       957:                        download_link($fileurl, $rev, $rev,
                    958:                                $defaultViewable ? "text/x-cvsweb-markup" :
                    959:                                undef);
3.100     knu       960:                        print "</td>\n<td>&nbsp;" if ($dirtable);
3.1       knu       961:
3.80      knu       962:                        if ($date) {
                    963:                                print " <i>", readableTime(time() - $date, 0),
                    964:                                    "</i>";
                    965:                        }
                    966:                        if ($show_author) {
3.100     knu       967:                                print "</td>\n<td>&nbsp;" if ($dirtable);
3.80      knu       968:                                print $author;
                    969:                        }
3.100     knu       970:                        print "</td>\n<td>&nbsp;" if ($dirtable);
3.80      knu       971:
                    972:                        if ($log) {
3.100     knu       973:                                print " <span style=\"font-size: smaller\">",
3.101     knu       974:                                    &htmlify(substr($log, 0, $shortLogLen), $allow_dir_extra);
3.80      knu       975:                                if (length $log > 80) {
                    976:                                        print "...";
                    977:                                }
3.100     knu       978:                                print "</span>";
3.80      knu       979:                        }
3.100     knu       980:                        print "</td>\n" if ($dirtable);
3.80      knu       981:                        print(($dirtable) ? "</tr>" : "<br>");
                    982:                        $dirrow++;
3.1       knu       983:                }
3.80      knu       984:                print "\n";
3.1       knu       985:        }
3.80      knu       986:
3.89      knu       987:        print($dirtable ? "</table>\n" : "</menu>\n");
                    988:
3.1       knu       989:        if ($filesexists && !$filesfound) {
3.80      knu       990:                print
3.89      knu       991:                    "<p><b>NOTE:</b> There are $filesexists files, but none matches the current tag ($input{only_with_tag}).</p>\n";
3.1       knu       992:        }
3.80      knu       993:        if ($input{only_with_tag} && (!%tags || !$tags{$input{only_with_tag}}))
                    994:        {
                    995:                %tags = %alltags
3.1       knu       996:        }
3.80      knu       997:
                    998:        if (scalar %tags || $input{only_with_tag} || $edit_option_form
                    999:            || defined($input{"options"}))
                   1000:        {
3.100     knu      1001:                print "<hr size=\"1\" noshade>\n";
3.1       knu      1002:        }
                   1003:
                   1004:        if (scalar %tags || $input{only_with_tag}) {
3.89      knu      1005:                print "<form method=\"get\" action=\"./\">\n";
3.80      knu      1006:                foreach my $var (@stickyvars) {
                   1007:                        print
3.89      knu      1008:                            "<input type=\"hidden\" name=\"$var\" value=\"$input{$var}\">\n"
3.80      knu      1009:                            if (defined($input{$var})
                   1010:                            && (!defined($DEFAULTVALUE{$var})
3.23      knu      1011:                            || $input{$var} ne $DEFAULTVALUE{$var})
3.80      knu      1012:                            && $input{$var} ne "" && $var ne "only_with_tag");
                   1013:                }
3.110     scop     1014:                print "<p><label for=\"only_with_tag\" accesskey=\"T\">";
                   1015:                print "Show only files with tag:</label>\n";
3.111     scop     1016:                print "<select id=\"only_with_tag\" name=\"only_with_tag\"";
3.89      knu      1017:                print " onchange=\"this.form.submit()\"" if $use_java_script;
3.80      knu      1018:                print ">";
3.89      knu      1019:                print "<option value=\"\">All tags / default branch</option>\n";
3.80      knu      1020:
                   1021:                foreach my $tag (reverse sort { lc $a cmp lc $b } keys %tags) {
3.89      knu      1022:                        print "<option",
3.80      knu      1023:                            defined($input{only_with_tag})
3.89      knu      1024:                            && $input{only_with_tag} eq $tag ? " selected" : "",
                   1025:                            ">$tag</option>\n";
3.80      knu      1026:                }
3.89      knu      1027:                print "</select>\n";
3.110     scop     1028:                print " <label for=\"path\" accesskey=\"P\">";
                   1029:                print "Module path or alias:</label>\n";
3.111     scop     1030:                printf "<input type=\"text\" id=\"path\" name=\"path\" value=\"%s\" size=\"15\">\n",
3.80      knu      1031:                    htmlquote($where);
3.110     scop     1032:                print "<input type=\"submit\" value=\"Go\" accesskey=\"G\"></p>\n";
3.89      knu      1033:                print "</form>\n";
1.9       fenner   1034:        }
3.46      knu      1035:
                   1036:        if ($allow_tar) {
3.80      knu      1037:                my ($basefile) = ($where =~ m,(?:.*/)?([^/]+),);
3.46      knu      1038:
3.80      knu      1039:                if (defined($basefile) && $basefile ne '') {
3.89      knu      1040:                        print "<hr noshade>\n",
                   1041:                            "<div align=\"center\">Download this directory in ";
3.80      knu      1042:
                   1043:                        # Mangle the filename so browsers show a reasonable
                   1044:                        # filename to download.
                   1045:                        print &link("tarball", "./$basefile.tar.gz$query"
                   1046:                                . ($query ? "&" : "?") . "tarball=1");
                   1047:                        if ($CMD{zip}) {
                   1048:                                print " or ",
                   1049:                                    &link("zip archive", "./$basefile.zip$query"
                   1050:                                        . ($query ? "&" : "?") . "tarball=1");
                   1051:                        }
3.100     knu      1052:                        print "</div>\n";
3.77      knu      1053:                }
3.46      knu      1054:        }
                   1055:
3.100     knu      1056:        if ($edit_option_form || defined($input{"options"})) {
                   1057:
                   1058:                my $formwhere = $scriptwhere;
                   1059:                $formwhere =~ s|Attic/?$|| if ($input{'hideattic'});
3.1       knu      1060:
3.89      knu      1061:                print "<form method=\"get\" action=\"${formwhere}\">\n";
                   1062:                print "<input type=\"hidden\" name=\"copt\" value=\"1\">\n";
3.80      knu      1063:                if ($cvstree ne $cvstreedefault) {
                   1064:                        print
3.89      knu      1065:                            "<input type=\"hidden\" name=\"cvsroot\" value=\"$cvstree\">\n";
3.80      knu      1066:                }
3.100     knu      1067:                print "<center>\n<table cellpadding=\"0\" cellspacing=\"0\">";
                   1068:                print "\n<tr style=\"background-color: $columnHeaderColorDefault\">\n";
                   1069:                print "<th colspan=\"2\">Preferences</th>\n</tr>\n";
3.110     scop     1070:                print "<tr>\n<td>";
                   1071:                print "<label for=\"sortby\" accesskey=\"F\">Sort files by ";
3.111     scop     1072:                print "</label><select id=\"sortby\" name=\"sortby\">\n";
3.100     knu      1073:                print "<option value=\"\">File</option>\n";
3.89      knu      1074:                print "<option", $bydate ? " selected" : "",
3.100     knu      1075:                    " value=\"date\">Age</option>\n";
3.89      knu      1076:                print "<option", $byauthor ? " selected" : "",
3.100     knu      1077:                    " value=\"author\">Author</option>\n"
3.80      knu      1078:                    if ($show_author);
3.89      knu      1079:                print "<option", $byrev ? " selected" : "",
3.100     knu      1080:                    " value=\"rev\">Revision</option>\n";
3.89      knu      1081:                print "<option", $bylog ? " selected" : "",
3.100     knu      1082:                    " value=\"log\">Log message</option>\n";
                   1083:                print "</select>\n</td>\n";
3.110     scop     1084:                print "<td><label for=\"logsort\" accesskey=\"L\">";
                   1085:                print "Sort log by: </label>";
3.80      knu      1086:                printLogSortSelect(0);
3.100     knu      1087:                print "</td>\n</tr>\n";
3.110     scop     1088:                print "<tr>\n<td><label for=\"f\" accesskey=\"D\">";
                   1089:                print "Diff format: </label>";
3.80      knu      1090:                printDiffSelect(0);
3.100     knu      1091:                print "</td>\n";
3.110     scop     1092:                print "<td><label for=\"hideattic\" accesskey=\"A\">";
                   1093:                print "Show Attic files: </label>";
3.111     scop     1094:                print "<input id=\"hideattic\" name=\"hideattic\" type=\"checkbox\"",
3.100     knu      1095:                    $input{'hideattic'} ? " checked" : "",
3.110     scop     1096:                     "></td>\n</tr>\n";
3.100     knu      1097:                print "<tr>\n<td align=\"center\" colspan=\"2\">";
3.110     scop     1098:                print "<input type=\"submit\" value=\"Change Options\" accesskey=\"C\">";
3.100     knu      1099:                print "</td>\n</tr>\n</table>\n</center>\n</form>\n";
1.12      fenner   1100:        }
3.100     knu      1101:        html_footer();
3.80      knu      1102: }
3.1       knu      1103:
                   1104: ###############################
                   1105: # View Files
                   1106: ###############################
3.80      knu      1107: elsif (-f $fullname . ',v') {
3.1       knu      1108:        if (defined($input{'rev'}) || $doCheckout) {
3.80      knu      1109:                &doCheckout($fullname, $input{'rev'});
                   1110:                gzipclose();
                   1111:                exit;
3.1       knu      1112:        }
3.80      knu      1113:
3.1       knu      1114:        if (defined($input{'annotate'}) && $allow_annotate) {
3.80      knu      1115:                &doAnnotate($input{'annotate'});
                   1116:                gzipclose();
                   1117:                exit;
3.1       knu      1118:        }
3.80      knu      1119:
3.1       knu      1120:        if (defined($input{'r1'}) && defined($input{'r2'})) {
3.80      knu      1121:                &doDiff(
                   1122:                        $fullname,     $input{'r1'},
                   1123:                        $input{'tr1'}, $input{'r2'},
                   1124:                        $input{'tr2'}, $input{'f'}
                   1125:                );
                   1126:                gzipclose();
                   1127:                exit;
3.1       knu      1128:        }
                   1129:        print("going to dolog($fullname)\n") if ($verbose);
                   1130:        &doLog($fullname);
3.80      knu      1131:
                   1132:        ##############################
                   1133:        # View Diff
                   1134:        ##############################
                   1135: } elsif ($fullname =~ s/\.diff$// && -f $fullname . ",v" && $input{'r1'}
                   1136:     && $input{'r2'})
                   1137: {
3.1       knu      1138:
                   1139:        # $where-diff-removal if 'cvs rdiff' is used
                   1140:        # .. but 'cvs rdiff'doesn't support some options
                   1141:        # rcsdiff does (-w and -p), so it is disabled
                   1142:        # $where =~ s/\.diff$//;
                   1143:
1.12      fenner   1144:        # Allow diffs using the ".diff" extension
                   1145:        # so that browsers that default to the URL
                   1146:        # for a save filename don't save diff's as
                   1147:        # e.g. foo.c
3.80      knu      1148:        &doDiff(
                   1149:                $fullname,     $input{'r1'}, $input{'tr1'}, $input{'r2'},
                   1150:                $input{'tr2'}, $input{'f'}
                   1151:        );
3.25      knu      1152:        gzipclose();
1.12      fenner   1153:        exit;
3.80      knu      1154: } elsif (($newname = $fullname) =~ s|/([^/]+)$|/Attic/$1| && -f $newname . ",v")
                   1155: {
                   1156:
1.12      fenner   1157:        # The file has been removed and is in the Attic.
                   1158:        # Send a redirect pointing to the file in the Attic.
3.1       knu      1159:        (my $newplace = $scriptwhere) =~ s|/([^/]+)$|/Attic/$1|;
3.83      knu      1160:        if ($ENV{QUERY_STRING} ne "") {
                   1161:                redirect("${newplace}?$ENV{QUERY_STRING}");
                   1162:        } else {
                   1163:                redirect($newplace);
                   1164:        }
1.12      fenner   1165:        exit;
3.80      knu      1166: } elsif (0 && (my @files = &safeglob($fullname . ",v"))) {
3.1       knu      1167:        http_header("text/plain");
1.12      fenner   1168:        print "You matched the following files:\n";
3.80      knu      1169:        print join ("\n", @files);
                   1170:
1.12      fenner   1171:        # Find the tags from each file
                   1172:        # Display a form offering diffs between said tags
3.80      knu      1173: } else {
                   1174:        my $fh = do { local (*FH); };
3.1       knu      1175:        my ($xtra, $module);
3.80      knu      1176:
1.12      fenner   1177:        # Assume it's a module name with a potential path following it.
3.44      knu      1178:        $xtra = (($module = $where) =~ s|/.*||) ? $& : '';
3.80      knu      1179:
1.12      fenner   1180:        # Is there an indexed version of modules?
3.60      knu      1181:        if (open($fh, "< $cvsroot/CVSROOT/modules")) {
3.80      knu      1182:                while (<$fh>) {
                   1183:                        if (/^(\S+)\s+(\S+)/o && $module eq $1
                   1184:                            && -d "$cvsroot/$2" && $module ne $2)
                   1185:                        {
                   1186:                                redirect("$scriptname/$2$xtra$query");
                   1187:                        }
1.12      fenner   1188:                }
                   1189:        }
3.103     knu      1190:        fatal("404 Not Found",
                   1191:              '%s: no such file or directory',
                   1192:              $where);
3.80      knu      1193: }
3.23      knu      1194:
3.25      knu      1195: gzipclose();
3.80      knu      1196:
3.1       knu      1197: ## End MAIN
                   1198:
3.12      knu      1199: sub printDiffSelect($) {
3.80      knu      1200:        my ($use_java_script) = @_;
                   1201:        my $f = $input{'f'};
3.37      knu      1202:
3.111     scop     1203:        print '<select id="f" name="f"';
3.89      knu      1204:        print ' onchange="this.form.submit()"' if $use_java_script;
3.100     knu      1205:        print ">\n";
3.80      knu      1206:
                   1207:        local $_;
                   1208:        for (@DIFFTYPES) {
3.100     knu      1209:                printf("<option value=\"%s\"%s>%s</option>\n", $_,
3.89      knu      1210:                    $f eq $_ ? ' selected' : '', "\u$DIFFTYPES{$_}{'descr'}");
3.80      knu      1211:        }
3.37      knu      1212:
3.89      knu      1213:        print "</select>";
3.37      knu      1214: }
                   1215:
                   1216: sub printLogSortSelect($) {
3.80      knu      1217:        my ($use_java_script) = @_;
3.37      knu      1218:
3.111     scop     1219:        print '<select id="logsort" name="logsort"';
3.89      knu      1220:        print ' onchange="this.form.submit()"' if $use_java_script;
3.100     knu      1221:        print ">\n";
3.37      knu      1222:
3.80      knu      1223:        local $_;
                   1224:        for (@LOGSORTKEYS) {
3.100     knu      1225:                printf("<option value=\"%s\"%s>%s</option>\n", $_,
3.89      knu      1226:                    $logsort eq $_ ? ' selected' : '',
3.80      knu      1227:                    "\u$LOGSORTKEYS{$_}{'descr'}");
                   1228:        }
                   1229:
3.89      knu      1230:        print "</select>";
3.1       knu      1231: }
                   1232:
3.12      knu      1233: sub findLastModifiedSubdirs(@) {
3.80      knu      1234:        my (@dirs) = @_;
                   1235:        my ($dirname, @files);
3.1       knu      1236:
3.80      knu      1237:        foreach $dirname (@dirs) {
                   1238:                next if ($dirname eq ".");
                   1239:                next if ($dirname eq "..");
                   1240:                my ($dir) = "$fullname/$dirname";
                   1241:                next if (!-d $dir);
                   1242:
                   1243:                my ($lastmod)     = undef;
                   1244:                my ($lastmodtime) = undef;
                   1245:                my $dh = do { local (*DH); };
                   1246:
3.95      knu      1247:                opendir($dh, $dir) or next;
3.80      knu      1248:                my (@filenames) = readdir($dh);
                   1249:                closedir($dh);
                   1250:
                   1251:                foreach my $filename (@filenames) {
                   1252:                        $filename = "$dirname/$filename";
                   1253:                        my ($file) = "$fullname/$filename";
                   1254:                        next if ($filename !~ /,v$/ || !-f $file);
                   1255:                        $filename =~ s/,v$//;
                   1256:                        my $modtime = -M $file;
                   1257:
                   1258:                        if (!defined($lastmod) || $modtime < $lastmodtime) {
                   1259:                                $lastmod     = $filename;
                   1260:                                $lastmodtime = $modtime;
                   1261:                        }
                   1262:                }
                   1263:                push (@files, $lastmod) if (defined($lastmod));
3.1       knu      1264:        }
3.80      knu      1265:        return @files;
1.12      fenner   1266: }
                   1267:
3.36      knu      1268: sub htmlify_sub(&$) {
3.80      knu      1269:        (my $proc, local $_) = @_;
                   1270:        my @a = split (m`(<a [^>]+>[^<]*</a>)`i);
                   1271:        my $linked;
                   1272:        my $result = '';
                   1273:
                   1274:        while (($_, $linked) = splice(@a, 0, 2)) {
                   1275:                &$proc();
                   1276:                $result .= $_      if defined($_);
                   1277:                $result .= $linked if defined($linked);
                   1278:        }
1.12      fenner   1279:
3.80      knu      1280:        $result;
3.36      knu      1281: }
1.12      fenner   1282:
3.36      knu      1283: sub htmlify($;$) {
3.80      knu      1284:        (local $_, my $extra) = @_;
3.1       knu      1285:
3.80      knu      1286:        $_ = htmlquote($_);
3.17      knu      1287:
3.80      knu      1288:        # get URL's as link
                   1289:        s{
3.36      knu      1290:       (http|ftp|https)://\S+
                   1291:      }{
                   1292:         &link($&, htmlunquote($&))
                   1293:      }egx;
                   1294:
3.80      knu      1295:        # get e-mails as link
                   1296:        $_ = htmlify_sub {
                   1297:                s<
3.36      knu      1298:          [\w+=\-.!]+@[\w\-]+(\.[\w\-]+)+
                   1299:            ><
                   1300:              &link($&, "mailto:$&")
                   1301:                >egix;
3.80      knu      1302:            }
                   1303:            $_;
3.36      knu      1304:
3.80      knu      1305:        if ($extra) {
3.36      knu      1306:
3.80      knu      1307:                # get PR #'s as link: "PR#nnnn" "PR: nnnn, ..." "PR nnnn, ..." "bin/nnnn"
3.101     knu      1308:                if (defined($prcgi) && defined($re_prkeyword))
3.80      knu      1309:                {
                   1310:                        my $prev;
3.36      knu      1311:
3.80      knu      1312:                        do {
                   1313:                                $prev = $_;
                   1314:
                   1315:                                $_ = htmlify_sub {
                   1316:                                        s{
3.54      knu      1317:                      (\b$re_prkeyword[:\#]?\s*
3.36      knu      1318:                       (?:
                   1319:                        \#?
                   1320:                        \d+[,\s]\s*
                   1321:                       )*
                   1322:                       \#?)
                   1323:                      (\d+)\b
                   1324:                     }{
3.42      knu      1325:                         $1 . &link($2, sprintf($prcgi, $2))
3.36      knu      1326:                     }egix;
3.80      knu      1327:                                    }
                   1328:                                    $_;
                   1329:                        } while ($_ ne $prev);
3.36      knu      1330:
3.101     knu      1331:                        if (defined($re_prcategories)) {
                   1332:                                $_ = htmlify_sub {
                   1333:                                        s{
                   1334:                          (\b$re_prcategories/(\d+)\b)
                   1335:                        }{
                   1336:                                &link($1, sprintf($prcgi, $2))
                   1337:                        }egox;
                   1338:                                    }
                   1339:                                    $_;
                   1340:                        }
3.80      knu      1341:                }
3.36      knu      1342:
3.80      knu      1343:                # get manpage specs as link: "foo.1" "foo(1)"
                   1344:                if (defined($mancgi)) {
                   1345:                        $_ = htmlify_sub {
                   1346:                                s{
3.53      knu      1347:                  (\b([a-zA-Z][\w.]+)
3.36      knu      1348:                   (?:
                   1349:                    \( ([0-9n]) \)\B
                   1350:                    |
                   1351:                    \.([0-9n])\b
                   1352:                   )
                   1353:                  )
                   1354:                 }{
3.53      knu      1355:                     &link($1, sprintf($mancgi, defined($3) ? $3 : $4, $2))
3.36      knu      1356:                 }egx;
3.80      knu      1357:                            }
                   1358:                            $_;
                   1359:                }
3.1       knu      1360:        }
                   1361:
3.80      knu      1362:        $_;
3.1       knu      1363: }
                   1364:
3.20      knu      1365: sub spacedHtmlText($;$) {
3.12      knu      1366:        local $_ = $_[0];
3.20      knu      1367:        my $ts = $_[1] || $tabstop;
3.1       knu      1368:
3.20      knu      1369:        # Cut trailing spaces and tabs
                   1370:        s/[ \t]+$//;
3.1       knu      1371:
3.80      knu      1372:        if (defined($ts)) {
                   1373:
                   1374:                # Expand tabs
                   1375:                1 while s/\t+/' ' x (length($&) * $ts - length($`) % $ts)/e
3.20      knu      1376:        }
3.1       knu      1377:
                   1378:        # replace <tab> and <space> (\001 is to protect us from htmlify)
                   1379:        # gzip can make excellent use of this repeating pattern :-)
                   1380:        if ($hr_breakable) {
3.80      knu      1381:
                   1382:                # make every other space 'breakable'
                   1383:                s/  / \001nbsp;/g;    # 2 * <space>
                   1384:                                      # leave single space as it is
3.20      knu      1385:        } else {
3.80      knu      1386:                s/ /\001nbsp;/g;
1.12      fenner   1387:        }
                   1388:
3.101     knu      1389:        $_ = htmlify($_, $allow_source_extra);
3.1       knu      1390:
                   1391:        # unescape
3.20      knu      1392:        y/\001/&/;
3.1       knu      1393:
3.12      knu      1394:        return $_;
1.12      fenner   1395: }
                   1396:
3.12      knu      1397: sub link($$) {
3.80      knu      1398:        my ($name, $url) = @_;
1.12      fenner   1399:
3.80      knu      1400:        $url =~ s/:/sprintf("%%%02x", ord($&))/eg
                   1401:            if $url =~ /^[^a-z]/;    # relative
3.67      knu      1402:
3.89      knu      1403:        sprintf '<a href="%s">%s</a>', hrefquote($url), $name;
1.12      fenner   1404: }
                   1405:
3.12      knu      1406: sub revcmp($$) {
3.80      knu      1407:        my ($rev1, $rev2) = @_;
3.15      knu      1408:
                   1409:        # make no comparison for a tag or a branch
                   1410:        return 0 if $rev1 =~ /[^\d.]/ || $rev2 =~ /[^\d.]/;
                   1411:
3.80      knu      1412:        my (@r1) = split (/\./, $rev1);
                   1413:        my (@r2) = split (/\./, $rev2);
                   1414:        my ($a, $b);
                   1415:
                   1416:        while (($a = shift (@r1)) && ($b = shift (@r2))) {
                   1417:                if ($a != $b) {
                   1418:                        return $a <=> $b;
                   1419:                }
1.12      fenner   1420:        }
                   1421:        if (@r1) { return 1; }
                   1422:        if (@r2) { return -1; }
                   1423:        return 0;
                   1424: }
                   1425:
3.103     knu      1426: sub fatal($$@) {
                   1427:        my ($errcode, $format, @args) = @_;
3.3       knu      1428:        if ($is_mod_perl) {
3.80      knu      1429:                Apache->request->status((split (/ /, $errcode))[0]);
                   1430:        } else {
3.4       knu      1431:                print "Status: $errcode\r\n";
3.1       knu      1432:        }
                   1433:        html_header("Error");
3.103     knu      1434:        print "<p>Error: ",
                   1435:          sprintf($format, map(htmlquote($_), @args)),
                   1436:          "</p>\n";
3.100     knu      1437:        html_footer();
1.12      fenner   1438:        exit(1);
                   1439: }
                   1440:
3.12      knu      1441: sub redirect($) {
3.80      knu      1442:        my ($url) = @_;
3.3       knu      1443:        if ($is_mod_perl) {
3.1       knu      1444:                Apache->request->status(301);
                   1445:                Apache->request->header_out(Location => $url);
3.80      knu      1446:        } else {
3.4       knu      1447:                print "Status: 301 Moved\r\n";
                   1448:                print "Location: $url\r\n";
3.1       knu      1449:        }
                   1450:        html_header("Moved");
3.100     knu      1451:        print "<p>This document is located ", &link('here', $url), "</p>\n";
                   1452:        html_footer();
1.12      fenner   1453:        exit(1);
                   1454: }
                   1455:
3.12      knu      1456: sub safeglob($) {
3.1       knu      1457:        my ($filename) = @_;
                   1458:        my ($dirname);
                   1459:        my (@results);
3.80      knu      1460:        my $dh = do { local (*DH); };
1.12      fenner   1461:
                   1462:        ($dirname = $filename) =~ s|/[^/]+$||;
                   1463:        $filename =~ s|.*/||;
                   1464:
3.1       knu      1465:        if (opendir($dh, $dirname)) {
                   1466:                my $glob = $filename;
                   1467:                my $t;
3.80      knu      1468:
                   1469:                #       transform filename from glob to regex.  Deal with:
                   1470:                #       [, {, ?, * as glob chars
                   1471:                #       make sure to escape all other regex chars
1.12      fenner   1472:                $glob =~ s/([\.\(\)\|\+])/\\$1/g;
                   1473:                $glob =~ s/\*/.*/g;
                   1474:                $glob =~ s/\?/./g;
                   1475:                $glob =~ s/{([^}]+)}/($t = $1) =~ s-,-|-g; "($t)"/eg;
3.1       knu      1476:                foreach (readdir($dh)) {
3.80      knu      1477:
1.12      fenner   1478:                        if (/^${glob}$/) {
3.80      knu      1479:                                push (@results, "$dirname/" . $_);
1.12      fenner   1480:                        }
                   1481:                }
3.80      knu      1482:                closedir($dh);
1.12      fenner   1483:        }
                   1484:
                   1485:        @results;
                   1486: }
                   1487:
3.58      knu      1488: sub search_path($) {
3.80      knu      1489:        my ($command) = @_;
                   1490:        my $d;
3.58      knu      1491:
3.80      knu      1492:        for $d (split (/:/, $command_path)) {
                   1493:                return "$d/$command" if -x "$d/$command";
                   1494:        }
3.58      knu      1495:
3.80      knu      1496:        '';
3.58      knu      1497: }
                   1498:
3.12      knu      1499: sub getMimeTypeFromSuffix($) {
3.80      knu      1500:        my ($fullname) = @_;
                   1501:        my ($mimetype, $suffix);
                   1502:        my $fh = do { local (*FH); };
                   1503:
                   1504:        ($suffix = $fullname) =~ s/^.*\.([^.]*)$/$1/;
                   1505:        $mimetype = $MTYPES{$suffix};
                   1506:        $mimetype = $MTYPES{'*'} if (!$mimetype);
                   1507:
                   1508:        if (!$mimetype && -f $mime_types) {
                   1509:
                   1510:                # okey, this is something special - search the
                   1511:                # mime.types database
                   1512:                open($fh, "<$mime_types");
                   1513:                while (<$fh>) {
                   1514:                        if ($_ =~ /^\s*(\S+\/\S+).*\b$suffix\b/) {
                   1515:                                $mimetype = $1;
                   1516:                                last;
                   1517:                        }
                   1518:                }
                   1519:                close($fh);
3.1       knu      1520:        }
3.12      knu      1521:
3.80      knu      1522:        # okey, didn't find anything useful ..
                   1523:        if (!($mimetype =~ /\S\/\S/)) {
                   1524:                $mimetype = "text/plain";
                   1525:        }
                   1526:        return $mimetype;
3.1       knu      1527: }
                   1528:
                   1529: ###############################
3.24      knu      1530: # read first lines like head(1)
                   1531: ###############################
                   1532: sub head($;$) {
3.80      knu      1533:        my $fh        = $_[0];
                   1534:        my $linecount = $_[1] || 10;
3.24      knu      1535:
3.80      knu      1536:        my @buf;
3.24      knu      1537:
3.80      knu      1538:        if ($linecount > 0) {
                   1539:                my $i;
                   1540:                for ($i = 0 ; !eof($fh) && $i < $linecount ; $i++) {
                   1541:                        push @buf, scalar <$fh>;
                   1542:                }
                   1543:        } else {
                   1544:                @buf = <$fh>;
3.24      knu      1545:        }
                   1546:
3.80      knu      1547:        @buf;
3.24      knu      1548: }
                   1549:
                   1550: ###############################
                   1551: # scan vim and Emacs directives
                   1552: ###############################
                   1553: sub scan_directives(@) {
3.80      knu      1554:        my $ts = undef;
3.24      knu      1555:
3.80      knu      1556:        for (@_) {
                   1557:                $ts = $1 if /\b(?:ts|tabstop|tab-width)[:=]\s*([1-9]\d*)\b/;
                   1558:        }
3.24      knu      1559:
3.80      knu      1560:        ('tabstop' => $ts);
3.24      knu      1561: }
                   1562:
3.86      knu      1563: sub openOutputFilter() {
                   1564:        return if !defined($output_filter) || $output_filter eq '';
                   1565:
3.95      knu      1566:        open(STDOUT, "|-") and return;
3.86      knu      1567:
                   1568:        # child of child
                   1569:        open(STDERR, '>/dev/null');
3.95      knu      1570:        exec($output_filter) or exit -1;
3.86      knu      1571: }
                   1572:
3.24      knu      1573: ###############################
3.1       knu      1574: # show Annotation
                   1575: ###############################
3.12      knu      1576: sub doAnnotate($$) {
3.80      knu      1577:        my ($rev) = @_;
                   1578:        my ($pid);
                   1579:        my ($pathname, $filename);
                   1580:        my $reader = do { local (*FH); };
                   1581:        my $writer = do { local (*FH); };
                   1582:
                   1583:        # make sure the revisions are wellformed, for security
                   1584:        # reasons ..
                   1585:        if ($rev =~ /[^\w.]/) {
3.103     knu      1586:                fatal("404 Not Found",
                   1587:                      'Malformed query "%s"',
                   1588:                      $ENV{QUERY_STRING});
3.80      knu      1589:        }
                   1590:
3.81      knu      1591:        if (&forbidden_file($fullname)) {
3.103     knu      1592:                fatal("403 Forbidden",
                   1593:                      'Access forbidden.  This file is mentioned in @ForbiddenFiles');
3.81      knu      1594:                return;
                   1595:        }
                   1596:
3.80      knu      1597:        ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1598:        ($filename = $where) =~ s/^.*\///;
                   1599:
                   1600:        # this seems to be necessary
                   1601:        $| = 1;
                   1602:        $| = 0;    # Flush
                   1603:
                   1604:        # this annotate version is based on the
                   1605:        # cvs annotate-demo Perl script by Cyclic Software
                   1606:        # It was written by Cyclic Software, http://www.cyclic.com/, and is in
                   1607:        # the public domain.
                   1608:        # we could abandon the use of rlog, rcsdiff and co using
                   1609:        # the cvsserver in a similiar way one day (..after rewrite)
                   1610:        $pid = open2($reader, $writer, $CMD{cvs}, @cvs_options, "server")
3.95      knu      1611:            or fatal("500 Internal Error",
3.103     knu      1612:                     'Fatal Error - unable to open cvs for annotation');
3.80      knu      1613:
                   1614:        # OK, first send the request to the server.  A simplified example is:
                   1615:        #     Root /home/kingdon/zwork/cvsroot
                   1616:        #     Argument foo/xx
                   1617:        #     Directory foo
                   1618:        #     /home/kingdon/zwork/cvsroot/foo
                   1619:        #     Directory .
                   1620:        #     /home/kingdon/zwork/cvsroot
                   1621:        #     annotate
                   1622:        # although as you can see there are a few more details.
                   1623:
                   1624:        print $writer "Root $cvsroot\n";
                   1625:        print $writer
                   1626:            "Valid-responses ok error Valid-requests Checked-in Updated Merged Removed M E\n";
                   1627:
                   1628:        # Don't worry about sending valid-requests, the server just needs to
                   1629:        # support "annotate" and if it doesn't, there isn't anything to be done.
                   1630:        print $writer "UseUnchanged\n";
                   1631:        print $writer "Argument -r\n";
                   1632:        print $writer "Argument $rev\n";
                   1633:        print $writer "Argument $where\n";
                   1634:
                   1635:        # The protocol requires us to fully fake a working directory (at
                   1636:        # least to the point of including the directories down to the one
                   1637:        # containing the file in question).
                   1638:        # So if $where is "dir/sdir/file", then @dirs will be ("dir","sdir","file")
                   1639:        my @dirs = split ('/', $where);
                   1640:        my $path = "";
                   1641:        foreach (@dirs) {
                   1642:
                   1643:                if ($path eq "") {
                   1644:
                   1645:                        # In our example, $_ is "dir".
                   1646:                        $path = $_;
                   1647:                } else {
                   1648:                        print $writer "Directory $path\n";
                   1649:                        print $writer "$cvsroot/$path\n";
                   1650:
                   1651:                        # In our example, $_ is "sdir" and $path becomes "dir/sdir"
                   1652:                        # And the next time, "file" and "dir/sdir/file" (which then gets
                   1653:                        # ignored, because we don't need to send Directory for the file).
                   1654:                        $path .= "/$_";
                   1655:                }
                   1656:        }
                   1657:
                   1658:        # And the last "Directory" before "annotate" is the top level.
                   1659:        print $writer "Directory .\n";
                   1660:        print $writer "$cvsroot\n";
                   1661:
                   1662:        print $writer "annotate\n";
                   1663:
                   1664:        # OK, we've sent our command to the server.  Thing to do is to
                   1665:        # close the writer side and get all the responses.  If "cvs server"
                   1666:        # were nicer about buffering, then we could just leave it open, I think.
3.95      knu      1667:        close($writer) or die "cannot close: $!";
3.80      knu      1668:
                   1669:        http_header();
                   1670:
                   1671:        navigateHeader($scriptwhere, $pathname, $filename, $rev, "annotate");
                   1672:        print
3.100     knu      1673:            "<h3 style=\"text-align: center\">Annotation of $pathname$filename, Revision $rev</h3>\n";
3.80      knu      1674:
                   1675:        # Ready to get the responses from the server.
                   1676:        # For example:
                   1677:        #     E Annotations for foo/xx
                   1678:        #     E ***************
                   1679:        #     M 1.3          (kingdon  06-Sep-97): hello
                   1680:        #     ok
                   1681:        my ($lineNr) = 0;
                   1682:        my ($oldLrev, $oldLusr) = ("", "");
                   1683:        my ($revprint, $usrprint);
                   1684:
                   1685:        if ($annTable) {
3.100     knu      1686:                print "<table style=\"border: none\" cellspacing=\"0\" cellpadding=\"0\">\n";
3.80      knu      1687:        } else {
                   1688:                print "<pre>";
                   1689:        }
                   1690:
                   1691:        # prefetch several lines
                   1692:        my @buf = head($reader);
                   1693:
                   1694:        my %d = scan_directives(@buf);
                   1695:
                   1696:        while (@buf || !eof($reader)) {
                   1697:                $_ = @buf ? shift @buf : <$reader>;
                   1698:
                   1699:                my @words = split;
                   1700:
                   1701:                # Adding one is for the (single) space which follows $words[0].
                   1702:                my $rest = substr($_, length($words[0]) + 1);
                   1703:                if ($words[0] eq "E") {
                   1704:                        next;
                   1705:                } elsif ($words[0] eq "M") {
                   1706:                        $lineNr++;
                   1707:                        (my $lrev = substr($_, 2,  13)) =~ y/ //d;
                   1708:                        (my $lusr = substr($_, 16, 9))  =~ y/ //d;
                   1709:                        my $line = substr($_, 36);
                   1710:                        my $isCurrentRev = ($rev eq $lrev);
                   1711:
                   1712:                        # we should parse the date here ..
                   1713:                        if ($lrev eq $oldLrev) {
                   1714:                                $revprint = sprintf('%-8s', '');
                   1715:                        } else {
                   1716:                                $revprint = sprintf('%-8s', $lrev);
                   1717:                                $revprint =~
                   1718:                                    s`\S+`&link($&, "$scriptwhere$query#rev$&")`e
                   1719:                                    ;    # `
                   1720:                                $oldLusr = '';
                   1721:                        }
                   1722:
                   1723:                        if ($lusr eq $oldLusr) {
                   1724:                                $usrprint = '';
                   1725:                        } else {
                   1726:                                $usrprint = $lusr;
                   1727:                        }
                   1728:                        $oldLrev = $lrev;
                   1729:                        $oldLusr = $lusr;
                   1730:
                   1731:                        # Set bold for text-based browsers only - graphical
                   1732:                        # browsers show bold fonts a bit wider than regular fonts,
                   1733:                        # so it looks irregular.
                   1734:                        print "<b>" if ($isCurrentRev && $is_textbased);
                   1735:
                   1736:                        printf "%s%s %-8s %4d:", $revprint,
                   1737:                            $isCurrentRev ? '!' : ' ', $usrprint, $lineNr;
                   1738:                        print spacedHtmlText($line, $d{'tabstop'});
                   1739:
                   1740:                        print "</b>" if ($isCurrentRev && $is_textbased);
                   1741:                } elsif ($words[0] eq "ok") {
3.12      knu      1742:
3.80      knu      1743:                        # We could complain about any text received after this, like the
                   1744:                        # CVS command line client.  But for simplicity, we don't.
                   1745:                } elsif ($words[0] eq "error") {
                   1746:                        fatal("500 Internal Error",
3.103     knu      1747:                              'Error occured during annotate: <b>%s</b>',
                   1748:                              $_);
3.80      knu      1749:                }
3.1       knu      1750:        }
3.80      knu      1751:
                   1752:        if ($annTable) {
                   1753:                print "</table>";
                   1754:        } else {
                   1755:                print "</pre>";
3.1       knu      1756:        }
3.95      knu      1757:        close($reader) or warn "cannot close: $!";
3.80      knu      1758:        wait;
3.1       knu      1759: }
                   1760:
                   1761: ###############################
                   1762: # make Checkout
                   1763: ###############################
3.12      knu      1764: sub doCheckout($$) {
3.80      knu      1765:        my ($fullname, $rev) = @_;
                   1766:        my ($mimetype, $revopt);
                   1767:        my $fh = do { local (*FH); };
                   1768:
                   1769:        if ($rev eq 'HEAD' || $rev eq '.') {
                   1770:                $rev = undef;
                   1771:        }
                   1772:
                   1773:        # make sure the revisions a wellformed, for security
                   1774:        # reasons ..
                   1775:        if (defined($rev) && $rev =~ /[^\w.]/) {
3.103     knu      1776:                fatal("404 Not Found",
                   1777:                      'Malformed query "%s"',
                   1778:                      $ENV{QUERY_STRING});
3.80      knu      1779:        }
                   1780:
3.81      knu      1781:        if (&forbidden_file($fullname)) {
3.103     knu      1782:                fatal("403 Forbidden",
                   1783:                      'Access forbidden.  This file is mentioned in @ForbiddenFiles');
3.81      knu      1784:                return;
                   1785:        }
                   1786:
3.80      knu      1787:        # get mimetype
                   1788:        if (defined($input{"content-type"})
                   1789:            && ($input{"content-type"} =~ /\S\/\S/))
                   1790:        {
                   1791:                $mimetype = $input{"content-type"}
                   1792:        } else {
                   1793:                $mimetype = &getMimeTypeFromSuffix($fullname);
                   1794:        }
                   1795:
                   1796:        if (defined($rev)) {
                   1797:                $revopt = "-r$rev";
                   1798:                if ($use_moddate) {
                   1799:                        readLog($fullname, $rev);
                   1800:                        $moddate = $date{$rev};
                   1801:                }
                   1802:        } else {
                   1803:                $revopt = "-rHEAD";
                   1804:
                   1805:                if ($use_moddate) {
                   1806:                        readLog($fullname);
                   1807:                        $moddate = $date{$symrev{HEAD}};
                   1808:                }
                   1809:        }
                   1810:
                   1811:        ### just for the record:
                   1812:        ### 'cvs co' seems to have a bug regarding single checkout of
                   1813:        ### directories/files having spaces in it;
                   1814:        ### this is an issue that should be resolved on cvs's side
                   1815:        #
                   1816:        # Safely for a child process to read from.
                   1817:        if (!open($fh, "-|")) {    # child
                   1818:                 # chdir to $tmpdir before to avoid non-readable cgi-bin directories
                   1819:                chdir($tmpdir);
                   1820:                open(STDERR, ">&STDOUT");    # Redirect stderr to stdout
3.91      knu      1821:
                   1822:                # work around a bug of cvs -p; expand symlinks
                   1823:                use Cwd 'abs_path';
                   1824:                exec($CMD{cvs}, @cvs_options,
                   1825:                     '-d', abs_path($cvsroot),
                   1826:                     'co', '-p',
3.95      knu      1827:                     $revopt, $where) or exit -1;
3.80      knu      1828:        }
                   1829:
                   1830:        if (eof($fh)) {
3.103     knu      1831:                fatal("404 Not Found",
                   1832:                      '%s is not (any longer) pertinent',
                   1833:                      $where);
3.80      knu      1834:        }
                   1835:
                   1836:        #===================================================================
                   1837:        #Checking out squid/src/ftp.c
                   1838:        #RCS:  /usr/src/CVS/squid/src/ftp.c,v
                   1839:        #VERS: 1.1.1.28.6.2
                   1840:        #***************
                   1841:
                   1842:        # Parse CVS header
                   1843:        my ($revision, $filename, $cvsheader);
                   1844:        $filename = "";
                   1845:        while (<$fh>) {
                   1846:                last if (/^\*\*\*\*/);
                   1847:                $revision = $1 if (/^VERS: (.*)$/);
                   1848:
                   1849:                if (/^Checking out (.*)$/) {
                   1850:                        $filename = $1;
                   1851:                        $filename =~ s/^\.\/*//;
                   1852:                }
                   1853:                $cvsheader .= $_;
                   1854:        }
                   1855:
                   1856:        if ($filename ne $where) {
3.103     knu      1857:                fatal("500 Internal Error",
                   1858:                      'Unexpected output from cvs co: %s',
                   1859:                      $cvsheader);
3.80      knu      1860:        }
                   1861:        $| = 1;
                   1862:
                   1863:        if ($mimetype eq "text/x-cvsweb-markup") {
                   1864:                &cvswebMarkup($fh, $fullname, $revision);
                   1865:        } else {
                   1866:                http_header($mimetype);
                   1867:                print <$fh>;
                   1868:        }
                   1869:        close($fh);
3.1       knu      1870: }
                   1871:
3.12      knu      1872: sub cvswebMarkup($$$) {
3.80      knu      1873:        my ($filehandle, $fullname, $revision) = @_;
                   1874:        my ($pathname,   $filename);
3.1       knu      1875:
3.80      knu      1876:        ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1877:        ($filename = $where) =~ s/^.*\///;
                   1878:        my ($fileurl) = urlencode($filename);
                   1879:
                   1880:        http_header();
                   1881:
                   1882:        navigateHeader($scriptwhere, $pathname, $filename, $revision, "view");
3.89      knu      1883:        print "<hr noshade>";
3.100     knu      1884:        print "<table width=\"100%\">\n<tr>\n<td style=\"background-color: $markupLogColor\">";
3.80      knu      1885:        print "File: ", &clickablePath($where, 1);
3.35      knu      1886:        print "&nbsp;(";
3.80      knu      1887:        &download_link($fileurl, $revision, "download");
3.35      knu      1888:        print ")";
3.24      knu      1889:
3.80      knu      1890:        if (!$defaultTextPlain) {
                   1891:                print "&nbsp;(";
                   1892:                &download_link($fileurl, $revision, "as text", "text/plain");
                   1893:                print ")";
                   1894:        }
3.89      knu      1895:        print "<br>\n";
3.80      knu      1896:
                   1897:        if ($show_log_in_markup) {
                   1898:                readLog($fullname);    #,$revision);
                   1899:                printLog($revision, 0);
                   1900:        } else {
3.89      knu      1901:                print "Version: <b>$revision</b><br>\n";
                   1902:                print "Tag: <b>", $input{only_with_tag}, "</b><br>\n"
3.80      knu      1903:                    if $input{only_with_tag};
                   1904:        }
3.100     knu      1905:        print "</td>\n</tr>\n</table>";
3.80      knu      1906:        my $url = download_url($fileurl, $revision, $mimetype);
3.89      knu      1907:        print "<hr noshade>";
3.80      knu      1908:
                   1909:        if ($mimetype =~ /^image/) {
3.89      knu      1910:                printf '<img src="%s" alt=""><br>', hrefquote("$url$barequery");
3.80      knu      1911:        } elsif ($mimetype =~ m%^application/pdf%) {
3.89      knu      1912:                printf '<embed src="%s" width="100%"><br>',
3.80      knu      1913:                    hrefquote("$url$barequery");
                   1914:        } elsif ($preformat_in_markup) {
3.89      knu      1915:                print "<pre>";
3.80      knu      1916:
                   1917:                # prefetch several lines
                   1918:                my @buf = head($filehandle);
                   1919:
                   1920:                my %d = scan_directives(@buf);
3.24      knu      1921:
3.80      knu      1922:                while (@buf || !eof($filehandle)) {
                   1923:                        $_ = @buf ? shift @buf : <$filehandle>;
3.24      knu      1924:
3.80      knu      1925:                        print spacedHtmlText($_, $d{'tabstop'});
                   1926:                }
3.89      knu      1927:                print "</pre>";
3.80      knu      1928:        } else {
3.89      knu      1929:                print "<pre>";
3.24      knu      1930:
3.80      knu      1931:                while (<$filehandle>) {
                   1932:                        print htmlquote($_);
                   1933:                }
3.89      knu      1934:                print "</pre>";
3.69      knu      1935:        }
1.12      fenner   1936: }
                   1937:
3.1       knu      1938: sub viewable($) {
3.80      knu      1939:        my ($mimetype) = @_;
3.1       knu      1940:
3.80      knu      1941:        $mimetype =~ m%^((text|image)/|application/pdf)%;
3.1       knu      1942: }
                   1943:
                   1944: ###############################
                   1945: # Show Colored Diff
                   1946: ###############################
3.12      knu      1947: sub doDiff($$$$$$) {
3.80      knu      1948:        my ($fullname, $r1, $tr1, $r2, $tr2, $f) = @_;
                   1949:        my $fh = do { local (*FH); };
3.37      knu      1950:        my ($rev1, $rev2, $sym1, $sym2, $f1, $f2);
1.12      fenner   1951:
3.81      knu      1952:        if (&forbidden_file($fullname)) {
3.103     knu      1953:                fatal("403 Forbidden",
                   1954:                      'Access forbidden.  This file is mentioned in @ForbiddenFiles');
3.81      knu      1955:                return;
                   1956:        }
                   1957:
1.12      fenner   1958:        if ($r1 =~ /([^:]+)(:(.+))?/) {
3.80      knu      1959:                $rev1 = $1;
                   1960:                $sym1 = $3;
1.12      fenner   1961:        }
3.1       knu      1962:        if ($r1 eq 'text') {
3.80      knu      1963:                $rev1 = $tr1;
                   1964:                $sym1 = "";
1.12      fenner   1965:        }
3.80      knu      1966:
1.12      fenner   1967:        if ($r2 =~ /([^:]+)(:(.+))?/) {
3.80      knu      1968:                $rev2 = $1;
                   1969:                $sym2 = $3;
1.12      fenner   1970:        }
3.1       knu      1971:        if ($r2 eq 'text') {
3.80      knu      1972:                $rev2 = $tr2;
                   1973:                $sym2 = "";
1.12      fenner   1974:        }
3.15      knu      1975:
3.1       knu      1976:        # make sure the revisions a wellformed, for security
                   1977:        # reasons ..
3.15      knu      1978:        if ($rev1 =~ /[^\w.]/ || $rev2 =~ /[^\w.]/) {
3.103     knu      1979:                fatal("404 Not Found",
                   1980:                      'Malformed query "%s"',
                   1981:                      $ENV{QUERY_STRING});
1.12      fenner   1982:        }
3.80      knu      1983:
                   1984:        #
                   1985:        # rev1 and rev2 are now both numeric revisions.
                   1986:        # Thus we do a DWIM here and swap them if rev1 is after rev2.
                   1987:        # XXX should we warn about the fact that we do this?
                   1988:        if (&revcmp($rev1, $rev2) > 0) {
                   1989:                my ($tmp1, $tmp2) = ($rev1, $sym1);
                   1990:                ($rev1, $sym1) = ($rev2, $sym2);
                   1991:                ($rev2, $sym2) = ($tmp1, $tmp2);
1.12      fenner   1992:        }
3.37      knu      1993:        my $difftype = $DIFFTYPES{$f};
                   1994:
                   1995:        if (!$difftype) {
3.103     knu      1996:                fatal("400 Bad arguments",
                   1997:                      'Diff format %s not understood',
                   1998:                      $f);
3.1       knu      1999:        }
                   2000:
3.37      knu      2001:        my @difftype       = @{$difftype->{'opts'}};
                   2002:        my $human_readable = $difftype->{'colored'};
                   2003:
3.1       knu      2004:        # apply special options
3.3       knu      2005:        if ($showfunc) {
3.80      knu      2006:                push @difftype, '-p' if $f ne 's';
3.1       knu      2007:
3.80      knu      2008:                my ($re1, $re2);
3.1       knu      2009:
3.80      knu      2010:                while (($re1, $re2) = each %funcline_regexp) {
                   2011:                        if ($fullname =~ /$re1/) {
                   2012:                                push @difftype, '-F', $re2;
                   2013:                                last;
                   2014:                        }
3.1       knu      2015:                }
3.3       knu      2016:        }
3.80      knu      2017:
3.3       knu      2018:        if ($human_readable) {
3.80      knu      2019:                if ($hr_ignwhite) {
                   2020:                        push @difftype, '-w';
                   2021:                }
                   2022:                if ($hr_ignkeysubst) {
                   2023:                        push @difftype, '-kk';
                   2024:                }
3.1       knu      2025:        }
3.80      knu      2026:
                   2027:        if (!open($fh, "-|")) {    # child
                   2028:                open(STDERR, ">&STDOUT");    # Redirect stderr to stdout
3.86      knu      2029:                openOutputFilter();
3.95      knu      2030:                exec($CMD{rcsdiff}, @difftype, "-r$rev1", "-r$rev2", $fullname) or exit -1;
3.1       knu      2031:        }
                   2032:        if ($human_readable) {
3.80      knu      2033:                http_header();
                   2034:                &human_readable_diff($fh, $rev2);
3.100     knu      2035:                html_footer();
3.80      knu      2036:                gzipclose();
                   2037:                exit;
                   2038:        } else {
                   2039:                http_header("text/plain");
3.1       knu      2040:        }
3.80      knu      2041:
                   2042:        #
                   2043:        #===================================================================
                   2044:        #RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
                   2045:        #retrieving revision 1.16
                   2046:        #retrieving revision 1.17
                   2047:        #diff -c -r1.16 -r1.17
                   2048:        #*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2049:        #--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
                   2050:        #
                   2051:        # Ideas:
                   2052:        # - nuke the stderr output if it's what we expect it to be
                   2053:        # - Add "no differences found" if the diff command supplied no output.
                   2054:        #
                   2055:        #*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2056:        #--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
                   2057:        # (bogus example, but...)
                   2058:        #
                   2059:        if (grep { $_ eq '-u' } @difftype) {
                   2060:                $f1 = '---';
                   2061:                $f2 = '\+\+\+';
                   2062:        } else {
                   2063:                $f1 = '\*\*\*';
                   2064:                $f2 = '---';
1.12      fenner   2065:        }
3.80      knu      2066:
3.1       knu      2067:        while (<$fh>) {
3.80      knu      2068:                if (m|^$f1 $cvsroot|o) {
                   2069:                        s|$cvsroot/||o;
                   2070:                        if ($sym1) {
                   2071:                                chop;
                   2072:                                $_ .= " $sym1\n";
                   2073:                        }
                   2074:                } elsif (m|^$f2 $cvsroot|o) {
                   2075:                        s|$cvsroot/||o;
                   2076:
                   2077:                        if ($sym2) {
                   2078:                                chop;
                   2079:                                $_ .= " $sym2\n";
                   2080:                        }
1.12      fenner   2081:                }
3.80      knu      2082:                print $_;
1.12      fenner   2083:        }
3.1       knu      2084:        close($fh);
1.12      fenner   2085: }
                   2086:
3.1       knu      2087: ###############################
                   2088: # Show Logs ..
                   2089: ###############################
3.12      knu      2090: sub getDirLogs($$@) {
3.80      knu      2091:        my ($cvsroot, $dirname, @otherFiles) = @_;
                   2092:        my ($state, $otherFiles, $tag, $file, $date, $branchpoint, $branch,
                   2093:            $log);
                   2094:        my ($rev, $revision, $revwanted, $filename, $head, $author);
                   2095:
                   2096:        $tag = $input{only_with_tag};
                   2097:
                   2098:        my ($DirName) = "$cvsroot/$where";
                   2099:        my (@files, @filetags);
                   2100:        my $fh = do { local (*FH); };
                   2101:
                   2102:        push (@files, &safeglob("$DirName/*,v"));
                   2103:        push (@files, &safeglob("$DirName/Attic/*,v"))
                   2104:            if (!$input{'hideattic'});
                   2105:        foreach $file (@otherFiles) {
                   2106:                push (@files, "$DirName/$file");
                   2107:        }
                   2108:
                   2109:        # just execute rlog if there are any files
                   2110:        if ($#files < 0) {
                   2111:                return;
                   2112:        }
                   2113:
                   2114:        if (defined($tag)) {
                   2115:
                   2116:                #can't use -r<tag> as - is allowed in tagnames, but misinterpreated by rlog..
3.86      knu      2117:                if (!open($fh, "-|")) {    # child
3.80      knu      2118:                        open(STDERR, '>/dev/null'); # rlog may complain; ignore.
3.86      knu      2119:                        openOutputFilter();
3.95      knu      2120:                        exec($CMD{rlog}, @files) or exit -1;
3.80      knu      2121:                }
                   2122:        } else {
                   2123:
3.86      knu      2124:                if (!open($fh, "-|")) {    # child
3.80      knu      2125:                        open(STDERR, '>/dev/null'); # rlog may complain; ignore.
3.86      knu      2126:                        openOutputFilter();
3.95      knu      2127:                        exec($CMD{rlog}, '-r', @files) or exit -1;
3.80      knu      2128:                }
3.1       knu      2129:        }
3.80      knu      2130:        $state = "start";
                   2131:
                   2132:        while (<$fh>) {
                   2133:                if ($state eq "start") {
                   2134:
                   2135:                        #Next file. Initialize file variables
                   2136:                        $rev         = '';
                   2137:                        $revwanted   = '';
                   2138:                        $branch      = '';
                   2139:                        $branchpoint = '';
                   2140:                        $filename    = '';
                   2141:                        $log         = '';
                   2142:                        $revision    = '';
                   2143:                        %symrev      = ();
                   2144:                        @filetags    = ();
                   2145:
                   2146:                        #jump to head state
                   2147:                        $state = "head";
                   2148:                }
                   2149:                print "$state:$_" if ($verbose);
                   2150:                again:
                   2151:
                   2152:                if ($state eq "head") {
                   2153:
                   2154:                        #$rcsfile = $1 if (/^RCS file: (.+)$/); #not used (yet)
                   2155:
                   2156:                        if (/^Working file: (.+)$/) {
                   2157:                                $filename = $1;
                   2158:                        } elsif (/^head: (.+)$/) {
                   2159:                                $head = $1;
                   2160:                        } elsif (/^branch: (.+)$/) {
                   2161:                                $branch = $1
                   2162:                        } elsif (/^symbolic names:/) {
                   2163:                                $state = "tags";
                   2164:                                ($branch = $head) =~ s/\.\d+$//
                   2165:                                    if $branch eq '';
                   2166:                                $branch =~ s/(\d+)$/0.$1/;
                   2167:                                $symrev{MAIN}  = $branch;
                   2168:                                $symrev{HEAD}  = $branch;
                   2169:                                $alltags{MAIN} = 1;
                   2170:                                $alltags{HEAD} = 1;
                   2171:                                push (@filetags, "MAIN", "HEAD");
                   2172:                        } elsif (/$LOG_REVSEPARATOR/o) {
                   2173:                                $state = "log";
                   2174:                                $rev   = '';
                   2175:                                $date  = '';
                   2176:                                $log   = '';
                   2177:
                   2178:                                # Try to reconstruct the relative filename if RCS spits out a full path
                   2179:                                $filename =~ s%^\Q$DirName\E/%%;
                   2180:                        }
3.41      knu      2181:                        next;
                   2182:                }
3.80      knu      2183:
                   2184:                if ($state eq "tags") {
3.94      knu      2185:                        if (/^\s+([^:]+):\s+([\d\.]+)\s*$/) {
3.80      knu      2186:                                push (@filetags, $1);
                   2187:                                $symrev{$1}  = $2;
                   2188:                                $alltags{$1} = 1;
                   2189:                                next;
                   2190:                        } elsif (/^\S/) {
                   2191:
                   2192:                                if (defined($tag)) {
                   2193:                                        if (defined($symrev{$tag})
                   2194:                                            || $tag eq "HEAD")
                   2195:                                        {
                   2196:                                                $revwanted =
                   2197:                                                    $symrev{$tag eq "HEAD" ?
                   2198:                                                    "MAIN" : $tag};
                   2199:                                                ($branch = $revwanted) =~
                   2200:                                                    s/\b0\.//;
                   2201:                                                ($branchpoint = $branch) =~
                   2202:                                                    s/\.?\d+$//;
                   2203:                                                $revwanted = ''
                   2204:                                                    if ($revwanted ne $branch);
                   2205:                                        } elsif ($tag ne "HEAD") {
                   2206:                                                print
                   2207:                                                    "Tag not found, skip this file"
                   2208:                                                    if ($verbose);
                   2209:                                                $state = "skip";
                   2210:                                                next;
                   2211:                                        }
                   2212:                                }
                   2213:
                   2214:                                foreach my $tagfound (@filetags) {
                   2215:                                        $tags{$tagfound} = 1;
                   2216:                                }
                   2217:                                $state = "head";
                   2218:                                goto again;
                   2219:                        }
                   2220:                }
                   2221:
                   2222:                if ($state eq "log") {
                   2223:                        if (/$LOG_REVSEPARATOR/o || /$LOG_FILESEPARATOR/o) {
                   2224:
                   2225:                                # End of a log entry.
                   2226:                                my $revbranch = $rev;
                   2227:                                $revbranch =~ s/\.\d+$//;
                   2228:                                print "$filename $rev Wanted: $revwanted ",
                   2229:                                    "Revbranch: $revbranch Branch: $branch ",
                   2230:                                    "Branchpoint: $branchpoint\n"
                   2231:                                    if ($verbose);
                   2232:
                   2233:                                if ($revwanted eq '' && $branch ne ''
                   2234:                                    && $branch eq $revbranch || !defined($tag))
                   2235:                                {
                   2236:                                        print
                   2237:                                            "File revision $rev found for branch $branch\n"
                   2238:                                            if ($verbose);
                   2239:                                        $revwanted = $rev;
                   2240:                                }
                   2241:
                   2242:                                if ($revwanted ne '' ? $rev eq $revwanted :
                   2243:                                    $branchpoint ne '' ? $rev eq $branchpoint :
                   2244:                                    0 && ($rev eq $head))
                   2245:                                {    # Don't think head is needed here..
                   2246:                                        print
                   2247:                                            "File info $rev found for $filename\n"
                   2248:                                            if ($verbose);
                   2249:                                        my @finfo =
                   2250:                                            ($rev, $date, $log, $author,
                   2251:                                                $filename);
                   2252:                                        my ($name);
                   2253:                                        ($name = $filename) =~ s%/.*%%;
                   2254:                                        $fileinfo{$name} = [@finfo];
                   2255:                                        $state = "done" if ($rev eq $revwanted);
                   2256:                                }
                   2257:                                $rev  = '';
                   2258:                                $date = '';
                   2259:                                $log  = '';
                   2260:                        } elsif ($date eq ''
                   2261:                            && m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);|
                   2262:                            )
                   2263:                        {
                   2264:                                my $yr = $1;
                   2265:
                   2266:                                # damn 2-digit year routines :-)
                   2267:                                if ($yr > 100) {
                   2268:                                        $yr -= 1900;
                   2269:                                }
                   2270:                                $date =
                   2271:                                    &Time::Local::timegm($6, $5, $4, $3, $2 - 1,
                   2272:                                        $yr);
                   2273:                                ($author) = /author: ([^;]+)/;
                   2274:                                $state = "log";
                   2275:                                $log   = '';
                   2276:                                next;
3.98      knu      2277:                        } elsif ($rev eq '' && /^revision (\d+(?:\.\d+)+).*$/) {
                   2278:                                $rev = $1; # .*$ eats up the locker(lockers?) info, if any
3.80      knu      2279:                                next;
                   2280:                        } else {
                   2281:                                $log .= $_;
                   2282:                        }
                   2283:                }
                   2284:
                   2285:                if (/$LOG_FILESEPARATOR/o) {
                   2286:                        $state = "start";
                   2287:                        next;
3.41      knu      2288:                }
3.1       knu      2289:        }
3.80      knu      2290:
                   2291:        if ($. == 0) {
3.103     knu      2292:                fatal("500 Internal Error",
                   2293:                      'Failed to spawn GNU rlog on <em>"%s"</em>.  <p>Did you set the <b>$command_path</b> in your configuration file correctly ? (Currently "%s"',
                   2294:                      join (", ", @files), $command_path);
3.1       knu      2295:        }
3.80      knu      2296:        close($fh);
3.1       knu      2297: }
                   2298:
3.12      knu      2299: sub readLog($;$) {
3.80      knu      2300:        my ($fullname, $revision) = @_;
3.1       knu      2301:        my ($symnames, $head, $rev, $br, $brp, $branch, $branchrev);
3.80      knu      2302:        my $fh = do { local (*FH); };
3.1       knu      2303:
                   2304:        if (defined($revision)) {
3.80      knu      2305:                $revision = "-r$revision";
                   2306:        } else {
                   2307:                $revision = "";
3.1       knu      2308:        }
                   2309:
                   2310:        undef %symrev;
                   2311:        undef %revsym;
                   2312:        undef @allrevisions;
                   2313:        undef %date;
                   2314:        undef %author;
                   2315:        undef %state;
                   2316:        undef %difflines;
                   2317:        undef %log;
1.12      fenner   2318:
                   2319:        print("Going to rlog '$fullname'\n") if ($verbose);
3.80      knu      2320:        if (!open($fh, "-|")) {    # child
                   2321:                if ($revision ne '') {
3.86      knu      2322:                        openOutputFilter();
3.95      knu      2323:                        exec($CMD{rlog}, $revision, $fullname) or exit -1;
3.80      knu      2324:                } else {
3.86      knu      2325:                        openOutputFilter();
3.95      knu      2326:                        exec($CMD{rlog}, $fullname) or exit -1;
3.80      knu      2327:                }
1.44      knu      2328:        }
3.80      knu      2329:
3.1       knu      2330:        while (<$fh>) {
3.80      knu      2331:                print if ($verbose);
                   2332:                if ($symnames) {
                   2333:                        if (/^\s+([^:]+):\s+([\d\.]+)/) {
                   2334:                                $symrev{$1} = $2;
                   2335:                        } else {
                   2336:                                $symnames = 0;
                   2337:                        }
                   2338:                } elsif (/^head:\s+([\d\.]+)/) {
                   2339:                        $head = $1;
                   2340:                } elsif (/^branch:\s+([\d\.]+)/) {
                   2341:                        $curbranch = $1;
                   2342:                } elsif (/^symbolic names/) {
                   2343:                        $symnames = 1;
                   2344:                } elsif (/^-----/) {
                   2345:                        last;
1.1       jfieber  2346:                }
                   2347:        }
3.1       knu      2348:        ($curbranch = $head) =~ s/\.\d+$// if (!defined($curbranch));
1.7       fenner   2349:
3.80      knu      2350:        # each log entry is of the form:
                   2351:        # ----------------------------
                   2352:        # revision 3.7.1.1
                   2353:        # date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
                   2354:        # log info
                   2355:        # ----------------------------
3.98      knu      2356:
                   2357:        # For a locked revision, the first line after the separator
                   2358:        # becomes smth like
                   2359:        # revision 9.19 locked by: vassilii;
                   2360:
1.1       jfieber  2361:        logentry:
3.80      knu      2362:
3.32      knu      2363:        while (!/$LOG_FILESEPARATOR/o) {
3.80      knu      2364:                $_ = <$fh>;
                   2365:                last logentry if (!defined($_));    # EOF
                   2366:                print "R:", $_ if ($verbose);
3.98      knu      2367:                if (/^revision (\d+(?:\.\d+)+)/) {
3.80      knu      2368:                        $rev = $1;
                   2369:                        unshift (@allrevisions, $rev);
                   2370:                } elsif (/$LOG_FILESEPARATOR/o || /$LOG_REVSEPARATOR/o) {
                   2371:                        next logentry;
                   2372:                } else {
                   2373:
                   2374:                        # The rlog output is syntactically ambiguous.  We must
                   2375:                        # have guessed wrong about where the end of the last log
                   2376:                        # message was.
                   2377:                        # Since this is likely to happen when people put rlog output
                   2378:                        # in their commit messages, don't even bother keeping
                   2379:                        # these lines since we don't know what revision they go with
                   2380:                        # any more.
                   2381:                        next logentry;
                   2382:                }
                   2383:                $_ = <$fh>;
                   2384:                print "D:", $_ if ($verbose);
                   2385:                if (
                   2386:                    m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);\s+state:\s+(\S+);\s+(lines:\s+([0-9\s+-]+))?|
                   2387:                    )
                   2388:                {
                   2389:                        my $yr = $1;
                   2390:
                   2391:                        # damn 2-digit year routines :-)
                   2392:                        if ($yr > 100) {
                   2393:                                $yr -= 1900;
                   2394:                        }
                   2395:                        $date{$rev} =
                   2396:                            &Time::Local::timegm($6, $5, $4, $3, $2 - 1, $yr);
                   2397:                        $author{$rev}    = $7;
                   2398:                        $state{$rev}     = $8;
                   2399:                        $difflines{$rev} = $10;
                   2400:                } else {
3.103     knu      2401:                        fatal("500 Internal Error",
                   2402:                              'Error parsing RCS output: %s',
                   2403:                              $_);
3.80      knu      2404:                }
                   2405:                line:
                   2406:
                   2407:                while (<$fh>) {
                   2408:                        print "L:", $_ if ($verbose);
                   2409:                        next line if (/^branches:\s/);
                   2410:                        last line
                   2411:                            if (/$LOG_FILESEPARATOR/o || /$LOG_REVSEPARATOR/o);
                   2412:                        $log{$rev} .= $_;
                   2413:                }
                   2414:                print "E:", $_ if ($verbose);
1.1       jfieber  2415:        }
3.1       knu      2416:        close($fh);
1.1       jfieber  2417:        print "Done reading RCS file\n" if ($verbose);
3.1       knu      2418:
3.80      knu      2419:        @revorder = reverse sort { revcmp($a, $b) } @allrevisions;
                   2420:        print "Done sorting revisions", join (" ", @revorder), "\n"
                   2421:            if ($verbose);
                   2422:
                   2423:        #
                   2424:        # HEAD is an artificial tag which is simply the highest tag number on the main
                   2425:        # branch, unless there is a branch tag in the RCS file in which case it's the
                   2426:        # highest revision on that branch.  Find it by looking through @revorder; it
                   2427:        # is the first commit listed on the appropriate branch.
                   2428:        # This is not neccesary the same revision as marked as head in the RCS file.
3.1       knu      2429:        my $headrev = $curbranch || "1";
3.45      knu      2430:        ($symrev{"MAIN"} = $headrev) =~ s/(\d+)$/0.$1/;
3.80      knu      2431:
3.1       knu      2432:        foreach $rev (@revorder) {
3.80      knu      2433:                if ($rev =~ /^(\S*)\.\d+$/ && $headrev eq $1) {
                   2434:                        $symrev{"HEAD"} = $rev;
                   2435:                        last;
                   2436:                }
1.1       jfieber  2437:        }
3.1       knu      2438:        ($symrev{"HEAD"} = $headrev) =~ s/\.\d+$//
3.80      knu      2439:            if (!defined($symrev{"HEAD"}));
1.1       jfieber  2440:        print "Done finding HEAD\n" if ($verbose);
3.80      knu      2441:
                   2442:        #
                   2443:        # Now that we know all of the revision numbers, we can associate
                   2444:        # absolute revision numbers with all of the symbolic names, and
                   2445:        # pass them to the form so that the same association doesn't have
                   2446:        # to be built then.
                   2447:        #
3.1       knu      2448:        undef @branchnames;
                   2449:        undef %branchpoint;
                   2450:        undef $sel;
                   2451:
                   2452:        foreach (reverse sort keys %symrev) {
3.80      knu      2453:                $rev = $symrev{$_};
                   2454:                if ($rev =~ /^((.*)\.)?\b0\.(\d+)$/) {
                   2455:                        push (@branchnames, $_);
                   2456:
                   2457:                        #
                   2458:                        # A revision number of A.B.0.D really translates into
                   2459:                        # "the highest current revision on branch A.B.D".
                   2460:                        #
                   2461:                        # If there is no branch A.B.D, then it translates into
                   2462:                        # the head A.B .
                   2463:                        #
                   2464:                        # This reasoning also applies to the main branch A.B,
                   2465:                        # with the branch number 0.A, with the exception that
                   2466:                        # it has no head to translate to if there is nothing on
                   2467:                        # the branch, but I guess this can never happen?
                   2468:                        #
                   2469:                        # (the code below gracefully forgets about the branch
                   2470:                        # if it should happen)
                   2471:                        #
                   2472:                        $head = defined($2) ? $2 : "";
                   2473:                        $branch = $3;
                   2474:                        $branchrev = $head . ($head ne "" ? "." : "") . $branch;
                   2475:                        my $regex;
                   2476:                        $regex = quotemeta $branchrev;
                   2477:                        $rev   = $head;
                   2478:
                   2479:                        foreach my $r (@revorder) {
                   2480:                                if ($r =~ /^${regex}\b/) {
                   2481:                                        $rev = $branchrev;
                   2482:                                        last;
                   2483:                                }
                   2484:                        }
                   2485:                        next if ($rev eq "");
                   2486:
                   2487:                        if ($rev ne $head && $head ne "") {
                   2488:                                $branchpoint{$head} .= ", "
                   2489:                                    if ($branchpoint{$head});
                   2490:                                $branchpoint{$head} .= $_;
                   2491:                        }
1.8       fenner   2492:                }
3.80      knu      2493:                $revsym{$rev} .= ", " if ($revsym{$rev});
                   2494:                $revsym{$rev} .= $_;
3.89      knu      2495:                $sel .= "<option value=\"${rev}:${_}\">$_</option>\n";
1.1       jfieber  2496:        }
                   2497:        print "Done associating revisions with branches\n" if ($verbose);
3.1       knu      2498:
                   2499:        my ($onlyonbranch, $onlybranchpoint);
                   2500:        if ($onlyonbranch = $input{'only_with_tag'}) {
3.80      knu      2501:                $onlyonbranch = $symrev{$onlyonbranch};
                   2502:                if ($onlyonbranch =~ s/\b0\.//) {
                   2503:                        ($onlybranchpoint = $onlyonbranch) =~ s/\.\d+$//;
                   2504:                } else {
                   2505:                        $onlybranchpoint = $onlyonbranch;
                   2506:                }
                   2507:
                   2508:                if (!defined($onlyonbranch) || $onlybranchpoint eq "") {
                   2509:                        fatal("404 Tag not found",
3.103     knu      2510:                              'Tag %s not defined',
                   2511:                              $input{'only_with_tag'});
3.80      knu      2512:                }
1.8       fenner   2513:        }
1.1       jfieber  2514:
3.1       knu      2515:        undef @revisions;
                   2516:
                   2517:        foreach (@allrevisions) {
3.80      knu      2518:                ($br  = $_)  =~ s/\.\d+$//;
                   2519:                ($brp = $br) =~ s/\.\d+$//;
                   2520:                next
                   2521:                    if ($onlyonbranch && $br ne $onlyonbranch
                   2522:                    && $_ ne $onlybranchpoint);
                   2523:                unshift (@revisions, $_);
3.1       knu      2524:        }
                   2525:
                   2526:        if ($logsort eq "date") {
3.80      knu      2527:
                   2528:                # Sort the revisions in commit order an secondary sort on revision
                   2529:                # (secondary sort needed for imported sources, or the first main
                   2530:                # revision gets before the same revision on the 1.1.1 branch)
                   2531:                @revdisplayorder =
                   2532:                    sort { $date{$b} <=> $date{$a} || -revcmp($a, $b) }
                   2533:                    @revisions;
                   2534:        } elsif ($logsort eq "rev") {
                   2535:
                   2536:                # Sort the revisions in revision order, highest first
                   2537:                @revdisplayorder = reverse sort { revcmp($a, $b) } @revisions;
                   2538:        } else {
                   2539:
                   2540:                # No sorting. Present in the same order as rlog / cvs log
                   2541:                @revdisplayorder = @revisions;
3.1       knu      2542:        }
                   2543:
                   2544: }
                   2545:
3.37      knu      2546: sub printDiffLinks($$) {
3.80      knu      2547:        my ($text, $url) = @_;
                   2548:        my @extra;
3.37      knu      2549:
3.80      knu      2550:        local $_;
                   2551:        for ($DIFFTYPES{$defaultDiffType}{'colored'} ? qw(u) : qw(h)) {
                   2552:                my $f = $_ eq $defaultDiffType ? '' : $_;
3.37      knu      2553:
3.80      knu      2554:                push @extra, &link(lc $DIFFTYPES{$_}{'descr'}, "$url&f=$f");
                   2555:        }
3.37      knu      2556:
3.80      knu      2557:        print &link($text, $url), ' (', join (', ', @extra), ')';
3.37      knu      2558: }
                   2559:
3.1       knu      2560: sub printLog($;$) {
                   2561:        my ($link, $br, $brp);
3.80      knu      2562:        ($_, $link) = @_;
                   2563:        ($br  = $_)  =~ s/\.\d+$//;
3.1       knu      2564:        ($brp = $br) =~ s/\.?\d+$//;
                   2565:        my ($isDead, $prev);
                   2566:
                   2567:        $link = 1 if (!defined($link));
                   2568:        $isDead = ($state{$_} eq "dead");
                   2569:
3.100     knu      2570:        print "<p>\n";
3.1       knu      2571:        if ($link && !$isDead) {
3.80      knu      2572:                my ($filename);
                   2573:                ($filename = $where) =~ s/^.*\///;
                   2574:                my ($fileurl) = urlencode($filename);
3.89      knu      2575:                print "<a name=\"rev$_\"></a>";
3.80      knu      2576:
                   2577:                if (defined($revsym{$_})) {
                   2578:                        foreach my $sym (split (", ", $revsym{$_})) {
3.89      knu      2579:                                print "<a name=\"$sym\"></a>";
3.80      knu      2580:                        }
3.1       knu      2581:                }
3.80      knu      2582:
                   2583:                if (defined($revsym{$br}) && $revsym{$br}
                   2584:                    && !defined($nameprinted{$br}))
                   2585:                {
                   2586:                        foreach my $sym (split (", ", $revsym{$br})) {
3.89      knu      2587:                                print "<a name=\"$sym\"></a>";
3.80      knu      2588:                        }
                   2589:                        $nameprinted{$br} = 1;
1.4       fenner   2590:                }
3.80      knu      2591:                print "\n Revision ";
                   2592:                &download_link($fileurl, $_, $_,
                   2593:                        $defaultViewable ? "text/x-cvsweb-markup" : undef);
                   2594:
                   2595:                if ($defaultViewable) {
                   2596:                        print " / (";
                   2597:                        &download_link($fileurl, $_, "download", $mimetype);
                   2598:                        print ")";
                   2599:                }
                   2600:
                   2601:                if (not $defaultTextPlain) {
                   2602:                        print " / (";
                   2603:                        &download_link($fileurl, $_, "as text", "text/plain");
                   2604:                        print ")";
                   2605:                }
                   2606:
                   2607:                if (!$defaultViewable) {
                   2608:                        print " / (";
                   2609:                        &download_link($fileurl, $_, "view",
                   2610:                                "text/x-cvsweb-markup");
                   2611:                        print ")";
                   2612:                }
                   2613:
                   2614:                if ($allow_annotate) {
                   2615:                        print " - ";
                   2616:                        print &link(
                   2617:                                'annotate',
                   2618:                                sprintf(
                   2619:                                        '%s/%s?annotate=%s%s', $scriptname,
                   2620:                                        urlencode($where),     $_,
                   2621:                                        $barequery
                   2622:                                )
                   2623:                        );
                   2624:                }
                   2625:
                   2626:                # Plus a select link if enabled, and this version isn't selected
                   2627:                if ($allow_version_select) {
                   2628:                        if ((!defined($input{"r1"}) || $input{"r1"} ne $_)) {
                   2629:                                print " - ";
                   2630:                                print &link(
                   2631:                                        '[select for diffs]',
                   2632:                                        sprintf(
                   2633:                                                '%s?r1=%s%s', $scriptwhere,
                   2634:                                                $_,           $barequery
                   2635:                                        )
                   2636:                                );
                   2637:                        } else {
                   2638:                                print " - <b>[selected]</b>";
                   2639:                        }
3.1       knu      2640:                }
3.80      knu      2641:        } else {
3.89      knu      2642:                print "Revision <b>$_</b>";
3.1       knu      2643:        }
3.80      knu      2644:
3.1       knu      2645:        if (/^1\.1\.1\.\d+$/) {
3.80      knu      2646:                print " <i>(vendor branch)</i>";
3.1       knu      2647:        }
                   2648:        if (defined @mytz) {
3.80      knu      2649:                my ($est) = $mytz[(localtime($date{$_}))[8]];
                   2650:                print ", <i>", scalar localtime($date{$_}), " $est</i> (";
3.1       knu      2651:        } else {
3.80      knu      2652:                print ", <i>", scalar gmtime($date{$_}), " UTC</i> (";
3.1       knu      2653:        }
3.80      knu      2654:        print readableTime(time() - $date{$_}, 1), " ago)";
3.1       knu      2655:        print " by ";
3.36      knu      2656:        print "<i>", $author{$_}, "</i>\n";
3.89      knu      2657:        print "<br>Branch: <b>", $link ? link_tags($revsym{$br}) : $revsym{$br},
3.80      knu      2658:            "</b>\n"
3.1       knu      2659:            if ($revsym{$br});
3.89      knu      2660:        print "<br>CVS Tags: <b>", $link ? link_tags($revsym{$_}) : $revsym{$_},
3.80      knu      2661:            "</b>"
3.1       knu      2662:            if ($revsym{$_});
3.89      knu      2663:        print "<br>Branch point for: <b>",
3.80      knu      2664:            $link ? link_tags($branchpoint{$_}) : $branchpoint{$_}, "</b>\n"
3.1       knu      2665:            if ($branchpoint{$_});
3.80      knu      2666:
3.1       knu      2667:        # Find the previous revision
3.80      knu      2668:        my @prevrev = split (/\./, $_);
3.1       knu      2669:        do {
3.80      knu      2670:                if (--$prevrev[$#prevrev] <= 0) {
                   2671:
                   2672:                        # If it was X.Y.Z.1, just make it X.Y
                   2673:                        pop (@prevrev);
                   2674:                        pop (@prevrev);
                   2675:                }
                   2676:                $prev = join (".", @prevrev);
3.1       knu      2677:        } until (defined($date{$prev}) || $prev eq "");
3.80      knu      2678:
3.1       knu      2679:        if ($prev ne "") {
3.80      knu      2680:                if ($difflines{$_}) {
                   2681:                        print
3.89      knu      2682:                            "<br>Changes since <b>$prev: $difflines{$_} lines</b>";
3.80      knu      2683:                }
3.1       knu      2684:        }
3.80      knu      2685:
3.1       knu      2686:        if ($isDead) {
3.89      knu      2687:                print "<br><b><i>FILE REMOVED</i></b>\n";
3.80      knu      2688:        } elsif ($link) {
                   2689:                my %diffrev = ();
                   2690:                $diffrev{$_} = 1;
                   2691:                $diffrev{""} = 1;
3.96      knu      2692:                print '<br>';
                   2693:                my $diff = 'Diff';
3.80      knu      2694:
                   2695:                #
                   2696:                # Offer diff to previous revision
                   2697:                if ($prev) {
                   2698:                        $diffrev{$prev} = 1;
                   2699:
                   2700:                        my $url =
                   2701:                            sprintf('%s.diff?r1=%s&r2=%s%s', $scriptwhere,
                   2702:                                $prev, $_, $barequery);
                   2703:
3.96      knu      2704:                        print $diff, " to previous ";
                   2705:                        $diff = '';
3.80      knu      2706:                        printDiffLinks($prev, $url);
                   2707:                }
                   2708:
                   2709:                #
                   2710:                # Plus, if it's on a branch, and it's not a vendor branch,
                   2711:                # offer a diff with the branch point.
                   2712:                if ($revsym{$brp} && !/^1\.1\.1\.\d+$/
                   2713:                    && !defined($diffrev{$brp}))
                   2714:                {
                   2715:                        my $url =
                   2716:                            sprintf('%s.diff?r1=%s&r2=%s%s', $scriptwhere, $brp,
                   2717:                                $_, $barequery);
                   2718:
3.96      knu      2719:                        print $diff, " to branchpoint ";
                   2720:                        $diff = '';
3.80      knu      2721:                        printDiffLinks($brp, $url);
                   2722:                }
                   2723:
                   2724:                #
                   2725:                # Plus, if it's on a branch, and it's not a vendor branch,
                   2726:                # offer to diff with the next revision of the higher branch.
                   2727:                # (e.g. change gets committed and then brought
                   2728:                # over to -stable)
                   2729:                if (/^\d+\.\d+\.\d+/ && !/^1\.1\.1\.\d+$/) {
                   2730:                        my ($i, $nextmain);
                   2731:
                   2732:                        for ($i = 0 ; $i < $#revorder && $revorder[$i] ne $_ ;
                   2733:                            $i++)
                   2734:                        {
                   2735:                        }
                   2736:                        my @tmp2 = split (/\./, $_);
                   2737:                        for ($nextmain = "" ; $i > 0 ; $i--) {
                   2738:                                my $next = $revorder[$i - 1];
                   2739:                                my @tmp1 = split (/\./, $next);
                   2740:
                   2741:                                if (@tmp1 < @tmp2) {
                   2742:                                        $nextmain = $next;
                   2743:                                        last;
                   2744:                                }
                   2745:
                   2746:                                # Only the highest version on a branch should have
                   2747:                                # a diff for the "next main".
                   2748:                                last
                   2749:                                    if (@tmp1 - 1 <= @tmp2
                   2750:                                    && join (".", @tmp1[0 .. $#tmp1 - 1]) eq
                   2751:                                    join (".", @tmp2[0 .. $#tmp1 - 1]));
                   2752:                        }
3.35      knu      2753:
3.80      knu      2754:                        if (!defined($diffrev{$nextmain})) {
                   2755:                                $diffrev{$nextmain} = 1;
3.35      knu      2756:
3.80      knu      2757:                                my $url =
                   2758:                                    sprintf('%s.diff?r1=%s&r2=%s%s',
                   2759:                                        $scriptwhere, $nextmain, $_,
                   2760:                                        $barequery);
3.35      knu      2761:
3.96      knu      2762:                                print $diff, " next main ";
                   2763:                                $diff = '';
3.80      knu      2764:                                printDiffLinks($nextmain, $url);
                   2765:                        }
1.1       jfieber  2766:                }
3.35      knu      2767:
3.80      knu      2768:                # Plus if user has selected only r1, then present a link
                   2769:                # to make a diff to that revision
                   2770:                if (defined($input{"r1"}) && !defined($diffrev{$input{"r1"}})) {
                   2771:                        $diffrev{$input{"r1"}} = 1;
                   2772:
                   2773:                        my $url =
                   2774:                            sprintf('%s.diff?r1=%s&r2=%s%s', $scriptwhere,
                   2775:                                $input{'r1'}, $_, $barequery);
                   2776:
3.96      knu      2777:                        print $diff, " to selected ";
                   2778:                        $diff = '';
3.80      knu      2779:                        printDiffLinks($input{'r1'}, $url);
                   2780:                }
3.96      knu      2781:
1.1       jfieber  2782:        }
3.100     knu      2783:        print "\n</p>\n<pre>\n";
3.101     knu      2784:        print &htmlify($log{$_}, $allow_log_extra);
3.89      knu      2785:        print "</pre>\n";
3.1       knu      2786: }
                   2787:
3.12      knu      2788: sub doLog($) {
3.80      knu      2789:        my ($fullname) = @_;
3.1       knu      2790:        my ($diffrev, $upwhere, $filename, $backurl);
3.12      knu      2791:
3.1       knu      2792:        readLog($fullname);
                   2793:
3.80      knu      2794:        html_header("CVS log for $where");
                   2795:        ($upwhere  = $where) =~ s|(Attic/)?[^/]+$||;
                   2796:        ($filename = $where) =~ s|^.*/||;
                   2797:        $backurl = $scriptname . "/" . urlencode($upwhere) . $query;
3.100     knu      2798:        print "<p>\n ";
3.80      knu      2799:        print &link($backicon, "$backurl#$filename"), " <b>Up to ",
3.100     knu      2800:            &clickablePath($upwhere, 1), "</b>\n</p>\n";
                   2801:        print "<p>\n ";
3.35      knu      2802:        print &link('Request diff between arbitrary revisions', '#diff');
3.100     knu      2803:        print "\n</p>\n<hr noshade>\n";
3.35      knu      2804:
3.100     knu      2805:        print "<p>\n";
3.1       knu      2806:        if ($curbranch) {
3.80      knu      2807:                print "Default branch: ", ($revsym{$curbranch} || $curbranch);
                   2808:        } else {
                   2809:                print "No default branch";
3.1       knu      2810:        }
3.89      knu      2811:        print "<br>\n";
3.80      knu      2812:
3.1       knu      2813:        if ($input{only_with_tag}) {
3.89      knu      2814:                print "Current tag: $input{only_with_tag}<br>\n";
3.1       knu      2815:        }
3.100     knu      2816:        print "</p>\n";
3.1       knu      2817:
                   2818:        undef %nameprinted;
                   2819:
3.80      knu      2820:        for (my $i = 0 ; $i <= $#revdisplayorder ; $i++) {
3.100     knu      2821:                print "<hr size=\"1\" noshade>\n";
3.80      knu      2822:                printLog($revdisplayorder[$i]);
3.1       knu      2823:        }
                   2824:
3.100     knu      2825:        print "<hr noshade>\n<p>\n";
3.89      knu      2826:        print "<a name=\"diff\">\n";
1.1       jfieber  2827:        print "This form allows you to request diff's between any two\n";
                   2828:        print "revisions of a file.  You may select a symbolic revision\n";
                   2829:        print "name using the selection box or you may type in a numeric\n";
                   2830:        print "name using the type-in text box.\n";
3.100     knu      2831:        print "</a>\n</p>\n";
3.80      knu      2832:        print
3.89      knu      2833:            "<form method=\"get\" action=\"${scriptwhere}.diff\" name=\"diff_select\">\n";
3.80      knu      2834:
                   2835:        foreach (@stickyvars) {
3.89      knu      2836:                printf('<input type="hidden" name="%s" value="%s">', $_,
3.80      knu      2837:                    $input{$_})
                   2838:                    if (defined($input{$_})
3.23      knu      2839:                    && ((!defined($DEFAULTVALUE{$_})
3.80      knu      2840:                    || $input{$_} ne $DEFAULTVALUE{$_}) && $input{$_} ne ""));
3.1       knu      2841:        }
3.100     knu      2842:        print "<table style=\"border: none\">\n<tr>\n";
3.110     scop     2843:        print "<td align=\"right\">";
                   2844:        print "<label for=\"r1\" accesskey=\"1\">Diffs between </label>\n";
3.111     scop     2845:        print "<select id=\"r1\" name=\"r1\">\n";
3.89      knu      2846:        print "<option value=\"text\" selected>Use Text Field</option>\n";
1.1       jfieber  2847:        print $sel;
3.89      knu      2848:        print "</select>\n";
3.1       knu      2849:        $diffrev = $revdisplayorder[$#revdisplayorder];
                   2850:        $diffrev = $input{"r1"} if (defined($input{"r1"}));
3.80      knu      2851:        print
3.110     scop     2852:            "<input type=\"text\" size=\"$inputTextSize\" name=\"tr1\" value=\"$diffrev\" onchange=\"this.form.r1.selectedIndex=0\"></td>\n";
3.100     knu      2853:        print "<td><br></td>\n</tr>\n";
3.110     scop     2854:        print "<tr>\n<td align=\"right\">";
                   2855:        print "<label for=\"r2\" accesskey=\"2\">and </label>\n";
3.111     scop     2856:        print "<select id=\"r2\" name=\"r2\">\n";
3.89      knu      2857:        print "<option value=\"text\" selected>Use Text Field</option>\n";
1.1       jfieber  2858:        print $sel;
3.89      knu      2859:        print "</select>\n";
3.1       knu      2860:        $diffrev = $revdisplayorder[0];
                   2861:        $diffrev = $input{"r2"} if (defined($input{"r2"}));
3.80      knu      2862:        print
3.110     scop     2863:            "<input type=\"text\" size=\"$inputTextSize\" name=\"tr2\" value=\"$diffrev\" onchange=\"this.form.r2.selectedIndex=0\"></td>\n";
                   2864:        print "<td><input type=\"submit\" value=\"  Get Diffs  \" accesskey=\"G\"></td>\n";
3.100     knu      2865:        print "</tr>\n</table>\n";
3.89      knu      2866:        print "</form>\n";
                   2867:        print "<hr noshade>\n";
                   2868:        print "<form method=\"get\" action=\"$scriptwhere\">\n";
3.100     knu      2869:        print "<table style=\"border: none\">\n";
3.110     scop     2870:        print "<tr>\n<td align=\"right\">";
                   2871:        print "<label for=\"f\" accesskey=\"D\">Preferred Diff type:";
                   2872:        print "</label></td>\n";
3.89      knu      2873:        print "<td>";
3.21      knu      2874:        printDiffSelect($use_java_script);
3.100     knu      2875:        print "</td>\n<td></td>\n</tr>\n";
3.80      knu      2876:
                   2877:        if (@branchnames) {
3.110     scop     2878:                print "<tr>\n<td align=\"right\">";
                   2879:                print "<label for=\"only_with_tag\" accesskey=\"B\">";
                   2880:                print "View only Branch:</label></td>\n";
3.89      knu      2881:                print "<td>";
                   2882:                print "<a name=\"branch\"></a>\n";
3.111     scop     2883:                print "<select id=\"only_with_tag\" name=\"only_with_tag\"";
3.89      knu      2884:                print " onchange=\"this.form.submit()\"" if $use_java_script;
3.80      knu      2885:                print ">\n";
3.89      knu      2886:                print "<option value=\"\"";
                   2887:                print " selected"
3.80      knu      2888:                    if (defined($input{"only_with_tag"})
                   2889:                    && $input{"only_with_tag"} eq "");
3.89      knu      2890:                print ">Show all branches</option>\n";
3.80      knu      2891:
                   2892:                foreach (reverse sort @branchnames) {
3.89      knu      2893:                        print "<option";
                   2894:                        print " selected"
3.80      knu      2895:                            if (defined($input{"only_with_tag"})
                   2896:                            && $input{"only_with_tag"} eq $_);
3.89      knu      2897:                        print ">${_}</option>\n";
3.80      knu      2898:                }
3.100     knu      2899:                print "</select></td>\n<td></td>\n</tr>\n";
1.7       fenner   2900:        }
3.80      knu      2901:
3.1       knu      2902:        foreach (@stickyvars) {
3.80      knu      2903:                next if ($_ eq "f");
                   2904:                next if ($_ eq "only_with_tag");
                   2905:                next if ($_ eq "logsort");
3.89      knu      2906:                print "<input type=\"hidden\" name=\"$_\" value=\"$input{$_}\">\n"
3.80      knu      2907:                    if (defined($input{$_})
3.23      knu      2908:                    && (!defined($DEFAULTVALUE{$_})
3.80      knu      2909:                    || $input{$_} ne $DEFAULTVALUE{$_}) && $input{$_} ne "");
3.1       knu      2910:        }
3.100     knu      2911:        print "<tr>\n<td align=\"right\">";
3.89      knu      2912:        print "<a name=\"logsort\"></a>\n";
3.110     scop     2913:        print "<label for=\"logsort\" accesskey=\"L\">Sort log by:";
                   2914:        print "</label></td>\n<td>";
3.37      knu      2915:        printLogSortSelect($use_java_script);
3.100     knu      2916:        print "</td>\n";
3.110     scop     2917:        print "<td><input type=\"submit\" value=\"  Set  \" accesskey=\"S\"></td>\n";
3.100     knu      2918:        print "</tr>\n</table>\n";
3.89      knu      2919:        print "</form>\n";
3.100     knu      2920:        html_footer();
1.10      wosch    2921: }
                   2922:
3.12      knu      2923: sub flush_diff_rows($$$$) {
3.80      knu      2924:        my $j;
                   2925:        my ($leftColRef, $rightColRef, $leftRow, $rightRow) = @_;
3.82      knu      2926:
                   2927:        if (!defined($state)) {
                   2928:                return;
                   2929:        }
                   2930:
3.80      knu      2931:        if ($state eq "PreChangeRemove") {    # we just got remove-lines before
                   2932:                for ($j = 0 ; $j < $leftRow ; $j++) {
                   2933:                        print
3.100     knu      2934:                            "<tr>\n<td class=\"diff-removed\">&nbsp;@$leftColRef[$j]</td>\n";
3.80      knu      2935:                        print
3.100     knu      2936:                            "<td class=\"diff-empty\">&nbsp;</td>\n</tr>\n";
3.80      knu      2937:                }
                   2938:        } elsif ($state eq "PreChange") {    # state eq "PreChange"
                   2939:                    # we got removes with subsequent adds
                   2940:
                   2941:                for ($j = 0 ; $j < $leftRow || $j < $rightRow ; $j++)
                   2942:                {    # dump out both cols
3.100     knu      2943:                        print "<tr>\n";
3.80      knu      2944:                        if ($j < $leftRow) {
                   2945:                                print
3.100     knu      2946:                                    "<td class=\"diff-changed\">&nbsp;@$leftColRef[$j]</td>";
3.80      knu      2947:                        } else {
                   2948:                                print
3.100     knu      2949:                                    "<td class=\"diff-changed-missing\">&nbsp;</td>";
3.80      knu      2950:                        }
3.100     knu      2951:                        print "\n";
3.80      knu      2952:
                   2953:                        if ($j < $rightRow) {
                   2954:                                print
3.100     knu      2955:                                    "<td class=\"diff-changed\">&nbsp;@$rightColRef[$j]</td>";
3.80      knu      2956:                        } else {
                   2957:                                print
3.100     knu      2958:                                    "<td class=\"diff-changed-missing\">&nbsp;</td>";
3.80      knu      2959:                        }
3.100     knu      2960:                        print "\n</tr>\n";
3.80      knu      2961:                }
                   2962:        }
3.1       knu      2963: }
                   2964:
                   2965: ##
                   2966: # Function to generate Human readable diff-files
                   2967: # human_readable_diff(String revision_to_return_to);
                   2968: ##
3.80      knu      2969: sub human_readable_diff($) {
                   2970:        my ($difftxt, $where_nd, $filename, $pathname, $scriptwhere_nd);
                   2971:        my ($fh, $rev) = @_;
                   2972:        my ($date1, $date2, $r1d, $r2d, $r1r, $r2r, $rev1, $rev2, $sym1, $sym2);
                   2973:        my (@rightCol, @leftCol);
                   2974:
                   2975:        ($where_nd       = $where)       =~ s/.diff$//;
                   2976:        ($filename       = $where_nd)    =~ s/^.*\///;
                   2977:        ($pathname       = $where_nd)    =~ s/(Attic\/)?[^\/]*$//;
                   2978:        ($scriptwhere_nd = $scriptwhere) =~ s/.diff$//;
                   2979:
                   2980:        navigateHeader($scriptwhere_nd, $pathname, $filename, $rev, "diff");
                   2981:
                   2982:        # Read header to pick up read revision and date, if possible
                   2983:        while (<$fh>) {
                   2984:                ($r1d, $r1r) = /\t(.*)\t(.*)$/ if (/^--- /);
                   2985:                ($r2d, $r2r) = /\t(.*)\t(.*)$/ if (/^\+\+\+ /);
                   2986:                last if (/^\+\+\+ /);
                   2987:        }
                   2988:
                   2989:        if (defined($r1r) && $r1r =~ /^(\d+\.)+\d+$/) {
                   2990:                $rev1  = $r1r;
                   2991:                $date1 = $r1d;
                   2992:        }
                   2993:        if (defined($r2r) && $r2r =~ /^(\d+\.)+\d+$/) {
                   2994:                $rev2  = $r2r;
                   2995:                $date2 = $r2d;
                   2996:        }
                   2997:
                   2998:        print
3.100     knu      2999:            "<h3 style=\"text-align: center\">Diff for /$where_nd between version $rev1 and $rev2</h3>\n",
                   3000:            # Using style=\"border: none\" here breaks NS 4.x badly...
3.89      knu      3001:            "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n",
3.100     knu      3002:            "<tr style=\"background-color: #ffffff\">\n", "<th width=\"50%\" valign=\"top\">",
3.80      knu      3003:            "version $rev1";
                   3004:        print ", $date1"         if (defined($date1));
                   3005:        print "<br>Tag: $sym1\n" if ($sym1);
3.89      knu      3006:        print "</th>\n", "<th width=\"50%\" valign=\"top\">", "version $rev2";
3.80      knu      3007:        print ", $date2"         if (defined($date2));
                   3008:        print "<br>Tag: $sym2\n" if ($sym1);
                   3009:        print "</th>\n";
                   3010:
                   3011:        my $leftRow  = 0;
                   3012:        my $rightRow = 0;
                   3013:        my ($oldline, $newline, $funname, $diffcode, $rest);
                   3014:
                   3015:        # Process diff text
                   3016:
                   3017:        # prefetch several lines
                   3018:        my @buf = head($fh);
                   3019:
                   3020:        my %d = scan_directives(@buf);
                   3021:
                   3022:        while (@buf || !eof($fh)) {
                   3023:                $difftxt = @buf ? shift @buf : <$fh>;
                   3024:
                   3025:                if ($difftxt =~ /^@@/) {
                   3026:                        ($oldline, $newline, $funname) =
                   3027:                            $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/;
                   3028:                        $funname = htmlquote($funname);
3.100     knu      3029:                        $funname =~ s/\s/&nbsp;/go;
3.80      knu      3030:                        print
3.100     knu      3031:                            "<tr class=\"diff-heading\">\n<td width=\"50%\">";
3.80      knu      3032:                        print
3.100     knu      3033:                            "<table width=\"100%\" border=\"1\" cellpadding=\"5\">\n<tr>\n<td><b>Line&nbsp;$oldline</b>";
3.80      knu      3034:                        print
3.100     knu      3035:                            "&nbsp;<span style=\"font-size: smaller\">$funname</span></td>\n</tr>\n</table>";
                   3036:                        print "</td>\n<td width=\"50%\">";
3.80      knu      3037:                        print
3.100     knu      3038:                            "<table width=\"100%\" border=\"1\" cellpadding=\"5\">\n<tr>\n<td><b>Line&nbsp;$newline</b>";
3.80      knu      3039:                        print
3.100     knu      3040:                            "&nbsp;<span style=\"font-size: smaller\">$funname</span></td>\n</tr>\n</table>\n";
3.80      knu      3041:                        print "</td>\n";
                   3042:                        $state    = "dump";
                   3043:                        $leftRow  = 0;
                   3044:                        $rightRow = 0;
                   3045:                } else {
                   3046:                        ($diffcode, $rest) = $difftxt =~ /^([-+ ])(.*)/;
                   3047:                        $_ = spacedHtmlText($rest, $d{'tabstop'});
                   3048:
                   3049:                        #########
                   3050:                        # little state machine to parse unified-diff output (Hen, zeller@think.de)
                   3051:                        # in order to get some nice 'ediff'-mode output
                   3052:                        # states:
                   3053:                        #  "dump"             - just dump the value
                   3054:                        #  "PreChangeRemove"  - we began with '-' .. so this could be the start of a 'change' area or just remove
                   3055:                        #  "PreChange"        - okey, we got several '-' lines and moved to '+' lines -> this is a change block
                   3056:                        ##########
                   3057:
                   3058:                        if ($diffcode eq '+') {
                   3059:                                if ($state eq "dump")
                   3060:                                { # 'change' never begins with '+': just dump out value
                   3061:                                        print
3.100     knu      3062:                                            "<tr>\n<td class=\"diff-empty\">&nbsp;</td>\n<td class=\"diff-added\">&nbsp;$_</td>\n</tr>\n";
3.80      knu      3063:                                } else {    # we got minus before
                   3064:                                        $state = "PreChange";
                   3065:                                        $rightCol[$rightRow++] = $_;
                   3066:                                }
                   3067:                        } elsif ($diffcode eq '-') {
                   3068:                                $state = "PreChangeRemove";
                   3069:                                $leftCol[$leftRow++] = $_;
                   3070:                        } else {    # empty diffcode
                   3071:                                flush_diff_rows \@leftCol, \@rightCol, $leftRow,
                   3072:                                    $rightRow;
3.100     knu      3073:                                print "<tr>\n<td class=\"diff-same\">&nbsp;$_</td>\n<td class=\"diff-same\">&nbsp;$_</td>\n</tr>\n";
3.80      knu      3074:                                $state    = "dump";
                   3075:                                $leftRow  = 0;
                   3076:                                $rightRow = 0;
                   3077:                        }
                   3078:                }
                   3079:        }
3.89      knu      3080:        close($fh);
                   3081:
3.80      knu      3082:        flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   3083:
                   3084:        # state is empty if we didn't have any change
                   3085:        if (!$state) {
3.100     knu      3086:                print "<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>\n";
                   3087:                print "<tr class=\"diff-empty\">\n";
3.80      knu      3088:                print
3.100     knu      3089:                    "<td colspan=\"2\" align=\"center\"><b>- No viewable change -</b></td>\n</tr>\n";
3.80      knu      3090:        }
3.100     knu      3091:        print "</table>\n";
3.80      knu      3092:
3.100     knu      3093:        print "<hr style=\"width: 100%\" noshade>\n";
                   3094:        print "<form method=\"get\" action=\"${scriptwhere}\">\n";
                   3095:        print "<table style=\"border: none\">\n<tr>\n<td>\n";
3.80      knu      3096:
                   3097:        # print legend
3.100     knu      3098:        print "<table border=\"1\">\n<tr>\n<td>";
                   3099:        print "Legend:<br><table style=\"border: none\" cellspacing=\"0\" cellpadding=\"1\">\n";
3.80      knu      3100:        print
3.100     knu      3101:            "<tr>\n<td align=\"center\" class=\"diff-removed\">Removed from v.$rev1</td>\n<td class=\"diff-empty\">&nbsp;</td>\n</tr>\n";
3.80      knu      3102:        print
3.100     knu      3103:            "<tr class=\"diff-changed\">\n<td align=\"center\" colspan=\"2\">changed lines</td>\n</tr>\n";
3.80      knu      3104:        print
3.100     knu      3105:            "<tr>\n<td class=\"diff-empty\">&nbsp;</td>\n<td align=\"center\" class=\"diff-added\">Added in v.$rev2</td>\n</tr>\n";
                   3106:        print "</table>\n</td>\n</tr>\n</table>\n</td>\n<td>";
3.80      knu      3107:
                   3108:        # Print format selector
                   3109:        foreach my $var (keys %input) {
                   3110:                next if ($var eq "f");
                   3111:                next
                   3112:                    if (defined($DEFAULTVALUE{$var})
                   3113:                    && $DEFAULTVALUE{$var} eq $input{$var});
3.89      knu      3114:                print "<input type=\"hidden\" name=\"", urlencode($var),
                   3115:                    "\" value=\"", urlencode($input{$var}), "\">\n";
3.80      knu      3116:        }
                   3117:        printDiffSelect($use_java_script);
3.89      knu      3118:        print "<input type=\"submit\" value=\"Show\">\n";
3.100     knu      3119:        print "</td>\n";
                   3120:
                   3121:        print "</tr>\n</table>\n";
3.89      knu      3122:        print "</form>\n";
3.1       knu      3123: }
                   3124:
3.12      knu      3125: sub navigateHeader($$$$$) {
3.80      knu      3126:        my ($swhere, $path, $filename, $rev, $title) = @_;
                   3127:        $swhere = "" if ($swhere eq $scriptwhere);
                   3128:        $swhere = './' . urlencode($filename) if ($swhere eq "");
3.64      knu      3129:
3.100     knu      3130:        # TODO: this should be moved into external CSS file.
                   3131:        my $css = '';
                   3132:        if ($title eq 'diff') {
                   3133:            $css = "
                   3134: <style type=\"text/css\">
                   3135: .diff-heading {
                   3136:   background-color: $diffcolorHeading;
                   3137: }
                   3138: .diff-same {
                   3139:   font-family: $difffontface;
                   3140:   font-size: smaller;
                   3141: }
                   3142: .diff-empty {
                   3143:   background-color: $diffcolorEmpty;
                   3144: }
                   3145: .diff-added {
                   3146:   background-color: $diffcolorAdd;
                   3147:   font-family: $difffontface;
                   3148:   font-size: smaller;
                   3149: }
                   3150: .diff-removed {
                   3151:   background-color: $diffcolorRemove;
                   3152:   font-family: $difffontface;
                   3153:   font-size: smaller;
                   3154: }
                   3155: .diff-changed {
                   3156:   background-color: $diffcolorChange;
                   3157:   font-family: $difffontface;
                   3158:   font-size: smaller;
                   3159: }
                   3160: .diff-changed-missing {
                   3161:   background-color: $diffcolorDarkChange;
                   3162: }
                   3163: </style>";
                   3164:        }
                   3165:
3.80      knu      3166:        print <<EOF;
3.100     knu      3167: $HTML_DOCTYPE
3.89      knu      3168: <html>
                   3169: <head>
3.100     knu      3170: <title>$path$filename - $title - $rev</title>$css
3.106     scop     3171: $HTML_META</head>
3.64      knu      3172: $body_tag_for_src
3.100     knu      3173: <table width="100%" style="border: none; background-color: $navigationHeaderColor" cellspacing="0" cellpadding="1">
3.89      knu      3174: <tr valign="bottom"><td>
3.64      knu      3175: EOF
                   3176:
3.80      knu      3177:        print &link($backicon, "$swhere$query#rev$rev");
                   3178:        print "<b>Return to ", &link($filename, "$swhere$query#rev$rev"),
                   3179:            " CVS log";
                   3180:        print "</b> $fileicon</td>";
                   3181:
3.89      knu      3182:        print "<td align=\"right\">$diricon <b>Up to ",
                   3183:          &clickablePath($path, 1),
3.80      knu      3184:            "</b></td>";
                   3185:        print "</tr></table>";
3.1       knu      3186: }
                   3187:
3.12      knu      3188: sub plural_write($$) {
3.80      knu      3189:        my ($num, $text) = @_;
                   3190:        if ($num != 1) {
                   3191:                $text .= "s";
                   3192:        }
                   3193:
                   3194:        if ($num > 0) {
                   3195:                return join (' ', $num, $text);
                   3196:        } else {
                   3197:                return "";
                   3198:        }
3.1       knu      3199: }
                   3200:
                   3201: ##
                   3202: # print readable timestamp in terms of
                   3203: # '..time ago'
                   3204: # H. Zeller <zeller@think.de>
                   3205: ##
3.12      knu      3206: sub readableTime($$) {
3.80      knu      3207:        my ($i, $break, $retval);
                   3208:        my ($secs, $long) = @_;
3.1       knu      3209:
3.80      knu      3210:        # this function works correct for time >= 2 seconds
                   3211:        if ($secs < 2) {
                   3212:                return "very little time";
                   3213:        }
                   3214:
                   3215:        my %desc = (
                   3216:                1,        'second', 60,     'minute', 3600,    'hour',
                   3217:                86400,    'day',    604800, 'week',   2628000, 'month',
                   3218:                31536000, 'year'
                   3219:        );
                   3220:        my @breaks = sort { $a <=> $b } keys %desc;
                   3221:        $i = 0;
3.1       knu      3222:
3.80      knu      3223:        while ($i <= $#breaks && $secs >= 2 * $breaks[$i]) {
                   3224:                $i++;
                   3225:        }
3.1       knu      3226:        $i--;
3.80      knu      3227:        $break  = $breaks[$i];
                   3228:        $retval = plural_write(int($secs / $break), $desc{$break});
                   3229:
                   3230:        if ($long == 1 && $i > 0) {
                   3231:                my $rest = $secs % $break;
                   3232:                $i--;
                   3233:                $break = $breaks[$i];
                   3234:                my $resttime = plural_write(int($rest / $break), $desc{$break});
                   3235:                if ($resttime) {
                   3236:                        $retval .= ", $resttime";
                   3237:                }
3.1       knu      3238:        }
                   3239:
3.80      knu      3240:        return $retval;
3.1       knu      3241: }
                   3242:
                   3243: ##
                   3244: # clickablePath(String pathname, boolean last_item_clickable)
                   3245: #
                   3246: # returns a html-ified path whereas each directory is a link for
                   3247: # faster navigation. last_item_clickable controls whether the
                   3248: # basename (last directory/file) is a link as well
                   3249: ##
                   3250: sub clickablePath($$) {
3.80      knu      3251:        my ($pathname, $clickLast) = @_;
                   3252:        my $retval = '';
3.12      knu      3253:
3.80      knu      3254:        if ($pathname eq '/') {
                   3255:
                   3256:                # this should never happen - chooseCVSRoot() is
                   3257:                # intended to do this
                   3258:                $retval = "[$cvstree]";
                   3259:        } else {
                   3260:                $retval .= ' ' . &link("[$cvstree]",
                   3261:                        sprintf('%s/%s#dirlist', $scriptname, $query));
                   3262:                my $wherepath = '';
                   3263:                my ($lastslash) = $pathname =~ m|/$|;
                   3264:
                   3265:                foreach (split (/\//, $pathname)) {
                   3266:                        $retval .= " / ";
                   3267:                        $wherepath .= "/$_";
                   3268:                        my ($last) = "$wherepath/" eq "/$pathname"
                   3269:                            || $wherepath eq "/$pathname";
                   3270:
                   3271:                        if ($clickLast || !$last) {
                   3272:                                $retval .= &link($_,
                   3273:                                        join ('', $scriptname,
                   3274:                                        urlencode($wherepath),
                   3275:                                        (!$last || $lastslash ? '/' : ''),
                   3276:                                        $query,
                   3277:                                        (!$last || $lastslash ? "#dirlist" : "")
                   3278:                                ));
                   3279:                        } else {    # do not make a link to the current dir
                   3280:                                $retval .= $_;
                   3281:                        }
                   3282:                }
3.1       knu      3283:        }
3.80      knu      3284:        return $retval;
3.1       knu      3285: }
                   3286:
                   3287: sub chooseCVSRoot() {
3.100     knu      3288:
                   3289:        print "<form method=\"get\" action=\"${scriptwhere}\">\n";
3.80      knu      3290:        if (2 <= @CVSROOT) {
                   3291:                my ($k);
                   3292:                foreach $k (keys %input) {
3.89      knu      3293:                        print "<input type=\"hidden\" name=\"$k\" value=\"$input{$k}\">\n"
3.80      knu      3294:                            if ($input{$k}) && ($k ne "cvsroot");
                   3295:                }
                   3296:
                   3297:                # Form-Elements look wierd in Netscape if the background
                   3298:                # isn't gray and the form elements are not placed
                   3299:                # within a table ...
3.100     knu      3300:                print "<table style=\"border: none\">\n<tr>\n";
3.110     scop     3301:                print "<td><label for=\"cvsroot\" accesskey=\"C\">CVS Root:</label></td>\n";
3.111     scop     3302:                print "<td>\n<select id=\"cvsroot\" name=\"cvsroot\"";
3.89      knu      3303:                print " onchange=\"this.form.submit()\"" if $use_java_script;
3.80      knu      3304:                print ">\n";
                   3305:
                   3306:                foreach $k (@CVSROOT) {
                   3307:                        print "<option value=\"$k\"";
                   3308:                        print " selected" if ($k eq $cvstree);
3.89      knu      3309:                        print ">",($CVSROOTdescr{$k} ? $CVSROOTdescr{$k} : $k),
3.80      knu      3310:                            "</option>\n";
                   3311:                }
3.100     knu      3312:                print "</select>\n</td>\n<td>";
3.80      knu      3313:        } else {
                   3314:
3.100     knu      3315:                # no choice -- but we need the form to select module/path,
                   3316:                # at least for Netscape
                   3317:                print "<p>\n";
3.80      knu      3318:                print "CVS Root: <b>[$cvstree]</b>";
3.1       knu      3319:        }
3.57      knu      3320:
3.110     scop     3321:        print " <label for=\"path\" accesskey=\"P\">Module path or alias:";
                   3322:        print "</label>\n";
3.111     scop     3323:        print "<input type=\"text\" id=\"path\" name=\"path\" value=\"\" size=\"15\">\n";
3.110     scop     3324:        print "<input type=\"submit\" value=\"Go\" accesskey=\"G\">";
3.80      knu      3325:
                   3326:        if (2 <= @CVSROOT) {
3.100     knu      3327:                print "</td>\n</tr>\n</table>";
                   3328:        } else {
                   3329:                print "</p>";
3.80      knu      3330:        }
3.100     knu      3331:        print "\n</form>";
3.1       knu      3332: }
                   3333:
                   3334: sub chooseMirror() {
3.80      knu      3335:        my ($mirror, $moremirrors);
                   3336:        $moremirrors = 0;
                   3337:
                   3338:        # This code comes from the original BSD-cvsweb
                   3339:        # and may not be useful for your site; If you don't
                   3340:        # set %MIRRORS this won't show up, anyway
                   3341:        #
                   3342:        # Should perhaps exlude the current site somehow..
                   3343:        if (keys %MIRRORS) {
3.109     scop     3344:                print "\nThis CVSweb is mirrored in:\n";
3.80      knu      3345:
                   3346:                foreach $mirror (keys %MIRRORS) {
                   3347:                        print ", " if ($moremirrors);
                   3348:                        print &link(htmlquote($mirror), $MIRRORS{$mirror});
                   3349:                        $moremirrors = 1;
                   3350:                }
                   3351:                print "<p>\n";
3.1       knu      3352:        }
                   3353: }
                   3354:
3.12      knu      3355: sub fileSortCmp() {
3.80      knu      3356:        my ($comp) = 0;
                   3357:        my ($c, $d, $af, $bf);
3.1       knu      3358:
3.80      knu      3359:        ($af = $a) =~ s/,v$//;
                   3360:        ($bf = $b) =~ s/,v$//;
                   3361:        my ($rev1, $date1, $log1, $author1, $filename1) = @{$fileinfo{$af}}
                   3362:            if (defined($fileinfo{$af}));
                   3363:        my ($rev2, $date2, $log2, $author2, $filename2) = @{$fileinfo{$bf}}
                   3364:            if (defined($fileinfo{$bf}));
                   3365:
                   3366:        if (defined($filename1) && defined($filename2) && $af eq $filename1
                   3367:            && $bf eq $filename2)
                   3368:        {
                   3369:
                   3370:                # Two files
                   3371:                $comp = -revcmp($rev1, $rev2) if ($byrev && $rev1 && $rev2);
                   3372:                $comp = ($date2 <=> $date1) if ($bydate && $date1 && $date2);
                   3373:                $comp = ($log1 cmp $log2) if ($bylog && $log1 && $log2);
                   3374:                $comp = ($author1 cmp $author2)
                   3375:                    if ($byauthor && $author1 && $author2);
                   3376:        }
                   3377:
                   3378:        if ($comp == 0) {
                   3379:
3.113   ! scop     3380:                # Directories first, then files under version control,
        !          3381:                # then other, "rogue" files.
        !          3382:                # Sort by filename if no other criteria available.
        !          3383:
        !          3384:                my $ad = ((-d "$fullname/$a") ? 'D'
        !          3385:                    : (defined($fileinfo{$af}) ? 'F' : 'R'));
        !          3386:                my $bd = ((-d "$fullname/$b") ? 'D'
        !          3387:                    : (defined($fileinfo{$bf}) ? 'F' : 'R'));
3.80      knu      3388:                ($c = $a) =~ s|.*/||;
                   3389:                ($d = $b) =~ s|.*/||;
                   3390:                $comp = ("$ad$c" cmp "$bd$d");
                   3391:        }
                   3392:        return $comp;
3.1       knu      3393: }
                   3394:
                   3395: # make A url for downloading
3.12      knu      3396: sub download_url($$;$) {
3.80      knu      3397:        my ($url, $revision, $mimetype) = @_;
3.1       knu      3398:
3.80      knu      3399:        $revision =~ s/\b0\.//;
3.1       knu      3400:
3.80      knu      3401:        if (defined($checkoutMagic)
                   3402:            && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup"))
                   3403:        {
                   3404:                my $path = $where;
3.84      knu      3405:                $path =~ s|[^/]+$||;
3.80      knu      3406:                $url = "$scriptname/$checkoutMagic/${path}$url";
                   3407:        }
                   3408:        $url .= "?rev=$revision";
                   3409:        $url .= '&content-type=' . urlencode($mimetype) if (defined($mimetype));
3.67      knu      3410:
3.80      knu      3411:        $url;
3.1       knu      3412: }
                   3413:
3.12      knu      3414: # Presents a link to download the
3.1       knu      3415: # selected revision
3.12      knu      3416: sub download_link($$$;$) {
3.80      knu      3417:        my ($url, $revision, $textlink, $mimetype) = @_;
                   3418:        my ($fullurl) = download_url($url, $revision, $mimetype);
3.35      knu      3419:
3.80      knu      3420:        $fullurl =~ s/:/sprintf("%%%02x", ord($&))/eg;
3.70      knu      3421:
3.89      knu      3422:        printf '<a href="%s"', hrefquote("$fullurl$barequery");
3.35      knu      3423:
3.80      knu      3424:        if ($open_extern_window
                   3425:            && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup"))
                   3426:        {
                   3427:                print ' target="cvs_checkout"';
                   3428:
                   3429:                # we should have
                   3430:                #   'if (document.cvswin==null) document.cvswin=window.open(...'
                   3431:                # in order to allow the user to resize the window; otherwise
                   3432:                # the user may resize the window, but on next checkout - zap -
                   3433:                # its original (configured s. cvsweb.conf) size is back again
                   3434:                # .. annoying (if $extern_window_(width|height) is defined)
                   3435:                # but this if (..) solution is far from perfect
                   3436:                # what we need to do as well is
                   3437:                # 1) save cvswin in an invisible frame that always exists
                   3438:                #    (document.cvswin will be void on next load)
                   3439:                # 2) on close of the cvs_checkout - window set the cvswin
                   3440:                #    variable to 'null' again - so that it will be
                   3441:                #    reopenend with the configured size
                   3442:                # anyone a JavaScript programmer ?
                   3443:                # .. so here without if (..):
                   3444:                # currently, the best way is to comment out the size parameters
                   3445:                # ($extern_window...) in cvsweb.conf.
                   3446:                if ($use_java_script) {
                   3447:                        my @attr = qw(resizeable scrollbars);
                   3448:
                   3449:                        push @attr, qw(status toolbar)
                   3450:                            if (defined($mimetype) && $mimetype eq "text/html");
                   3451:
                   3452:                        push @attr, "width=$extern_window_width"
                   3453:                            if (defined($extern_window_width));
                   3454:
                   3455:                        push @attr, "height=$extern_window_height"
                   3456:                            if (defined($extern_window_height));
                   3457:
3.89      knu      3458:                        # We need the "return false" here to prevent browsers
                   3459:                        # from following the href after the onclick handler.
                   3460:                        # This would effectively load the same document in
                   3461:                        # the same window *twice*.
3.80      knu      3462:                        printf
3.89      knu      3463:                            q` onclick="window.open('%s','cvs_checkout','%s');return false"`,
3.80      knu      3464:                            hrefquote($fullurl), join (',', @attr);
                   3465:                }
3.1       knu      3466:        }
3.89      knu      3467:        print "><b>$textlink</b></a>";
3.1       knu      3468: }
                   3469:
                   3470: # Returns a Query string with the
                   3471: # specified parameter toggled
                   3472: sub toggleQuery($$) {
3.80      knu      3473:        my ($toggle, $value) = @_;
                   3474:        my ($newquery, $var);
                   3475:        my (%vars);
                   3476:        %vars = %input;
                   3477:
                   3478:        if (defined($value)) {
                   3479:                $vars{$toggle} = $value;
                   3480:        } else {
                   3481:                $vars{$toggle} = $vars{$toggle} ? 0 : 1;
                   3482:        }
                   3483:
                   3484:        # Build a new query of non-default paramenters
                   3485:        $newquery = "";
                   3486:        foreach $var (@stickyvars) {
                   3487:                my ($value) = defined($vars{$var}) ? $vars{$var} : "";
                   3488:                my ($default) =
                   3489:                    defined($DEFAULTVALUE{$var}) ? $DEFAULTVALUE{$var} : "";
                   3490:
                   3491:                if ($value ne $default) {
                   3492:                        $newquery .= "&" if ($newquery ne "");
                   3493:                        $newquery .= urlencode($var) . "=" . urlencode($value);
                   3494:                }
                   3495:        }
                   3496:
                   3497:        if ($newquery) {
                   3498:                return '?' . $newquery;
                   3499:        }
                   3500:        return "";
3.1       knu      3501: }
                   3502:
3.12      knu      3503: sub urlencode($) {
3.80      knu      3504:        local ($_) = @_;
3.35      knu      3505:
3.80      knu      3506:        s/[\000-+{-\377]/sprintf("%%%02x", ord($&))/ge;
3.35      knu      3507:
3.80      knu      3508:        $_;
3.35      knu      3509: }
                   3510:
                   3511: sub htmlquote($) {
3.80      knu      3512:        local ($_) = @_;
3.35      knu      3513:
3.80      knu      3514:        # Special Characters; RFC 1866
                   3515:        s/&/&amp;/g;
                   3516:        s/\"/&quot;/g;
                   3517:        s/</&lt;/g;
                   3518:        s/>/&gt;/g;
3.35      knu      3519:
3.80      knu      3520:        $_;
3.1       knu      3521: }
                   3522:
3.36      knu      3523: sub htmlunquote($) {
3.80      knu      3524:        local ($_) = @_;
3.36      knu      3525:
3.80      knu      3526:        # Special Characters; RFC 1866
                   3527:        s/&quot;/\"/g;
                   3528:        s/&lt;/</g;
                   3529:        s/&gt;/>/g;
                   3530:        s/&amp;/&/g;
3.36      knu      3531:
3.80      knu      3532:        $_;
3.36      knu      3533: }
                   3534:
3.46      knu      3535: sub hrefquote($) {
3.80      knu      3536:        local ($_) = @_;
3.46      knu      3537:
3.80      knu      3538:        y/ /+/;
3.46      knu      3539:
3.80      knu      3540:        htmlquote($_)
3.46      knu      3541: }
                   3542:
3.12      knu      3543: sub http_header(;$) {
3.80      knu      3544:        my $content_type = shift || "text/html";
3.50      knu      3545:
3.80      knu      3546:        $content_type .= "; charset=$charset"
                   3547:            if $content_type =~ m,^text/, && defined($charset) && $charset;
                   3548:
                   3549:        if (defined($moddate)) {
                   3550:                if ($is_mod_perl) {
                   3551:                        Apache->request->header_out(
                   3552:                                "Last-Modified" => scalar gmtime($moddate)
                   3553:                                . " GMT");
                   3554:                } else {
                   3555:                        print "Last-Modified: ", scalar gmtime($moddate),
                   3556:                            " GMT\r\n";
                   3557:                }
                   3558:        }
3.50      knu      3559:
3.1       knu      3560:        if ($is_mod_perl) {
3.80      knu      3561:                Apache->request->content_type($content_type);
                   3562:        } else {
3.89      knu      3563:                print "Content-Type: $content_type\r\n";
3.1       knu      3564:        }
3.80      knu      3565:
                   3566:        if ($allow_compress && $maycompress) {
                   3567:                if ($has_zlib
                   3568:                    || (defined($CMD{gzip}) && open(GZIP, "| $CMD{gzip} -1 -c"))
                   3569:                    )
                   3570:                {
                   3571:
                   3572:                        if ($is_mod_perl) {
                   3573:                                Apache->request->content_encoding("x-gzip");
                   3574:                                Apache->request->header_out(
                   3575:                                        Vary => "Accept-Encoding");
                   3576:                                Apache->request->send_http_header;
                   3577:                        } else {
3.89      knu      3578:                                print "Content-Encoding: x-gzip\r\n";
3.80      knu      3579:                                print "Vary: Accept-Encoding\r\n"
                   3580:                                    ;            #RFC 2068, 14.43
                   3581:                                print "\r\n";    # Close headers
                   3582:                        }
                   3583:                        $| = 1;
                   3584:                        $| = 0;    # Flush header output
                   3585:
                   3586:                        if ($has_zlib) {
                   3587:                                tie *GZIP, __PACKAGE__, \*STDOUT;
                   3588:                        }
                   3589:                        select(GZIP);
                   3590:                        $gzip_open = 1;
                   3591:
                   3592:                        #           print "<!-- gzipped -->" if ($content_type =~ m|^text/html\b|);
                   3593:                } else {
                   3594:                        if ($is_mod_perl) {
                   3595:                                Apache->request->send_http_header;
                   3596:                        } else {
                   3597:                                print "\r\n";    # Close headers
                   3598:                        }
                   3599:                        print
3.100     knu      3600:                            "<span style=\"font-size: smaller\">Unable to find gzip binary in the <b>\$command_path</b> ($command_path) to compress output</span><br>";
3.80      knu      3601:                }
                   3602:        } else {
                   3603:
                   3604:                if ($is_mod_perl) {
                   3605:                        Apache->request->send_http_header;
                   3606:                } else {
                   3607:                        print "\r\n";    # Close headers
                   3608:                }
3.1       knu      3609:        }
                   3610: }
                   3611:
                   3612: sub html_header($) {
3.80      knu      3613:        my ($title) = @_;
                   3614:        http_header("text/html");
                   3615:        print <<EOH;
3.100     knu      3616: $HTML_DOCTYPE
3.1       knu      3617: <html>
3.40      knu      3618: <head>
3.1       knu      3619: <title>$title</title>
3.106     scop     3620: $HTML_META</head>
3.1       knu      3621: $body_tag
                   3622: $logo <h1 align="center">$title</h1>
                   3623: EOH
                   3624: }
                   3625:
3.12      knu      3626: sub html_footer() {
3.100     knu      3627:        print "<hr noshade>\n<address>$address</address>\n</body>\n</html>\n";
3.1       knu      3628: }
                   3629:
3.12      knu      3630: sub link_tags($) {
3.80      knu      3631:        my ($tags) = @_;
                   3632:        my ($ret)  = "";
                   3633:        my ($fileurl, $filename);
                   3634:
                   3635:        ($filename = $where) =~ s/^.*\///;
                   3636:        $fileurl = './' . urlencode($filename);
                   3637:
                   3638:        foreach my $sym (split (", ", $tags)) {
                   3639:                $ret .= ",\n" if ($ret ne "");
                   3640:                $ret .=
                   3641:                    &link($sym, $fileurl . toggleQuery('only_with_tag', $sym));
                   3642:        }
                   3643:        return "$ret\n";
3.1       knu      3644: }
                   3645:
                   3646: #
3.81      knu      3647: # See if a module is listed in the config file's @HideModules list.
3.1       knu      3648: #
3.12      knu      3649: sub forbidden_module($) {
3.80      knu      3650:        my ($module) = @_;
3.81      knu      3651:        local $_;
3.12      knu      3652:
3.81      knu      3653:        for (@HideModules) {
                   3654:                return 1 if $module =~ $_;
3.80      knu      3655:        }
3.81      knu      3656:        return 0;
                   3657: }
3.1       knu      3658:
3.81      knu      3659: sub forbidden_file($) {
                   3660:        my ($path) = @_;
                   3661:        $path =  substr($path, length($cvsroot) + 1);
                   3662:        local $_;
                   3663:        for (@ForbiddenFiles) {
                   3664:                return 1 if $path =~ $_;
                   3665:        }
3.80      knu      3666:        return 0;
3.25      knu      3667: }
                   3668:
                   3669: # Close the GZIP handle remove the tie.
                   3670:
                   3671: sub gzipclose {
                   3672:        if ($gzip_open) {
3.80      knu      3673:                select(STDOUT);
                   3674:                close(GZIP);
                   3675:                untie *GZIP;
                   3676:                $gzip_open = 0;
3.25      knu      3677:        }
3.23      knu      3678: }
                   3679:
                   3680: # implement a gzipped file handle via the Compress:Zlib compression
                   3681: # library.
                   3682:
                   3683: sub MAGIC1() { 0x1f }
                   3684: sub MAGIC2() { 0x8b }
3.80      knu      3685: sub OSCODE() { 3 }
3.23      knu      3686:
                   3687: sub TIEHANDLE {
                   3688:        my ($class, $out) = @_;
3.80      knu      3689:        my ($d) = Compress::Zlib::deflateInit(
                   3690:                -Level      => Compress::Zlib::Z_BEST_COMPRESSION(),
                   3691:                -WindowBits => -Compress::Zlib::MAX_WBITS()
                   3692:        ) or return undef;
3.23      knu      3693:        my ($o) = {
                   3694:                handle => $out,
3.80      knu      3695:                dh     => $d,
                   3696:                crc    => 0,
                   3697:                len    => 0,
3.23      knu      3698:        };
3.80      knu      3699:        my ($header) =
                   3700:            pack("c10", MAGIC1, MAGIC2, Compress::Zlib::Z_DEFLATED(), 0, 0, 0,
                   3701:            0, 0, 0, OSCODE);
3.23      knu      3702:        print {$o->{handle}} $header;
                   3703:        return bless($o, $class);
                   3704: }
                   3705:
                   3706: sub PRINT {
3.80      knu      3707:        my ($o)   = shift;
                   3708:        my ($buf) = join (defined $, ? $, : "", @_);
3.23      knu      3709:        my ($len) = length($buf);
                   3710:        my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3711:        print {$o->{handle}} $compressed if defined($compressed);
                   3712:        $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3713:        $o->{len} += $len;
                   3714:        return $len;
                   3715: }
                   3716:
                   3717: sub PRINTF {
3.80      knu      3718:        my ($o)   = shift;
3.23      knu      3719:        my ($fmt) = shift;
                   3720:        my ($buf) = sprintf($fmt, @_);
                   3721:        my ($len) = length($buf);
                   3722:        my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3723:        print {$o->{handle}} $compressed if defined($compressed);
                   3724:        $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3725:        $o->{len} += $len;
                   3726:        return $len;
                   3727: }
                   3728:
                   3729: sub WRITE {
                   3730:        my ($o, $buf, $len, $off) = @_;
                   3731:        my ($compressed, $status) = $o->{dh}->deflate(substr($buf, 0, $len));
                   3732:        print {$o->{handle}} $compressed if defined($compressed);
                   3733:        $o->{crc} = Compress::Zlib::crc32(substr($buf, 0, $len), $o->{crc});
                   3734:        $o->{len} += $len;
                   3735:        return $len;
                   3736: }
                   3737:
                   3738: sub CLOSE {
                   3739:        my ($o) = @_;
3.80      knu      3740:        return if !defined($o->{dh});
3.23      knu      3741:        my ($buf) = $o->{dh}->flush();
                   3742:        $buf .= pack("V V", $o->{crc}, $o->{len});
                   3743:        print {$o->{handle}} $buf;
                   3744:        undef $o->{dh};
                   3745: }
                   3746:
                   3747: sub DESTROY {
                   3748:        my ($o) = @_;
                   3749:        CLOSE($o);
1.1       jfieber  3750: }

CVSweb