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

Annotation of cvsweb/cvsweb.cgi, Revision 3.220

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

CVSweb