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

Annotation of cvsweb/cvsweb.cgi, Revision 3.284

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

CVSweb