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

Annotation of cvsweb/cvsweb.cgi, Revision 3.132

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

CVSweb