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

Annotation of cvsweb/cvsweb.cgi, Revision 4.22

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

CVSweb