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

Annotation of cvsweb/cvsweb.cgi, Revision 4.18

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

CVSweb