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

Annotation of cvsweb/cvsweb.cgi, Revision 4.20

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

CVSweb