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

Annotation of cvsweb/cvsweb.cgi, Revision 4.40

4.5       schwarze    1: #!/usr/bin/perl
4.40    ! schwarze    2: # $Id: cvsweb.cgi,v 4.39 2019/11/29 23:42:40 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: }
3.80      knu      1289:
3.276     scop     1290: else {
3.120     scop     1291:   # Assume it's a module name with a potential path following it.
3.268     scop     1292:   my $module;
3.266     scop     1293:   my $xtra = (($module = $where) =~ s|(/.*)||) ? $1 : '';
3.80      knu      1294:
3.120     scop     1295:   # Is there an indexed version of modules?
3.268     scop     1296:   my $fh = do { local (*FH); };
                   1297:   if (open($fh, catfile($cvsroot, 'CVSROOT', 'modules'))) {
3.120     scop     1298:     while (<$fh>) {
                   1299:       if (/^(\S+)\s+(\S+)/o
                   1300:           && $module eq $1
3.268     scop     1301:           && $module ne $2
                   1302:           && -d "$cvsroot/$2")
3.120     scop     1303:       {
3.267     scop     1304:         close($fh);
3.120     scop     1305:         redirect("$scriptname/$2$xtra$query");
                   1306:       }
                   1307:     }
3.267     scop     1308:     close($fh);
3.120     scop     1309:   }
                   1310:   fatal("404 Not Found", '%s: no such file or directory', $where);
3.80      knu      1311: }
3.23      knu      1312:
3.25      knu      1313: gzipclose();
3.80      knu      1314:
3.1       knu      1315: ## End MAIN
                   1316:
3.175     scop     1317:
4.17      schwarze 1318: sub printDiffSelect()
3.120     scop     1319: {
                   1320:   print '<select id="f" name="f"';
                   1321:   print ">\n";
                   1322:
3.175     scop     1323:   for my $difftype (@DIFFTYPES) {
3.120     scop     1324:     printf("<option value=\"%s\"%s>%s</option>\n",
3.208     scop     1325:            $difftype, $input{f} eq $difftype ? ' selected="selected"' : '',
3.175     scop     1326:            "\u$DIFFTYPES{$difftype}{descr}");
3.120     scop     1327:   }
                   1328:
                   1329:   print "</select>";
                   1330: }
                   1331:
3.175     scop     1332:
3.168     scop     1333: sub printDiffSelectStickyVars()
                   1334: {
                   1335:   while (my ($key, $val) = each %input) {
                   1336:     next if ($key eq 'f');
                   1337:     next if (defined($DEFAULTVALUE{$key}) && $DEFAULTVALUE{$key} eq $val);
3.206     scop     1338:     print "<input type=\"hidden\" name=\"", htmlquote($key), "\" value=\"",
3.208     scop     1339:       htmlquote($val), "\" />\n";
3.168     scop     1340:   }
                   1341: }
                   1342:
3.175     scop     1343:
4.17      schwarze 1344: sub printLogSortSelect()
3.120     scop     1345: {
                   1346:   print '<select id="logsort" name="logsort"';
                   1347:   print ">\n";
                   1348:
3.175     scop     1349:   for my $sortkey (@LOGSORTKEYS) {
3.120     scop     1350:     printf("<option value=\"%s\"%s>%s</option>\n",
3.208     scop     1351:            $sortkey, $logsort eq $sortkey ? ' selected="selected"' : '',
3.175     scop     1352:            "\u$LOGSORTKEYS{$sortkey}{descr}");
3.120     scop     1353:   }
                   1354:
                   1355:   print "</select>";
                   1356: }
                   1357:
3.175     scop     1358:
3.197     scop     1359: #
                   1360: # Find the last modified, version controlled files in the given directories.
                   1361: # Compares solely based on modification timestamps.  Files in the returned list
                   1362: # are without the ,v suffix, and unreadable files have been filtered out.
                   1363: #
3.120     scop     1364: sub findLastModifiedSubdirs(@)
                   1365: {
                   1366:   my (@dirs) = @_;
                   1367:
3.175     scop     1368:   my @files;
                   1369:   foreach my $dirname (@dirs) {
3.196     scop     1370:     next if ($dirname eq curdir() || $dirname eq updir());
                   1371:     my $dir = catdir($fullname, $dirname);
3.120     scop     1372:     next if (!-d $dir);
                   1373:
                   1374:     my $dh = do { local (*DH); };
                   1375:     opendir($dh, $dir) or next;
3.233     scop     1376:     my (@filenames) = grep(!forbidden(catfile($dir, $_)), readdir($dh));
3.120     scop     1377:     closedir($dh);
                   1378:
3.175     scop     1379:     my $lastmod     = undef;
                   1380:     my $lastmodtime = undef;
3.120     scop     1381:     foreach my $filename (@filenames) {
3.197     scop     1382:       ($filename) =
                   1383:         (catfile($dirname, $filename) =~ VALID_PATH) or next; # untaint
3.196     scop     1384:       my ($file) = catfile($fullname, $filename);
3.197     scop     1385:       next if ($filename !~ /,v$/o || !-f $file || !-r _);
                   1386:       my $modtime = -M _;
3.120     scop     1387:       if (!defined($lastmod) || $modtime < $lastmodtime) {
3.233     scop     1388:         ($lastmod    = $filename) =~ s/,v$//;
3.120     scop     1389:         $lastmodtime = $modtime;
                   1390:       }
                   1391:     }
3.175     scop     1392:     push(@files, $lastmod) if (defined($lastmod));
3.120     scop     1393:   }
                   1394:   return @files;
                   1395: }
                   1396:
3.175     scop     1397:
3.120     scop     1398: sub htmlify_sub(&$)
                   1399: {
                   1400:   (my $proc, local $_) = @_;
3.261     scop     1401:   my @a = split(m|(<a [^>]+>[^<]*</a>)|i);
3.120     scop     1402:   my $linked;
                   1403:   my $result = '';
                   1404:
                   1405:   while (($_, $linked) = splice(@a, 0, 2)) {
                   1406:     &$proc();
                   1407:     $result .= $_      if defined($_);
                   1408:     $result .= $linked if defined($linked);
                   1409:   }
                   1410:
3.175     scop     1411:   return $result;
3.120     scop     1412: }
                   1413:
3.175     scop     1414:
3.120     scop     1415: sub htmlify($;$)
                   1416: {
                   1417:   (local $_, my $extra) = @_;
                   1418:
                   1419:   $_ = htmlquote($_);
                   1420:
                   1421:   # get URL's as link
                   1422:   s{
3.130     scop     1423:     ((https?|ftp)://.+?)([\s\']|&(quot|[lg]t);)
3.120     scop     1424:    }{
3.130     scop     1425:      &link($1, htmlunquote($1)) . $3
3.120     scop     1426:    }egx;
                   1427:
3.290     scop     1428:   if ($allow_mailtos) {
                   1429:     # Make mailto: links from email addresses.
                   1430:     $_ = htmlify_sub {
                   1431:       s<
                   1432:         ([\w+=\-.!]+@[\w\-]+(?:\.[\w\-]+)+)
                   1433:        ><
                   1434:          &link($1, "mailto:$1")
                   1435:        >egix;
                   1436:     } $_;
                   1437:   }
3.120     scop     1438:
                   1439:   if ($extra) {
                   1440:     # get manpage specs as link: "foo.1" "foo(1)"
                   1441:     if (defined($mancgi)) {
                   1442:       $_ = htmlify_sub {
                   1443:         s{
3.143     knu      1444:           (
                   1445:            \b ( \w[\w+\-.]* (?: ::\w[\w+\-.]*)* )
3.120     scop     1446:            (?:
3.143     knu      1447:             \( ([0-9n]) \) \B
3.120     scop     1448:             |
3.143     knu      1449:             \. ([0-9n]) \b
3.120     scop     1450:            )
                   1451:           )
                   1452:          }{
3.149     scop     1453:             my($text, $name, $section) = ($1, $2, defined($3) ? $3 : $4);
                   1454:             ($name =~ /[A-Za-z]/ && $name !~ /\.(:|$)/)
4.6       schwarze 1455:              ? &link($text, sprintf($mancgi, uri_escape($name), $section))
3.149     scop     1456:               : $text;
3.120     scop     1457:          }egx;
                   1458:       } $_;
                   1459:     }
                   1460:   }
                   1461:
3.175     scop     1462:   return $_;
3.120     scop     1463: }
                   1464:
3.175     scop     1465:
3.120     scop     1466: sub spacedHtmlText($;$)
                   1467: {
3.175     scop     1468:   (local $_, my $ts) = @_;
3.224     scop     1469:   return '' unless defined($_);
3.284     scop     1470:   $ts ||= $tabstop || 8;
3.120     scop     1471:
3.286     scop     1472:   # Expand tabs
                   1473:   1 while s/(.*?)(\t+)/$1 . ' ' x (length($2) * $ts - length($1) % $ts)/e;
3.120     scop     1474:
                   1475:   if ($hr_breakable) {
3.277     scop     1476:     s/^ /\001nbsp;/;      # protect leading and...
                   1477:     s/ $/\001nbsp;/;      # ...trailing whitespace (mostly for String::Ediff),
                   1478:     s/  / \001nbsp;/g;    # ...and leave every other space 'breakable'
3.120     scop     1479:   } else {
                   1480:     s/ /\001nbsp;/g;
                   1481:   }
                   1482:
                   1483:   $_ = htmlify($_, $allow_source_extra);
                   1484:
                   1485:   # unescape
                   1486:   y/\001/&/;
                   1487:
                   1488:   return $_;
                   1489: }
                   1490:
3.175     scop     1491:
3.120     scop     1492: # Note that this doesn't htmlquote the first argument...
                   1493: sub link($$)
                   1494: {
                   1495:   my ($name, $url) = @_;
3.271     scop     1496:   return sprintf('<a href="%s">%s</a>', htmlquote($url), $name);
3.120     scop     1497: }
                   1498:
3.175     scop     1499:
3.120     scop     1500: sub revcmp($$)
                   1501: {
                   1502:   my ($rev1, $rev2) = @_;
                   1503:
                   1504:   # make no comparison for a tag or a branch
                   1505:   return 0 if $rev1 =~ /[^\d.]/ || $rev2 =~ /[^\d.]/;
                   1506:
3.261     scop     1507:   my (@r1) = split(/\./, $rev1);
                   1508:   my (@r2) = split(/\./, $rev2);
3.120     scop     1509:   my ($a, $b);
                   1510:
3.261     scop     1511:   while (($a = shift(@r1)) && ($b = shift(@r2))) {
                   1512:     return $a <=> $b unless ($a == $b);
3.120     scop     1513:   }
                   1514:   if (@r1) { return  1; }
                   1515:   if (@r2) { return -1; }
                   1516:   return 0;
                   1517: }
                   1518:
3.175     scop     1519:
3.287     scop     1520: #
                   1521: # Signal a fatal error.
                   1522: #
3.120     scop     1523: sub fatal($$@)
                   1524: {
                   1525:   my ($errcode, $format, @args) = @_;
3.185     scop     1526:   print "Status: $errcode\r\n";
3.131     scop     1527:   html_header('Error');
3.215     scop     1528:   print '<div id="error">Error: ',
                   1529:     sprintf($format, map(htmlquote($_), @args)), "</div>\n";
3.120     scop     1530:   html_footer();
                   1531:   exit(1);
3.287     scop     1532: }
                   1533:
                   1534:
                   1535: #
3.269     scop     1536: # Sends a redirect to the given URL.
                   1537: #
                   1538: sub redirect($;$)
3.120     scop     1539: {
3.269     scop     1540:   my ($url, $permanent) = @_;
                   1541:   my ($status, $text);
                   1542:   if ($permanent) {
                   1543:     $status = '301';
                   1544:     $text   = 'Moved Permanently';
                   1545:   } else {
                   1546:     $status = '302';
                   1547:     $text   = 'Found';
                   1548:   }
                   1549:   print "Status: $status $text\r\n", "Location: $url\r\n";
                   1550:   html_header($text);
                   1551:   print "<p>This document has moved ", &link('here', $url), ".</p>\n";
3.120     scop     1552:   html_footer();
                   1553:   exit(1);
                   1554: }
                   1555:
3.175     scop     1556:
3.120     scop     1557: sub safeglob($)
                   1558: {
                   1559:   my ($filename) = @_;
                   1560:
3.175     scop     1561:   (my $dirname = $filename) =~ s|/[^/]+$||;
3.120     scop     1562:   $filename =~ s|.*/||;
                   1563:
3.175     scop     1564:   my @results;
                   1565:   my $dh = do { local (*DH); };
3.120     scop     1566:   if (opendir($dh, $dirname)) {
                   1567:     my $glob = $filename;
                   1568:     my $t;
                   1569:
3.149     scop     1570:     #   transform filename from glob to regex.  Deal with:
                   1571:     #   [, {, ?, * as glob chars
                   1572:     #   make sure to escape all other regex chars
3.120     scop     1573:     $glob =~ s/([\.\(\)\|\+])/\\$1/g;
                   1574:     $glob =~ s/\*/.*/g;
                   1575:     $glob =~ s/\?/./g;
                   1576:     $glob =~ s/{([^}]+)}/($t = $1) =~ s-,-|-g; "($t)"/eg;
3.222     scop     1577:     $glob = qr/^$glob$/;
                   1578:
3.120     scop     1579:     foreach (readdir($dh)) {
3.222     scop     1580:       if ($_ =~ $glob && $_ =~ VALID_PATH) {
3.194     scop     1581:         push(@results, catfile($dirname, $1)); # untaint
3.120     scop     1582:       }
                   1583:     }
                   1584:     closedir($dh);
                   1585:   }
                   1586:
3.175     scop     1587:   return @results;
3.120     scop     1588: }
                   1589:
3.160     scop     1590:
3.227     scop     1591: #
3.219     scop     1592: # Gets the MIME type for the given file name.
                   1593: #
3.156     scop     1594: sub getMimeType($;$)
3.120     scop     1595: {
3.156     scop     1596:   my ($fullname, $binary) = @_;
                   1597:   $binary = ($keywordsubstitution && $keywordsubstitution =~ /b/)
                   1598:     unless defined($binary);
                   1599:
                   1600:   (my $suffix = $fullname) =~ s/^.*\.([^.]*)$/$1/;
                   1601:
                   1602:   my $mimetype = $MTYPES{$suffix};
                   1603:   $mimetype  ||= $MimeTypes->mimeTypeOf($fullname) if defined($MimeTypes);
                   1604:
                   1605:   if (!$mimetype && $suffix ne '*' && -f $mime_types && -r _) {
                   1606:     my $fh = do { local (*FH); };
                   1607:     if (open($fh, $mime_types)) {
                   1608:       my $re = sprintf('^\s*(\S+\/\S+)\s.+\b%s\b', quotemeta($suffix));
3.222     scop     1609:       $re = qr/$re/;
3.156     scop     1610:       while (my $line = <$fh>) {
                   1611:         if ($line =~ $re) {
                   1612:           $mimetype = $1;
                   1613:           $MTYPES{$suffix} = $mimetype;
                   1614:           last;
                   1615:         }
3.120     scop     1616:       }
3.156     scop     1617:       close($fh);
                   1618:     } else {
                   1619:       warn("Can't open MIME types file $mime_types for reading: $!");
3.120     scop     1620:     }
                   1621:   }
                   1622:
3.156     scop     1623:   $mimetype ||= $MTYPES{'*'};
                   1624:   $mimetype ||= $binary ? 'application/octet-stream' : 'text/plain';
3.120     scop     1625:   return $mimetype;
3.1       knu      1626: }
                   1627:
