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

Annotation of cvsweb/cvsweb.cgi, Revision 3.131

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

CVSweb