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

Annotation of cvsweb/cvsweb.cgi, Revision 3.269

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

CVSweb