3.156     scop     1628:
3.1       knu      1629: ###############################
3.24      knu      1630: # read first lines like head(1)
                   1631: ###############################
3.120     scop     1632: sub head($;$)
                   1633: {
3.175     scop     1634:   my ($fh, $linecount) = @_;
                   1635:   $linecount ||= 10;
3.24      knu      1636:
3.120     scop     1637:   my @buf;
                   1638:   if ($linecount > 0) {
3.184     scop     1639:     for (my $i = 0; !eof($fh) && $i < $linecount; $i++) {
3.120     scop     1640:       push @buf, scalar <$fh>;
                   1641:     }
                   1642:   } else {
                   1643:     @buf = <$fh>;
                   1644:   }
3.175     scop     1645:   return @buf;
                   1646: }
3.120     scop     1647:
3.24      knu      1648:
                   1649: ###############################
                   1650: # scan vim and Emacs directives
                   1651: ###############################
3.120     scop     1652: sub scan_directives(@)
                   1653: {
                   1654:   my $ts = undef;
3.24      knu      1655:
3.120     scop     1656:   for (@_) {
                   1657:     $ts = $1 if /\b(?:ts|tabstop|tab-width)[:=]\s*([1-9]\d*)\b/;
                   1658:   }
3.24      knu      1659:
3.120     scop     1660:   ('tabstop' => $ts);
3.24      knu      1661: }
                   1662:
3.175     scop     1663:
3.24      knu      1664: ###############################
3.1       knu      1665: # show Annotation
                   1666: ###############################
3.275     scop     1667: sub doAnnotate($$)
3.120     scop     1668: {
3.275     scop     1669:   my ($rev, $tag) = @_;
                   1670:   $rev = $tag || 'HEAD' if ($rev eq '.');
3.262     scop     1671:   (my $pathname = $where) =~ s|((?<=/)Attic/)?[^/]*$||;
3.261     scop     1672:   (my $filename = $where) =~ s|^.*/||;
3.126     scop     1673:
3.174     scop     1674:   # This annotate version is based on the cvs annotate-demo Perl script by
                   1675:   # Cyclic Software.  It was written by Cyclic Software,
                   1676:   # http://www.cyclic.com/, and is in the public domain.
                   1677:   # We could abandon the use of rlog, rcsdiff and co using
                   1678:   # the cvs server in a similiar way one day (..after rewrite).
                   1679:
                   1680:   local (*CVS_IN, *CVS_OUT);
3.215     scop     1681:   my $annotate_err;
3.174     scop     1682:   my ($h, $err) =
                   1683:     startproc([ $CMD{cvs}, @annotate_options, 'server' ],
3.215     scop     1684:               '<pipe', \*CVS_IN, '>pipe', \*CVS_OUT,
3.229     scop     1685:               '2>', \$annotate_err);
3.215     scop     1686:   fatal('500 Internal Error',
                   1687:         'Annotate failure (exit status %s), output: <pre>%s</pre>',
                   1688:         $? >> 8 || -1, $err)
                   1689:     unless $h;
3.120     scop     1690:
                   1691:   # OK, first send the request to the server.  A simplified example is:
                   1692:   #     Root /home/kingdon/zwork/cvsroot
                   1693:   #     Argument foo/xx
                   1694:   #     Directory foo
                   1695:   #     /home/kingdon/zwork/cvsroot/foo
                   1696:   #     Directory .
                   1697:   #     /home/kingdon/zwork/cvsroot
                   1698:   #     annotate
                   1699:   # although as you can see there are a few more details.
                   1700:
3.174     scop     1701:   print CVS_IN "Root $cvsroot\n";
                   1702:   print CVS_IN
3.120     scop     1703:     "Valid-responses ok error Valid-requests Checked-in Updated Merged Removed M E\n";
                   1704:
                   1705:   # Don't worry about sending valid-requests, the server just needs to
                   1706:   # support "annotate" and if it doesn't, there isn't anything to be done.
3.174     scop     1707:   print CVS_IN "UseUnchanged\n";
                   1708:   print CVS_IN "Argument -r\n";
                   1709:   print CVS_IN "Argument $rev\n";
                   1710:   print CVS_IN "Argument $where\n";
3.120     scop     1711:
                   1712:   # The protocol requires us to fully fake a working directory (at
                   1713:   # least to the point of including the directories down to the one
                   1714:   # containing the file in question).
3.123     scop     1715:   # So if $where is "dir/sdir/file", then dirs will be ("dir","sdir","file")
                   1716:   my $path = '';
                   1717:   foreach my $dir (split('/', $where)) {
3.120     scop     1718:
                   1719:     if ($path eq "") {
3.123     scop     1720:       # In our example, $dir is "dir".
                   1721:       $path = $dir;
3.120     scop     1722:     } else {
3.174     scop     1723:       print CVS_IN "Directory $path\n";
                   1724:       print CVS_IN "$cvsroot/$path\n";
3.120     scop     1725:
                   1726:       # In our example, $_ is "sdir" and $path becomes "dir/sdir"
                   1727:       # And the next time, "file" and "dir/sdir/file" (which then gets
                   1728:       # ignored, because we don't need to send Directory for the file).
3.123     scop     1729:       $path .= "/$dir";
3.120     scop     1730:     }
                   1731:   }
3.123     scop     1732:   undef $path;
3.120     scop     1733:
                   1734:   # And the last "Directory" before "annotate" is the top level.
3.174     scop     1735:   print CVS_IN "Directory .\n";
                   1736:   print CVS_IN "$cvsroot\n";
3.120     scop     1737:
3.174     scop     1738:   print CVS_IN "annotate\n";
3.120     scop     1739:
                   1740:   # OK, we've sent our command to the server.  Thing to do is to
3.215     scop     1741:   # close the writer side and get all the responses.
                   1742:   if (!close(CVS_IN)) {
3.229     scop     1743:     $h->finish();
3.215     scop     1744:     fatal('500 Internal Error',
                   1745:           'Annotate failure (exit status %s): <code>%s</code>, output: ' .
                   1746:           '<pre>%s</pre>', $? >> 8, $!, $annotate_err);
                   1747:   }
3.120     scop     1748:
3.133     scop     1749:   navigateHeader($scriptwhere, $pathname, $filename, $rev, 'annotate');
3.120     scop     1750:
3.275     scop     1751:   my $revtype = ($rev =~ /\./) ? 'revision' : 'tag'; # TODO: tag -> branch/tag?
3.136     scop     1752:   print '<h3 style="text-align: center">Annotation of ',
3.275     scop     1753:     htmlquote("$pathname$filename"), ", $revtype $rev</h3>\n";
3.120     scop     1754:
                   1755:   # Ready to get the responses from the server.
                   1756:   # For example:
                   1757:   #     E Annotations for foo/xx
                   1758:   #     E ***************
                   1759:   #     M 1.3          (kingdon  06-Sep-97): hello
                   1760:   #     ok
                   1761:   my ($lineNr) = 0;
                   1762:   my ($oldLrev, $oldLusr) = ("", "");
                   1763:   my ($revprint, $usrprint);
                   1764:
                   1765:   if ($annTable) {
3.179     scop     1766:     print <<EOF;
                   1767: <table style="border: none" cellspacing="0" cellpadding="0" summary="Annotation">
                   1768: EOF
3.120     scop     1769:   } else {
                   1770:     print "<pre>";
                   1771:   }
                   1772:
                   1773:   # prefetch several lines
3.174     scop     1774:   my @buf = head(*CVS_OUT);
3.120     scop     1775:
                   1776:   my %d = scan_directives(@buf);
                   1777:
3.174     scop     1778:   while (@buf || !eof(*CVS_OUT)) {
3.197     scop     1779:
3.174     scop     1780:     $_ = @buf ? shift @buf : <CVS_OUT>;
3.120     scop     1781:     my @words = split;
                   1782:
                   1783:     # Adding one is for the (single) space which follows $words[0].
                   1784:     my $rest = substr($_, length($words[0]) + 1);
                   1785:     if ($words[0] eq "E") {
                   1786:       next;
                   1787:     } elsif ($words[0] eq "M") {
                   1788:       $lineNr++;
                   1789:       (my $lrev = substr($_, 2,  13)) =~ y/ //d;
                   1790:       (my $lusr = substr($_, 16, 9))  =~ y/ //d;
                   1791:       my $line = substr($_, 36);
3.275     scop     1792:       # TODO: this does not work for branch/tag revisions.
3.120     scop     1793:       my $isCurrentRev = ($rev eq $lrev);
                   1794:
                   1795:       # we should parse the date here ..
                   1796:       if ($lrev eq $oldLrev) {
                   1797:         $revprint = sprintf('%-8s', '');
                   1798:       } else {
                   1799:         $revprint = sprintf('%-8s', $lrev);
3.266     scop     1800:         $revprint =~ s|(\S+)|&link($1, uri_escape($filename)."$query#rev$1")|e;
3.120     scop     1801:         $oldLusr = '';
                   1802:       }
                   1803:
3.268     scop     1804:       $usrprint = ($lusr eq $oldLusr) ? '' : $lusr;
3.120     scop     1805:       $oldLrev = $lrev;
                   1806:       $oldLusr = $lusr;
                   1807:
3.250     scop     1808:       print $is_textbased ? '<b>' : '<span class="current-rev">'
                   1809:         if $isCurrentRev;
3.120     scop     1810:
3.136     scop     1811:       $usrprint = sprintf('%-8s', $usrprint);
                   1812:       printf '%s%s %s %4d:', $revprint, $isCurrentRev ? '!' : ' ',
                   1813:         htmlquote($usrprint), $lineNr;
3.254     scop     1814:       print spacedHtmlText($line, $d{tabstop});
3.120     scop     1815:
3.250     scop     1816:       print $is_textbased ? '</b>' : '</span>' if $isCurrentRev;
                   1817:
3.120     scop     1818:     } elsif ($words[0] eq "ok") {
                   1819:       # We could complain about any text received after this, like the
                   1820:       # CVS command line client.  But for simplicity, we don't.
3.275     scop     1821:
3.120     scop     1822:     } elsif ($words[0] eq "error") {
                   1823:       fatal("500 Internal Error",
                   1824:             'Error occured during annotate: <b>%s</b>', $_);
                   1825:     }
                   1826:   }
3.229     scop     1827:   $h->finish();
3.120     scop     1828:
                   1829:   if ($annTable) {
                   1830:     print "</table>";
                   1831:   } else {
                   1832:     print "</pre>";
                   1833:   }
3.124     scop     1834:   html_footer();
3.1       knu      1835: }
                   1836:
                   1837: ###############################
                   1838: # make Checkout
                   1839: ###############################
3.275     scop     1840: sub doCheckout($$$)
3.120     scop     1841: {
3.275     scop     1842:   my ($fullname, $rev, $tag) = @_;
                   1843:   $rev = $tag || undef if (!$rev || $rev eq '.');
3.120     scop     1844:
3.219     scop     1845:   # Start resolving whether we will do a markup view or not.
                   1846:   my $do_markup = undef;
3.226     scop     1847:   my $want_type = $input{'content-type'};
                   1848:
3.219     scop     1849:   # No markup if markup disallowed.
                   1850:   $do_markup = 0 unless $allow_markup;
3.226     scop     1851:
3.219     scop     1852:   # No markup if checkout magic cookie in URL.
                   1853:   $do_markup = 0 if (!defined($do_markup) && $doCheckout);
3.226     scop     1854:
3.219     scop     1855:   # Do markup if explicitly asked using cvsweb-markup content type.  If the
                   1856:   # asked content type is anything else, no markup.
3.226     scop     1857:   if (!defined($do_markup) && $want_type) {
                   1858:     if ($want_type =~ CVSWEBMARKUP) {
                   1859:       $want_type = undef;
                   1860:       $do_markup = 1;
                   1861:     } else {
                   1862:       $do_markup = 0;
                   1863:     }
3.219     scop     1864:   }
                   1865:
                   1866:   # Ok, if $do_markup is still undefined, we know that a download has not been
                   1867:   # explicitly asked.  For the last check further down below we'll need to
                   1868:   # know if the file is binary, and possibly run a log on it.
                   1869:   my $needlog = $do_markup || $use_moddate;
3.120     scop     1870:
3.163     scop     1871:   my $moddate = undef;
3.175     scop     1872:   my $revopt;
3.120     scop     1873:   if (defined($rev)) {
                   1874:     $revopt = "-r$rev";
3.219     scop     1875:     if ($needlog) {
3.120     scop     1876:       readLog($fullname, $rev);
3.275     scop     1877:       $moddate   = $date{$rev};
                   1878:       # TODO: even this does not work for branch tags, but only normal tags :(
                   1879:       $moddate ||= $date{$symrev{$rev}} if defined($symrev{$rev});
3.120     scop     1880:     }
                   1881:   } else {
                   1882:     $revopt = "-rHEAD";
3.219     scop     1883:     if ($needlog) {
3.120     scop     1884:       readLog($fullname);
                   1885:       $moddate = $date{$symrev{HEAD}};
                   1886:     }
                   1887:   }
                   1888:
3.194     scop     1889:   my $cr = abs_path($cvsroot) || $cvsroot;
                   1890:   # abs_path() taints when run as a CGI...
                   1891:   if ($cr =~ VALID_PATH) {
                   1892:     $cr = $1;
                   1893:   } else {
                   1894:     fatal('500 Internal Error', 'Illegal CVS root: <code>%s</code>', $cr);
                   1895:   }
3.183     scop     1896:   # Use abs_path() to work around a bug of cvs -p; expand symlinks if we can.
3.194     scop     1897:   my @cmd = ($CMD{cvs}, @cvs_options, '-d', $cr, 'co', '-p', $revopt, $where);
3.183     scop     1898:
3.174     scop     1899:   local (*CVS_OUT, *CVS_ERR);
                   1900:   my ($h, $err) =
3.229     scop     1901:     startproc(\@cmd, \"", '>pipe', \*CVS_OUT, '2>pipe', \*CVS_ERR);
3.215     scop     1902:   fatal('500 Internal Error',
                   1903:         'Checkout failure (exit status %s), output: <pre>%s</pre>',
                   1904:         $? >> 8 || -1, $err)
                   1905:     unless $h;
3.120     scop     1906:
3.174     scop     1907:   if (eof(CVS_ERR)) {
3.229     scop     1908:     $h->finish();
3.120     scop     1909:     fatal("404 Not Found", '%s is not (any longer) pertinent', $where);
                   1910:   }
                   1911:
                   1912:   #===================================================================
                   1913:   #Checking out squid/src/ftp.c
                   1914:   #RCS:  /usr/src/CVS/squid/src/ftp.c,v
                   1915:   #VERS: 1.1.1.28.6.2
                   1916:   #***************
                   1917:
                   1918:   # Parse CVS header
                   1919:   my ($revision, $filename, $cvsheader);
                   1920:   $filename = "";
3.174     scop     1921:   while (<CVS_ERR>) {
3.120     scop     1922:     last if (/^\*\*\*\*/);
                   1923:     $revision = $1 if (/^VERS: (.*)$/);
                   1924:
                   1925:     if (/^Checking out (.*)$/) {
3.214     scop     1926:       ($filename = $1) =~ s|^\./+||;
3.120     scop     1927:     }
                   1928:     $cvsheader .= $_;
                   1929:   }
3.219     scop     1930:   close(CVS_ERR);
3.120     scop     1931:
                   1932:   if ($filename ne $where) {
3.229     scop     1933:     $h->finish();
3.120     scop     1934:     fatal("500 Internal Error",
3.214     scop     1935:           'Unexpected output from cvs co: <pre>%s</pre> ' .
                   1936:           '(expected "<code>%s</code>" but got "<code>%s</code>")',
                   1937:           $cvsheader, $where, $filename);
3.120     scop     1938:   }
                   1939:
3.219     scop     1940:   # Last checks whether we'll do markup or not.
                   1941:   my $isbin = $keywordsubstitution && $keywordsubstitution =~ /b/;
                   1942:   my $mimetype = getMimeType($fullname, $isbin);
                   1943:
                   1944:   # If we still are not sure whether to do markup or not:
                   1945:   # if the MIME type is "viewable" or this is not a binary file, do.
                   1946:   $do_markup = !$isbin || viewable($mimetype) unless defined($do_markup);
                   1947:
                   1948:   if ($do_markup) {
                   1949:
                   1950:     # If this is something we'll be linking to in the markup view, we are
                   1951:     # done with this particular output from "cvs co" and must discard it.
                   1952:     my $linked = $mimetype =~ m{^image/|application/pdf$}i;
                   1953:     if ($linked) {
                   1954:       close(CVS_OUT);
3.229     scop     1955:       $h->finish();
3.219     scop     1956:     }
                   1957:
3.163     scop     1958:     # Here we know the last modified date, but don't know if tags have been
3.219     scop     1959:     # added afterwards (those are shown in the markup view): no last-modified.
                   1960:     cvswebMarkup(\*CVS_OUT, $fullname, $revision, $isbin, $mimetype, $needlog);
                   1961:
3.229     scop     1962:     $h->finish() unless $linked;
3.219     scop     1963:
3.120     scop     1964:   } else {
3.226     scop     1965:     http_header($want_type || $mimetype, $moddate);
3.174     scop     1966:     local $/ = undef;
                   1967:     print <CVS_OUT>;
3.229     scop     1968:     $h->finish();
3.120     scop     1969:   }
1.12      fenner   1970: }
                   1971:
