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

Annotation of cvsweb/cvsweb.cgi, Revision 3.55

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

CVSweb