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

Annotation of cvsweb/cvsweb.cgi, Revision 3.87

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

CVSweb