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

Annotation of cvsweb/cvsweb.cgi, Revision 3.213

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

CVSweb