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

Annotation of cvsweb/cvsweb.cgi, Revision 4.27

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

CVSweb