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

Annotation of cvsweb/cvsweb.cgi, Revision 3.127

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

CVSweb