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

Annotation of cvsweb/cvsweb.cgi, Revision 3.238

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

CVSweb