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

Annotation of cvsweb/cvsweb.cgi, Revision 3.184

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

CVSweb