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

Annotation of cvsweb/cvsweb.cgi, Revision 3.186

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

CVSweb