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

Annotation of cvsweb/cvsweb.cgi, Revision 3.123

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

CVSweb