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

Annotation of cvsweb/cvsweb.cgi, Revision 3.119.2.9

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

CVSweb