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

Annotation of cvsweb/cvsweb.cgi, Revision 3.161

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

CVSweb