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

Annotation of cvsweb/cvsweb.cgi, Revision 3.86

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

CVSweb