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

Annotation of cvsweb/cvsweb.cgi, Revision 4.32

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

CVSweb