3.174     scop     1972:
3.219     scop     1973: sub cvswebMarkup($$$$$$;$)
3.120     scop     1974: {
3.219     scop     1975:   my ($filehandle, $fullname, $rev, $isbin, $mimetype, $logged, $mod) = @_;
3.1       knu      1976:
3.262     scop     1977:   (my $pathname = $where) =~ s|((?<=/)Attic/)?[^/]*$||;
3.261     scop     1978:   (my $filename = $where) =~ s|^.*/||;
3.120     scop     1979:
3.219     scop     1980:   navigateHeader($scriptwhere, $pathname, $filename, $rev, 'view', $mod);
3.120     scop     1981:
3.144     scop     1982:   print <<EOF;
3.208     scop     1983: <hr />
3.182     scop     1984: <div class="log-markup">
                   1985: File:&nbsp;
3.144     scop     1986: EOF
3.219     scop     1987:   print &clickablePath($where, 1), "<br />\n";
3.120     scop     1988:
                   1989:   if ($show_log_in_markup) {
3.219     scop     1990:     readLog($fullname) unless $logged; #,$rev);
                   1991:     printLog($rev, $mimetype, $isbin);
3.120     scop     1992:   } else {
3.244     scop     1993:     print "Revision: <b>$rev</b><br />\n";
                   1994:     print 'Tag: ', htmlquote($input{only_with_tag}), "<br />\n"
3.120     scop     1995:       if $input{only_with_tag};
                   1996:   }
3.208     scop     1997:   print "</div>\n<hr />";
3.271     scop     1998:   my $url = download_url(uri_escape($filename), $rev, $mimetype);
3.120     scop     1999:
3.219     scop     2000:   if ($mimetype =~ m|^image/|i) {
3.208     scop     2001:     printf '<img src="%s" alt="%s" /><br />',
3.271     scop     2002:       $url . $barequery, htmlquote($filename);
3.219     scop     2003:   } elsif (lc($mimetype) eq 'application/pdf') {
3.208     scop     2004:     printf '<embed src="%s" width="100%%" height="100%%" /><br />',
3.271     scop     2005:       $url . $barequery;
3.120     scop     2006:   } else {
3.208     scop     2007:     print "<pre>\n";
3.167     scop     2008:     my $linenumbers = $input{ln} || 0;
3.200     scop     2009:       my $ln  = 0;
                   2010:       my @buf = ();
                   2011:       my $ts  = undef;
                   2012:
                   2013:       if ($preformat_in_markup) {
                   2014:         # prefetch several lines
                   2015:         @buf = head($filehandle);
                   2016:         my %d = scan_directives(@buf);
                   2017:         $ts = $d{tabstop};
                   2018:       }
                   2019:
                   2020:       while (@buf || !eof($filehandle)) {
                   2021:         $_ = @buf ? shift @buf : <$filehandle>;
3.167     scop     2022:         if ($linenumbers) {
                   2023:           $ln++;
                   2024:           printf '<a id="l%d" class="src">%5d: </a>', ($ln) x 2;
3.149     scop     2025:         }
3.200     scop     2026:         print $preformat_in_markup ? spacedHtmlText($_, $ts) : htmlquote($_);
3.148     scop     2027:       }
3.149     scop     2028:     print "</pre>\n";
3.120     scop     2029:   }
3.131     scop     2030:   html_footer();
3.120     scop     2031: }
                   2032:
3.156     scop     2033:
3.120     scop     2034: sub viewable($)
                   2035: {
3.219     scop     2036:   return shift =~ m{^((text|image)/|application/pdf)}i;
3.156     scop     2037: }
3.120     scop     2038:
3.1       knu      2039:
                   2040: ###############################
                   2041: # Show Colored Diff
                   2042: ###############################
3.120     scop     2043: sub doDiff($$$$$$)
                   2044: {
                   2045:   my ($fullname, $r1, $tr1, $r2, $tr2, $f) = @_;
                   2046:
3.233     scop     2047:   if (forbidden($fullname)) {
                   2048:     fatal('403 Forbidden', 'Access to %s forbidden.', $where);
3.120     scop     2049:   }
                   2050:
3.175     scop     2051:   my ($rev1, $sym1);
3.120     scop     2052:   if ($r1 =~ /([^:]+)(:(.+))?/) {
                   2053:     $rev1 = $1;
                   2054:     $sym1 = $3;
                   2055:   }
                   2056:   if ($r1 eq 'text') {
                   2057:     $rev1 = $tr1;
                   2058:     $sym1 = "";
                   2059:   }
                   2060:
3.175     scop     2061:   my ($rev2, $sym2);
3.120     scop     2062:   if ($r2 =~ /([^:]+)(:(.+))?/) {
                   2063:     $rev2 = $1;
                   2064:     $sym2 = $3;
                   2065:   }
                   2066:   if ($r2 eq 'text') {
                   2067:     $rev2 = $tr2;
                   2068:     $sym2 = "";
                   2069:   }
                   2070:
                   2071:   #
                   2072:   # rev1 and rev2 are now both numeric revisions.
                   2073:   # Thus we do a DWIM here and swap them if rev1 is after rev2.
                   2074:   # XXX should we warn about the fact that we do this?
                   2075:   if (&revcmp($rev1, $rev2) > 0) {
                   2076:     my ($tmp1, $tmp2) = ($rev1, $sym1);
                   2077:     ($rev1, $sym1) = ($rev2, $sym2);
                   2078:     ($rev2, $sym2) = ($tmp1, $tmp2);
                   2079:   }
3.225     scop     2080:
                   2081:   my $mimetype = getMimeType($fullname);
                   2082:
                   2083:   $f = $DEFAULTVALUE{f} || 'u' if ($f =~ /^ext\d*$/);
3.120     scop     2084:   my $difftype = $DIFFTYPES{$f};
                   2085:   if (!$difftype) {
                   2086:     fatal("400 Bad arguments", 'Diff format %s not understood', $f);
                   2087:   }
                   2088:
3.254     scop     2089:   my @difftype       = @{$difftype->{opts}};
                   2090:   my $human_readable = $difftype->{colored};
3.120     scop     2091:
4.2       schwarze 2092:   # Apply special diff options.
4.35      schwarze 2093:   push @difftype, '-p';
3.120     scop     2094:
                   2095:   if ($human_readable) {
3.247     scop     2096:     push(@difftype, '-w')  if $hr_ignwhite;
                   2097:     push(@difftype, '-kk') if $hr_ignkeysubst;
3.120     scop     2098:   }
                   2099:
3.175     scop     2100:   my $fh = do { local (*FH); };
3.120     scop     2101:   if (!open($fh, "-|")) {    # child
                   2102:     open(STDERR, ">&STDOUT");    # Redirect stderr to stdout
3.166     scop     2103:     exec($CMD{rcsdiff}, @rcsdiff_options, @difftype, "-r$rev1", "-r$rev2",
                   2104:          $fullname) or exit -1;
3.120     scop     2105:   }
3.166     scop     2106:
3.120     scop     2107:   if ($human_readable) {
3.168     scop     2108:     #
                   2109:     # Human readable diff.
                   2110:     #
                   2111:     human_readable_diff($fh, $rev2);
                   2112:     html_footer();
                   2113:     gzipclose();
                   2114:     exit;
3.120     scop     2115:   } else {
3.168     scop     2116:     #
                   2117:     # Plain diff.
                   2118:     #
3.120     scop     2119:     http_header("text/plain");
                   2120:   }
                   2121:
                   2122:   #
                   2123:   #===================================================================
                   2124:   #RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
                   2125:   #retrieving revision 1.16
                   2126:   #retrieving revision 1.17
                   2127:   #diff -c -r1.16 -r1.17
                   2128:   #*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2129:   #--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
                   2130:   #
                   2131:   # Ideas:
                   2132:   # - nuke the stderr output if it's what we expect it to be
                   2133:   # - Add "no differences found" if the diff command supplied no output.
                   2134:   #
                   2135:   #*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                   2136:   #--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
                   2137:   # (bogus example, but...)
                   2138:   #
3.175     scop     2139:   my ($f1, $f2);
3.120     scop     2140:   if (grep { $_ eq '-u' } @difftype) {
                   2141:     $f1 = '---';
                   2142:     $f2 = '\+\+\+';
                   2143:   } else {
                   2144:     $f1 = '\*\*\*';
                   2145:     $f2 = '---';
                   2146:   }
                   2147:
                   2148:   while (<$fh>) {
                   2149:     if (m|^$f1 $cvsroot|o) {
                   2150:       s|$cvsroot/||o;
                   2151:       if ($sym1) {
                   2152:         chop;
                   2153:         $_ .= " $sym1\n";
                   2154:       }
                   2155:     } elsif (m|^$f2 $cvsroot|o) {
                   2156:       s|$cvsroot/||o;
                   2157:
                   2158:       if ($sym2) {
                   2159:         chop;
                   2160:         $_ .= " $sym2\n";
                   2161:       }
                   2162:     }
                   2163:     print $_;
                   2164:   }
                   2165:   close($fh);
1.12      fenner   2166: }
                   2167:
3.175     scop     2168:
3.1       knu      2169: ###############################
                   2170: # Show Logs ..
                   2171: ###############################
