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

Annotation of cvsweb/cvsweb.cgi, Revision 4.39

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

CVSweb