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

Annotation of cvsweb/cvsweb.cgi, Revision 3.143

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.141     scop       60:   $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:
3.142     scop      194: # Get rid of unsafe environment vars.  Don't do this in BEGIN...
                    195: delete(@ENV{qw(PATH IFS CDPATH ENV BASH_ENV)});
3.59      knu       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:   {
3.141     scop      589:     @fatal = ('500 Internal Error', 'cvs export failure: %s: %s', $!, $module);
3.120     scop      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{
3.143   ! knu      1297:           (
        !          1298:            \b ( \w[\w+\-.]* (?: ::\w[\w+\-.]*)* )
3.120     scop     1299:            (?:
3.143   ! knu      1300:             \( ([0-9n]) \) \B
3.120     scop     1301:             |
3.143   ! knu      1302:             \. ([0-9n]) \b
3.120     scop     1303:            )
                   1304:           )
                   1305:          }{
3.143   ! knu      1306:            my($text, $name, $section) = ($1, $2, defined($3) ? $3 : $4);
        !          1307:            ($name =~ /[^\d:]/ && $name !~ /\.(:|$)/)
        !          1308:             ? &link($text, sprintf($mancgi, $section, $name))
        !          1309:              : $text;
3.120     scop     1310:          }egx;
                   1311:       } $_;
                   1312:     }
                   1313:   }
                   1314:
                   1315:   $_;
                   1316: }
                   1317:
                   1318: sub spacedHtmlText($;$)
                   1319: {
                   1320:   local $_ = $_[0];
                   1321:   my $ts = $_[1] || $tabstop;
                   1322:
                   1323:   # Cut trailing spaces and tabs
                   1324:   s/[ \t]+$//;
                   1325:
                   1326:   if (defined($ts)) {
                   1327:
                   1328:     # Expand tabs
                   1329:     1 while s/\t+/' ' x (length($&) * $ts - length($`) % $ts)/e;
                   1330:   }
                   1331:
                   1332:   # replace <tab> and <space> (\001 is to protect us from htmlify)
                   1333:   # gzip can make excellent use of this repeating pattern :-)
                   1334:   if ($hr_breakable) {
                   1335:
                   1336:     # make every other space 'breakable'
                   1337:     s/  / \001nbsp;/g;    # 2 * <space>
                   1338:                           # leave single space as it is
                   1339:   } else {
                   1340:     s/ /\001nbsp;/g;
                   1341:   }
                   1342:
                   1343:   $_ = htmlify($_, $allow_source_extra);
                   1344:
                   1345:   # unescape
                   1346:   y/\001/&/;
                   1347:
                   1348:   return $_;
                   1349: }
                   1350:
                   1351: # Note that this doesn't htmlquote the first argument...
                   1352: sub link($$)
                   1353: {
                   1354:   my ($name, $url) = @_;
                   1355:
                   1356:   $url =~ s/:/sprintf("%%%02x", ord($&))/eg
                   1357:     if $url =~ /^[^a-z]/;    # relative
                   1358:
                   1359:   sprintf '<a href="%s">%s</a>', hrefquote($url), $name;
                   1360: }
                   1361:
                   1362: sub revcmp($$)
                   1363: {
                   1364:   my ($rev1, $rev2) = @_;
                   1365:
                   1366:   # make no comparison for a tag or a branch
                   1367:   return 0 if $rev1 =~ /[^\d.]/ || $rev2 =~ /[^\d.]/;
                   1368:
                   1369:   my (@r1) = split (/\./, $rev1);
                   1370:   my (@r2) = split (/\./, $rev2);
                   1371:   my ($a, $b);
                   1372:
                   1373:   while (($a = shift (@r1)) && ($b = shift (@r2))) {
                   1374:     if ($a != $b) {
                   1375:       return $a <=> $b;
                   1376:     }
                   1377:   }
                   1378:   if (@r1) { return  1; }
                   1379:   if (@r2) { return -1; }
                   1380:   return 0;
                   1381: }
                   1382:
                   1383: sub fatal($$@)
                   1384: {
                   1385:   my ($errcode, $format, @args) = @_;
                   1386:   if ($is_mod_perl) {
                   1387:     Apache->request->status((split (/ /, $errcode))[0]);
                   1388:   } else {
                   1389:     print "Status: $errcode\r\n";
                   1390:   }
3.131     scop     1391:   html_header('Error');
3.120     scop     1392:   print "<p>Error: ", sprintf($format, map(htmlquote($_), @args)), "</p>\n";
                   1393:   html_footer();
                   1394:   exit(1);
                   1395: }
                   1396:
                   1397: sub redirect($)
                   1398: {
                   1399:   my ($url) = @_;
                   1400:   if ($is_mod_perl) {
                   1401:     Apache->request->status(301);
                   1402:     Apache->request->header_out(Location => $url);
                   1403:   } else {
                   1404:     print "Status: 301 Moved\r\n";
                   1405:     print "Location: $url\r\n";
                   1406:   }
3.131     scop     1407:   html_header('Moved');
3.120     scop     1408:   print "<p>This document is located ", &link('here', $url), "</p>\n";
                   1409:   html_footer();
                   1410:   exit(1);
                   1411: }
                   1412:
                   1413: sub safeglob($)
                   1414: {
                   1415:   my ($filename) = @_;
                   1416:   my ($dirname);
                   1417:   my (@results);
                   1418:   my $dh = do { local (*DH); };
                   1419:
                   1420:   ($dirname = $filename) =~ s|/[^/]+$||;
                   1421:   $filename =~ s|.*/||;
                   1422:
                   1423:   if (opendir($dh, $dirname)) {
                   1424:     my $glob = $filename;
                   1425:     my $t;
                   1426:
                   1427:     #  transform filename from glob to regex.  Deal with:
                   1428:     #  [, {, ?, * as glob chars
                   1429:     #  make sure to escape all other regex chars
                   1430:     $glob =~ s/([\.\(\)\|\+])/\\$1/g;
                   1431:     $glob =~ s/\*/.*/g;
                   1432:     $glob =~ s/\?/./g;
                   1433:     $glob =~ s/{([^}]+)}/($t = $1) =~ s-,-|-g; "($t)"/eg;
                   1434:     foreach (readdir($dh)) {
                   1435:
                   1436:       if (/^${glob}$/) {
                   1437:         push (@results, "$dirname/" . $_);
                   1438:       }
                   1439:     }
                   1440:     closedir($dh);
                   1441:   }
                   1442:
                   1443:   @results;
                   1444: }
                   1445:
                   1446: sub search_path($)
                   1447: {
                   1448:   my ($command) = @_;
                   1449:   my $d;
                   1450:
                   1451:   for $d (split (/:/, $command_path)) {
                   1452:     return "$d/$command" if -x "$d/$command";
                   1453:   }
                   1454:
                   1455:   return '';
                   1456: }
                   1457:
                   1458: sub getMimeTypeFromSuffix($)
                   1459: {
                   1460:   my ($fullname) = @_;
                   1461:   my ($mimetype, $suffix);
                   1462:   my $fh = do { local (*FH); };
                   1463:
                   1464:   ($suffix = $fullname) =~ s/^.*\.([^.]*)$/$1/;
                   1465:   $mimetype = $MTYPES{$suffix};
                   1466:   $mimetype = $MTYPES{'*'} if (!$mimetype);
                   1467:
                   1468:   if (!$mimetype && -f $mime_types) {
                   1469:
                   1470:     # okey, this is something special - search the
                   1471:     # mime.types database
                   1472:     open($fh, "<$mime_types");
                   1473:     while (<$fh>) {
                   1474:       if ($_ =~ /^\s*(\S+\/\S+).*\b$suffix\b/) {
                   1475:         $mimetype = $1;
                   1476:         last;
                   1477:       }
                   1478:     }
                   1479:     close($fh);
                   1480:   }
                   1481:
                   1482:   # okey, didn't find anything useful ..
                   1483:   if (!($mimetype =~ /\S\/\S/)) {
                   1484:     $mimetype = "text/plain";
                   1485:   }
                   1486:   return $mimetype;
3.1       knu      1487: }
                   1488:
                   1489: ###############################
3.24      knu      1490: # read first lines like head(1)
                   1491: ###############################
3.120     scop     1492: sub head($;$)
                   1493: {
                   1494:   my $fh        = $_[0];
                   1495:   my $linecount = $_[1] || 10;
3.24      knu      1496:
3.120     scop     1497:   my @buf;
                   1498:
                   1499:   if ($linecount > 0) {
                   1500:     my $i;
                   1501:     for ($i = 0; !eof($fh) && $i < $linecount; $i++) {
                   1502:       push @buf, scalar <$fh>;
                   1503:     }
                   1504:   } else {
                   1505:     @buf = <$fh>;
                   1506:   }
                   1507:
                   1508:   @buf;
3.24      knu      1509: }
                   1510:
                   1511: ###############################
                   1512: # scan vim and Emacs directives
                   1513: ###############################
3.120     scop     1514: sub scan_directives(@)
                   1515: {
                   1516:   my $ts = undef;
3.24      knu      1517:
3.120     scop     1518:   for (@_) {
                   1519:     $ts = $1 if /\b(?:ts|tabstop|tab-width)[:=]\s*([1-9]\d*)\b/;
                   1520:   }
3.24      knu      1521:
3.120     scop     1522:   ('tabstop' => $ts);
3.24      knu      1523: }
                   1524:
3.120     scop     1525: sub openOutputFilter()
                   1526: {
                   1527:   return if !defined($output_filter) || $output_filter eq '';
3.86      knu      1528:
3.120     scop     1529:   open(STDOUT, "|-") and return;
3.86      knu      1530:
3.120     scop     1531:   # child of child
                   1532:   open(STDERR, '>/dev/null');
                   1533:   exec($output_filter) or exit -1;
3.86      knu      1534: }
                   1535:
3.24      knu      1536: ###############################
3.1       knu      1537: # show Annotation
                   1538: ###############################
3.133     scop     1539: sub doAnnotate($)
3.120     scop     1540: {
                   1541:   my ($rev) = @_;
                   1542:   my ($pid);
                   1543:   my ($pathname, $filename);
                   1544:   my $reader = do { local (*FH); };
                   1545:   my $writer = do { local (*FH); };
                   1546:
                   1547:   # make sure the revisions are wellformed, for security
                   1548:   # reasons ..
                   1549:   if ($rev =~ /[^\w.]/) {
                   1550:     fatal("404 Not Found", 'Malformed query "%s"', $ENV{QUERY_STRING});
                   1551:   }
                   1552:
                   1553:   ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1554:   ($filename = $where) =~ s/^.*\///;
                   1555:
                   1556:   # this seems to be necessary
                   1557:   $| = 1;
                   1558:   $| = 0;    # Flush
                   1559:
3.126     scop     1560:   # Work around a mod_perl bug (?) in order to make open2() work.
                   1561:   # Search for "untie STDIN" in mod_perl mailing list archives for more info.
                   1562:   my $old_stdin;
                   1563:   if ($is_mod_perl && ($old_stdin = tied *STDIN)) {
                   1564:     local $^W = undef;
                   1565:     untie *STDIN;
                   1566:   }
                   1567:
3.120     scop     1568:   # this annotate version is based on the
                   1569:   # cvs annotate-demo Perl script by Cyclic Software
                   1570:   # It was written by Cyclic Software, http://www.cyclic.com/, and is in
                   1571:   # the public domain.
                   1572:   # we could abandon the use of rlog, rcsdiff and co using
                   1573:   # the cvsserver in a similiar way one day (..after rewrite)
3.128     scop     1574:   $pid = open2($reader, $writer, $CMD{cvs}, @annotate_options, 'server')
3.120     scop     1575:     or fatal("500 Internal Error",
                   1576:              'Fatal Error - unable to open cvs for annotation');
3.126     scop     1577:
                   1578:   # Re-tie STDIN if we fiddled around with it earlier, just to be sure.
                   1579:   tie(*STDIN, ref($old_stdin), $old_stdin) if ($old_stdin && !tied(*STDIN));
3.120     scop     1580:
                   1581:   # OK, first send the request to the server.  A simplified example is:
                   1582:   #     Root /home/kingdon/zwork/cvsroot
                   1583:   #     Argument foo/xx
                   1584:   #     Directory foo
                   1585:   #     /home/kingdon/zwork/cvsroot/foo
                   1586:   #     Directory .
                   1587:   #     /home/kingdon/zwork/cvsroot
                   1588:   #     annotate
                   1589:   # although as you can see there are a few more details.
                   1590:
                   1591:   print $writer "Root $cvsroot\n";
                   1592:   print $writer
                   1593:     "Valid-responses ok error Valid-requests Checked-in Updated Merged Removed M E\n";
                   1594:
                   1595:   # Don't worry about sending valid-requests, the server just needs to
                   1596:   # support "annotate" and if it doesn't, there isn't anything to be done.
                   1597:   print $writer "UseUnchanged\n";
                   1598:   print $writer "Argument -r\n";
                   1599:   print $writer "Argument $rev\n";
                   1600:   print $writer "Argument $where\n";
                   1601:
                   1602:   # The protocol requires us to fully fake a working directory (at
                   1603:   # least to the point of including the directories down to the one
                   1604:   # containing the file in question).
3.123     scop     1605:   # So if $where is "dir/sdir/file", then dirs will be ("dir","sdir","file")
                   1606:   my $path = '';
                   1607:   foreach my $dir (split('/', $where)) {
3.120     scop     1608:
                   1609:     if ($path eq "") {
3.123     scop     1610:       # In our example, $dir is "dir".
                   1611:       $path = $dir;
3.120     scop     1612:     } else {
                   1613:       print $writer "Directory $path\n";
                   1614:       print $writer "$cvsroot/$path\n";
                   1615:
                   1616:       # In our example, $_ is "sdir" and $path becomes "dir/sdir"
                   1617:       # And the next time, "file" and "dir/sdir/file" (which then gets
                   1618:       # ignored, because we don't need to send Directory for the file).
3.123     scop     1619:       $path .= "/$dir";
3.120     scop     1620:     }
                   1621:   }
3.123     scop     1622:   undef $path;
3.120     scop     1623:
                   1624:   # And the last "Directory" before "annotate" is the top level.
                   1625:   print $writer "Directory .\n";
                   1626:   print $writer "$cvsroot\n";
                   1627:
                   1628:   print $writer "annotate\n";
                   1629:
                   1630:   # OK, we've sent our command to the server.  Thing to do is to
                   1631:   # close the writer side and get all the responses.  If "cvs server"
                   1632:   # were nicer about buffering, then we could just leave it open, I think.
                   1633:   close($writer) or die "cannot close: $!";
                   1634:
3.133     scop     1635:   navigateHeader($scriptwhere, $pathname, $filename, $rev, 'annotate');
3.120     scop     1636:
3.136     scop     1637:   print '<h3 style="text-align: center">Annotation of ',
                   1638:     htmlquote("$pathname$filename"), ", revision $rev</h3>\n";
3.120     scop     1639:
                   1640:   # Ready to get the responses from the server.
                   1641:   # For example:
                   1642:   #     E Annotations for foo/xx
                   1643:   #     E ***************
                   1644:   #     M 1.3          (kingdon  06-Sep-97): hello
                   1645:   #     ok
                   1646:   my ($lineNr) = 0;
                   1647:   my ($oldLrev, $oldLusr) = ("", "");
                   1648:   my ($revprint, $usrprint);
                   1649:
                   1650:   if ($annTable) {
                   1651:     print
                   1652:       "<table style=\"border: none\" cellspacing=\"0\" cellpadding=\"0\">\n";
                   1653:   } else {
                   1654:     print "<pre>";
                   1655:   }
                   1656:
                   1657:   # prefetch several lines
                   1658:   my @buf = head($reader);
                   1659:
                   1660:   my %d = scan_directives(@buf);
                   1661:
                   1662:   while (@buf || !eof($reader)) {
                   1663:     $_ = @buf ? shift @buf : <$reader>;
                   1664:
                   1665:     my @words = split;
                   1666:
                   1667:     # Adding one is for the (single) space which follows $words[0].
                   1668:     my $rest = substr($_, length($words[0]) + 1);
                   1669:     if ($words[0] eq "E") {
                   1670:       next;
                   1671:     } elsif ($words[0] eq "M") {
                   1672:       $lineNr++;
                   1673:       (my $lrev = substr($_, 2,  13)) =~ y/ //d;
                   1674:       (my $lusr = substr($_, 16, 9))  =~ y/ //d;
                   1675:       my $line = substr($_, 36);
                   1676:       my $isCurrentRev = ($rev eq $lrev);
                   1677:
                   1678:       # we should parse the date here ..
                   1679:       if ($lrev eq $oldLrev) {
                   1680:         $revprint = sprintf('%-8s', '');
                   1681:       } else {
                   1682:         $revprint = sprintf('%-8s', $lrev);
                   1683:         $revprint =~ s`\S+`&link($&, "$scriptwhere$query#rev$&")`e;    # `
                   1684:         $oldLusr = '';
                   1685:       }
                   1686:
                   1687:       if ($lusr eq $oldLusr) {
                   1688:         $usrprint = '';
                   1689:       } else {
                   1690:         $usrprint = $lusr;
                   1691:       }
                   1692:       $oldLrev = $lrev;
                   1693:       $oldLusr = $lusr;
                   1694:
                   1695:       # Set bold for text-based browsers only - graphical
                   1696:       # browsers show bold fonts a bit wider than regular fonts,
                   1697:       # so it looks irregular.
                   1698:       print "<b>" if ($isCurrentRev && $is_textbased);
                   1699:
3.136     scop     1700:       $usrprint = sprintf('%-8s', $usrprint);
                   1701:       printf '%s%s %s %4d:', $revprint, $isCurrentRev ? '!' : ' ',
                   1702:         htmlquote($usrprint), $lineNr;
3.120     scop     1703:       print spacedHtmlText($line, $d{'tabstop'});
                   1704:
                   1705:       print "</b>" if ($isCurrentRev && $is_textbased);
                   1706:     } elsif ($words[0] eq "ok") {
                   1707:
                   1708:       # We could complain about any text received after this, like the
                   1709:       # CVS command line client.  But for simplicity, we don't.
                   1710:     } elsif ($words[0] eq "error") {
                   1711:       fatal("500 Internal Error",
                   1712:             'Error occured during annotate: <b>%s</b>', $_);
                   1713:     }
                   1714:   }
                   1715:
                   1716:   if ($annTable) {
                   1717:     print "</table>";
                   1718:   } else {
                   1719:     print "</pre>";
                   1720:   }
3.124     scop     1721:   html_footer();
                   1722:
3.120     scop     1723:   close($reader) or warn "cannot close: $!";
                   1724:   wait;
3.1       knu      1725: }
                   1726:
                   1727: ###############################
                   1728: # make Checkout
                   1729: ###############################
