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

Annotation of cvsweb/cvsweb.cgi, Revision 3.194

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

CVSweb