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

Annotation of cvsweb/cvsweb.cgi, Revision 3.140

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

CVSweb