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

Annotation of cvsweb/cvsweb.cgi, Revision 4.21

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

CVSweb