3.120     scop     1730: sub doCheckout($$)
                   1731: {
                   1732:   my ($fullname, $rev) = @_;
                   1733:   my ($mimetype, $revopt);
                   1734:   my $fh = do { local (*FH); };
                   1735:
                   1736:   if ($rev eq 'HEAD' || $rev eq '.') {
                   1737:     $rev = undef;
                   1738:   }
                   1739:
                   1740:   # make sure the revisions a wellformed, for security
                   1741:   # reasons ..
                   1742:   if (defined($rev) && $rev =~ /[^\w.]/) {
                   1743:     fatal("404 Not Found", 'Malformed query "%s"', $ENV{QUERY_STRING});
                   1744:   }
                   1745:
                   1746:   # get mimetype
                   1747:   if (defined($input{"content-type"})
                   1748:     && ($input{"content-type"} =~ /\S\/\S/))
                   1749:   {
                   1750:     $mimetype = $input{"content-type"};
                   1751:   } else {
                   1752:     $mimetype = &getMimeTypeFromSuffix($fullname);
                   1753:   }
                   1754:
                   1755:   if (defined($rev)) {
                   1756:     $revopt = "-r$rev";
                   1757:     if ($use_moddate) {
                   1758:       readLog($fullname, $rev);
                   1759:       $moddate = $date{$rev};
                   1760:     }
                   1761:   } else {
                   1762:     $revopt = "-rHEAD";
                   1763:
                   1764:     if ($use_moddate) {
                   1765:       readLog($fullname);
                   1766:       $moddate = $date{$symrev{HEAD}};
                   1767:     }
                   1768:   }
                   1769:
                   1770:   ### just for the record:
                   1771:   ### 'cvs co' seems to have a bug regarding single checkout of
                   1772:   ### directories/files having spaces in it;
                   1773:   ### this is an issue that should be resolved on cvs's side
                   1774:   #
                   1775:   # Safely for a child process to read from.
                   1776:   if (!open($fh, "-|")) {    # child
                   1777:     # chdir to $tmpdir before to avoid non-readable cgi-bin directories
                   1778:     chdir($tmpdir);
                   1779:     open(STDERR, ">&STDOUT");    # Redirect stderr to stdout
                   1780:
                   1781:     # work around a bug of cvs -p; expand symlinks
                   1782:     exec($CMD{cvs}, @cvs_options, '-d', abs_path($cvsroot), 'co', '-p',
                   1783:          $revopt, $where)
                   1784:       or exit -1;
                   1785:   }
                   1786:
                   1787:   if (eof($fh)) {
                   1788:     fatal("404 Not Found", '%s is not (any longer) pertinent', $where);
                   1789:   }
                   1790:
                   1791:   #===================================================================
                   1792:   #Checking out squid/src/ftp.c
                   1793:   #RCS:  /usr/src/CVS/squid/src/ftp.c,v
                   1794:   #VERS: 1.1.1.28.6.2
                   1795:   #***************
                   1796:
                   1797:   # Parse CVS header
                   1798:   my ($revision, $filename, $cvsheader);
                   1799:   $filename = "";
                   1800:   while (<$fh>) {
                   1801:     last if (/^\*\*\*\*/);
                   1802:     $revision = $1 if (/^VERS: (.*)$/);
                   1803:
                   1804:     if (/^Checking out (.*)$/) {
                   1805:       $filename = $1;
                   1806:       $filename =~ s/^\.\/*//;
                   1807:     }
                   1808:     $cvsheader .= $_;
                   1809:   }
                   1810:
                   1811:   if ($filename ne $where) {
                   1812:     fatal("500 Internal Error",
                   1813:           'Unexpected output from cvs co: %s', $cvsheader);
                   1814:   }
                   1815:   $| = 1;
                   1816:
                   1817:   if ($mimetype eq "text/x-cvsweb-markup") {
                   1818:     &cvswebMarkup($fh, $fullname, $revision);
                   1819:   } else {
                   1820:     http_header($mimetype);
                   1821:     print <$fh>;
                   1822:   }
                   1823:   close($fh);
1.12      fenner   1824: }
                   1825:
3.120     scop     1826: sub cvswebMarkup($$$)
                   1827: {
                   1828:   my ($filehandle, $fullname, $revision) = @_;
                   1829:   my ($pathname,   $filename);
3.1       knu      1830:
3.120     scop     1831:   ($pathname = $where) =~ s/(Attic\/)?[^\/]*$//;
                   1832:   ($filename = $where) =~ s/^.*\///;
                   1833:   my ($fileurl) = urlencode($filename);
                   1834:
3.133     scop     1835:   navigateHeader($scriptwhere, $pathname, $filename, $revision, 'view');
3.120     scop     1836:
                   1837:   print "<hr noshade>";
3.129     scop     1838:   print "<table width=\"100%\">\n<tr>\n<td class=\"log-markup\">";
3.120     scop     1839:   print "File: ", &clickablePath($where, 1);
                   1840:   print "&nbsp;(";
                   1841:   &download_link($fileurl, $revision, "download");
                   1842:   print ")";
3.132     scop     1843:   if ($allow_cvsgraph) {
                   1844:     print ' - ', &graph_link('', $revision, 'graph');
                   1845:   }
3.120     scop     1846:
                   1847:   if (!$defaultTextPlain) {
                   1848:     print "&nbsp;(";
                   1849:     &download_link($fileurl, $revision, "as text", "text/plain");
                   1850:     print ")";
                   1851:   }
                   1852:   print "<br>\n";
                   1853:
                   1854:   if ($show_log_in_markup) {
                   1855:     readLog($fullname);    #,$revision);
                   1856:     printLog($revision, 0);
                   1857:   } else {
                   1858:     print "Version: <b>$revision</b><br>\n";
                   1859:     print "Tag: <b>", $input{only_with_tag}, "</b><br>\n"
                   1860:       if $input{only_with_tag};
                   1861:   }
                   1862:   print "</td>\n</tr>\n</table>";
                   1863:   my $url = download_url($fileurl, $revision, $mimetype);
                   1864:   print "<hr noshade>";
                   1865:
                   1866:   if ($mimetype =~ /^image/) {
                   1867:     printf '<img src="%s" alt=""><br>', hrefquote("$url$barequery");
                   1868:   } elsif ($mimetype =~ m%^application/pdf%) {
                   1869:     printf '<embed src="%s" width="100%"><br>', hrefquote("$url$barequery");
                   1870:   } elsif ($preformat_in_markup) {
                   1871:     print "<pre>";
                   1872:
                   1873:     # prefetch several lines
                   1874:     my @buf = head($filehandle);
                   1875:
                   1876:     my %d = scan_directives(@buf);
                   1877:
                   1878:     while (@buf || !eof($filehandle)) {
                   1879:       $_ = @buf ? shift @buf : <$filehandle>;
                   1880:
                   1881:       print spacedHtmlText($_, $d{'tabstop'});
                   1882:     }
                   1883:     print "</pre>";
                   1884:   } else {
                   1885:     print "<pre>";
                   1886:
                   1887:     while (<$filehandle>) {
                   1888:       print htmlquote($_);
                   1889:     }
                   1890:     print "</pre>";
                   1891:   }
3.131     scop     1892:   html_footer();
3.120     scop     1893: }
                   1894:
                   1895: sub viewable($)
                   1896: {
                   1897:   my ($mimetype) = @_;
                   1898:
                   1899:   $mimetype =~ m%^((text|image)/|application/pdf)%;
3.1       knu      1900: }
                   1901:
                   1902: ###############################
                   1903: # Show Colored Diff
                   1904: ###############################
3.120     scop     1905: sub doDiff($$$$$$)
                   1906: {
                   1907:   my ($fullname, $r1, $tr1, $r2, $tr2, $f) = @_;
                   1908:   my $fh = do { local (*FH); };
                   1909:   my ($rev1, $rev2, $sym1, $sym2, $f1, $f2);
                   1910:
                   1911:   if (&forbidden_file($fullname)) {
                   1912:     fatal("403 Forbidden",
                   1913:           'Access forbidden.  This file is mentioned in @ForbiddenFiles');
                   1914:   }
                   1915:
                   1916:   if ($r1 =~ /([^:]+)(:(.+))?/) {
                   1917:     $rev1 = $1;
                   1918:     $sym1 = $3;
                   1919:   }
                   1920:   if ($r1 eq 'text') {
                   1921:     $rev1 = $tr1;
                   1922:     $sym1 = "";
                   1923:   }
                   1924:
                   1925:   if ($r2 =~ /([^:]+)(:(.+))?/) {
                   1926:     $rev2 = $1;
                   1927:     $sym2 = $3;
                   1928:   }
                   1929:   if ($r2 eq 'text') {
                   1930:     $rev2 = $tr2;
                   1931:     $sym2 = "";
                   1932:   }
                   1933:
                   1934:   # make sure the revisions a wellformed, for security
                   1935:   # reasons ..
                   1936:   if ($rev1 =~ /[^\w.]/ || $rev2 =~ /[^\w.]/) {
                   1937:     fatal("404 Not Found", 'Malformed query "%s"', $ENV{QUERY_STRING});
                   1938:   }
                   1939:
                   1940:   #
                   1941:   # rev1 and rev2 are now both numeric revisions.
                   1942:   # Thus we do a DWIM here and swap them if rev1 is after rev2.
                   1943:   # XXX should we warn about the fact that we do this?
                   1944:   if (&revcmp($rev1, $rev2) > 0) {
                   1945:     my ($tmp1, $tmp2) = ($rev1, $sym1);
                   1946:     ($rev1, $sym1) = ($rev2, $sym2);
                   1947:     ($rev2, $sym2) = ($tmp1, $tmp2);
                   1948:   }
                   1949:   my $difftype = $DIFFTYPES{$f};
                   1950:
                   1951:   if (!$difftype) {
                   1952:     fatal("400 Bad arguments", 'Diff format %s not understood', $f);
                   1953:   }
                   1954:
                   1955:   my @difftype       = @{$difftype->{'opts'}};
                   1956:   my $human_readable = $difftype->{'colored'};
                   1957:
                   1958:   # apply special options
                   1959:   if ($showfunc) {
                   1960:     push @difftype, '-p' if $f ne 's';
                   1961:
                   1962:     my ($re1, $re2);
                   1963:
                   1964:     while (($re1, $re2) = each %funcline_regexp) {
                   1965:       if ($fullname =~ /$re1/) {
                   1966:         push @difftype, '-F', $re2;
                   1967:         last;
                   1968:       }
                   1969:     }
                   1970:   }
                   1971:
                   1972:   if ($human_readable) {
                   1973:     if ($hr_ignwhite) {
                   1974:       push @difftype, '-w';
                   1975:     }
                   1976:     if ($hr_ignkeysubst) {
                   1977:       push @difftype, '-kk';
                   1978:     }
                   1979:   }
                   1980:
                   1981:   if (!open($fh, "-|")) {    # child
                   1982:     open(STDERR, ">&STDOUT");    # Redirect stderr to stdout
                   1983:     openOutputFilter();
                   1984:     exec($CMD{rcsdiff}, @difftype, "-r$rev1", "-r$rev2", $fullname) or exit -1;
                   1985:   }
                   1986:   if ($human_readable) {
                   1987:     &human_readable_diff($fh, $rev2);
                   1988:     html_footer();
                   1989:     gzipclose();
                   1990:     exit;
                   1991:   } else {
                   1992:     http_header("text/plain");
                   1993:   }
                   1994:
                   1995:   #
                   1996:   #===================================================================
                   1997:   #RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
                   1998:   #retrieving revision 1.16
                   1999:   #retrieving revision 1.17
                   2000:   #diff -c -r1.16 -r1.17
                   2001:   #*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2002:   #--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
                   2003:   #
                   2004:   # Ideas:
                   2005:   # - nuke the stderr output if it's what we expect it to be
                   2006:   # - Add "no differences found" if the diff command supplied no output.
                   2007:   #
                   2008:   #*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2009:   #--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
                   2010:   # (bogus example, but...)
                   2011:   #
                   2012:   if (grep { $_ eq '-u' } @difftype) {
                   2013:     $f1 = '---';
                   2014:     $f2 = '\+\+\+';
                   2015:   } else {
                   2016:     $f1 = '\*\*\*';
                   2017:     $f2 = '---';
                   2018:   }
                   2019:
                   2020:   while (<$fh>) {
                   2021:     if (m|^$f1 $cvsroot|o) {
                   2022:       s|$cvsroot/||o;
                   2023:       if ($sym1) {
                   2024:         chop;
                   2025:         $_ .= " $sym1\n";
                   2026:       }
                   2027:     } elsif (m|^$f2 $cvsroot|o) {
                   2028:       s|$cvsroot/||o;
                   2029:
                   2030:       if ($sym2) {
                   2031:         chop;
                   2032:         $_ .= " $sym2\n";
                   2033:       }
                   2034:     }
                   2035:     print $_;
                   2036:   }
                   2037:   close($fh);
1.12      fenner   2038: }
                   2039:
3.1       knu      2040: ###############################
                   2041: # Show Logs ..
                   2042: ###############################
3.120     scop     2043: sub getDirLogs($$@)
                   2044: {
                   2045:   my ($cvsroot, $dirname, @otherFiles) = @_;
3.139     scop     2046:   my $tag = $input{only_with_tag};
                   2047:   my $DirName = "$cvsroot/$where";
3.120     scop     2048:
3.139     scop     2049:   my @files = &safeglob("$DirName/*,v");
                   2050:   push (@files, &safeglob("$DirName/Attic/*,v")) if (!$input{'hideattic'});
3.120     scop     2051:   foreach my $file (@otherFiles) {
                   2052:     push (@files, "$DirName/$file");
                   2053:   }
                   2054:
3.139     scop     2055:   # If there are no files, we're done.
3.120     scop     2056:   return unless @files;
                   2057:
3.139     scop     2058:   my @cmd = ($CMD{rlog});
                   2059:   # Can't use -r<tag> as '-' is allowed in tagnames,
                   2060:   # but misinterpreted by rlog.
                   2061:   push(@cmd, '-r') unless defined($tag);
3.120     scop     2062:
3.139     scop     2063:   my $fh = do { local (*FH); };
                   2064:   if (!open($fh, '-|')) {            # Child
                   2065:     open(STDERR, '>/dev/null');      # Ignore rlog's complaints.
                   2066:     openOutputFilter();
                   2067:     if ($file_list_len && $file_list_len > 1) {
                   2068:       while (scalar(@files) > $file_list_len) {  # Process files in chunks.
                   2069:         system(@cmd, splice(@files, 0, $file_list_len)) == 0 or exit -1;
                   2070:       }
3.120     scop     2071:     }
3.139     scop     2072:     exec(@cmd, @files) or exit -1;
                   2073:   }
                   2074:   undef @cmd;
3.120     scop     2075:
3.139     scop     2076:   my $state = 'start';
                   2077:   my ($date, $branchpoint, $branch, $log, @filetags);
                   2078:   my ($rev, $revision, $revwanted, $filename, $head, $author);
3.120     scop     2079:
                   2080:   while (<$fh>) {
                   2081:     if ($state eq "start") {
                   2082:
                   2083:       #Next file. Initialize file variables
                   2084:       $rev         = '';
                   2085:       $revwanted   = '';
                   2086:       $branch      = '';
                   2087:       $branchpoint = '';
                   2088:       $filename    = '';
                   2089:       $log         = '';
                   2090:       $revision    = '';
                   2091:       %symrev      = ();
                   2092:       @filetags    = ();
                   2093:
                   2094:       #jump to head state
                   2095:       $state = "head";
                   2096:     }
                   2097:     print "$state:$_" if ($verbose);
                   2098:     again:
                   2099:
                   2100:     if ($state eq "head") {
                   2101:
                   2102:       #$rcsfile = $1 if (/^RCS file: (.+)$/); #not used (yet)
                   2103:
                   2104:       if (/^Working file: (.+)$/) {
                   2105:         $filename = $1;
                   2106:       } elsif (/^head: (.+)$/) {
                   2107:         $head = $1;
                   2108:       } elsif (/^branch: (.+)$/) {
                   2109:         $branch = $1;
                   2110:       } elsif (/^symbolic names:/) {
                   2111:         $state = "tags";
                   2112:         ($branch = $head) =~ s/\.\d+$//
                   2113:           if $branch eq '';
                   2114:         $branch =~ s/(\d+)$/0.$1/;
                   2115:         $symrev{MAIN}  = $branch;
                   2116:         $symrev{HEAD}  = $branch;
                   2117:         $alltags{MAIN} = 1;
                   2118:         $alltags{HEAD} = 1;
                   2119:         push (@filetags, "MAIN", "HEAD");
                   2120:       } elsif (/$LOG_REVSEPARATOR/o) {
                   2121:         $state = "log";
                   2122:         $rev   = '';
                   2123:         $date  = '';
                   2124:         $log   = '';
                   2125:
                   2126:         # Try to reconstruct the relative filename if RCS spits out a full path
                   2127:         $filename =~ s%^\Q$DirName\E/%%;
                   2128:       }
                   2129:       next;
                   2130:     }
                   2131:
                   2132:     if ($state eq "tags") {
                   2133:       if (/^\s+([^:]+):\s+([\d\.]+)\s*$/) {
                   2134:         push (@filetags, $1);
                   2135:         $symrev{$1}  = $2;
                   2136:         $alltags{$1} = 1;
                   2137:         next;
                   2138:       } elsif (/^\S/) {
                   2139:
                   2140:         if (defined($tag)) {
                   2141:           if (defined($symrev{$tag}) || $tag eq "HEAD") {
                   2142:             $revwanted    = $symrev{$tag eq "HEAD" ? "MAIN" : $tag};
                   2143:             ($branch      = $revwanted) =~ s/\b0\.//;
                   2144:             ($branchpoint = $branch)    =~ s/\.?\d+$//;
                   2145:             $revwanted    = '' if ($revwanted ne $branch);
                   2146:           } elsif ($tag ne "HEAD") {
                   2147:             print "Tag not found, skip this file" if ($verbose);
                   2148:             $state = "skip";
                   2149:             next;
                   2150:           }
                   2151:         }
                   2152:
                   2153:         foreach my $tagfound (@filetags) {
                   2154:           $tags{$tagfound} = 1;
                   2155:         }
                   2156:         $state = "head";
                   2157:         goto again;
                   2158:       }
                   2159:     }
                   2160:
                   2161:     if ($state eq "log") {
                   2162:       if (/$LOG_REVSEPARATOR/o || /$LOG_FILESEPARATOR/o) {
                   2163:
                   2164:         # End of a log entry.
                   2165:         my $revbranch = $rev;
                   2166:         $revbranch =~ s/\.\d+$//;
                   2167:         print "$filename $rev Wanted: $revwanted ",
                   2168:           "Revbranch: $revbranch Branch: $branch ",
                   2169:           "Branchpoint: $branchpoint\n"
                   2170:           if ($verbose);
                   2171:
                   2172:         if ($revwanted eq '' && $branch ne '' && $branch eq $revbranch
                   2173:             || !defined($tag))
                   2174:         {
                   2175:           print "File revision $rev found for branch $branch\n"
                   2176:             if ($verbose);
                   2177:           $revwanted = $rev;
                   2178:         }
                   2179:
                   2180:         if ($revwanted ne ''
                   2181:             ? $rev eq $revwanted
                   2182:             : $branchpoint ne ''
                   2183:               ? $rev eq $branchpoint
                   2184:               : 0
                   2185:             && ($rev eq $head))
                   2186:         {    # Don't think head is needed here..
                   2187:           print "File info $rev found for $filename\n"
                   2188:             if ($verbose);
                   2189:           my @finfo = ($rev, $date, $log, $author, $filename);
                   2190:           my ($name);
                   2191:           ($name = $filename) =~ s%/.*%%;
                   2192:           $fileinfo{$name} = [@finfo];
                   2193:           $state = "done" if ($rev eq $revwanted);
                   2194:         }
                   2195:         $rev  = '';
                   2196:         $date = '';
                   2197:         $log  = '';
                   2198:       } elsif ($date eq ''
                   2199:                && m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);|)
                   2200:       {
                   2201:         my $yr = $1;
                   2202:
                   2203:         # damn 2-digit year routines :-)
                   2204:         if ($yr > 100) {
                   2205:           $yr -= 1900;
                   2206:         }
                   2207:         $date = &Time::Local::timegm($6, $5, $4, $3, $2 - 1, $yr);
                   2208:         ($author) = /author: ([^;]+)/;
                   2209:         $state = "log";
                   2210:         $log   = '';
                   2211:         next;
                   2212:       } elsif ($rev eq '' && /^revision (\d+(?:\.\d+)+).*$/) {
                   2213:         $rev = $1;    # .*$ eats up the locker(lockers?) info, if any
                   2214:         next;
                   2215:       } else {
                   2216:         $log .= $_;
                   2217:       }
                   2218:     }
                   2219:
                   2220:     if (/$LOG_FILESEPARATOR/o) {
                   2221:       $state = "start";
                   2222:       next;
                   2223:     }
                   2224:   }
                   2225:
3.139     scop     2226:   my $linesread = $. || 0;
                   2227:   close($fh);
                   2228:
                   2229:   if ($linesread == 0) {
3.137     scop     2230:     fatal('500 Internal Error',
                   2231:           '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     2232:           join (', ', @files), $command_path);
3.120     scop     2233:   }
                   2234: }
                   2235:
                   2236: sub readLog($;$)
                   2237: {
                   2238:   my ($fullname, $revision) = @_;
                   2239:   my ($symnames, $head, $rev, $br, $brp, $branch, $branchrev);
                   2240:   my $fh = do { local (*FH); };
                   2241:
                   2242:   if (defined($revision)) {
                   2243:     $revision = "-r$revision";
                   2244:   } else {
                   2245:     $revision = "";
                   2246:   }
                   2247:
                   2248:   undef %symrev;
                   2249:   undef %revsym;
                   2250:   undef @allrevisions;
                   2251:   undef %date;
                   2252:   undef %author;
                   2253:   undef %state;
                   2254:   undef %difflines;
                   2255:   undef %log;
                   2256:
                   2257:   print("Going to rlog '$fullname'\n") if ($verbose);
                   2258:   if (!open($fh, "-|")) {    # child
                   2259:     if ($revision ne '') {
                   2260:       openOutputFilter();
                   2261:       exec($CMD{rlog}, $revision, $fullname) or exit -1;
                   2262:     } else {
                   2263:       openOutputFilter();
                   2264:       exec($CMD{rlog}, $fullname) or exit -1;
                   2265:     }
                   2266:   }
                   2267:
                   2268:   while (<$fh>) {
                   2269:     print if ($verbose);
                   2270:     if ($symnames) {
                   2271:       if (/^\s+([^:]+):\s+([\d\.]+)/) {
                   2272:         $symrev{$1} = $2;
                   2273:       } else {
                   2274:         $symnames = 0;
                   2275:       }
                   2276:     } elsif (/^head:\s+([\d\.]+)/) {
                   2277:       $head = $1;
                   2278:     } elsif (/^branch:\s+([\d\.]+)/) {
                   2279:       $curbranch = $1;
                   2280:     } elsif (/^symbolic names/) {
                   2281:       $symnames = 1;
                   2282:     } elsif (/^-----/) {
                   2283:       last;
                   2284:     }
                   2285:   }
                   2286:   ($curbranch = $head) =~ s/\.\d+$// if (!defined($curbranch));
                   2287:
                   2288:   # each log entry is of the form:
                   2289:   # ----------------------------
                   2290:   # revision 3.7.1.1
                   2291:   # date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
                   2292:   # log info
                   2293:   # ----------------------------
                   2294:
                   2295:   # For a locked revision, the first line after the separator
                   2296:   # becomes smth like
                   2297:   # revision 9.19      locked by: vassilii;
                   2298:
                   2299:   logentry:
                   2300:
                   2301:   while (!/$LOG_FILESEPARATOR/o) {
                   2302:     $_ = <$fh>;
                   2303:     last logentry if (!defined($_));    # EOF
                   2304:     print "R:", $_ if ($verbose);
                   2305:     if (/^revision (\d+(?:\.\d+)+)/) {
                   2306:       $rev = $1;
                   2307:       unshift (@allrevisions, $rev);
                   2308:     } elsif (/$LOG_FILESEPARATOR/o || /$LOG_REVSEPARATOR/o) {
                   2309:       next logentry;
                   2310:     } else {
                   2311:
                   2312:       # The rlog output is syntactically ambiguous.  We must
                   2313:       # have guessed wrong about where the end of the last log
                   2314:       # message was.
                   2315:       # Since this is likely to happen when people put rlog output
                   2316:       # in their commit messages, don't even bother keeping
                   2317:       # these lines since we don't know what revision they go with
                   2318:       # any more.
                   2319:       next logentry;
                   2320:     }
                   2321:     $_ = <$fh>;
                   2322:     print "D:", $_ if ($verbose);
                   2323:     if (
                   2324:       m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);\s+state:\s+(\S+);\s+(lines:\s+([0-9\s+-]+))?|
                   2325:       )
                   2326:     {
                   2327:       my $yr = $1;
                   2328:
                   2329:       # damn 2-digit year routines :-)
                   2330:       if ($yr > 100) {
                   2331:         $yr -= 1900;
                   2332:       }
                   2333:       $date{$rev} = &Time::Local::timegm($6, $5, $4, $3, $2 - 1, $yr);
                   2334:       $author{$rev}    = $7;
                   2335:       $state{$rev}     = $8;
                   2336:       $difflines{$rev} = $10;
                   2337:     } else {
                   2338:       fatal("500 Internal Error", 'Error parsing RCS output: %s', $_);
                   2339:     }
                   2340:
                   2341:   line:
                   2342:     while (<$fh>) {
                   2343:       print "L:", $_ if ($verbose);
                   2344:       next line if (/^branches:\s/);
                   2345:       last line if (/$LOG_FILESEPARATOR/o || /$LOG_REVSEPARATOR/o);
                   2346:       $log{$rev} .= $_;
                   2347:     }
                   2348:     print "E:", $_ if ($verbose);
                   2349:   }
                   2350:   close($fh);
                   2351:   print "Done reading RCS file\n" if ($verbose);
                   2352:
                   2353:   @revorder = reverse sort { revcmp($a, $b) } @allrevisions;
                   2354:   print "Done sorting revisions", join (" ", @revorder), "\n"
                   2355:     if ($verbose);
                   2356:
                   2357:   #
                   2358:   # HEAD is an artificial tag which is simply the highest tag number on the main
                   2359:   # branch, unless there is a branch tag in the RCS file in which case it's the
                   2360:   # highest revision on that branch.  Find it by looking through @revorder; it
                   2361:   # is the first commit listed on the appropriate branch.
                   2362:   # This is not neccesary the same revision as marked as head in the RCS file.
                   2363:   my $headrev = $curbranch || "1";
                   2364:   ($symrev{"MAIN"} = $headrev) =~ s/(\d+)$/0.$1/;
                   2365:
                   2366:   foreach $rev (@revorder) {
                   2367:     if ($rev =~ /^(\S*)\.\d+$/ && $headrev eq $1) {
                   2368:       $symrev{"HEAD"} = $rev;
                   2369:       last;
                   2370:     }
                   2371:   }
                   2372:   ($symrev{"HEAD"} = $headrev) =~ s/\.\d+$//
                   2373:     if (!defined($symrev{"HEAD"}));
                   2374:   print "Done finding HEAD\n" if ($verbose);
                   2375:
                   2376:   #
                   2377:   # Now that we know all of the revision numbers, we can associate
                   2378:   # absolute revision numbers with all of the symbolic names, and
                   2379:   # pass them to the form so that the same association doesn't have
                   2380:   # to be built then.
                   2381:   #
                   2382:   undef @branchnames;
                   2383:   undef %branchpoint;
                   2384:   undef $sel;
                   2385:
                   2386:   foreach (reverse sort keys %symrev) {
                   2387:     $rev = $symrev{$_};
                   2388:     if ($rev =~ /^((.*)\.)?\b0\.(\d+)$/) {
                   2389:       push (@branchnames, $_);
                   2390:
                   2391:       #
                   2392:       # A revision number of A.B.0.D really translates into
                   2393:       # "the highest current revision on branch A.B.D".
                   2394:       #
                   2395:       # If there is no branch A.B.D, then it translates into
                   2396:       # the head A.B .
                   2397:       #
                   2398:       # This reasoning also applies to the main branch A.B,
                   2399:       # with the branch number 0.A, with the exception that
                   2400:       # it has no head to translate to if there is nothing on
                   2401:       # the branch, but I guess this can never happen?
                   2402:       #
                   2403:       # (the code below gracefully forgets about the branch
                   2404:       # if it should happen)
                   2405:       #
                   2406:       $head = defined($2) ? $2 : "";
                   2407:       $branch = $3;
                   2408:       $branchrev = $head . ($head ne "" ? "." : "") . $branch;
                   2409:       my $regex;
                   2410:       $regex = quotemeta $branchrev;
                   2411:       $rev   = $head;
                   2412:
                   2413:       foreach my $r (@revorder) {
                   2414:         if ($r =~ /^${regex}\b/) {
                   2415:           $rev = $branchrev;
                   2416:           last;
                   2417:         }
                   2418:       }
                   2419:       next if ($rev eq "");
                   2420:
                   2421:       if ($rev ne $head && $head ne "") {
3.131     scop     2422:         $branchpoint{$head} .= ', ' if ($branchpoint{$head});
3.120     scop     2423:         $branchpoint{$head} .= $_;
                   2424:       }
                   2425:     }
                   2426:     $revsym{$rev} .= ", " if ($revsym{$rev});
                   2427:     $revsym{$rev} .= $_;
3.136     scop     2428:     $sel .= sprintf("<option value=\"%s:%s\">%s</option>\n",
                   2429:                     htmlquote($rev), (htmlquote($_)) x 2);
3.120     scop     2430:   }
                   2431:   print "Done associating revisions with branches\n" if ($verbose);
                   2432:
                   2433:   my ($onlyonbranch, $onlybranchpoint);
                   2434:   if ($onlyonbranch = $input{'only_with_tag'}) {
                   2435:     $onlyonbranch = $symrev{$onlyonbranch};
                   2436:     if ($onlyonbranch =~ s/\b0\.//) {
                   2437:       ($onlybranchpoint = $onlyonbranch) =~ s/\.\d+$//;
                   2438:     } else {
                   2439:       $onlybranchpoint = $onlyonbranch;
                   2440:     }
                   2441:
                   2442:     if (!defined($onlyonbranch) || $onlybranchpoint eq "") {
                   2443:       fatal("404 Tag not found", 'Tag %s not defined',
3.131     scop     2444:             $input{'only_with_tag'});
3.120     scop     2445:     }
                   2446:   }
                   2447:
                   2448:   undef @revisions;
                   2449:
                   2450:   foreach (@allrevisions) {
                   2451:     ($br  = $_)  =~ s/\.\d+$//;
                   2452:     ($brp = $br) =~ s/\.\d+$//;
                   2453:     next if ($onlyonbranch
                   2454:              && $br ne $onlyonbranch
                   2455:              && $_  ne $onlybranchpoint);
                   2456:     unshift (@revisions, $_);
                   2457:   }
                   2458:
                   2459:   if ($logsort eq "date") {
                   2460:
                   2461:     # Sort the revisions in commit order an secondary sort on revision
                   2462:     # (secondary sort needed for imported sources, or the first main
                   2463:     # revision gets before the same revision on the 1.1.1 branch)
                   2464:     @revdisplayorder =
                   2465:       sort { $date{$b} <=> $date{$a} || -revcmp($a, $b) } @revisions;
                   2466:   } elsif ($logsort eq "rev") {
                   2467:
                   2468:     # Sort the revisions in revision order, highest first
                   2469:     @revdisplayorder = reverse sort { revcmp($a, $b) } @revisions;
                   2470:   } else {
                   2471:
                   2472:     # No sorting. Present in the same order as rlog / cvs log
                   2473:     @revdisplayorder = @revisions;
                   2474:   }
                   2475:
                   2476: }
                   2477:
                   2478: sub printDiffLinks($$)
                   2479: {
                   2480:   my ($text, $url) = @_;
                   2481:   my @extra;
                   2482:
                   2483:   local $_;
                   2484:   for ($DIFFTYPES{$defaultDiffType}{'colored'} ? qw(u) : qw(h)) {
                   2485:     my $f = $_ eq $defaultDiffType ? '' : $_;
                   2486:
                   2487:     push @extra, &link(lc $DIFFTYPES{$_}{'descr'}, "$url&f=$f");
                   2488:   }
                   2489:
                   2490:   print &link($text, $url), ' (', join (', ', @extra), ')';
                   2491: }
                   2492:
                   2493: sub printLog($;$)
                   2494: {
                   2495:   my ($link, $br, $brp);
                   2496:   ($_, $link) = @_;
                   2497:   ($br  = $_)  =~ s/\.\d+$//;
                   2498:   ($brp = $br) =~ s/\.?\d+$//;
3.131     scop     2499:   my $prev;
3.120     scop     2500:
                   2501:   $link = 1 if (!defined($link));
3.131     scop     2502:   my $isDead = ($state{$_} eq 'dead');
                   2503:   my $isSelected = (defined($input{'r1'}) && $input{'r1'} eq $_);
3.120     scop     2504:
3.131     scop     2505:   print "<div class=\"diff-selected\">\n" if $isSelected;
3.120     scop     2506:   print "<p>\n";
3.131     scop     2507:
3.120     scop     2508:   if ($link && !$isDead) {
                   2509:     my ($filename);
                   2510:     ($filename = $where) =~ s/^.*\///;
                   2511:     my ($fileurl) = urlencode($filename);
                   2512:     print "<a name=\"rev$_\"></a>";
                   2513:
                   2514:     if (defined($revsym{$_})) {
                   2515:       foreach my $sym (split (", ", $revsym{$_})) {
3.136     scop     2516:         print '<a name="', hrefquote($sym), '"></a>';
3.120     scop     2517:       }
                   2518:     }
                   2519:
                   2520:     if (defined($revsym{$br})
                   2521:         && $revsym{$br}
                   2522:         && !defined($nameprinted{$br}))
                   2523:     {
                   2524:       foreach my $sym (split (", ", $revsym{$br})) {
3.136     scop     2525:         print '<a name="', hrefquote($sym), '"></a>';
3.120     scop     2526:       }
                   2527:       $nameprinted{$br} = 1;
                   2528:     }
                   2529:     print "\n Revision ";
                   2530:     &download_link($fileurl, $_, $_,
                   2531:       $defaultViewable ? "text/x-cvsweb-markup" : undef);
                   2532:
                   2533:     if ($defaultViewable) {
                   2534:       print " / (";
                   2535:       &download_link($fileurl, $_, "download", $mimetype);
                   2536:       print ")";
                   2537:     }
                   2538:
                   2539:     if (not $defaultTextPlain) {
                   2540:       print " / (";
                   2541:       &download_link($fileurl, $_, "as text", "text/plain");
                   2542:       print ")";
                   2543:     }
                   2544:
                   2545:     if (!$defaultViewable) {
                   2546:       print " / (";
                   2547:       &download_link($fileurl, $_, "view", "text/x-cvsweb-markup");
                   2548:       print ")";
                   2549:     }
                   2550:
                   2551:     if ($allow_annotate) {
                   2552:       print " - ";
                   2553:       print &link('annotate',
                   2554:                   sprintf('%s/%s?annotate=%s%s',
                   2555:                           $scriptname, urlencode($where), $_, $barequery));
                   2556:     }
                   2557:
                   2558:     # Plus a select link if enabled, and this version isn't selected
                   2559:     if ($allow_version_select) {
3.131     scop     2560:       print ' - ';
                   2561:       if ($isSelected) {
                   2562:         print '<b>[selected]</b>';
3.120     scop     2563:       } else {
3.131     scop     2564:         print &link('[select&nbsp;for&nbsp;diffs]',
                   2565:                     sprintf('%s?r1=%s%s#rev%s',
                   2566:                             $scriptwhere, $_, $barequery, $_));
3.120     scop     2567:       }
                   2568:     }
                   2569:   } else {
                   2570:     print "Revision <b>$_</b>";
                   2571:   }
                   2572:
                   2573:   if (/^1\.1\.1\.\d+$/) {
                   2574:     print " <i>(vendor branch)</i>";
                   2575:   }
                   2576:   if (defined @mytz) {
                   2577:     my ($est) = $mytz[(localtime($date{$_}))[8]];
                   2578:     print ", <i>", scalar localtime($date{$_}), " $est</i> (";
                   2579:   } else {
                   2580:     print ", <i>", scalar gmtime($date{$_}), " UTC</i> (";
                   2581:   }
3.136     scop     2582:   print readableTime(time() - $date{$_}, 1), ' ago)';
                   2583:   print ' by <i>', htmlquote($author{$_}), "</i>\n";
                   2584:
                   2585:   if ($revsym{$br}) {
                   2586:     printf "<br>Branch: <b>%s</b>\n",
                   2587:       $link ? link_tags($revsym{$br}) : htmlquote($revsym{$br});
                   2588:   }
                   2589:   if ($revsym{$_}) {
                   2590:     printf "<br>CVS Tags: <b>%s</b>\n",
                   2591:       $link ? link_tags($revsym{$_}) : htmlquote($revsym{$_});
                   2592:   }
                   2593:   if ($branchpoint{$_}) {
                   2594:     printf "<br>Branch point for: <b>%s</b>\n",
                   2595:       $link ? link_tags($branchpoint{$_}) : htmlquote($branchpoint{$_});
                   2596:   }
3.120     scop     2597:
                   2598:   # Find the previous revision
                   2599:   my @prevrev = split (/\./, $_);
                   2600:   do {
                   2601:     if (--$prevrev[$#prevrev] <= 0) {
                   2602:
                   2603:       # If it was X.Y.Z.1, just make it X.Y
                   2604:       pop (@prevrev);
                   2605:       pop (@prevrev);
                   2606:     }
                   2607:     $prev = join (".", @prevrev);
                   2608:   } until (defined($date{$prev}) || $prev eq "");
                   2609:
                   2610:   if ($prev ne "") {
                   2611:     if ($difflines{$_}) {
                   2612:       print "<br>Changes since <b>$prev: $difflines{$_} lines</b>";
                   2613:     }
                   2614:   }
                   2615:
                   2616:   if ($isDead) {
                   2617:     print "<br><b><i>FILE REMOVED</i></b>\n";
                   2618:   } elsif ($link) {
                   2619:     my %diffrev = ();
                   2620:     $diffrev{$_} = 1;
                   2621:     $diffrev{""} = 1;
                   2622:     print '<br>';
                   2623:     my $diff = 'Diff';
                   2624:
                   2625:     #
                   2626:     # Offer diff to previous revision
                   2627:     if ($prev) {
                   2628:       $diffrev{$prev} = 1;
                   2629:
                   2630:       my $url =
                   2631:         sprintf('%s.diff?r1=%s&r2=%s%s', $scriptwhere, $prev, $_, $barequery);
                   2632:
                   2633:       print $diff, " to previous ";
                   2634:       $diff = '';
                   2635:       printDiffLinks($prev, $url);
                   2636:     }
                   2637:
                   2638:     #
                   2639:     # Plus, if it's on a branch, and it's not a vendor branch,
                   2640:     # offer a diff with the branch point.
                   2641:     if ($revsym{$brp}
                   2642:       && !/^1\.1\.1\.\d+$/
                   2643:       && !defined($diffrev{$brp}))
                   2644:     {
                   2645:       my $url =
                   2646:         sprintf('%s.diff?r1=%s&r2=%s%s', $scriptwhere, $brp, $_, $barequery);
                   2647:
                   2648:       print $diff, " to branchpoint ";
                   2649:       $diff = '';
                   2650:       printDiffLinks($brp, $url);
                   2651:     }
                   2652:
                   2653:     #
                   2654:     # Plus, if it's on a branch, and it's not a vendor branch,
                   2655:     # offer to diff with the next revision of the higher branch.
                   2656:     # (e.g. change gets committed and then brought
                   2657:     # over to -stable)
                   2658:     if (/^\d+\.\d+\.\d+/ && !/^1\.1\.1\.\d+$/) {
                   2659:       my ($i, $nextmain);
                   2660:
                   2661:       for ($i = 0; $i < $#revorder && $revorder[$i] ne $_; $i++) {
                   2662:       }
                   2663:       my @tmp2 = split (/\./, $_);
                   2664:       for ($nextmain = ""; $i > 0; $i--) {
                   2665:         my $next = $revorder[$i - 1];
                   2666:         my @tmp1 = split (/\./, $next);
                   2667:
                   2668:         if (@tmp1 < @tmp2) {
                   2669:           $nextmain = $next;
                   2670:           last;
                   2671:         }
                   2672:
                   2673:         # Only the highest version on a branch should have
                   2674:         # a diff for the "next main".
                   2675:         last
                   2676:           if (@tmp1 - 1 <= @tmp2
                   2677:           && join (".", @tmp1[0 .. $#tmp1 - 1]) eq
                   2678:           join (".", @tmp2[0 .. $#tmp1 - 1]));
                   2679:       }
                   2680:
                   2681:       if (!defined($diffrev{$nextmain})) {
                   2682:         $diffrev{$nextmain} = 1;
                   2683:
                   2684:         my $url = sprintf('%s.diff?r1=%s&r2=%s%s',
                   2685:           $scriptwhere, $nextmain, $_, $barequery);
                   2686:
                   2687:         print $diff, " next main ";
                   2688:         $diff = '';
                   2689:         printDiffLinks($nextmain, $url);
                   2690:       }
                   2691:     }
                   2692:
                   2693:     # Plus if user has selected only r1, then present a link
                   2694:     # to make a diff to that revision
                   2695:     if (defined($input{"r1"}) && !defined($diffrev{$input{"r1"}})) {
                   2696:       $diffrev{$input{"r1"}} = 1;
                   2697:
                   2698:       my $url = sprintf('%s.diff?r1=%s&r2=%s%s',
                   2699:         $scriptwhere, $input{'r1'}, $_, $barequery);
                   2700:
                   2701:       print $diff, " to selected ";
                   2702:       $diff = '';
                   2703:       printDiffLinks($input{'r1'}, $url);
                   2704:     }
                   2705:
                   2706:   }
                   2707:   print "\n</p>\n<pre>\n";
                   2708:   print &htmlify($log{$_}, $allow_log_extra);
                   2709:   print "</pre>\n";
3.131     scop     2710:   print "</div>\n" if $isSelected;
3.120     scop     2711: }
                   2712:
3.132     scop     2713: #
                   2714: # Generates the HTML view for CvsGraph.
                   2715: #
                   2716: sub doGraphView($)
                   2717: {
                   2718:   my ($rev) = @_;
                   2719:
                   2720:   (my $pathname = $where) =~ s|[^/]*$||;
                   2721:   (my $filename = $where) =~ s|^.*/||;
                   2722:
                   2723:   navigateHeader($scriptwhere, $pathname, $filename, $rev, 'graph');
                   2724:
                   2725:   my $title = sprintf 'Revision graph of %s', htmlquote($pathname . $filename);
                   2726:   my $mapname = 'CvsGraphMap';
                   2727:   my $ctype = $defaultViewable ? '&amp;content-type=text/x-cvsweb-markup' : '';
                   2728:
                   2729:   print '<h3 style="text-align: center">', $title, "</h3>\n",
                   2730:     '<div align="center"><img border="0" ';
                   2731:   printf 'usemap="#%s" src="?graph=%s&amp;makeimage=1" alt="%s"></div>',
                   2732:     $mapname, $rev, $title;
3.135     scop     2733:   print "\n";
3.132     scop     2734:
3.135     scop     2735:   my @graph_cmd =
                   2736:     ($CMD{cvsgraph},
                   2737:      '-r', $cvsroot,
                   2738:      '-i',
                   2739:      '-M', $mapname,
                   2740:      "-Omap_branch_href=\"href=\\\"./?only_with_tag=%t$barequery\\\"\"",
                   2741:      "-Omap_rev_href=\"href=\\\"?rev=%R$barequery$ctype\\\"\"",
                   2742:      "-Omap_diff_href=\"href=\\\"%F.diff?r1=%P&amp;r2=%R$barequery\\\"\"",
                   2743:      );
3.132     scop     2744:   push(@graph_cmd, '-c', $cvsgraph_config) if $cvsgraph_config;
                   2745:
                   2746:   my $pipe = IO::Pipe->new();
                   2747:   $pipe->reader(@graph_cmd, $pathname . $filename . ',v');
                   2748:   while (<$pipe>) {
                   2749:     print;
                   2750:   }
                   2751:   $pipe = undef;
                   2752:
                   2753:   html_footer();
                   2754: }
                   2755:
                   2756: #
                   2757: # Generates a graph using CvsGraph.
                   2758: #
                   2759: sub doGraph($)
                   2760: {
                   2761:   my ($rev) = @_;
                   2762:
                   2763:   (my $pathname = $where) =~ s|[^/]*$||;
                   2764:   (my $filename = $where) =~ s|^.*/||;
                   2765:
                   2766:   http_header('image/png');
                   2767:
                   2768:   my @graph_cmd = ($CMD{cvsgraph}, '-r', $cvsroot);
                   2769:   push(@graph_cmd, '-c', $cvsgraph_config) if $cvsgraph_config;
                   2770:
                   2771:   my $pipe = IO::Pipe->new();
                   2772:   $pipe->reader(@graph_cmd, $pathname . $filename . ',v');
                   2773:   {
                   2774:     local $/ = undef;
                   2775:     binmode(\*STDOUT);
                   2776:     print <$pipe>;
                   2777:   }
                   2778:   $pipe = undef;
                   2779: }
                   2780:
                   2781:
3.120     scop     2782: sub doLog($)
                   2783: {
                   2784:   my ($fullname) = @_;
                   2785:   my ($diffrev, $upwhere, $filename, $backurl);
                   2786:
                   2787:   readLog($fullname);
                   2788:
                   2789:   html_header("CVS log for $where");
                   2790:   ($upwhere  = $where) =~ s|(Attic/)?[^/]+$||;
                   2791:   ($filename = $where) =~ s|^.*/||;
                   2792:   $backurl = $scriptname . "/" . urlencode($upwhere) . $query;
                   2793:   print "<p>\n ";
                   2794:   print &link($backicon, "$backurl#$filename"), " <b>Up to ",
                   2795:     &clickablePath($upwhere, 1), "</b>\n</p>\n";
                   2796:   print "<p>\n ";
                   2797:   print &link('Request diff between arbitrary revisions', '#diff');
3.132     scop     2798:   if ($allow_cvsgraph) {
                   2799:     print ' / ', &graph_link('', 1, 'Display revisions graphically');
                   2800:   }
3.120     scop     2801:   print "\n</p>\n<hr noshade>\n";
                   2802:
                   2803:   print "<p>\n";
                   2804:   if ($curbranch) {
                   2805:     print "Default branch: ", ($revsym{$curbranch} || $curbranch);
                   2806:   } else {
                   2807:     print "No default branch";
                   2808:   }
                   2809:   print "<br>\n";
                   2810:
                   2811:   if ($input{only_with_tag}) {
                   2812:     print "Current tag: $input{only_with_tag}<br>\n";
                   2813:   }
                   2814:   print "</p>\n";
                   2815:
                   2816:   undef %nameprinted;
                   2817:
                   2818:   for (my $i = 0; $i <= $#revdisplayorder; $i++) {
                   2819:     print "<hr size=\"1\" noshade>\n";
                   2820:     printLog($revdisplayorder[$i]);
                   2821:   }
                   2822:
                   2823:   print "<hr noshade>\n<p>\n";
                   2824:   print "<a name=\"diff\">\n";
                   2825:   print "This form allows you to request diff's between any two\n";
                   2826:   print "revisions of a file.  You may select a symbolic revision\n";
                   2827:   print "name using the selection box or you may type in a numeric\n";
                   2828:   print "name using the type-in text box.\n";
                   2829:   print "</a>\n</p>\n";
                   2830:   print
                   2831:     "<form method=\"get\" action=\"${scriptwhere}.diff\" name=\"diff_select\">\n";
                   2832:
                   2833:   foreach (@stickyvars) {
                   2834:     printf('<input type="hidden" name="%s" value="%s">', $_, $input{$_})
                   2835:       if (defined($input{$_})
                   2836:           && ((!defined($DEFAULTVALUE{$_}) || $input{$_} ne $DEFAULTVALUE{$_})
                   2837:               && $input{$_} ne ""));
                   2838:   }
                   2839:   print "<table style=\"border: none\">\n<tr>\n";
                   2840:   print "<td align=\"right\">";
                   2841:   print "<label for=\"r1\" accesskey=\"1\">Diffs between </label>\n";
                   2842:   print "<select id=\"r1\" name=\"r1\">\n";
                   2843:   print "<option value=\"text\" selected>Use Text Field</option>\n";
                   2844:   print $sel;
                   2845:   print "</select>\n";
                   2846:   $diffrev = $revdisplayorder[$#revdisplayorder];
                   2847:   $diffrev = $input{"r1"} if (defined($input{"r1"}));
                   2848:   print
                   2849:     "<input type=\"text\" size=\"$inputTextSize\" name=\"tr1\" value=\"$diffrev\" onchange=\"this.form.r1.selectedIndex=0\"></td>\n";
                   2850:   print "<td><br></td>\n</tr>\n";
                   2851:   print "<tr>\n<td align=\"right\">";
                   2852:   print "<label for=\"r2\" accesskey=\"2\">and </label>\n";
                   2853:   print "<select id=\"r2\" name=\"r2\">\n";
                   2854:   print "<option value=\"text\" selected>Use Text Field</option>\n";
                   2855:   print $sel;
                   2856:   print "</select>\n";
                   2857:   $diffrev = $revdisplayorder[0];
                   2858:   $diffrev = $input{"r2"} if (defined($input{"r2"}));
                   2859:   print
                   2860:     "<input type=\"text\" size=\"$inputTextSize\" name=\"tr2\" value=\"$diffrev\" onchange=\"this.form.r2.selectedIndex=0\"></td>\n";
                   2861:   print
                   2862:     "<td><input type=\"submit\" value=\"  Get Diffs  \" accesskey=\"G\"></td>\n";
                   2863:   print "</tr>\n</table>\n";
                   2864:   print "</form>\n";
                   2865:   print "<hr noshade>\n";
                   2866:   print "<form method=\"get\" action=\"$scriptwhere\">\n";
                   2867:   print "<table style=\"border: none\">\n";
                   2868:   print "<tr>\n<td align=\"right\">";
                   2869:   print "<label for=\"f\" accesskey=\"D\">Preferred Diff type:";
                   2870:   print "</label></td>\n";
                   2871:   print "<td>";
                   2872:   printDiffSelect($use_java_script);
                   2873:   print "</td>\n<td></td>\n</tr>\n";
                   2874:
                   2875:   if (@branchnames) {
                   2876:     print "<tr>\n<td align=\"right\">";
                   2877:     print "<label for=\"only_with_tag\" accesskey=\"B\">";
                   2878:     print "View only Branch:</label></td>\n";
                   2879:     print "<td>";
                   2880:     print "<a name=\"branch\"></a>\n";
                   2881:     print "<select id=\"only_with_tag\" name=\"only_with_tag\"";
                   2882:     print " onchange=\"this.form.submit()\"" if $use_java_script;
                   2883:     print ">\n";
                   2884:     print "<option value=\"\"";
                   2885:     print " selected"
                   2886:       if (defined($input{"only_with_tag"})
                   2887:       && $input{"only_with_tag"} eq "");
                   2888:     print ">Show all branches</option>\n";
                   2889:
                   2890:     foreach (reverse sort @branchnames) {
3.136     scop     2891:       my $selected =
                   2892:         (defined($input{"only_with_tag"}) && $input{"only_with_tag"} eq $_);
                   2893:       printf("<option%s>%s</option>\n",
                   2894:              $selected ? ' selected' : '',
                   2895:              htmlquote($_));
3.120     scop     2896:     }
                   2897:     print "</select></td>\n<td></td>\n</tr>\n";
                   2898:   }
                   2899:
                   2900:   foreach (@stickyvars) {
                   2901:     next if ($_ eq "f");
                   2902:     next if ($_ eq "only_with_tag");
                   2903:     next if ($_ eq "logsort");
                   2904:     print "<input type=\"hidden\" name=\"$_\" value=\"$input{$_}\">\n"
                   2905:       if (defined($input{$_})
                   2906:           && (!defined($DEFAULTVALUE{$_})
                   2907:               || $input{$_} ne $DEFAULTVALUE{$_})
                   2908:           && $input{$_} ne "");
                   2909:   }
                   2910:   print "<tr>\n<td align=\"right\">";
                   2911:   print "<a name=\"logsort\"></a>\n";
                   2912:   print "<label for=\"logsort\" accesskey=\"L\">Sort log by:";
                   2913:   print "</label></td>\n<td>";
                   2914:   printLogSortSelect($use_java_script);
                   2915:   print "</td>\n";
                   2916:   print "<td><input type=\"submit\" value=\"  Set  \" accesskey=\"S\"></td>\n";
                   2917:   print "</tr>\n</table>\n";
                   2918:   print "</form>\n";
                   2919:   html_footer();
                   2920: }
                   2921:
                   2922: sub flush_diff_rows($$$$)
                   2923: {
                   2924:   my ($leftColRef, $rightColRef, $leftRow, $rightRow) = @_;
                   2925:
                   2926:   if (!defined($state)) {
                   2927:     return;
                   2928:   }
                   2929:
                   2930:   if ($state eq "PreChangeRemove") {    # we just got remove-lines before
3.133     scop     2931:     for (my $j = 0; $j < $leftRow; $j++) {
3.120     scop     2932:       print "<tr>\n<td class=\"diff-removed\">&nbsp;@$leftColRef[$j]</td>\n";
                   2933:       print "<td class=\"diff-empty\">&nbsp;</td>\n</tr>\n";
                   2934:     }
                   2935:   } elsif ($state eq "PreChange") {     # state eq "PreChange"
                   2936:                                         # we got removes with subsequent adds
                   2937:
3.133     scop     2938:     for (my $j = 0; $j < $leftRow || $j < $rightRow; $j++) {  # dump both cols
3.120     scop     2939:       print "<tr>\n";
                   2940:       if ($j < $leftRow) {
                   2941:         print "<td class=\"diff-changed\">&nbsp;@$leftColRef[$j]</td>";
                   2942:       } else {
                   2943:         print "<td class=\"diff-changed-missing\">&nbsp;</td>";
                   2944:       }
                   2945:       print "\n";
                   2946:
                   2947:       if ($j < $rightRow) {
                   2948:         print "<td class=\"diff-changed\">&nbsp;@$rightColRef[$j]</td>";
                   2949:       } else {
                   2950:         print "<td class=\"diff-changed-missing\">&nbsp;</td>";
                   2951:       }
                   2952:       print "\n</tr>\n";
                   2953:     }
                   2954:   }
3.1       knu      2955: }
                   2956:
                   2957: ##
                   2958: # Function to generate Human readable diff-files
                   2959: # human_readable_diff(String revision_to_return_to);
                   2960: ##
3.120     scop     2961: sub human_readable_diff($)
                   2962: {
                   2963:   my ($difftxt, $where_nd, $filename, $pathname, $scriptwhere_nd);
                   2964:   my ($fh, $rev) = @_;
                   2965:   my ($date1, $date2, $r1d, $r2d, $r1r, $r2r, $rev1, $rev2, $sym1, $sym2);
                   2966:   my (@rightCol, @leftCol);
                   2967:
                   2968:   ($where_nd       = $where)       =~ s/.diff$//;
                   2969:   ($filename       = $where_nd)    =~ s/^.*\///;
                   2970:   ($pathname       = $where_nd)    =~ s/(Attic\/)?[^\/]*$//;
                   2971:   ($scriptwhere_nd = $scriptwhere) =~ s/.diff$//;
                   2972:
3.133     scop     2973:   navigateHeader($scriptwhere_nd, $pathname, $filename, $rev, 'diff');
3.120     scop     2974:
                   2975:   # Read header to pick up read revision and date, if possible
                   2976:   while (<$fh>) {
                   2977:     ($r1d, $r1r) = /\t(.*)\t(.*)$/ if (/^--- /);
                   2978:     ($r2d, $r2r) = /\t(.*)\t(.*)$/ if (/^\+\+\+ /);
                   2979:     last if (/^\+\+\+ /);
                   2980:   }
                   2981:
                   2982:   if (defined($r1r) && $r1r =~ /^(\d+\.)+\d+$/) {
                   2983:     $rev1  = $r1r;
                   2984:     $date1 = $r1d;
                   2985:   }
                   2986:   if (defined($r2r) && $r2r =~ /^(\d+\.)+\d+$/) {
                   2987:     $rev2  = $r2r;
                   2988:     $date2 = $r2d;
                   2989:   }
                   2990:
                   2991:   print
                   2992:     "<h3 style=\"text-align: center\">Diff for /$where_nd between version $rev1 and $rev2</h3>\n",
                   2993:
                   2994:     # Using style=\"border: none\" here breaks NS 4.x badly...
                   2995:     "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n",
                   2996:     "<tr style=\"background-color: #ffffff\">\n",
3.129     scop     2997:     "<th style=\"text-align: center; vertical-align: top\" width=\"50%\">",
                   2998:     "version $rev1";
3.120     scop     2999:   print ", $date1"         if (defined($date1));
                   3000:   print "<br>Tag: $sym1\n" if ($sym1);
3.129     scop     3001:   print "</th>\n<th style=\"text-align: center; vertical-align: top\" width=\"50%\">version $rev2";
3.120     scop     3002:   print ", $date2"         if (defined($date2));
                   3003:   print "<br>Tag: $sym2\n" if ($sym1);
                   3004:   print "</th>\n";
                   3005:
                   3006:   my $leftRow  = 0;
                   3007:   my $rightRow = 0;
                   3008:   my ($oldline, $newline, $funname, $diffcode, $rest);
                   3009:
                   3010:   # Process diff text
                   3011:
                   3012:   # prefetch several lines
                   3013:   my @buf = head($fh);
                   3014:
                   3015:   my %d = scan_directives(@buf);
                   3016:
                   3017:   while (@buf || !eof($fh)) {
                   3018:     $difftxt = @buf ? shift @buf : <$fh>;
                   3019:
                   3020:     if ($difftxt =~ /^@@/) {
                   3021:       ($oldline, $newline, $funname) =
                   3022:         $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/;
                   3023:       $funname = htmlquote($funname);
                   3024:       $funname =~ s/\s/&nbsp;/go;
                   3025:       print "<tr class=\"diff-heading\">\n<td width=\"50%\">";
                   3026:       print
                   3027:         "<table width=\"100%\" border=\"1\" cellpadding=\"5\">\n<tr>\n<td><b>Line&nbsp;$oldline</b>";
                   3028:       print
                   3029:         "&nbsp;<span style=\"font-size: smaller\">$funname</span></td>\n</tr>\n</table>";
                   3030:       print "</td>\n<td width=\"50%\">";
                   3031:       print
                   3032:         "<table width=\"100%\" border=\"1\" cellpadding=\"5\">\n<tr>\n<td><b>Line&nbsp;$newline</b>";
                   3033:       print
                   3034:         "&nbsp;<span style=\"font-size: smaller\">$funname</span></td>\n</tr>\n</table>\n";
                   3035:       print "</td>\n";
                   3036:       $state    = "dump";
                   3037:       $leftRow  = 0;
                   3038:       $rightRow = 0;
                   3039:     } else {
                   3040:       ($diffcode, $rest) = $difftxt =~ /^([-+ ])(.*)/;
                   3041:       $_ = spacedHtmlText($rest, $d{'tabstop'});
                   3042:
                   3043:       #########
                   3044:       # little state machine to parse unified-diff output (Hen, zeller@think.de)
                   3045:       # in order to get some nice 'ediff'-mode output
                   3046:       # states:
                   3047:       #  "dump"             - just dump the value
                   3048:       #  "PreChangeRemove"  - we began with '-' .. so this could be the start of a 'change' area or just remove
                   3049:       #  "PreChange"        - okey, we got several '-' lines and moved to '+' lines -> this is a change block
                   3050:       ##########
                   3051:
                   3052:       if ($diffcode eq '+') {
                   3053:         if ($state eq "dump")
                   3054:         {    # 'change' never begins with '+': just dump out value
                   3055:           print
                   3056:             "<tr>\n<td class=\"diff-empty\">&nbsp;</td>\n<td class=\"diff-added\">&nbsp;$_</td>\n</tr>\n";
                   3057:         } else {    # we got minus before
                   3058:           $state = "PreChange";
                   3059:           $rightCol[$rightRow++] = $_;
                   3060:         }
                   3061:       } elsif ($diffcode eq '-') {
                   3062:         $state = "PreChangeRemove";
                   3063:         $leftCol[$leftRow++] = $_;
                   3064:       } else {    # empty diffcode
                   3065:         flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   3066:         print
                   3067:           "<tr>\n<td class=\"diff-same\">&nbsp;$_</td>\n<td class=\"diff-same\">&nbsp;$_</td>\n</tr>\n";
                   3068:         $state    = "dump";
                   3069:         $leftRow  = 0;
                   3070:         $rightRow = 0;
                   3071:       }
                   3072:     }
                   3073:   }
                   3074:   close($fh);
                   3075:
                   3076:   flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   3077:
                   3078:   # state is empty if we didn't have any change
                   3079:   if (!$state) {
                   3080:     print "<tr>\n<td colspan=\"2\">&nbsp;</td>\n</tr>\n";
                   3081:     print "<tr class=\"diff-empty\">\n";
                   3082:     print
                   3083:       "<td colspan=\"2\" align=\"center\"><b>- No viewable change -</b></td>\n</tr>\n";
                   3084:   }
                   3085:   print "</table>\n";
                   3086:
                   3087:   print "<hr style=\"width: 100%\" noshade>\n";
                   3088:   print "<form method=\"get\" action=\"${scriptwhere}\">\n";
                   3089:   print "<table style=\"border: none\">\n<tr>\n<td>\n";
                   3090:
                   3091:   # print legend
                   3092:   print "<table border=\"1\">\n<tr>\n<td>";
                   3093:   print
                   3094:     "Legend:<br><table style=\"border: none\" cellspacing=\"0\" cellpadding=\"1\">\n";
                   3095:   print
                   3096:     "<tr>\n<td align=\"center\" class=\"diff-removed\">Removed from v.$rev1</td>\n<td class=\"diff-empty\">&nbsp;</td>\n</tr>\n";
                   3097:   print
                   3098:     "<tr class=\"diff-changed\">\n<td align=\"center\" colspan=\"2\">changed lines</td>\n</tr>\n";
                   3099:   print
                   3100:     "<tr>\n<td class=\"diff-empty\">&nbsp;</td>\n<td align=\"center\" class=\"diff-added\">Added in v.$rev2</td>\n</tr>\n";
                   3101:   print "</table>\n</td>\n</tr>\n</table>\n</td>\n<td>";
                   3102:
                   3103:   # Print format selector
                   3104:   foreach my $var (keys %input) {
                   3105:     next if ($var eq "f");
                   3106:     next if (defined($DEFAULTVALUE{$var})
                   3107:              && $DEFAULTVALUE{$var} eq $input{$var});
                   3108:     print "<input type=\"hidden\" name=\"", urlencode($var), "\" value=\"",
                   3109:       urlencode($input{$var}), "\">\n";
                   3110:   }
                   3111:   printDiffSelect($use_java_script);
                   3112:   print "<input type=\"submit\" value=\"Show\">\n";
                   3113:   print "</td>\n";
                   3114:
                   3115:   print "</tr>\n</table>\n";
                   3116:   print "</form>\n";
                   3117: }
                   3118:
                   3119: sub navigateHeader($$$$$)
                   3120: {
                   3121:   my ($swhere, $path, $filename, $rev, $title) = @_;
                   3122:   $swhere = "" if ($swhere eq $scriptwhere);
                   3123:   $swhere = './' . urlencode($filename) if ($swhere eq "");
                   3124:
3.134     scop     3125:   my $css = $cssurl ?
                   3126:     "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssurl\">\n" : '';
                   3127:
3.136     scop     3128:   my $qfile = htmlquote($filename);
                   3129:   my $qpath = htmlquote($path);
3.100     knu      3130:
3.133     scop     3131:   http_header();
                   3132:
3.120     scop     3133:   print <<EOF;
3.100     knu      3134: $HTML_DOCTYPE
3.89      knu      3135: <html>
                   3136: <head>
3.136     scop     3137: <title>$qpath$qfile - $title - $rev</title>
3.134     scop     3138: $HTML_META$css</head>
3.129     scop     3139: <body class="src">
                   3140: <table width="100%" class="navigate-header" style="border: none" cellspacing="0" cellpadding="1">
3.89      knu      3141: <tr valign="bottom"><td>
3.64      knu      3142: EOF
                   3143:
3.120     scop     3144:   print &link($backicon, "$swhere$query#rev$rev");
3.136     scop     3145:   printf '<b>Return to %s CVS log',
                   3146:     &link(htmlquote($filename), "$swhere$query#rev$rev");
3.120     scop     3147:   print "</b> $fileicon</td>";
                   3148:
                   3149:   print "<td align=\"right\">$diricon <b>Up to ", &clickablePath($path, 1),
                   3150:     "</b></td>";
                   3151:   print "</tr></table>";
                   3152: }
                   3153:
                   3154: sub plural_write($$)
                   3155: {
                   3156:   my ($num, $text) = @_;
                   3157:   if ($num != 1) {
                   3158:     $text .= "s";
                   3159:   }
                   3160:
                   3161:   if ($num > 0) {
                   3162:     return join (' ', $num, $text);
                   3163:   } else {
                   3164:     return "";
                   3165:   }
3.1       knu      3166: }
                   3167:
                   3168: ##
                   3169: # print readable timestamp in terms of
                   3170: # '..time ago'
                   3171: # H. Zeller <zeller@think.de>
                   3172: ##
3.120     scop     3173: sub readableTime($$)
                   3174: {
                   3175:   my ($secs, $long) = @_;
                   3176:
                   3177:   # this function works correct for time >= 2 seconds
                   3178:   if ($secs < 2) {
                   3179:     return "very little time";
                   3180:   }
                   3181:
                   3182:   my %desc = (
3.133     scop     3183:               1        => 'second',
                   3184:               60       => 'minute',
                   3185:               3600     => 'hour',
                   3186:               86400    => 'day',
                   3187:               604800   => 'week',
                   3188:               2628000  => 'month',
                   3189:               31536000 => 'year'
                   3190:              );
                   3191:
3.120     scop     3192:   my @breaks = sort { $a <=> $b } keys %desc;
3.133     scop     3193:   my $i = 0;
3.120     scop     3194:
                   3195:   while ($i <= $#breaks && $secs >= 2 * $breaks[$i]) {
                   3196:     $i++;
                   3197:   }
                   3198:   $i--;
3.133     scop     3199:   my $break  = $breaks[$i];
                   3200:   my $retval = plural_write(int($secs / $break), $desc{$break});
3.120     scop     3201:
                   3202:   if ($long == 1 && $i > 0) {
                   3203:     my $rest = $secs % $break;
                   3204:     $i--;
                   3205:     $break = $breaks[$i];
                   3206:     my $resttime = plural_write(int($rest / $break), $desc{$break});
                   3207:     if ($resttime) {
                   3208:       $retval .= ", $resttime";
                   3209:     }
                   3210:   }
3.1       knu      3211:
3.120     scop     3212:   return $retval;
3.1       knu      3213: }
                   3214:
                   3215: ##
                   3216: # clickablePath(String pathname, boolean last_item_clickable)
                   3217: #
                   3218: # returns a html-ified path whereas each directory is a link for
                   3219: # faster navigation. last_item_clickable controls whether the
                   3220: # basename (last directory/file) is a link as well
                   3221: ##
3.120     scop     3222: sub clickablePath($$)
                   3223: {
                   3224:   my ($pathname, $clickLast) = @_;
                   3225:   my $retval = '';
                   3226:
                   3227:   if ($pathname eq '/') {
                   3228:
                   3229:     # this should never happen - chooseCVSRoot() is
                   3230:     # intended to do this
                   3231:     $retval = "[$cvstree]";
                   3232:   } else {
                   3233:     $retval .=
                   3234:       ' ' . &link("[$cvstree]", sprintf('%s/%s#dirlist', $scriptname, $query));
                   3235:     my $wherepath = '';
                   3236:     my ($lastslash) = $pathname =~ m|/$|;
                   3237:
                   3238:     foreach (split (/\//, $pathname)) {
                   3239:       $retval .= " / ";
                   3240:       $wherepath .= "/$_";
                   3241:       my ($last) = "$wherepath/" eq "/$pathname"
                   3242:         || $wherepath eq "/$pathname";
                   3243:
                   3244:       if ($clickLast || !$last) {
3.136     scop     3245:         $retval .= &link(htmlquote($_),
3.120     scop     3246:                          join ('',
                   3247:                                $scriptname, urlencode($wherepath),
                   3248:                                (!$last || $lastslash ? '/' : ''), $query,
                   3249:                                (!$last || $lastslash ? "#dirlist" : "")));
                   3250:       } else {    # do not make a link to the current dir
3.136     scop     3251:         $retval .= htmlquote($_);
3.120     scop     3252:       }
                   3253:     }
                   3254:   }
                   3255:   return $retval;
                   3256: }
                   3257:
                   3258: sub chooseCVSRoot()
                   3259: {
                   3260:
                   3261:   print "<form method=\"get\" action=\"${scriptwhere}\">\n";
                   3262:   if (2 <= @CVSROOT) {
3.133     scop     3263:     foreach my $k (keys %input) {
3.120     scop     3264:       print "<input type=\"hidden\" name=\"$k\" value=\"$input{$k}\">\n"
                   3265:         if ($input{$k}) && ($k ne "cvsroot");
                   3266:     }
                   3267:
                   3268:     # Form-Elements look wierd in Netscape if the background
                   3269:     # isn't gray and the form elements are not placed
                   3270:     # within a table ...
                   3271:     print "<table style=\"border: none\">\n<tr>\n";
                   3272:     print
                   3273:       "<td><label for=\"cvsroot\" accesskey=\"C\">CVS Root:</label></td>\n";
                   3274:     print "<td>\n<select id=\"cvsroot\" name=\"cvsroot\"";
                   3275:     print " onchange=\"this.form.submit()\"" if $use_java_script;
                   3276:     print ">\n";
                   3277:
3.133     scop     3278:     foreach my $k (@CVSROOT) {
3.136     scop     3279:       printf("<option value=\"%s\"%s>%s</option>\n",
                   3280:              htmlquote($k),
                   3281:              ($k eq $cvstree) ? ' selected' : '',
                   3282:              htmlquote($CVSROOTdescr{$k} || $k));
3.120     scop     3283:     }
                   3284:     print "</select>\n</td>\n<td>";
                   3285:   } else {
                   3286:
                   3287:     # no choice -- but we need the form to select module/path,
                   3288:     # at least for Netscape
                   3289:     print "<p>\n";
                   3290:     print "CVS Root: <b>[$cvstree]</b>";
                   3291:   }
                   3292:
                   3293:   print " <label for=\"mpath\" accesskey=\"M\">Module path or alias:";
                   3294:   print "</label>\n";
                   3295:   print
                   3296:     "<input type=\"text\" id=\"mpath\" name=\"path\" value=\"\" size=\"15\">\n";
                   3297:   print "<input type=\"submit\" value=\"Go\" accesskey=\"O\">";
                   3298:
                   3299:   if (2 <= @CVSROOT) {
                   3300:     print "</td>\n</tr>\n</table>";
                   3301:   } else {
                   3302:     print "</p>";
                   3303:   }
                   3304:   print "\n</form>";
                   3305: }
                   3306:
                   3307: sub chooseMirror()
                   3308: {
                   3309:
                   3310:   # This code comes from the original BSD-cvsweb
                   3311:   # and may not be useful for your site; If you don't
                   3312:   # set %MIRRORS this won't show up, anyway.
                   3313:   scalar(%MIRRORS) or return;
                   3314:
                   3315:   # Should perhaps exclude the current site somehow...
                   3316:   print "\n<p>\nThis CVSweb is mirrored in\n";
                   3317:
                   3318:   my @tmp = map(&link(htmlquote($_), $MIRRORS{$_}), sort keys %MIRRORS);
                   3319:   my $tmp = pop (@tmp);
                   3320:
                   3321:   if (scalar(@tmp)) {
                   3322:     print join (', ', @tmp), ' and ';
                   3323:   }
                   3324:
                   3325:   print "$tmp.\n</p>\n";
                   3326: }
                   3327:
                   3328: sub fileSortCmp()
                   3329: {
                   3330:   my ($comp) = 0;
                   3331:   my ($c, $d, $af, $bf);
                   3332:
                   3333:   ($af = $a) =~ s/,v$//;
                   3334:   ($bf = $b) =~ s/,v$//;
                   3335:   my ($rev1, $date1, $log1, $author1, $filename1) = @{$fileinfo{$af}}
                   3336:     if (defined($fileinfo{$af}));
                   3337:   my ($rev2, $date2, $log2, $author2, $filename2) = @{$fileinfo{$bf}}
                   3338:     if (defined($fileinfo{$bf}));
                   3339:
3.140     scop     3340:   if (defined($filename1) && defined($filename2) &&
                   3341:       $af eq $filename1   && $bf eq $filename2)
3.120     scop     3342:   {
                   3343:
                   3344:     # Two files
3.140     scop     3345:     $comp = -revcmp($rev1, $rev2)             if ($byrev  && $rev1  && $rev2);
                   3346:     $comp = ($date2   <=> $date1)             if ($bydate && $date1 && $date2);
                   3347:     if ($input{ignorecase}) {
                   3348:       $comp = (uc($log1)    cmp uc($log2))    if ($bylog && $log1 && $log2);
                   3349:       $comp = (uc($author1) cmp uc($author2)) if ($byauthor &&
                   3350:                                                   $author1 && $author2);
                   3351:     } else {
                   3352:       $comp = ($log1    cmp $log2)            if ($bylog && $log1 && $log2);
                   3353:       $comp = ($author1 cmp $author2)         if ($byauthor &&
                   3354:                                                   $author1 && $author2);
                   3355:     }
3.120     scop     3356:   }
                   3357:
                   3358:   if ($comp == 0) {
                   3359:
                   3360:     # Directories first, then files under version control,
                   3361:     # then other, "rogue" files.
                   3362:     # Sort by filename if no other criteria available.
                   3363:
                   3364:     my $ad = (
                   3365:       (-d "$fullname/$a")
                   3366:       ? 'D'
                   3367:       : (defined($fileinfo{$af}) ? 'F' : 'R')
                   3368:     );
                   3369:     my $bd = (
                   3370:       (-d "$fullname/$b")
                   3371:       ? 'D'
                   3372:       : (defined($fileinfo{$bf}) ? 'F' : 'R')
                   3373:     );
                   3374:     ($c = $a) =~ s|.*/||;
                   3375:     ($d = $b) =~ s|.*/||;
3.140     scop     3376:     $comp = $input{ignorecase} ?
                   3377:       (uc("$ad$c") cmp uc("$bd$d")) : ("$ad$c" cmp "$bd$d");
3.120     scop     3378:   }
                   3379:   return $comp;
3.1       knu      3380: }
                   3381:
                   3382: # make A url for downloading
3.120     scop     3383: sub download_url($$;$)
                   3384: {
                   3385:   my ($url, $revision, $mimetype) = @_;
3.1       knu      3386:
3.120     scop     3387:   $revision =~ s/\b0\.//;
3.1       knu      3388:
3.120     scop     3389:   if (defined($checkoutMagic)
                   3390:     && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup"))
                   3391:   {
                   3392:     my $path = $where;
                   3393:     $path =~ s|[^/]+$||;
                   3394:     $url = "$scriptname/$checkoutMagic/${path}$url";
                   3395:   }
                   3396:   $url .= "?rev=$revision";
                   3397:   $url .= '&content-type=' . urlencode($mimetype) if (defined($mimetype));
3.67      knu      3398:
3.120     scop     3399:   $url;
3.1       knu      3400: }
                   3401:
3.12      knu      3402: # Presents a link to download the
3.1       knu      3403: # selected revision
3.120     scop     3404: sub download_link($$$;$)
                   3405: {
                   3406:   my ($url, $revision, $textlink, $mimetype) = @_;
                   3407:   my ($fullurl) = download_url($url, $revision, $mimetype);
                   3408:
                   3409:   $fullurl =~ s/:/sprintf("%%%02x", ord($&))/eg;
                   3410:
                   3411:   printf '<a href="%s"', hrefquote("$fullurl$barequery");
                   3412:
                   3413:   if ($open_extern_window
                   3414:       && (!defined($mimetype) || $mimetype ne "text/x-cvsweb-markup"))
                   3415:   {
                   3416:     print ' target="cvs_checkout"';
                   3417:
                   3418:     # we should have
                   3419:     #   'if (document.cvswin==null) document.cvswin=window.open(...'
                   3420:     # in order to allow the user to resize the window; otherwise
                   3421:     # the user may resize the window, but on next checkout - zap -
                   3422:     # its original (configured s. cvsweb.conf) size is back again
                   3423:     # .. annoying (if $extern_window_(width|height) is defined)
                   3424:     # but this if (..) solution is far from perfect
                   3425:     # what we need to do as well is
                   3426:     # 1) save cvswin in an invisible frame that always exists
                   3427:     #    (document.cvswin will be void on next load)
                   3428:     # 2) on close of the cvs_checkout - window set the cvswin
                   3429:     #    variable to 'null' again - so that it will be
                   3430:     #    reopenend with the configured size
                   3431:     # anyone a JavaScript programmer ?
                   3432:     # .. so here without if (..):
                   3433:     # currently, the best way is to comment out the size parameters
                   3434:     # ($extern_window...) in cvsweb.conf.
                   3435:     if ($use_java_script) {
                   3436:       my @attr = qw(resizable scrollbars);
                   3437:
                   3438:       push @attr, qw(status toolbar)
                   3439:         if (defined($mimetype) && $mimetype eq "text/html");
                   3440:
                   3441:       push @attr, "width=$extern_window_width"
                   3442:         if (defined($extern_window_width));
                   3443:
                   3444:       push @attr, "height=$extern_window_height"
                   3445:         if (defined($extern_window_height));
                   3446:
                   3447:       # We need the "return false" here to prevent browsers
                   3448:       # from following the href after the onclick handler.
                   3449:       # This would effectively load the same document in
                   3450:       # the same window *twice*.
                   3451:       printf q` onclick="window.open('%s','cvs_checkout','%s');return false"`,
                   3452:         hrefquote("$fullurl$barequery"), join (',', @attr);
                   3453:     }
                   3454:   }
                   3455:   print "><b>$textlink</b></a>";
3.132     scop     3456: }
                   3457:
                   3458: sub graph_link($$;$)
                   3459: {
                   3460:   my ($url, $revision, $text) = @_;
                   3461:   $text ||= $graphicon;
                   3462:   return sprintf('<a href="%s?graph=%s%s">%s</a>',
                   3463:                  hrefquote($url), hrefquote($revision), hrefquote($barequery),
                   3464:                  $text);
3.1       knu      3465: }
                   3466:
                   3467: # Returns a Query string with the
                   3468: # specified parameter toggled
3.140     scop     3469: sub toggleQuery($;$)
3.120     scop     3470: {
                   3471:   my ($toggle, $value) = @_;
3.133     scop     3472:
                   3473:   my %vars = %input;
3.120     scop     3474:
                   3475:   if (defined($value)) {
                   3476:     $vars{$toggle} = $value;
                   3477:   } else {
                   3478:     $vars{$toggle} = $vars{$toggle} ? 0 : 1;
                   3479:   }
                   3480:
                   3481:   # Build a new query of non-default paramenters
3.133     scop     3482:   my $newquery = "";
                   3483:   foreach my $var (@stickyvars) {
3.120     scop     3484:     my ($value)   = defined($vars{$var})         ? $vars{$var}         : "";
                   3485:     my ($default) = defined($DEFAULTVALUE{$var}) ? $DEFAULTVALUE{$var} : "";
                   3486:
                   3487:     if ($value ne $default) {
                   3488:       $newquery .= "&" if ($newquery ne "");
                   3489:       $newquery .= urlencode($var) . "=" . urlencode($value);
                   3490:     }
                   3491:   }
                   3492:
                   3493:   if ($newquery) {
                   3494:     return '?' . $newquery;
                   3495:   }
                   3496:   return "";
                   3497: }
                   3498:
                   3499: sub urlencode($)
                   3500: {
                   3501:   local ($_) = @_;
                   3502:
                   3503:   s/[\000-+{-\377]/sprintf("%%%02x", ord($&))/ge;
                   3504:
                   3505:   $_;
                   3506: }
                   3507:
                   3508: sub htmlquote($)
                   3509: {
                   3510:   local ($_) = @_;
                   3511:
                   3512:   # Special Characters; RFC 1866
                   3513:   s/&/&amp;/g;
                   3514:   s/\"/&quot;/g;
                   3515:   s/</&lt;/g;
                   3516:   s/>/&gt;/g;
                   3517:
                   3518:   $_;
                   3519: }
                   3520:
                   3521: sub htmlunquote($)
                   3522: {
                   3523:   local ($_) = @_;
                   3524:
                   3525:   # Special Characters; RFC 1866
                   3526:   s/&quot;/\"/g;
                   3527:   s/&lt;/</g;
                   3528:   s/&gt;/>/g;
                   3529:   s/&amp;/&/g;
                   3530:
                   3531:   $_;
                   3532: }
                   3533:
                   3534: sub hrefquote($)
                   3535: {
                   3536:   local ($_) = @_;
                   3537:
                   3538:   y/ /+/;
                   3539:
                   3540:   htmlquote($_);
                   3541: }
                   3542:
                   3543: sub http_header(;$)
                   3544: {
                   3545:   my $content_type = shift || "text/html";
                   3546:
                   3547:   $content_type .= "; charset=$charset"
                   3548:     if $content_type =~ m,^text/, && defined($charset) && $charset;
                   3549:
                   3550:   if (defined($moddate)) {
                   3551:     if ($is_mod_perl) {
                   3552:       Apache->request->header_out(
                   3553:         "Last-Modified" => scalar gmtime($moddate) . " GMT");
                   3554:     } else {
                   3555:       print "Last-Modified: ", scalar gmtime($moddate), " GMT\r\n";
                   3556:     }
                   3557:   }
                   3558:
                   3559:   if ($is_mod_perl) {
                   3560:     Apache->request->content_type($content_type);
                   3561:   } else {
                   3562:     print "Content-Type: $content_type\r\n";
                   3563:   }
                   3564:
                   3565:   if ($allow_compress && $maycompress) {
                   3566:     if ($has_zlib
                   3567:         || (defined($CMD{gzip}) && open(GZIP, "| $CMD{gzip} -1 -c")))
                   3568:     {
                   3569:
                   3570:       if ($is_mod_perl) {
                   3571:         Apache->request->content_encoding("x-gzip");
                   3572:         Apache->request->header_out(Vary => "Accept-Encoding");
                   3573:         Apache->request->send_http_header;
                   3574:       } else {
                   3575:         print "Content-Encoding: x-gzip\r\n";
                   3576:         print "Vary: Accept-Encoding\r\n";    #RFC 2068, 14.43
                   3577:         print "\r\n";                         # Close headers
                   3578:       }
                   3579:       $| = 1;
                   3580:       $| = 0;                                 # Flush header output
                   3581:
                   3582:       if ($has_zlib) {
                   3583:         tie *GZIP, __PACKAGE__, \*STDOUT;
                   3584:       }
                   3585:       select(GZIP);
                   3586:       $gzip_open = 1;
                   3587:
                   3588:       #            print "<!-- gzipped -->" if ($content_type =~ m|^text/html\b|);
                   3589:     } else {
                   3590:       if ($is_mod_perl) {
                   3591:         Apache->request->send_http_header;
                   3592:       } else {
                   3593:         print "\r\n";    # Close headers
                   3594:       }
                   3595:       print
                   3596:         "<span style=\"font-size: smaller\">Unable to find gzip binary in the <b>\$command_path</b> ($command_path) to compress output</span><br>";
                   3597:     }
                   3598:   } else {
                   3599:
                   3600:     if ($is_mod_perl) {
                   3601:       Apache->request->send_http_header;
                   3602:     } else {
                   3603:       print "\r\n";      # Close headers
                   3604:     }
                   3605:   }
                   3606: }
                   3607:
3.131     scop     3608:
3.120     scop     3609: sub html_header($)
                   3610: {
3.131     scop     3611:   my $title = htmlquote(shift);
3.134     scop     3612:   my $css = $cssurl ?
                   3613:     "<link rel=\"stylesheet\" type=\"text/css\" href=\"$cssurl\">\n" : '';
                   3614:   http_header('text/html');
3.120     scop     3615:   print <<EOH;
3.100     knu      3616: $HTML_DOCTYPE
3.1       knu      3617: <html>
3.40      knu      3618: <head>
3.134     scop     3619: <title>$title</title>
                   3620: $HTML_META$css</head>
3.129     scop     3621: <body>
3.1       knu      3622: $logo <h1 align="center">$title</h1>
                   3623: EOH
                   3624: }
                   3625:
3.120     scop     3626: sub html_footer()
                   3627: {
3.127     scop     3628:   print "<hr noshade>\n<address>$address</address>\n" if $address;
                   3629:   print "</body>\n</html>\n";
3.1       knu      3630: }
                   3631:
3.120     scop     3632: sub link_tags($)
                   3633: {
                   3634:   my ($tags) = @_;
                   3635:   my ($ret)  = "";
                   3636:   my ($fileurl, $filename);
                   3637:
                   3638:   ($filename = $where) =~ s/^.*\///;
                   3639:   $fileurl = './' . urlencode($filename);
                   3640:
                   3641:   foreach my $sym (split (", ", $tags)) {
                   3642:     $ret .= ",\n" if ($ret ne "");
3.136     scop     3643:     $ret .= &link(htmlquote($sym),
                   3644:                   $fileurl . toggleQuery('only_with_tag', $sym));
3.120     scop     3645:   }
                   3646:   return "$ret\n";
3.1       knu      3647: }
                   3648:
                   3649: #
3.81      knu      3650: # See if a module is listed in the config file's @HideModules list.
3.1       knu      3651: #
3.120     scop     3652: sub forbidden_module($)
                   3653: {
                   3654:   my ($module) = @_;
                   3655:   local $_;
                   3656:
                   3657:   for (@HideModules) {
                   3658:     return 1 if $module =~ $_;
                   3659:   }
                   3660:   return 0;
                   3661: }
                   3662:
                   3663: sub forbidden_file($)
                   3664: {
                   3665:   my ($path) = @_;
                   3666:   $path = substr($path, length($cvsroot) + 1);
                   3667:   local $_;
                   3668:   for (@ForbiddenFiles) {
                   3669:     return 1 if $path =~ $_;
                   3670:   }
                   3671:   return 0;
3.129     scop     3672: }
                   3673:
3.25      knu      3674:
                   3675: # Close the GZIP handle remove the tie.
                   3676:
3.120     scop     3677: sub gzipclose
                   3678: {
                   3679:   if ($gzip_open) {
                   3680:     select(STDOUT);
                   3681:     close(GZIP);
                   3682:     untie *GZIP;
                   3683:     $gzip_open = 0;
                   3684:   }
3.23      knu      3685: }
                   3686:
                   3687: # implement a gzipped file handle via the Compress:Zlib compression
                   3688: # library.
                   3689:
                   3690: sub MAGIC1() { 0x1f }
                   3691: sub MAGIC2() { 0x8b }
3.80      knu      3692: sub OSCODE() { 3 }
3.23      knu      3693:
3.120     scop     3694: sub TIEHANDLE
                   3695: {
                   3696:   my ($class, $out) = @_;
                   3697:   my ($d) = Compress::Zlib::deflateInit(
                   3698:     -Level      => Compress::Zlib::Z_BEST_COMPRESSION(),
                   3699:     -WindowBits => -Compress::Zlib::MAX_WBITS()
                   3700:     )
                   3701:     or return undef;
                   3702:   my ($o) = { handle => $out,
                   3703:               dh     => $d,
                   3704:               crc    => 0,
                   3705:               len    => 0,
                   3706:             };
                   3707:   my ($header) = pack("c10",
                   3708:                       MAGIC1, MAGIC2, Compress::Zlib::Z_DEFLATED(),
                   3709:                       0, 0, 0, 0, 0, 0, OSCODE);
                   3710:   print {$o->{handle}} $header;
                   3711:   return bless($o, $class);
                   3712: }
                   3713:
                   3714: sub PRINT
                   3715: {
                   3716:   my ($o)   = shift;
                   3717:   my ($buf) = join (defined $, ? $, : "", @_);
                   3718:   my ($len) = length($buf);
                   3719:   my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3720:   print {$o->{handle}} $compressed if defined($compressed);
                   3721:   $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3722:   $o->{len} += $len;
                   3723:   return $len;
                   3724: }
                   3725:
                   3726: sub PRINTF
                   3727: {
                   3728:   my ($o)   = shift;
                   3729:   my ($fmt) = shift;
                   3730:   my ($buf) = sprintf($fmt, @_);
                   3731:   my ($len) = length($buf);
                   3732:   my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3733:   print {$o->{handle}} $compressed if defined($compressed);
                   3734:   $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3735:   $o->{len} += $len;
                   3736:   return $len;
                   3737: }
                   3738:
                   3739: sub WRITE
                   3740: {
                   3741:   my ($o, $buf, $len, $off) = @_;
                   3742:   my ($compressed, $status) = $o->{dh}->deflate(substr($buf, 0, $len));
                   3743:   print {$o->{handle}} $compressed if defined($compressed);
                   3744:   $o->{crc} = Compress::Zlib::crc32(substr($buf, 0, $len), $o->{crc});
                   3745:   $o->{len} += $len;
                   3746:   return $len;
                   3747: }
                   3748:
                   3749: sub CLOSE
                   3750: {
                   3751:   my ($o) = @_;
                   3752:   return if !defined($o->{dh});
                   3753:   my ($buf) = $o->{dh}->flush();
                   3754:   $buf .= pack("V V", $o->{crc}, $o->{len});
                   3755:   print {$o->{handle}} $buf;
                   3756:   undef $o->{dh};
                   3757: }
                   3758:
                   3759: sub DESTROY
                   3760: {
                   3761:   my ($o) = @_;
                   3762:   CLOSE($o);
1.1       jfieber  3763: }

CVSweb