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

Annotation of cvsweb/cvsweb.cgi, Revision 3.180

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

CVSweb