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

Annotation of cvsweb/cvsweb.cgi, Revision 4.13

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

CVSweb