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

Annotation of cvsweb/cvsweb.cgi, Revision 3.191

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

CVSweb