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

Annotation of cvsweb/cvsweb.cgi, Revision 3.37

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

CVSweb