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

Annotation of cvsweb/cvsweb.cgi, Revision 4.10

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

CVSweb