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

Annotation of cvsweb/cvsweb.cgi, Revision 1.1.1.18

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

CVSweb