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

Annotation of cvsweb/cvsweb.cgi, Revision 3.210

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

CVSweb