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

Annotation of cvsweb/cvsweb.cgi, Revision 3.79

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

CVSweb