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

Annotation of cvsweb/cvsweb.cgi, Revision 3.254

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

CVSweb