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

Annotation of cvsweb/cvsweb.cgi, Revision 3.241

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

CVSweb