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

Annotation of cvsweb/cvsweb.cgi, Revision 4.24

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

CVSweb