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

Annotation of cvsweb/cvsweb.cgi, Revision 3.218

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

CVSweb