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

Annotation of cvsweb/cvsweb.cgi, Revision 3.190

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