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

Annotation of cvsweb/cvsweb.cgi, Revision 4.19

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

CVSweb