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

Annotation of cvsweb/cvsweb.cgi, Revision 3.198

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

CVSweb