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

Annotation of cvsweb/cvsweb.cgi, Revision 4.25

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

CVSweb