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

Annotation of cvsweb/cvsweb.cgi, Revision 3.207

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

CVSweb