3.120     scop     2172: sub getDirLogs($$@)
                   2173: {
                   2174:   my ($cvsroot, $dirname, @otherFiles) = @_;
3.139     scop     2175:   my $tag = $input{only_with_tag};
3.196     scop     2176:   my $DirName = catdir($cvsroot, $where);
3.120     scop     2177:
3.139     scop     2178:   my @files = &safeglob("$DirName/*,v");
3.254     scop     2179:   push (@files, &safeglob("$DirName/Attic/*,v")) unless $input{hideattic};
3.120     scop     2180:   foreach my $file (@otherFiles) {
3.196     scop     2181:     push(@files, catfile($DirName, $file));
3.120     scop     2182:   }
                   2183:
3.150     scop     2184:   # Weed out unreadable files.
                   2185:   my $i = 0;
                   2186:   my @unreadable = ();
                   2187:   while ($i < scalar(@files)) {
3.197     scop     2188:     # Note: last modified files from subdirs returned by
                   2189:     # findLastModifiedSubdirs() come without the ,v suffix so they're not
                   2190:     # found here, but have already been checked for readability.  *cough*
                   2191:     if (-r $files[$i] || !-e _) {
3.150     scop     2192:       $i++;
                   2193:     } else {
                   2194:       push(@unreadable, splice(@files, $i, 1));
                   2195:     }
                   2196:   }
                   2197:
3.139     scop     2198:   # If there are no files, we're done.
3.150     scop     2199:   return @unreadable unless @files;
3.120     scop     2200:
3.139     scop     2201:   my @cmd = ($CMD{rlog});
                   2202:   # Can't use -r<tag> as '-' is allowed in tagnames,
                   2203:   # but misinterpreted by rlog.
                   2204:   push(@cmd, '-r') unless defined($tag);
3.120     scop     2205:
3.139     scop     2206:   my $fh = do { local (*FH); };
3.273     scop     2207:   if (!open($fh, '-|')) {                       # Child
                   2208:     open(STDERR, '>', devnull()) unless $DEBUG; # Ignore rlog's complaints.
3.139     scop     2209:     if ($file_list_len && $file_list_len > 1) {
                   2210:       while (scalar(@files) > $file_list_len) {  # Process files in chunks.
                   2211:         system(@cmd, splice(@files, 0, $file_list_len)) == 0 or exit -1;
                   2212:       }
3.120     scop     2213:     }
3.139     scop     2214:     exec(@cmd, @files) or exit -1;
                   2215:   }
                   2216:   undef @cmd;
3.120     scop     2217:
3.139     scop     2218:   my $state = 'start';
                   2219:   my ($date, $branchpoint, $branch, $log, @filetags);
3.152     scop     2220:   my ($rev, $revision, $revwanted, $filename, $head, $author, $keywordsubst);
3.120     scop     2221:
                   2222:   while (<$fh>) {
                   2223:     if ($state eq "start") {
                   2224:
                   2225:       #Next file. Initialize file variables
                   2226:       $rev         = '';
                   2227:       $revwanted   = '';
                   2228:       $branch      = '';
                   2229:       $branchpoint = '';
                   2230:       $filename    = '';
                   2231:       $log         = '';
                   2232:       $revision    = '';
                   2233:       %symrev      = ();
                   2234:       @filetags    = ();
3.152     scop     2235:       $keywordsubst= '';
3.120     scop     2236:
                   2237:       #jump to head state
                   2238:       $state = "head";
                   2239:     }
3.144     scop     2240:
3.120     scop     2241:     again:
                   2242:
                   2243:     if ($state eq "head") {
                   2244:
                   2245:       #$rcsfile = $1 if (/^RCS file: (.+)$/); #not used (yet)
                   2246:
                   2247:       if (/^Working file: (.+)$/) {
                   2248:         $filename = $1;
                   2249:       } elsif (/^head: (.+)$/) {
                   2250:         $head = $1;
                   2251:       } elsif (/^branch: (.+)$/) {
                   2252:         $branch = $1;
3.152     scop     2253:       } elsif (/^keyword substitution: (.+)$/) {
                   2254:         $keywordsubst = $1;
3.120     scop     2255:       } elsif (/^symbolic names:/) {
                   2256:         $state = "tags";
                   2257:         ($branch = $head) =~ s/\.\d+$//
                   2258:           if $branch eq '';
                   2259:         $branch =~ s/(\d+)$/0.$1/;
                   2260:         $symrev{MAIN}  = $branch;
                   2261:         $symrev{HEAD}  = $branch;
                   2262:         $alltags{MAIN} = 1;
                   2263:         $alltags{HEAD} = 1;
                   2264:         push (@filetags, "MAIN", "HEAD");
3.222     scop     2265:       } elsif ($_ =~ LOG_REVSEPR) {
3.120     scop     2266:         $state = "log";
                   2267:         $rev   = '';
                   2268:         $date  = '';
                   2269:         $log   = '';
                   2270:
                   2271:         # Try to reconstruct the relative filename if RCS spits out a full path
                   2272:         $filename =~ s%^\Q$DirName\E/%%;
                   2273:       }
                   2274:       next;
                   2275:     }
                   2276:
                   2277:     if ($state eq "tags") {
                   2278:       if (/^\s+([^:]+):\s+([\d\.]+)\s*$/) {
                   2279:         push (@filetags, $1);
                   2280:         $symrev{$1}  = $2;
                   2281:         $alltags{$1} = 1;
                   2282:         next;
                   2283:       } elsif (/^\S/) {
                   2284:
                   2285:         if (defined($tag)) {
                   2286:           if (defined($symrev{$tag}) || $tag eq "HEAD") {
                   2287:             $revwanted    = $symrev{$tag eq "HEAD" ? "MAIN" : $tag};
                   2288:             ($branch      = $revwanted) =~ s/\b0\.//;
                   2289:             ($branchpoint = $branch)    =~ s/\.?\d+$//;
                   2290:             $revwanted    = '' if ($revwanted ne $branch);
                   2291:           } elsif ($tag ne "HEAD") {
                   2292:             $state = "skip";
                   2293:             next;
                   2294:           }
                   2295:         }
                   2296:
                   2297:         foreach my $tagfound (@filetags) {
                   2298:           $tags{$tagfound} = 1;
                   2299:         }
                   2300:         $state = "head";
                   2301:         goto again;
                   2302:       }
                   2303:     }
                   2304:
                   2305:     if ($state eq "log") {
3.222     scop     2306:       if ($_ =~ LOG_REVSEPR || $_ =~ LOG_FILESEPR) {
3.120     scop     2307:
                   2308:         # End of a log entry.
                   2309:         my $revbranch = $rev;
                   2310:         $revbranch =~ s/\.\d+$//;
                   2311:
                   2312:         if ($revwanted eq '' && $branch ne '' && $branch eq $revbranch
                   2313:             || !defined($tag))
                   2314:         {
                   2315:           $revwanted = $rev;
                   2316:         }
                   2317:
                   2318:         if ($revwanted ne ''
                   2319:             ? $rev eq $revwanted
                   2320:             : $branchpoint ne ''
                   2321:               ? $rev eq $branchpoint
                   2322:               : 0
                   2323:             && ($rev eq $head))
                   2324:         {    # Don't think head is needed here..
3.152     scop     2325:           my @finfo = ($rev, $date, $log, $author, $filename, $keywordsubst);
                   2326:           (my $name = $filename) =~ s%/.*%%;
3.120     scop     2327:           $fileinfo{$name} = [@finfo];
                   2328:           $state = "done" if ($rev eq $revwanted);
                   2329:         }
                   2330:         $rev  = '';
                   2331:         $date = '';
                   2332:         $log  = '';
                   2333:       } elsif ($date eq ''
                   2334:                && m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);|)
                   2335:       {
3.172     scop     2336:         my $yr    = $1;
                   2337:         $yr      -= 1900 if ($yr > 100); # Damn 2-digit year routines :-)
                   2338:         $date     = timegm($6, $5, $4, $3, $2 - 1, $yr);
3.120     scop     2339:         ($author) = /author: ([^;]+)/;
3.172     scop     2340:         $state    = 'log';
                   2341:         $log      = '';
3.120     scop     2342:         next;
                   2343:       } elsif ($rev eq '' && /^revision (\d+(?:\.\d+)+).*$/) {
                   2344:         $rev = $1;    # .*$ eats up the locker(lockers?) info, if any
                   2345:         next;
                   2346:       } else {
                   2347:         $log .= $_;
                   2348:       }
                   2349:     }
                   2350:
3.222     scop     2351:     if ($_ =~ LOG_FILESEPR) {
3.120     scop     2352:       $state = "start";
                   2353:       next;
                   2354:     }
                   2355:   }
                   2356:
3.139     scop     2357:   my $linesread = $. || 0;
                   2358:   close($fh);
                   2359:
                   2360:   if ($linesread == 0) {
3.137     scop     2361:     fatal('500 Internal Error',
4.29      schwarze 2362:           'Failed to spawn rlog on <em>"%s"</em>',
                   2363:           htmlquote(join(', ', @files)));
3.120     scop     2364:   }
3.150     scop     2365:   return @unreadable;
3.120     scop     2366: }
                   2367:
3.175     scop     2368:
3.120     scop     2369: sub readLog($;$)
                   2370: {
                   2371:   my ($fullname, $revision) = @_;
                   2372:   my ($symnames, $head, $rev, $br, $brp, $branch, $branchrev);
                   2373:
                   2374:   undef %symrev;
                   2375:   undef %revsym;
                   2376:   undef @allrevisions;
                   2377:   undef %date;
                   2378:   undef %author;
                   2379:   undef %state;
                   2380:   undef %difflines;
                   2381:   undef %log;
3.152     scop     2382:   $keywordsubstitution = '';
3.120     scop     2383:
3.175     scop     2384:   my $fh = do { local (*FH); };
3.120     scop     2385:   if (!open($fh, "-|")) {    # child
3.275     scop     2386:     $revision = defined($revision) ? "-r$revision" : '';
                   2387:     if ($revision =~ /\./) {
                   2388:       # Normal revision, not a branch/tag name.
3.120     scop     2389:       exec($CMD{rlog}, $revision, $fullname) or exit -1;
                   2390:     } else {
                   2391:       exec($CMD{rlog}, $fullname) or exit -1;
                   2392:     }
                   2393:   }
                   2394:
3.217     scop     2395:   my $curbranch = undef;
3.120     scop     2396:   while (<$fh>) {
                   2397:     if ($symnames) {
                   2398:       if (/^\s+([^:]+):\s+([\d\.]+)/) {
                   2399:         $symrev{$1} = $2;
3.152     scop     2400:         next;
3.120     scop     2401:       } else {
                   2402:         $symnames = 0;
                   2403:       }
3.152     scop     2404:     }
                   2405:     if (/^head:\s+([\d\.]+)/) {
3.120     scop     2406:       $head = $1;
                   2407:     } elsif (/^branch:\s+([\d\.]+)/) {
                   2408:       $curbranch = $1;
                   2409:     } elsif (/^symbolic names/) {
                   2410:       $symnames = 1;
3.152     scop     2411:     } elsif (/^keyword substitution: (.+)$/) {
                   2412:       $keywordsubstitution = $1;
3.120     scop     2413:     } elsif (/^-----/) {
                   2414:       last;
                   2415:     }
                   2416:   }
                   2417:   ($curbranch = $head) =~ s/\.\d+$// if (!defined($curbranch));
                   2418:
                   2419:   # each log entry is of the form:
                   2420:   # ----------------------------
                   2421:   # revision 3.7.1.1
                   2422:   # date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
                   2423:   # log info
                   2424:   # ----------------------------
                   2425:
                   2426:   # For a locked revision, the first line after the separator
                   2427:   # becomes smth like
3.149     scop     2428:   # revision 9.19       locked by: vassilii;
3.120     scop     2429:
                   2430:   logentry:
                   2431:
3.222     scop     2432:   while ($_ !~ LOG_FILESEPR) {
3.120     scop     2433:     $_ = <$fh>;
                   2434:     last logentry if (!defined($_));    # EOF
                   2435:     if (/^revision (\d+(?:\.\d+)+)/) {
                   2436:       $rev = $1;
3.261     scop     2437:       unshift(@allrevisions, $rev);
3.222     scop     2438:     } elsif ($_ =~ LOG_FILESEPR || $_ =~ LOG_REVSEPR) {
3.120     scop     2439:       next logentry;
                   2440:     } else {
                   2441:
                   2442:       # The rlog output is syntactically ambiguous.  We must
                   2443:       # have guessed wrong about where the end of the last log
                   2444:       # message was.
                   2445:       # Since this is likely to happen when people put rlog output
                   2446:       # in their commit messages, don't even bother keeping
                   2447:       # these lines since we don't know what revision they go with
                   2448:       # any more.
                   2449:       next logentry;
                   2450:     }
                   2451:     $_ = <$fh>;
                   2452:     if (
                   2453:       m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);\s+state:\s+(\S+);\s+(lines:\s+([0-9\s+-]+))?|
                   2454:       )
                   2455:     {
3.172     scop     2456:       my $yr           = $1;
                   2457:       $yr             -= 1900 if ($yr > 100); # Damn 2-digit year routines :-)
                   2458:       $date{$rev}      = timegm($6, $5, $4, $3, $2 - 1, $yr);
3.120     scop     2459:       $author{$rev}    = $7;
                   2460:       $state{$rev}     = $8;
                   2461:       $difflines{$rev} = $10;
                   2462:     } else {
                   2463:       fatal("500 Internal Error", 'Error parsing RCS output: %s', $_);
                   2464:     }
                   2465:
                   2466:   line:
                   2467:     while (<$fh>) {
                   2468:       next line if (/^branches:\s/);
3.222     scop     2469:       last line if ($_ =~ LOG_FILESEPR || $_ =~ LOG_REVSEPR);
3.120     scop     2470:       $log{$rev} .= $_;
                   2471:     }
                   2472:   }
                   2473:   close($fh);
                   2474:
                   2475:   @revorder = reverse sort { revcmp($a, $b) } @allrevisions;
                   2476:
                   2477:   #
                   2478:   # HEAD is an artificial tag which is simply the highest tag number on the main
                   2479:   # branch, unless there is a branch tag in the RCS file in which case it's the
                   2480:   # highest revision on that branch.  Find it by looking through @revorder; it
                   2481:   # is the first commit listed on the appropriate branch.
                   2482:   # This is not neccesary the same revision as marked as head in the RCS file.
                   2483:   my $headrev = $curbranch || "1";
3.254     scop     2484:   ($symrev{MAIN} = $headrev) =~ s/(\d+)$/0.$1/;
3.120     scop     2485:
                   2486:   foreach $rev (@revorder) {
                   2487:     if ($rev =~ /^(\S*)\.\d+$/ && $headrev eq $1) {
3.254     scop     2488:       $symrev{HEAD} = $rev;
3.120     scop     2489:       last;
                   2490:     }
                   2491:   }
3.254     scop     2492:   ($symrev{HEAD} = $headrev) =~ s/\.\d+$// unless defined($symrev{HEAD});
3.120     scop     2493:
                   2494:   #
                   2495:   # Now that we know all of the revision numbers, we can associate
                   2496:   # absolute revision numbers with all of the symbolic names, and
                   2497:   # pass them to the form so that the same association doesn't have
                   2498:   # to be built then.
                   2499:   #
                   2500:   undef @branchnames;
                   2501:   undef %branchpoint;
                   2502:   undef $sel;
                   2503:
                   2504:   foreach (reverse sort keys %symrev) {
                   2505:     $rev = $symrev{$_};
                   2506:     if ($rev =~ /^((.*)\.)?\b0\.(\d+)$/) {
                   2507:       push (@branchnames, $_);
                   2508:
                   2509:       #
                   2510:       # A revision number of A.B.0.D really translates into
                   2511:       # "the highest current revision on branch A.B.D".
                   2512:       #
                   2513:       # If there is no branch A.B.D, then it translates into
                   2514:       # the head A.B .
                   2515:       #
                   2516:       # This reasoning also applies to the main branch A.B,
                   2517:       # with the branch number 0.A, with the exception that
                   2518:       # it has no head to translate to if there is nothing on
                   2519:       # the branch, but I guess this can never happen?
                   2520:       #
                   2521:       # (the code below gracefully forgets about the branch
                   2522:       # if it should happen)
                   2523:       #
                   2524:       $head = defined($2) ? $2 : "";
                   2525:       $branch = $3;
                   2526:       $branchrev = $head . ($head ne "" ? "." : "") . $branch;
3.222     scop     2527:       $rev = $head;
                   2528:
                   2529:       my $regex = '^' . quotemeta($branchrev) . '\b';
                   2530:       $regex = qr/$regex/;
3.120     scop     2531:
                   2532:       foreach my $r (@revorder) {
3.222     scop     2533:         if ($r =~ $regex) {
3.120     scop     2534:           $rev = $branchrev;
                   2535:           last;
                   2536:         }
                   2537:       }
                   2538:       next if ($rev eq "");
                   2539:
                   2540:       if ($rev ne $head && $head ne "") {
3.131     scop     2541:         $branchpoint{$head} .= ', ' if ($branchpoint{$head});
3.120     scop     2542:         $branchpoint{$head} .= $_;
                   2543:       }
                   2544:     }
                   2545:     $revsym{$rev} .= ", " if ($revsym{$rev});
                   2546:     $revsym{$rev} .= $_;
3.136     scop     2547:     $sel .= sprintf("<option value=\"%s:%s\">%s</option>\n",
                   2548:                     htmlquote($rev), (htmlquote($_)) x 2);
3.120     scop     2549:   }
                   2550:
                   2551:   my ($onlyonbranch, $onlybranchpoint);
3.254     scop     2552:   if ($onlyonbranch = $input{only_with_tag}) {
3.120     scop     2553:     $onlyonbranch = $symrev{$onlyonbranch};
3.275     scop     2554:     if ($onlyonbranch && $onlyonbranch =~ s/\b0\.//) {
3.120     scop     2555:       ($onlybranchpoint = $onlyonbranch) =~ s/\.\d+$//;
                   2556:     } else {
                   2557:       $onlybranchpoint = $onlyonbranch;
                   2558:     }
                   2559:
                   2560:     if (!defined($onlyonbranch) || $onlybranchpoint eq "") {
3.275     scop     2561:       fatal("404 Tag not found", 'Tag "<code>%s</code>" is not defined.',
3.254     scop     2562:             $input{only_with_tag});
3.120     scop     2563:     }
                   2564:   }
                   2565:
                   2566:   undef @revisions;
                   2567:
                   2568:   foreach (@allrevisions) {
                   2569:     ($br  = $_)  =~ s/\.\d+$//;
                   2570:     ($brp = $br) =~ s/\.\d+$//;
                   2571:     next if ($onlyonbranch
                   2572:              && $br ne $onlyonbranch
                   2573:              && $_  ne $onlybranchpoint);
3.261     scop     2574:     unshift(@revisions, $_);
3.120     scop     2575:   }
                   2576:
                   2577:   if ($logsort eq "date") {
                   2578:
                   2579:     # Sort the revisions in commit order an secondary sort on revision
                   2580:     # (secondary sort needed for imported sources, or the first main
                   2581:     # revision gets before the same revision on the 1.1.1 branch)
                   2582:     @revdisplayorder =
                   2583:       sort { $date{$b} <=> $date{$a} || -revcmp($a, $b) } @revisions;
                   2584:   } elsif ($logsort eq "rev") {
                   2585:
                   2586:     # Sort the revisions in revision order, highest first
                   2587:     @revdisplayorder = reverse sort { revcmp($a, $b) } @revisions;
                   2588:   } else {
                   2589:
                   2590:     # No sorting. Present in the same order as rlog / cvs log
                   2591:     @revdisplayorder = @revisions;
                   2592:   }
                   2593:
