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

Annotation of cvsweb/cvsweb.cgi, Revision 3.192

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

CVSweb