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

Annotation of cvsweb/cvsweb.cgi, Revision 3.199

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

CVSweb