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

Annotation of cvsweb/cvsweb.cgi, Revision 3.59

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

CVSweb