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

Annotation of cvsweb/cvsweb.cgi, Revision 3.56

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

CVSweb