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

Annotation of cvsweb/cvsweb.cgi, Revision 3.217

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

CVSweb