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

Annotation of cvsweb/cvsweb.cgi, Revision 4.33

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

CVSweb