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

Annotation of cvsweb/cvsweb.cgi, Revision 3.98

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

CVSweb