3.217     scop     2594:   return $curbranch;
3.120     scop     2595: }
                   2596:
3.175     scop     2597:
3.225     scop     2598: sub getDiffLinks($$$)
3.120     scop     2599: {
3.225     scop     2600:   my ($url, $mimetype, $isbin) = @_;
                   2601:
                   2602:   my @links = ();
                   2603:   if (!$isbin) { # Offer ordinary diff only for non-binary files.
                   2604:     push(@links, &link('preferred', $url));
                   2605:     for my $difftype ($DIFFTYPES{$defaultDiffType}{colored} ? qw(u) : qw(h)) {
                   2606:       my $f = $difftype eq $defaultDiffType ? '' : $difftype;
                   2607:       push(@links,
                   2608:            &link(htmlquote(lc($DIFFTYPES{$difftype}{descr})), "$url;f=$f"));
                   2609:     }
                   2610:   }
                   2611:   return @links;
3.120     scop     2612: }
                   2613:
3.175     scop     2614:
3.219     scop     2615: sub printLog($$$;$$)
3.120     scop     2616: {
3.219     scop     2617:   # inlogview: 1 if in log view, otherwise in markup view.
                   2618:   ($_, my $mimetype, my $isbin, my $inlogview, my $isSelected) = @_;
3.175     scop     2619:   (my $br  = $_)  =~ s/\.\d+$//;
                   2620:   (my $brp = $br) =~ s/\.?\d+$//;
3.120     scop     2621:
3.219     scop     2622:   print "<a name=\"rev$_\"></a>";
                   2623:   if (defined($revsym{$_})) {
3.261     scop     2624:     foreach my $sym (split(", ", $revsym{$_})) {
3.271     scop     2625:       print '<a name="', htmlquote($sym), '"></a>';
3.120     scop     2626:     }
3.219     scop     2627:   }
                   2628:   if ($revsym{$br} && !defined($nameprinted{$br})) {
3.261     scop     2629:     foreach my $sym (split(", ", $revsym{$br})) {
3.271     scop     2630:       print '<a name="', htmlquote($sym), '"></a>';
3.120     scop     2631:     }
3.219     scop     2632:     $nameprinted{$br} = 1;
                   2633:   }
3.120     scop     2634:
3.271     scop     2635:   print "\n Revision <b>$_</b>";
3.219     scop     2636:   if (/^1\.1\.1\.\d+$/) {
                   2637:     print " <i>(vendor branch)</i>";
                   2638:   }
3.261     scop     2639:
3.271     scop     2640:   (my $filename = $where) =~ s|^.*/||;
                   2641:   my $fileurl   = uri_escape($filename);
                   2642:   undef $filename;
                   2643:
3.261     scop     2644:   my $isDead = ($state{$_} eq 'dead');
3.219     scop     2645:   if (!$isDead) {
3.261     scop     2646:
3.220     scop     2647:     print ': ', download_link($fileurl, $_, 'download', $mimetype);
3.219     scop     2648:
                   2649:     my @vlinks = ();
                   2650:     push(@vlinks, display_link($fileurl, $_, 'text', 'text/plain'))
                   2651:       unless $isbin;
                   2652:     push(@vlinks, display_link($fileurl, $_, 'markup', 'text/x-cvsweb-markup'))
                   2653:       if ($allow_markup && $inlogview && (!$isbin || viewable($mimetype)));
3.268     scop     2654:     if (!$isbin && $allow_annotate) {
3.219     scop     2655:       push(@vlinks,
                   2656:            &link('annotated',
3.271     scop     2657:                  sprintf('%s?annotate=%s%s', $fileurl, $_, $barequery)));
3.219     scop     2658:     }
                   2659:     print ' - view: ', join(', ', @vlinks) if @vlinks;
                   2660:     undef @vlinks;
                   2661:
4.39      schwarze 2662:     unless ($isbin) {
3.243     scop     2663:       print ' - ';
                   2664:       if ($isSelected) {
                   2665:         print '<b>[selected&nbsp;for&nbsp;diffs]</b>';
                   2666:       } else {
                   2667:         print &link('select&nbsp;for&nbsp;diffs',
                   2668:                     sprintf('%s?r1=%s%s#rev%s',
3.271     scop     2669:                             $fileurl, $_, $barequery, $_));
3.120     scop     2670:       }
                   2671:     }
                   2672:   }
3.219     scop     2673:   print "<br />\n";
3.120     scop     2674:
3.219     scop     2675:   print '<i>';
4.24      schwarze 2676:   print scalar gmtime($date{$_}), ' UTC</i> (';
3.136     scop     2677:   print readableTime(time() - $date{$_}, 1), ' ago)';
3.208     scop     2678:   print ' by <i>', htmlquote($author{$_}), "</i><br />\n";
3.136     scop     2679:
3.243     scop     2680:   printf("Branches: %s<br />\n", link_tags($revsym{$br})) if $revsym{$br};
                   2681:   printf("CVS tags: %s<br />\n", link_tags($revsym{$_}))  if $revsym{$_};
                   2682:   printf("Branch point for: %s<br />\n", link_tags($branchpoint{$_}))
                   2683:     if $branchpoint{$_};
3.120     scop     2684:
                   2685:   # Find the previous revision
3.175     scop     2686:   my $prev;
3.261     scop     2687:   my @prevrev = split(/\./, $_);
3.120     scop     2688:   do {
                   2689:     if (--$prevrev[$#prevrev] <= 0) {
                   2690:
                   2691:       # If it was X.Y.Z.1, just make it X.Y
                   2692:       pop (@prevrev);
                   2693:       pop (@prevrev);
                   2694:     }
                   2695:     $prev = join (".", @prevrev);
                   2696:   } until (defined($date{$prev}) || $prev eq "");
                   2697:
                   2698:   if ($isDead) {
3.219     scop     2699:     print "<b><i>FILE REMOVED</i></b><br />\n";
3.243     scop     2700:   } else {
3.120     scop     2701:     my %diffrev = ();
                   2702:     $diffrev{$_} = 1;
                   2703:     $diffrev{""} = 1;
3.293     scop     2704:     my $diff = 'Diff to:';
3.219     scop     2705:     my $printed = 0;
3.120     scop     2706:
                   2707:     #
                   2708:     # Offer diff to previous revision
                   2709:     if ($prev) {
                   2710:       $diffrev{$prev} = 1;
                   2711:       my $url =
3.271     scop     2712:         sprintf('%s.diff?r1=%s;r2=%s%s', $fileurl, $prev, $_, $barequery);
3.225     scop     2713:       if (my @dlinks = getDiffLinks($url, $mimetype, $isbin)) {
3.293     scop     2714:         print $diff, ' previous ', $prev, ': ', join(', ', @dlinks);
                   2715:         $diff = ';'; $printed = 1;
3.225     scop     2716:       }
3.120     scop     2717:     }
                   2718:
                   2719:     #
                   2720:     # Plus, if it's on a branch, and it's not a vendor branch,
                   2721:     # offer a diff with the branch point.
                   2722:     if ($revsym{$brp}
                   2723:       && !/^1\.1\.1\.\d+$/
                   2724:       && !defined($diffrev{$brp}))
                   2725:     {
                   2726:       my $url =
3.271     scop     2727:         sprintf('%s.diff?r1=%s;r2=%s%s', $fileurl, $brp, $_, $barequery);
3.225     scop     2728:       if (my @dlinks = getDiffLinks($url, $mimetype, $isbin)) {
3.293     scop     2729:         print $diff, ' branchpoint ', $brp, ': ', join(', ', @dlinks);
                   2730:         $diff = ';'; $printed = 1;
3.225     scop     2731:       }
3.120     scop     2732:     }
                   2733:
                   2734:     #
                   2735:     # Plus, if it's on a branch, and it's not a vendor branch,
                   2736:     # offer to diff with the next revision of the higher branch.
                   2737:     # (e.g. change gets committed and then brought
                   2738:     # over to -stable)
                   2739:     if (/^\d+\.\d+\.\d+/ && !/^1\.1\.1\.\d+$/) {
                   2740:       my ($i, $nextmain);
                   2741:
                   2742:       for ($i = 0; $i < $#revorder && $revorder[$i] ne $_; $i++) {
                   2743:       }
3.261     scop     2744:       my @tmp2 = split(/\./, $_);
3.120     scop     2745:       for ($nextmain = ""; $i > 0; $i--) {
                   2746:         my $next = $revorder[$i - 1];
3.261     scop     2747:         my @tmp1 = split(/\./, $next);
3.120     scop     2748:
                   2749:         if (@tmp1 < @tmp2) {
                   2750:           $nextmain = $next;
                   2751:           last;
                   2752:         }
                   2753:
                   2754:         # Only the highest version on a branch should have
3.293     scop     2755:         # a diff for the "next MAIN".
3.120     scop     2756:         last
                   2757:           if (@tmp1 - 1 <= @tmp2
                   2758:           && join (".", @tmp1[0 .. $#tmp1 - 1]) eq
                   2759:           join (".", @tmp2[0 .. $#tmp1 - 1]));
                   2760:       }
                   2761:
                   2762:       if (!defined($diffrev{$nextmain})) {
                   2763:         $diffrev{$nextmain} = 1;
3.203     scop     2764:         my $url = sprintf('%s.diff?r1=%s;r2=%s%s',
3.271     scop     2765:           $fileurl, $nextmain, $_, $barequery);
3.225     scop     2766:         if (my @dlinks = getDiffLinks($url, $mimetype, $isbin)) {
3.293     scop     2767:           print $diff, ' next MAIN ', $nextmain, ': ', join(', ', @dlinks);
                   2768:           $diff = ';'; $printed = 1;
3.225     scop     2769:         }
3.120     scop     2770:       }
                   2771:     }
                   2772:
                   2773:     # Plus if user has selected only r1, then present a link
                   2774:     # to make a diff to that revision
3.254     scop     2775:     if (defined($input{r1}) && !defined($diffrev{$input{r1}})) {
                   2776:       $diffrev{$input{r1}} = 1;
3.203     scop     2777:       my $url = sprintf('%s.diff?r1=%s;r2=%s%s',
3.271     scop     2778:         $fileurl, $input{r1}, $_, $barequery);
3.225     scop     2779:       if (my @dlinks = getDiffLinks($url, $mimetype, $isbin)) {
3.293     scop     2780:         print $diff, ' selected ', $input{r1}, ': ', join(', ', @dlinks);
                   2781:         $diff = ';'; $printed = 1;
3.225     scop     2782:       }
3.120     scop     2783:     }
                   2784:
3.219     scop     2785:     print "<br />\n" if $printed;
                   2786:   }
                   2787:
                   2788:   if ($prev ne "" && $difflines{$_}) {
                   2789:     printf "Changes since revision %s: %s lines<br />\n",
                   2790:       htmlquote($prev), htmlquote($difflines{$_});
3.120     scop     2791:   }
3.189     scop     2792:
3.219     scop     2793:   print "<pre class=\"log\">\n";
3.120     scop     2794:   print &htmlify($log{$_}, $allow_log_extra);
                   2795:   print "</pre>\n";
                   2796: }
                   2797:
3.175     scop     2798:
3.120     scop     2799: sub doLog($)
                   2800: {
                   2801:   my ($fullname) = @_;
                   2802:
3.217     scop     2803:   my $curbranch = readLog($fullname);
3.120     scop     2804:
                   2805:   html_header("CVS log for $where");
3.144     scop     2806:
3.271     scop     2807:   my $upwhere = $where;
3.144     scop     2808:   (my $filename = $where) =~ s|^.*/||;
3.271     scop     2809:   my $backurl = "./$query#" . uri_escape($filename);
                   2810:   if ($where =~ m|^(.*?)((?<=/)Attic/)?[^/]+$|) {
                   2811:     $upwhere = $1;
                   2812:     $backurl = ".$backurl" if $2; # skip over Attic
                   2813:   }
3.144     scop     2814:
3.219     scop     2815:   my $isbin = $keywordsubstitution =~ /b/;
                   2816:   my $mimetype = getMimeType($filename, $isbin);
                   2817:
3.120     scop     2818:   print "<p>\n ";
3.271     scop     2819:   print &link($backicon, $backurl), " <b>Up to ",
3.120     scop     2820:     &clickablePath($upwhere, 1), "</b>\n</p>\n";
                   2821:   print "<p>\n ";
                   2822:   print &link('Request diff between arbitrary revisions', '#diff');
3.208     scop     2823:   print "\n</p>\n<hr />\n";
3.120     scop     2824:
                   2825:   print "<p>\n";
3.152     scop     2826:
3.219     scop     2827:   my $explain = $isbin ? ' (i.e.: CVS considers this a binary file)' : '';
3.208     scop     2828:   print "Keyword substitution: $keywordsubstitution$explain<br />\n";
3.217     scop     2829:   if ($curbranch) {
                   2830:     print "Default branch: ", ($revsym{$curbranch} || $curbranch);
                   2831:   } else {
                   2832:     print "No default branch";
                   2833:   }
3.208     scop     2834:   print "<br />\n";
3.120     scop     2835:
3.208     scop     2836:   print 'Current tag: ', htmlquote($input{only_with_tag}), "<br />\n"
3.153     scop     2837:     if $input{only_with_tag};
3.120     scop     2838:   print "</p>\n";
                   2839:
                   2840:   undef %nameprinted;
                   2841:
3.182     scop     2842:   for my $r (@revdisplayorder) {
3.208     scop     2843:     print "<hr />\n";
3.182     scop     2844:     my $sel = (defined($input{r1}) && $input{r1} eq $r);
                   2845:     print "<div class=\"diff-selected\">\n" if $sel;
3.219     scop     2846:     printLog($r, $mimetype, $isbin, 1, $sel);
3.182     scop     2847:     print "</div>\n" if $sel;
3.120     scop     2848:   }
                   2849:
3.144     scop     2850:   printf(<<EOF, $scriptwhere);
3.208     scop     2851: <hr />
                   2852: <form method="get" action="%s.diff" id="diff_select">
3.189     scop     2853: <fieldset>
                   2854: <legend>Diff request</legend>
3.144     scop     2855: <p>
                   2856:  <a name="diff">
3.189     scop     2857:   This form allows you to request diffs between any two revisions of a file.
3.144     scop     2858:   You may select a symbolic revision name using the selection box or you may
                   2859:   type in a numeric name using the type-in text box.
                   2860:  </a>
                   2861: </p>
                   2862: EOF
3.120     scop     2863:
                   2864:   foreach (@stickyvars) {
3.208     scop     2865:     printf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
3.154     scop     2866:            $_, htmlquote($input{$_}))
3.194     scop     2867:       if (defined($input{$_}) &&
                   2868:           (!defined($DEFAULTVALUE{$_}) || $input{$_} ne $DEFAULTVALUE{$_}));
3.120     scop     2869:   }
3.144     scop     2870:
                   2871:   print <<EOF;
3.189     scop     2872: <table summary="Diff between arbitrary revisions">
3.144     scop     2873: <tr>
3.189     scop     2874: <td class="opt-label">
                   2875: <label for="r1" accesskey="1">Diffs between</label>
                   2876: </td>
                   2877: <td class="opt-value">
3.144     scop     2878: <select id="r1" name="r1">
3.208     scop     2879: <option value="text" selected="selected">Use Text Field</option>
3.144     scop     2880: EOF
                   2881:   print $sel, "</select>\n";
                   2882:
                   2883:   my $diffrev = defined($input{r1}) ?
                   2884:     $input{r1} : $revdisplayorder[$#revdisplayorder];
                   2885:
4.36      schwarze 2886:   printf(<<EOF, $diffrev);
                   2887: <input type="text" size="12" name="tr1" value="%s" onchange="this.form.r1.selectedIndex=0" />
3.144     scop     2888: </td>
3.189     scop     2889: <td></td>
                   2890: </tr>
                   2891: <tr>
                   2892: <td class="opt-label">
                   2893: <label for="r2" accesskey="2">and</label>
                   2894: </td>
                   2895: <td class="opt-value">
3.144     scop     2896: <select id="r2" name="r2">
3.208     scop     2897: <option value="text" selected="selected">Use Text Field</option>
3.144     scop     2898: EOF
                   2899:   print $sel, "</select>\n";
                   2900:
                   2901:   $diffrev = defined($input{r2}) ? $input{r2} : $revdisplayorder[0];
                   2902:
4.36      schwarze 2903:   printf(<<EOF, $diffrev, $scriptwhere);
                   2904: <input type="text" size="12" name="tr2" value="%s" onchange="this.form.r2.selectedIndex=0" />
3.144     scop     2905: </td>
3.208     scop     2906: <td><input type="submit" value="Get Diffs" accesskey="G" /></td>
3.144     scop     2907: </tr>
                   2908: </table>
3.189     scop     2909: </fieldset>
3.144     scop     2910: </form>
                   2911: <form method="get" action="%s">
3.189     scop     2912: <fieldset>
                   2913: <legend>Log view options</legend>
                   2914: <table summary="Log view options">
3.144     scop     2915: <tr>
3.189     scop     2916: <td class="opt-label">
                   2917: <label for="f" accesskey="D">Preferred diff type:</label>
                   2918: </td>
                   2919: <td class="opt-value">
3.144     scop     2920: EOF
4.17      schwarze 2921:   printDiffSelect();
3.144     scop     2922:   print <<EOF;
3.189     scop     2923: </td>
                   2924: <td></td>
3.144     scop     2925: </tr>
                   2926: EOF
3.120     scop     2927:
                   2928:   if (@branchnames) {
3.144     scop     2929:
4.17      schwarze 2930:     print <<EOF;
3.144     scop     2931: <tr>
3.189     scop     2932: <td class="opt-label">
3.188     scop     2933: <label for="only_with_tag" accesskey="B">View only branch:</label>
3.189     scop     2934: </td>
                   2935: <td class="opt-value">
                   2936: <a name="branch">
4.17      schwarze 2937: <select id="only_with_tag" name="only_with_tag">
3.144     scop     2938: EOF
3.120     scop     2939:
3.193     scop     2940:     my @tmp = ();
                   2941:     my $selfound = 0;
3.120     scop     2942:     foreach (reverse sort @branchnames) {
3.136     scop     2943:       my $selected =
3.254     scop     2944:         (defined($input{only_with_tag}) && $input{only_with_tag} eq $_);
3.193     scop     2945:       $selfound ||= $selected;
                   2946:       push(@tmp, sprintf('<option%s>%s</option>',
3.208     scop     2947:                          $selected ? ' selected="selected"' : '',
3.193     scop     2948:                          htmlquote($_)));
3.120     scop     2949:     }
3.193     scop     2950:     printf("<option value=\"\"%s>Show all branches</option>\n",
3.208     scop     2951:            $selfound ? '' : ' selected="selected"');
3.193     scop     2952:     print join("\n", @tmp);
                   2953:
3.144     scop     2954:     print <<EOF
                   2955: </select>
3.189     scop     2956: </a>
3.144     scop     2957: </td>
                   2958: <td></td>
                   2959: </tr>
                   2960: EOF
3.120     scop     2961:   }
                   2962:
3.144     scop     2963:   print <<EOF;
3.189     scop     2964: <tr>
                   2965: <td class="opt-label">
                   2966: <label for="logsort" accesskey="L">Sort log by:</label>
                   2967: </td>
                   2968: <td>
3.144     scop     2969: EOF
4.17      schwarze 2970:   printLogSortSelect();
3.144     scop     2971:   print <<EOF;
3.189     scop     2972: </td>
3.208     scop     2973: <td><input type="submit" value="Set" accesskey="S" /></td>
3.189     scop     2974: </tr>
3.144     scop     2975: </table>
                   2976: EOF
3.154     scop     2977:   foreach (@stickyvars) {
                   2978:     next if ($_ eq "f");
                   2979:     next if ($_ eq "only_with_tag");
                   2980:     next if ($_ eq "logsort");
3.296     scop     2981:     printf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
                   2982:            $_, htmlquote($input{$_}))
3.154     scop     2983:       if (defined($input{$_})
3.194     scop     2984:           && (!defined($DEFAULTVALUE{$_}) || $input{$_} ne $DEFAULTVALUE{$_}));
3.154     scop     2985:   }
3.189     scop     2986:   print "</fieldset>\n</form>\n";
3.120     scop     2987:   html_footer();
                   2988: }
                   2989:
3.175     scop     2990:
3.120     scop     2991: sub flush_diff_rows($$$$)
                   2992: {
                   2993:   my ($leftColRef, $rightColRef, $leftRow, $rightRow) = @_;
                   2994:
3.175     scop     2995:   return unless defined($state);
3.120     scop     2996:
                   2997:   if ($state eq "PreChangeRemove") {    # we just got remove-lines before
3.133     scop     2998:     for (my $j = 0; $j < $leftRow; $j++) {
3.256     scop     2999:       printf(<<EOF, spacedHtmlText(@$leftColRef[$j]));
3.144     scop     3000: <tr>
3.260     scop     3001:  <td class="diff diff-removed">&nbsp;%s</td>
                   3002:  <td class="diff diff-empty">&nbsp;</td>
3.144     scop     3003: </tr>
                   3004: EOF
3.120     scop     3005:     }
                   3006:   } elsif ($state eq "PreChange") {     # state eq "PreChange"
                   3007:                                         # we got removes with subsequent adds
3.176     scop     3008:       for (my $j = 0; $j < $leftRow || $j < $rightRow; $j++) { # dump both cols
                   3009:         print "<tr>\n";
                   3010:         if ($j < $leftRow) {
3.260     scop     3011:           print '<td class="diff diff-changed">&nbsp;' .
3.176     scop     3012:             spacedHtmlText(@$leftColRef[$j]) . '</td>';
                   3013:         } else {
3.260     scop     3014:           print '<td class="diff diff-changed-missing">&nbsp;</td>';
3.176     scop     3015:         }
                   3016:         print "\n";
3.120     scop     3017:
3.176     scop     3018:         if ($j < $rightRow) {
3.260     scop     3019:           print '<td class="diff diff-changed">&nbsp;' .
3.176     scop     3020:             spacedHtmlText(@$rightColRef[$j]) . '</td>';
                   3021:         } else {
3.260     scop     3022:           print '<td class="diff diff-changed-missing">&nbsp;</td>';
3.176     scop     3023:         }
                   3024:         print "\n</tr>\n";
3.120     scop     3025:       }
                   3026:   }
3.1       knu      3027: }
                   3028:
3.175     scop     3029:
3.263     scop     3030: #
                   3031: # Generates "human readable", HTMLified diffs.
                   3032: #
3.144     scop     3033: sub human_readable_diff($$)
3.120     scop     3034: {
                   3035:   my ($fh, $rev) = @_;
3.144     scop     3036:
3.169     scop     3037:   (my $where_nd       = $where)       =~ s|\.diff$||;
                   3038:   (my $filename       = $where_nd)    =~ s|^.*/||;
3.262     scop     3039:   (my $pathname       = $where_nd)    =~ s|((?<=/)Attic/)?[^/]*$||;
3.169     scop     3040:   (my $scriptwhere_nd = $scriptwhere) =~ s|\.diff$||;
3.120     scop     3041:
3.133     scop     3042:   navigateHeader($scriptwhere_nd, $pathname, $filename, $rev, 'diff');
3.120     scop     3043:
3.178     scop     3044:   # Read header to pick up read revision and date, if possible.
                   3045:
                   3046:   my ($r1d, $r1r, $r2d, $r2r);
3.120     scop     3047:   while (<$fh>) {
                   3048:     ($r1d, $r1r) = /\t(.*)\t(.*)$/ if (/^--- /);
                   3049:     ($r2d, $r2r) = /\t(.*)\t(.*)$/ if (/^\+\+\+ /);
                   3050:     last if (/^\+\+\+ /);
                   3051:   }
                   3052:
3.178     scop     3053:   my ($rev1, $date1);
3.120     scop     3054:   if (defined($r1r) && $r1r =~ /^(\d+\.)+\d+$/) {
                   3055:     $rev1  = $r1r;
                   3056:     $date1 = $r1d;
                   3057:   }
3.178     scop     3058:   my ($rev2, $date2);
3.120     scop     3059:   if (defined($r2r) && $r2r =~ /^(\d+\.)+\d+$/) {
                   3060:     $rev2  = $r2r;
                   3061:     $date2 = $r2d;
                   3062:   }
3.292     scop     3063:   $rev1  = $input{r1}      unless defined($rev1);
                   3064:   $rev1  = $input{tr1}     if (defined($rev1) && $rev1 eq 'text');
3.178     scop     3065:   $rev1  = 'unknown-left'  unless defined($rev1);
3.292     scop     3066:   $rev2  = $input{r2}      unless defined($rev2);
                   3067:   $rev2  = $input{tr2}     if (defined($rev2) && $rev2 eq 'text');
3.178     scop     3068:   $rev2  = 'unknown-right' unless defined($rev2);
                   3069:   $date1 = defined($date1) ? ', ' . htmlquote($date1) : '';
                   3070:   $date2 = defined($date2) ? ', ' . htmlquote($date2) : '';
3.120     scop     3071:
3.281     scop     3072:   my $link = uri_escape($filename) . ($query ? "$query;" : '?');
3.210     scop     3073:
3.144     scop     3074:   # Using <table style=\"border: none\" here breaks NS 4.x badly...
3.210     scop     3075:   print <<EOF;
                   3076: <h3 style="text-align: center">Diff for /$where_nd between versions $rev1 and $rev2</h3>
                   3077: <table border="0" cellspacing="0" cellpadding="0" width="100%" summary="Diff output">
3.144     scop     3078: <tr style="background-color: #ffffff">
3.210     scop     3079: <th style="text-align: center; vertical-align: top" width="50%">
3.281     scop     3080: <a href="${link}rev=$rev1">version $rev1</a>$date1
3.178     scop     3081: </th>
3.210     scop     3082: <th style="text-align: center; vertical-align: top" width="50%">
3.281     scop     3083: <a href="${link}rev=$rev2">version $rev2</a>$date2
3.144     scop     3084: </th>
3.178     scop     3085: </tr>
3.144     scop     3086: EOF
3.120     scop     3087:
                   3088:   # Process diff text
                   3089:   # prefetch several lines
                   3090:   my @buf = head($fh);
3.144     scop     3091:   my %d = scan_directives(@buf);
3.120     scop     3092:
3.144     scop     3093:   my $leftRow  = 0;
                   3094:   my $rightRow = 0;
                   3095:   my ($difftxt, @rightCol, @leftCol, $oldline, $newline, $funname);
3.120     scop     3096:
3.281     scop     3097:   $link .= 'content-type=text%2Fx-cvsweb-markup;';
                   3098:   $link .= 'ln=1;' unless ($link =~ /\?.*\bln=1\b/);
3.210     scop     3099:
3.120     scop     3100:   while (@buf || !eof($fh)) {
                   3101:     $difftxt = @buf ? shift @buf : <$fh>;
                   3102:
                   3103:     if ($difftxt =~ /^@@/) {
                   3104:       ($oldline, $newline, $funname) =
                   3105:         $difftxt =~ /@@ \-([0-9]+).*\+([0-9]+).*@@(.*)/;
                   3106:       $funname = htmlquote($funname);
                   3107:       $funname =~ s/\s/&nbsp;/go;
3.182     scop     3108:       $funname &&= "&nbsp;<span style=\"font-size: smaller\">$funname</span>";
3.210     scop     3109:       my $ol = $oldline || 1;
                   3110:       my $nl = $newline || 1;
3.144     scop     3111:
3.210     scop     3112:       print <<EOF;
3.182     scop     3113: <tr>
3.210     scop     3114: <td width="50%" class="diff-heading">
3.281     scop     3115:  <b><a href="${link}rev=$rev1#l$ol">Line&nbsp;$oldline</a></b>$funname
3.182     scop     3116: </td>
3.210     scop     3117: <td width="50%" class="diff-heading">
3.281     scop     3118:  <b><a href="${link}rev=$rev2#l$nl">Line&nbsp;$newline</a></b>$funname
3.144     scop     3119: </td>
3.208     scop     3120: </tr>
3.144     scop     3121: EOF
                   3122:
3.120     scop     3123:       $state    = "dump";
                   3124:       $leftRow  = 0;
                   3125:       $rightRow = 0;
                   3126:     } else {
3.224     scop     3127:       my ($diffcode, $rest) = $difftxt =~ /^([-+ ])(.*)/;
                   3128:       $diffcode = '' unless defined($diffcode);
3.176     scop     3129:       $_ = $rest;
3.120     scop     3130:
                   3131:       #########
                   3132:       # little state machine to parse unified-diff output (Hen, zeller@think.de)
                   3133:       # in order to get some nice 'ediff'-mode output
                   3134:       # states:
                   3135:       #  "dump"             - just dump the value
                   3136:       #  "PreChangeRemove"  - we began with '-' .. so this could be the start of a 'change' area or just remove
                   3137:       #  "PreChange"        - okey, we got several '-' lines and moved to '+' lines -> this is a change block
                   3138:       ##########
                   3139:
                   3140:       if ($diffcode eq '+') {
                   3141:         if ($state eq "dump")
                   3142:         {    # 'change' never begins with '+': just dump out value
3.254     scop     3143:           $_ = spacedHtmlText($rest, $d{tabstop});
3.144     scop     3144:           printf(<<EOF, $_);
                   3145: <tr>
3.260     scop     3146:  <td class="diff diff-empty">&nbsp;</td>
                   3147:  <td class="diff diff-added">&nbsp;%s</td>
3.144     scop     3148: </tr>
                   3149: EOF
3.120     scop     3150:         } else {    # we got minus before
                   3151:           $state = "PreChange";
                   3152:           $rightCol[$rightRow++] = $_;
                   3153:         }
                   3154:       } elsif ($diffcode eq '-') {
                   3155:         $state = "PreChangeRemove";
                   3156:         $leftCol[$leftRow++] = $_;
                   3157:       } else {    # empty diffcode
                   3158:         flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
3.254     scop     3159:         $_ = spacedHtmlText($rest, $d{tabstop});
3.147     scop     3160:         printf(<<EOF, $_, $_);
3.144     scop     3161: <tr>
3.260     scop     3162:  <td class="diff diff-same">&nbsp;%s</td>
                   3163:  <td class="diff diff-same">&nbsp;%s</td>
3.144     scop     3164: </tr>
                   3165: EOF
3.120     scop     3166:         $state    = "dump";
                   3167:         $leftRow  = 0;
                   3168:         $rightRow = 0;
                   3169:       }
                   3170:     }
                   3171:   }
                   3172:   close($fh);
                   3173:
                   3174:   flush_diff_rows \@leftCol, \@rightCol, $leftRow, $rightRow;
                   3175:
                   3176:   # state is empty if we didn't have any change
                   3177:   if (!$state) {
3.144     scop     3178:     print <<EOF;
                   3179: <tr>
                   3180:  <td colspan="2">&nbsp;</td>
                   3181: </tr>
3.260     scop     3182: <tr class="diff diff-empty">
3.144     scop     3183:  <td colspan="2" align="center"><b>- No viewable change -</b></td>
                   3184: </tr>
                   3185: EOF
3.120     scop     3186:   }
                   3187:
3.182     scop     3188:   printf(<<EOF, $scriptwhere);
3.144     scop     3189: </table>
3.208     scop     3190: <hr style="width: 100%%" />
3.144     scop     3191: <form method="get" action="%s">
3.182     scop     3192: <div style="float: left">
3.208     scop     3193: <label for="f">Diff format:<br />
3.144     scop     3194: EOF
3.168     scop     3195:   printDiffSelectStickyVars();
4.17      schwarze 3196:   printDiffSelect();
3.182     scop     3197:   printf(<<EOF, $rev1, $rev2);
3.179     scop     3198: </label>
3.208     scop     3199: <input type="submit" value="Show" />
3.182     scop     3200: </div>
                   3201: <table style="float: right; border: thin outset" cellspacing="0" cellpadding="1" title="Legend" summary="Legend">
                   3202:  <tr>
3.260     scop     3203:   <td align="center" class="diff diff-removed">Removed from v.%s</td>
                   3204:   <td class="diff diff-empty">&nbsp;</td>
                   3205:  </tr><tr class="diff diff-changed">
3.182     scop     3206:   <td align="center" colspan="2">changed lines</td>
                   3207:  </tr><tr>
3.260     scop     3208:   <td class="diff diff-empty">&nbsp;</td>
                   3209:   <td align="center" class="diff diff-added">Added in v.%s</td>
3.182     scop     3210:  </tr>
3.144     scop     3211: </table>
                   3212: </form>
3.208     scop     3213: <br clear="all" />
3.144     scop     3214: EOF
3.167     scop     3215: }
                   3216:
3.120     scop     3217:
3.271     scop     3218: #
                   3219: # The passed in $path and $filename should not be URI escaped, and $swhere
                   3220: # *should* be.
                   3221: #
3.163     scop     3222: sub navigateHeader($$$$$;$)
3.120     scop     3223: {
3.163     scop     3224:   my ($swhere, $path, $filename, $rev, $title, $moddate) = @_;
3.120     scop     3225:   $swhere = "" if ($swhere eq $scriptwhere);
3.271     scop     3226:   $swhere = './' . uri_escape($filename) if ($swhere eq "");
3.120     scop     3227:
3.136     scop     3228:   my $qfile = htmlquote($filename);
                   3229:   my $qpath = htmlquote($path);
3.274     scop     3230:   my $trev  = $rev ? " - " . htmlquote($rev) : '';
3.100     knu      3231:
3.298     simon    3232:   http_header('text/html', $moddate);
3.133     scop     3233:
3.120     scop     3234:   print <<EOF;
3.100     knu      3235: $HTML_DOCTYPE
3.89      knu      3236: <html>
                   3237: <head>
3.170     scop     3238: <title>$qpath$qfile - $title$trev</title>
3.144     scop     3239: $HTML_META$CSS</head>
3.129     scop     3240: <body class="src">
3.265     scop     3241: <table class="navigate-header" width="100%" summary="Navigation">
3.184     scop     3242:  <tr>
                   3243:   <td>
3.64      knu      3244: EOF
                   3245:
3.274     scop     3246:   my $frag = '';
                   3247:   if ($rev) {
                   3248:     $frag  = '#';
                   3249:     $frag .= 'rev' if ($rev =~ /\./);  # Normal revision: prefix with "rev".
                   3250:     $frag .= $rev;                     # Append revision/branch/tag.
                   3251:   }
                   3252:   my $backurl = "$swhere$query$frag";
                   3253:
                   3254:   print &link($backicon, $backurl);
                   3255:   printf '<b>Return to %s CVS log', &link($qfile, $backurl);
3.120     scop     3256:   print "</b> $fileicon</td>";
                   3257:
3.144     scop     3258:   printf(<<EOF, $diricon, &clickablePath($path, 1));
3.182     scop     3259:   <td style="text-align: right">%s <b>Up to %s</b></td>
3.144     scop     3260:  </tr>
                   3261: </table>
                   3262: EOF
3.120     scop     3263: }
                   3264:
3.175     scop     3265:
3.120     scop     3266: sub plural_write($$)
                   3267: {
                   3268:   my ($num, $text) = @_;
                   3269:   if ($num != 1) {
                   3270:     $text .= "s";
                   3271:   }
                   3272:
                   3273:   if ($num > 0) {
                   3274:     return join (' ', $num, $text);
                   3275:   } else {
                   3276:     return "";
                   3277:   }
3.1       knu      3278: }
                   3279:
3.175     scop     3280:
3.1       knu      3281: ##
                   3282: # print readable timestamp in terms of
                   3283: # '..time ago'
                   3284: # H. Zeller <zeller@think.de>
                   3285: ##
3.120     scop     3286: sub readableTime($$)
                   3287: {
                   3288:   my ($secs, $long) = @_;
                   3289:
3.175     scop     3290:   # This function works correctly for time >= 2 seconds.
                   3291:   return 'very little time' if ($secs < 2);
3.120     scop     3292:
                   3293:   my %desc = (
3.133     scop     3294:               1        => 'second',
                   3295:               60       => 'minute',
                   3296:               3600     => 'hour',
                   3297:               86400    => 'day',
                   3298:               604800   => 'week',
                   3299:               2628000  => 'month',
                   3300:               31536000 => 'year'
                   3301:              );
                   3302:
3.120     scop     3303:   my @breaks = sort { $a <=> $b } keys %desc;
3.133     scop     3304:   my $i = 0;
3.120     scop     3305:
                   3306:   while ($i <= $#breaks && $secs >= 2 * $breaks[$i]) {
                   3307:     $i++;
                   3308:   }
                   3309:   $i--;
3.133     scop     3310:   my $break  = $breaks[$i];
                   3311:   my $retval = plural_write(int($secs / $break), $desc{$break});
3.120     scop     3312:
                   3313:   if ($long == 1 && $i > 0) {
                   3314:     my $rest = $secs % $break;
                   3315:     $i--;
                   3316:     $break = $breaks[$i];
                   3317:     my $resttime = plural_write(int($rest / $break), $desc{$break});
                   3318:     if ($resttime) {
                   3319:       $retval .= ", $resttime";
                   3320:     }
                   3321:   }
3.1       knu      3322:
3.120     scop     3323:   return $retval;
3.1       knu      3324: }
                   3325:
3.175     scop     3326:
3.1       knu      3327: #
3.263     scop     3328: # Returns a htmlified path where each directory is a link for faster
                   3329: # navigation.  $clickLast controls whether the basename
3.271     scop     3330: # (last directory/file) is a link as well.  The passed in $pathname should
                   3331: # *not* be URI escaped.
3.263     scop     3332: #
3.120     scop     3333: sub clickablePath($$)
                   3334: {
                   3335:   my ($pathname, $clickLast) = @_;
                   3336:
3.154     scop     3337:   my $root = '[' . htmlquote($CVSROOTdescr{$cvstree} || $cvstree) . ']';
                   3338:
3.144     scop     3339:   # This should never happen (see chooseCVSRoot()), but let's be sure...
3.154     scop     3340:   return $root if ($pathname eq '/');
3.120     scop     3341:
3.144     scop     3342:   my $retval =
3.154     scop     3343:     ' ' . &link($root, sprintf('%s/%s#dirlist', $scriptname, $query));
3.144     scop     3344:   my $wherepath = '';
                   3345:   my ($lastslash) = $pathname =~ m|/$|;
                   3346:
3.261     scop     3347:   foreach (split(m|/|, $pathname)) {
3.144     scop     3348:     $retval .= ' / ';
                   3349:     $wherepath .= "/$_";
                   3350:     my $last = "$wherepath/" eq "/$pathname" || $wherepath eq "/$pathname";
                   3351:
                   3352:     if ($clickLast || !$last) {
                   3353:       $retval .= &link(htmlquote($_),
                   3354:                        join ('',
3.271     scop     3355:                              $scriptname, uri_escape_path($wherepath),
3.144     scop     3356:                              (!$last || $lastslash ? '/' : ''), $query,
                   3357:                              (!$last || $lastslash ? "#dirlist" : "")));
                   3358:     } else {    # do not make a link to the current dir
                   3359:       $retval .= htmlquote($_);
3.120     scop     3360:     }
                   3361:   }
                   3362:   return $retval;
                   3363: }
                   3364:
3.144     scop     3365:
3.120     scop     3366: sub chooseCVSRoot()
                   3367: {
3.268     scop     3368:   print "<form method=\"get\" action=\"$scriptwhere\">\n<p>\n";
3.120     scop     3369:   if (2 <= @CVSROOT) {
3.133     scop     3370:     foreach my $k (keys %input) {
3.208     scop     3371:       printf("<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
3.154     scop     3372:              htmlquote($k), htmlquote($input{$k}))
                   3373:         if ($input{$k} && $k ne 'cvsroot');
3.120     scop     3374:     }
                   3375:
4.17      schwarze 3376:     print <<EOF;
3.208     scop     3377: <label for="cvsroot" accesskey="C">CVS Root:
4.17      schwarze 3378: <select id="cvsroot" name="cvsroot">
3.144     scop     3379: EOF
3.120     scop     3380:
3.133     scop     3381:     foreach my $k (@CVSROOT) {
3.136     scop     3382:       printf("<option value=\"%s\"%s>%s</option>\n",
                   3383:              htmlquote($k),
3.208     scop     3384:              ($k eq $cvstree) ? ' selected="selected"' : '',
3.136     scop     3385:              htmlquote($CVSROOTdescr{$k} || $k));
3.120     scop     3386:     }
3.208     scop     3387:     print '</select></label>';
3.120     scop     3388:   } else {
                   3389:
                   3390:     # no choice -- but we need the form to select module/path,
                   3391:     # at least for Netscape
3.208     scop     3392:     printf "CVS Root: <b>[%s]</b>",
3.154     scop     3393:       htmlquote($CVSROOTdescr{$cvstree} || $cvstree);
3.120     scop     3394:   }
                   3395:
3.144     scop     3396:   print <<EOF;
                   3397: <label for="mpath" accesskey="M">
                   3398: Module path or alias:
3.208     scop     3399: <input type="text" id="mpath" name="path" value="" size="15" />
3.144     scop     3400: </label>
3.208     scop     3401: <input type="submit" value="Go" accesskey="O" />
                   3402: </p>
                   3403: </form>
3.144     scop     3404: EOF
3.120     scop     3405: }
                   3406:
3.175     scop     3407:
3.120     scop     3408: sub fileSortCmp()
                   3409: {
3.175     scop     3410:   (my $af = $a) =~ s/,v$//;
                   3411:   (my $bf = $b) =~ s/,v$//;
3.120     scop     3412:   my ($rev1, $date1, $log1, $author1, $filename1) = @{$fileinfo{$af}}
                   3413:     if (defined($fileinfo{$af}));
                   3414:   my ($rev2, $date2, $log2, $author2, $filename2) = @{$fileinfo{$bf}}
                   3415:     if (defined($fileinfo{$bf}));
                   3416:
3.175     scop     3417:   my $comp = 0;
3.140     scop     3418:   if (defined($filename1) && defined($filename2) &&
                   3419:       $af eq $filename1   && $bf eq $filename2)
3.120     scop     3420:   {
                   3421:
                   3422:     # Two files
3.140     scop     3423:     $comp = -revcmp($rev1, $rev2)             if ($byrev  && $rev1  && $rev2);
                   3424:     $comp = ($date2   <=> $date1)             if ($bydate && $date1 && $date2);
                   3425:     if ($input{ignorecase}) {
                   3426:       $comp = (uc($log1)    cmp uc($log2))    if ($bylog && $log1 && $log2);
                   3427:       $comp = (uc($author1) cmp uc($author2)) if ($byauthor &&
                   3428:                                                   $author1 && $author2);
                   3429:     } else {
                   3430:       $comp = ($log1    cmp $log2)            if ($bylog && $log1 && $log2);
                   3431:       $comp = ($author1 cmp $author2)         if ($byauthor &&
                   3432:                                                   $author1 && $author2);
                   3433:     }
3.120     scop     3434:   }
                   3435:
                   3436:   if ($comp == 0) {
                   3437:
                   3438:     # Directories first, then files under version control,
                   3439:     # then other, "rogue" files.
                   3440:     # Sort by filename if no other criteria available.
                   3441:
                   3442:     my $ad = (
                   3443:       (-d "$fullname/$a")
                   3444:       ? 'D'
                   3445:       : (defined($fileinfo{$af}) ? 'F' : 'R')
                   3446:     );
                   3447:     my $bd = (
                   3448:       (-d "$fullname/$b")
                   3449:       ? 'D'
                   3450:       : (defined($fileinfo{$bf}) ? 'F' : 'R')
                   3451:     );
3.175     scop     3452:     (my $c = $a) =~ s|.*/||;
                   3453:     (my $d = $b) =~ s|.*/||;
3.144     scop     3454:
                   3455:     my ($l, $r) = ("$ad$c", "$bd$d");
                   3456:     $comp = $input{ignorecase} ? (uc($l) cmp uc($r)) : ($l cmp $r);
                   3457:
                   3458:     # Parent dir is always first, then Attic.
                   3459:     if ($comp != 0) {
                   3460:       if ($l eq 'D..') {
3.149     scop     3461:         $comp = -1;
3.144     scop     3462:       } elsif ($r eq 'D..') {
3.149     scop     3463:         $comp = 1;
3.144     scop     3464:       } elsif ($l eq 'DAttic') {
3.149     scop     3465:         $comp = -1;
3.144     scop     3466:       } elsif ($r eq 'DAttic') {
3.149     scop     3467:         $comp = 1;
3.144     scop     3468:       }
                   3469:     }
3.120     scop     3470:   }
                   3471:   return $comp;
3.1       knu      3472: }
                   3473:
3.220     scop     3474: #
                   3475: # Returns a URL to download the selected revision.
3.271     scop     3476: # Expects the passed in URL to be URI escaped, relative, and without a query
                   3477: # string.
3.220     scop     3478: #
3.120     scop     3479: sub download_url($$;$)
                   3480: {
                   3481:   my ($url, $revision, $mimetype) = @_;
3.252     scop     3482:   my @dots = $revision =~ /\./g;
                   3483:   $revision =~ s/\b0\.(?=\d+$)// if (scalar(@dots) & 1);
3.1       knu      3484:
3.222     scop     3485:   if (!defined($mimetype) || $mimetype !~ CVSWEBMARKUP) {
3.120     scop     3486:     my $path = $where;
                   3487:     $path =~ s|[^/]+$||;
3.261     scop     3488:     $url = "$scriptname/$CheckoutMagic/$path$url";
3.120     scop     3489:   }
3.219     scop     3490:   $url .= '?rev=' . uri_escape($revision);
                   3491:   $url .= ';content-type=' . uri_escape($mimetype) if $mimetype;
3.67      knu      3492:
3.175     scop     3493:   return $url;
3.1       knu      3494: }
                   3495:
3.175     scop     3496: #
3.220     scop     3497: # Returns a link to download the selected revision.
3.271     scop     3498: # Expects the passed in URL to be URI escaped, relative,
                   3499: # and without a query string.
3.175     scop     3500: #
3.120     scop     3501: sub download_link($$$;$)
                   3502: {
                   3503:   my ($url, $revision, $textlink, $mimetype) = @_;
3.220     scop     3504:   return sprintf('<a href="%s" class="download-link">%s</a>',
3.271     scop     3505:                  download_url($url, $revision, $mimetype) . $barequery,
                   3506:                  htmlquote($textlink));
3.218     scop     3507: }
3.120     scop     3508:
3.219     scop     3509: #
                   3510: # Returns a URL to display the selected revision.
3.263     scop     3511: # Expects the passed in URL to be URI escaped, and without a query string.
3.219     scop     3512: #
                   3513: sub display_url($$;$)
                   3514: {
                   3515:   my ($url, $revision, $mimetype) = @_;
                   3516:   $url .= '?rev=' . uri_escape($revision);
                   3517:   $url .= ';content-type=' . uri_escape($mimetype) if $mimetype;
                   3518:   return $url;
                   3519: }
                   3520:
                   3521: #
                   3522: # Returns a link to display the selected revision.
3.263     scop     3523: # Expects the passed in URL to be URI escaped, and without a query string.
3.219     scop     3524: #
                   3525: sub display_link($$;$$)
                   3526: {
                   3527:   my ($url, $revision, $textlink, $mtype) = @_;
                   3528:   $textlink = $revision unless defined($textlink);
                   3529:   return sprintf('<a href="%s" class="display-link">%s</a>',
3.271     scop     3530:                  display_url($url, $revision, $mtype) . $barequery,
3.219     scop     3531:                  htmlquote($textlink));
3.1       knu      3532: }
                   3533:
                   3534: # Returns a Query string with the
                   3535: # specified parameter toggled
3.140     scop     3536: sub toggleQuery($;$)
3.120     scop     3537: {
                   3538:   my ($toggle, $value) = @_;
3.133     scop     3539:
                   3540:   my %vars = %input;
3.120     scop     3541:
                   3542:   if (defined($value)) {
                   3543:     $vars{$toggle} = $value;
                   3544:   } else {
                   3545:     $vars{$toggle} = $vars{$toggle} ? 0 : 1;
                   3546:   }
                   3547:
                   3548:   # Build a new query of non-default paramenters
3.133     scop     3549:   my $newquery = "";
                   3550:   foreach my $var (@stickyvars) {
3.120     scop     3551:     my ($value)   = defined($vars{$var})         ? $vars{$var}         : "";
                   3552:     my ($default) = defined($DEFAULTVALUE{$var}) ? $DEFAULTVALUE{$var} : "";
                   3553:
                   3554:     if ($value ne $default) {
3.203     scop     3555:       $newquery .= ';' if ($newquery ne "");
3.206     scop     3556:       $newquery .= uri_escape($var) . '=' . uri_escape($value);
3.120     scop     3557:     }
                   3558:   }
                   3559:
                   3560:   if ($newquery) {
                   3561:     return '?' . $newquery;
                   3562:   }
                   3563:   return "";
                   3564: }
                   3565:
                   3566: sub htmlquote($)
                   3567: {
                   3568:   local ($_) = @_;
                   3569:   # Special Characters; RFC 1866
                   3570:   s/&/&amp;/g;
                   3571:   s/\"/&quot;/g;
4.7       schwarze 3572:   s/"/&quot;/g;
3.120     scop     3573:   s/</&lt;/g;
                   3574:   s/>/&gt;/g;
3.175     scop     3575:   return $_;
3.120     scop     3576: }
                   3577:
                   3578: sub htmlunquote($)
                   3579: {
                   3580:   local ($_) = @_;
                   3581:   # Special Characters; RFC 1866
                   3582:   s/&quot;/\"/g;
                   3583:   s/&lt;/</g;
                   3584:   s/&gt;/>/g;
                   3585:   s/&amp;/&/g;
3.175     scop     3586:   return $_;
3.120     scop     3587: }
                   3588:
3.271     scop     3589: sub uri_escape_path($)
3.120     scop     3590: {
3.271     scop     3591:   return join('/', map(uri_escape($_), split(m|/+|, shift, -1)));
3.120     scop     3592: }
                   3593:
3.163     scop     3594: sub http_header(;$$)
3.120     scop     3595: {
3.163     scop     3596:   my ($content_type, $moddate) = @_;
                   3597:   $content_type ||= 'text/html';
4.38      schwarze 3598:   $content_type .= '; charset="UTF-8"' if $content_type =~ /^text\//;
3.120     scop     3599:
3.185     scop     3600:   # Note that in the following, we explicitly join() and concatenate the
                   3601:   # headers instead of printing them as an array.  This is because some
                   3602:   # systems, eg. early versions of mod_perl 2 don't quite get it if the
                   3603:   # last \r\n\r\n isn't included in the last "payload" header print().
                   3604:
                   3605:   my @headers = ();
                   3606:   # TODO: ctime(3) from scalar gmtime() isn't HTTP compliant, see HTTP::Date.
                   3607:   push(@headers, 'Last-Modified: ' . scalar gmtime($moddate) . ' GMT')
                   3608:     if $moddate;
                   3609:   push(@headers, 'Content-Type: ' . $content_type);
4.10      schwarze 3610:   push(@headers, "Content-Security-Policy: default-src 'none'; " .
                   3611:     "img-src 'self'; style-src 'unsafe-inline'");
3.120     scop     3612:
                   3613:   if ($allow_compress && $maycompress) {
3.299     simon    3614:       push(@headers, 'Content-Encoding: gzip');
3.185     scop     3615:       push(@headers, 'Vary: Accept-Encoding');     # RFC 2616, 14.44
                   3616:       print join("\r\n", @headers) . "\r\n\r\n";
                   3617:
3.120     scop     3618:       $| = 1;
3.185     scop     3619:       $| = 0;                                      # Flush header output.
3.120     scop     3620:
4.13      schwarze 3621:       tie(*GZIP, __PACKAGE__, \*STDOUT);
3.120     scop     3622:       select(GZIP);
                   3623:       $gzip_open = 1;
                   3624:   } else {
3.185     scop     3625:     print join("\r\n", @headers) . "\r\n\r\n";
3.120     scop     3626:   }
                   3627: }
                   3628:
3.131     scop     3629:
3.163     scop     3630: sub html_header($;$)
3.120     scop     3631: {
3.163     scop     3632:   my ($title, $moddate) = @_;
3.288     scop     3633:   $title  = htmlquote($title);
                   3634:   my $l   = $logo || '';
                   3635:   my $css = $CSS || '';
3.163     scop     3636:   http_header('text/html', $moddate);
3.120     scop     3637:   print <<EOH;
3.100     knu      3638: $HTML_DOCTYPE
3.1       knu      3639: <html>
3.40      knu      3640: <head>
3.134     scop     3641: <title>$title</title>
3.288     scop     3642: $HTML_META$css</head>
3.129     scop     3643: <body>
3.182     scop     3644: $l <h1>$title</h1>
3.1       knu      3645: EOH
                   3646: }
                   3647:
3.120     scop     3648: sub html_footer()
                   3649: {
3.208     scop     3650:   print "<hr />\n<address>$address</address>\n" if $address;
3.127     scop     3651:   print "</body>\n</html>\n";
3.1       knu      3652: }
                   3653:
3.120     scop     3654: sub link_tags($)
                   3655: {
                   3656:   my ($tags) = @_;
                   3657:
3.261     scop     3658:   (my $filename = $where) =~ s|^.*/||;
3.271     scop     3659:   my $fileurl = './' . uri_escape($filename);
3.120     scop     3660:
3.261     scop     3661:   my $ret = "";
                   3662:   foreach my $sym (split(", ", $tags)) {
3.120     scop     3663:     $ret .= ",\n" if ($ret ne "");
3.136     scop     3664:     $ret .= &link(htmlquote($sym),
                   3665:                   $fileurl . toggleQuery('only_with_tag', $sym));
3.120     scop     3666:   }
3.219     scop     3667:   return $ret;
3.1       knu      3668: }
                   3669:
3.175     scop     3670:
3.1       knu      3671: #
3.233     scop     3672: # See if a file/dir is listed in the config file's @ForbiddenFiles list.
                   3673: # Takes a full file system path or one relative to $cvsroot, and strips the
                   3674: # trailing ",v" if present, then compares.  Returns 1 if forbidden, else 0.
3.221     scop     3675: #
3.233     scop     3676: sub forbidden($)
3.120     scop     3677: {
3.233     scop     3678:   (my $path = canonpath(shift)) =~ s/,v$//;
                   3679:   $path =~ s|^$cvsroot/+||;
3.175     scop     3680:   for my $forbidden_re (@ForbiddenFiles) {
                   3681:     return 1 if ($path =~ $forbidden_re);
3.120     scop     3682:   }
                   3683:   return 0;
3.174     scop     3684: }
                   3685:
                   3686:
                   3687: #
                   3688: # Starts a process using IPC::Run.  All arguments are passed to
                   3689: # IPC::Run::start() as-is.  Returns an array ($harness, $error) where
                   3690: # $harness is from IPC::Run if start() succeeds, undef otherwise.  In case
3.184     scop     3691: # of an error, $error contains the error message.
3.174     scop     3692: #
3.191     scop     3693: sub startproc(@)
3.174     scop     3694: {
3.215     scop     3695:   my $h = my $err = undef;
3.174     scop     3696:   eval {
                   3697:     local $SIG{__DIE__};
3.215     scop     3698:     $h = IPC::Run::start(@_) or die("return code: $?");
3.174     scop     3699:   };
3.215     scop     3700:   if ($@) {
3.229     scop     3701:     $h->finish() if $h;
3.215     scop     3702:     $h = undef;
                   3703:     $err = "'@{$_[0]}' failed: $@";
                   3704:   }
3.174     scop     3705:   return ($h, $err);
3.215     scop     3706: }
                   3707:
                   3708: #
                   3709: # Runs a process using IPC::Run.  All arguments are passed to
                   3710: # IPC::Run::run() as-is.  Returns an array ($exitcode, $errormsg).
                   3711: #
                   3712: sub runproc(@)
                   3713: {
                   3714:   eval {
                   3715:     local $SIG{__DIE__};
                   3716:     IPC::Run::run(@_);
                   3717:   };
                   3718:   my $exitcode = $? >> 8;
                   3719:   my $errormsg  = undef;
                   3720:   if ($@) {
                   3721:     $exitcode ||= -1;
                   3722:     $errormsg = "'@{$_[0]}' failed: $@";
                   3723:   }
                   3724:   return ($exitcode, $errormsg);
3.225     scop     3725: }
                   3726:
                   3727: #
3.173     scop     3728: # Close the GZIP handle, and remove the tie.
3.225     scop     3729: #
3.120     scop     3730: sub gzipclose
                   3731: {
                   3732:   if ($gzip_open) {
                   3733:     select(STDOUT);
                   3734:     close(GZIP);
                   3735:     untie *GZIP;
                   3736:     $gzip_open = 0;
                   3737:   }
3.23      knu      3738: }
                   3739:
                   3740: # implement a gzipped file handle via the Compress:Zlib compression
                   3741: # library.
                   3742:
                   3743: sub MAGIC1() { 0x1f }
                   3744: sub MAGIC2() { 0x8b }
3.80      knu      3745: sub OSCODE() { 3 }
3.23      knu      3746:
3.120     scop     3747: sub TIEHANDLE
                   3748: {
                   3749:   my ($class, $out) = @_;
                   3750:   my ($d) = Compress::Zlib::deflateInit(
                   3751:     -Level      => Compress::Zlib::Z_BEST_COMPRESSION(),
                   3752:     -WindowBits => -Compress::Zlib::MAX_WBITS()
                   3753:     )
                   3754:     or return undef;
                   3755:   my ($o) = { handle => $out,
                   3756:               dh     => $d,
                   3757:               crc    => 0,
                   3758:               len    => 0,
                   3759:             };
4.12      schwarze 3760:   my ($header) = pack("C10",
3.120     scop     3761:                       MAGIC1, MAGIC2, Compress::Zlib::Z_DEFLATED(),
                   3762:                       0, 0, 0, 0, 0, 0, OSCODE);
                   3763:   print {$o->{handle}} $header;
                   3764:   return bless($o, $class);
                   3765: }
                   3766:
                   3767: sub PRINT
                   3768: {
                   3769:   my ($o)   = shift;
3.164     scop     3770:   my ($buf) = join (defined($,) ? $, : "", @_);
3.120     scop     3771:   my ($len) = length($buf);
                   3772:   my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3773:   print {$o->{handle}} $compressed if defined($compressed);
                   3774:   $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3775:   $o->{len} += $len;
                   3776:   return $len;
                   3777: }
                   3778:
                   3779: sub PRINTF
                   3780: {
                   3781:   my ($o)   = shift;
                   3782:   my ($fmt) = shift;
                   3783:   my ($buf) = sprintf($fmt, @_);
                   3784:   my ($len) = length($buf);
                   3785:   my ($compressed, $status) = $o->{dh}->deflate($buf);
                   3786:   print {$o->{handle}} $compressed if defined($compressed);
                   3787:   $o->{crc} = Compress::Zlib::crc32($buf, $o->{crc});
                   3788:   $o->{len} += $len;
                   3789:   return $len;
                   3790: }
                   3791:
                   3792: sub WRITE
                   3793: {
                   3794:   my ($o, $buf, $len, $off) = @_;
                   3795:   my ($compressed, $status) = $o->{dh}->deflate(substr($buf, 0, $len));
                   3796:   print {$o->{handle}} $compressed if defined($compressed);
                   3797:   $o->{crc} = Compress::Zlib::crc32(substr($buf, 0, $len), $o->{crc});
                   3798:   $o->{len} += $len;
                   3799:   return $len;
                   3800: }
                   3801:
                   3802: sub CLOSE
                   3803: {
                   3804:   my ($o) = @_;
                   3805:   return if !defined($o->{dh});
                   3806:   my ($buf) = $o->{dh}->flush();
                   3807:   $buf .= pack("V V", $o->{crc}, $o->{len});
                   3808:   print {$o->{handle}} $buf;
                   3809:   undef $o->{dh};
                   3810: }
                   3811:
                   3812: sub DESTROY
                   3813: {
                   3814:   my ($o) = @_;
                   3815:   CLOSE($o);
1.1       jfieber  3816: }
3.149     scop     3817:
                   3818: # Local variables:
                   3819: # indent-tabs-mode: nil
                   3820: # cperl-indent-level: 2
                   3821: # End:

CVSweb