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

Annotation of cvsweb/cvsweb.cgi, Revision 3.152

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

CVSweb