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

Annotation of cvsweb/cvsweb.cgi, Revision 1.9

1.1       jfieber     1: #!/usr/bin/perl -s
                      2: #
                      3: # cvsweb - a CGI interface to the CVS tree.
                      4: #
                      5: # Written by Bill Fenner <fenner@parc.xerox.com> on his own time.
                      6: # Insert BSD copyright here.
                      7: #
                      8: #HTTP_USER_AGENT: Mozilla/1.1N (X11; I; SunOS 4.1.3_U1 sun4m) via proxy gateway CERN-HTTPD/3.0 libwww/2.17
                      9: #SERVER_NAME: www.freebsd.org
                     10: #QUERY_STRING: baz
                     11: #SCRIPT_FILENAME: /usr/local/www/cgi-bin/env.pl
                     12: #SERVER_PORT: 80
                     13: #HTTP_ACCEPT: */*, image/gif, image/x-xbitmap, image/jpeg
                     14: #SERVER_PROTOCOL: HTTP/1.0
                     15: #HTTP_COOKIE: s=beta26429821397802167
                     16: #PATH_INFO: /foo/bar
                     17: #REMOTE_ADDR: 13.1.64.94
                     18: #DOCUMENT_ROOT: /usr/local/www/data/
                     19: #PATH: /sbin:/bin:/usr/sbin:/usr/bin
                     20: #PATH_TRANSLATED: /usr/local/www/data//foo/bar
                     21: #GATEWAY_INTERFACE: CGI/1.1
                     22: #REQUEST_METHOD: GET
                     23: #SCRIPT_NAME: /cgi-bin/env.pl
                     24: #SERVER_SOFTWARE: Apache/1.0.0
                     25: #REMOTE_HOST: beta.xerox.com
                     26: #SERVER_ADMIN: webmaster@freebsd.org
                     27: #
                     28: require 'timelocal.pl';
                     29: require 'ctime.pl';
                     30:
1.2       jfieber    31: $hsty_base = "";
                     32: require 'cgi-style.pl';
                     33:
1.1       jfieber    34: $cvsroot = '/home/ncvs';
                     35: $intro = "
                     36: This is a WWW interface to the FreeBSD CVS tree.
                     37: You can browse the file hierarchy by picking directories
                     38: (which have slashes after them, e.g. <b>src/</b>).
                     39: If you pick a file, you will see the revision history
                     40: for that file.
                     41: Selecting a revision number will download that revision of
                     42: the file.  There is a link at each revision to display
                     43: diffs between that revision and the previous one, and
                     44: a form at the bottom of the page that allows you to
                     45: display diffs between arbitrary revisions.
                     46: <p>
1.7       fenner     47: If you would like to use this CGI script on your own web server and
                     48: CVS tree, see <A HREF=\"http://www.freebsd.org/~fenner/cvsweb/\">
                     49: the CVSWeb distribution site</A>.
                     50: <p>
1.1       jfieber    51: Please send any suggestions, comments, etc. to
                     52: <A HREF=\"mailto:fenner@freebsd.org\">Bill Fenner &lt;fenner@freebsd.org&gt;</A>
                     53: ";
                     54: $shortinstr = "
                     55: Click on a directory to enter that directory. Click on a file to display
                     56: its revision history and to get a
                     57: chance to display diffs between revisions.
                     58: ";
                     59:
                     60: $verbose = $v;
                     61: ($where = $ENV{'PATH_INFO'}) =~ s|^/||;
                     62: $where =~ s|/$||;
                     63: $fullname = $cvsroot . '/' . $where;
                     64: ($scriptname = $ENV{'SCRIPT_NAME'}) =~ s|^/?|/|;
                     65: $scriptname =~ s|/$||;
                     66: $scriptwhere = $scriptname . '/' . $where;
                     67: $scriptwhere =~ s|/$||;
                     68:
                     69: if (!-d $cvsroot) {
                     70:        &fatal("500 Internal Error",'$CVSROOT not found!');
                     71: }
1.7       fenner     72:
1.9     ! fenner     73: if ($query = $ENV{'QUERY_STRING'}) {
        !            74:     foreach (split(/&/, $query)) {
1.7       fenner     75:        s/%(..)/sprintf("%c", hex($1))/ge;      # unquote %-quoted
                     76:        if (/(\S+)=(.*)/) {
                     77:            $input{$1} = $2;
                     78:        } else {
                     79:            $input{$_}++;
                     80:        }
                     81:     }
1.9     ! fenner     82:     $query = "?" . $query;
1.7       fenner     83: }
1.1       jfieber    84: if (-d $fullname) {
                     85:        opendir(DIR, $fullname) || &fatal("404 Not Found","$where: $!");
                     86:        @dir = readdir(DIR);
                     87:        closedir(DIR);
                     88:        if ($where eq '') {
1.2       jfieber    89:            print &html_header("FreeBSD CVS Repository");
1.1       jfieber    90:            print $intro;
                     91:        } else {
1.2       jfieber    92:            print &html_header("/$where");
1.1       jfieber    93:            print $shortinstr;
                     94:        }
                     95:        print "<p>Current directory: <b>/$where</b>\n";
1.2       jfieber    96:        print "<P><HR NOSHADE>\n";
1.1       jfieber    97:        # Using <MENU> in this manner violates the HTML2.0 spec but
                     98:        # provides the results that I want in most browsers.  Another
                     99:        # case of layout spooging up HTML.
                    100:        print "<MENU>\n";
1.9     ! fenner    101:        lookingforattic:
        !           102:        for ($i = 0; $i <= $#dir; $i++) {
        !           103:                if ($dir[$i] eq "Attic") {
        !           104:                        last lookingforattic;
        !           105:                }
        !           106:        }
        !           107:        if (!$input{"showattic"} && ($i <= $#dir) &&
        !           108:                                opendir(DIR, $fullname . "/Attic")) {
        !           109:                splice(@dir, $i, 1,
        !           110:                        grep((s|^|Attic/|,!m|/\.|), readdir(DIR)));
        !           111:                closedir(DIR);
        !           112:        }
        !           113:        # Sort without the Attic/ pathname.
        !           114:        foreach (sort {($c=$a)=~s|.*/||;($d=$b)=~s|.*/||;($c cmp $d)} @dir) {
1.1       jfieber   115:            if ($_ eq '.') {
                    116:                next;
                    117:            }
1.9     ! fenner    118:            if (s|^Attic/||) {
        !           119:                $attic = " (in the Attic)";
        !           120:            } else {
        !           121:                $attic = "";
        !           122:            }
1.1       jfieber   123:            if ($_ eq '..') {
                    124:                next if ($where eq '');
                    125:                ($updir = $scriptwhere) =~ s|[^/]+$||;
                    126:                print "<IMG SRC=\"/icons/back.gif\"> ",
1.9     ! fenner    127:                    &link("Previous Directory",$updir . $query), "<BR>";
1.7       fenner    128: #              print "<IMG SRC=???> ",
                    129: #                  &link("Directory-wide diffs", $scriptwhere . '/*'), "<BR>";
1.1       jfieber   130:            } elsif (-d $fullname . "/" . $_) {
                    131:                print "<IMG SRC=\"/icons/dir.gif\"> ",
1.9     ! fenner    132:                    &link($_ . "/", $scriptwhere . '/' . $_ . '/' . $query), $attic, "<BR>";
1.1       jfieber   133:            } elsif (s/,v$//) {
1.7       fenner    134: # TODO: add date/time?  How about sorting?
1.1       jfieber   135:                print "<IMG SRC=\"/icons/text.gif\"> ",
1.9     ! fenner    136:                    &link($_, $scriptwhere . '/' .
        !           137:                            ($attic ? "Attic/" : "") . $_ . $query),
        !           138:                    $attic, "<BR>";
1.1       jfieber   139:            }
                    140:        }
                    141:        print "</MENU>\n";
1.9     ! fenner    142:        if ($input{"only_on_branch"}) {
        !           143:            print "<HR><FORM METHOD=\"GET\" ACTION=\"${scriptwhere}\">\n";
        !           144:            print "Currently showing only branch $input{'only_on_branch'}.\n";
        !           145:            $input{"only_on_branch"}="";
        !           146:            foreach $k (keys %input) {
        !           147:                print "<INPUT TYPE=hidden NAME=$k VALUE=$input{$k}>\n" if $input{$k};
        !           148:            }
        !           149:            print "<INPUT TYPE=SUBMIT VALUE=\"Show all branches\">\n";
        !           150:            print "</FORM>\n";
        !           151:        }
        !           152:        $formwhere = $scriptwhere;
        !           153:        $formwhere =~ s|Attic/?$|| if ($input{"showattic"});
        !           154:        print "<HR><FORM METHOD=\"GET\" ACTION=\"${formwhere}\">\n";
        !           155:        $input{"showattic"}=!$input{"showattic"};
        !           156:        foreach $k (keys %input) {
        !           157:            print "<INPUT TYPE=hidden NAME=$k VALUE=$input{$k}>\n" if $input{$k};
        !           158:        }
        !           159:        print "<INPUT TYPE=SUBMIT VALUE=\"";
        !           160:        print ($input{"showattic"} ? "Show" : "Hide");
        !           161:        print " attic directories\">\n";
        !           162:        print "</FORM>\n";
1.2       jfieber   163:        print &html_footer;
1.1       jfieber   164:        print "</BODY></HTML>\n";
                    165: } elsif (-f $fullname . ',v') {
1.7       fenner    166:        if ($input{'rev'} =~ /^[\d\.]+$/) {
                    167:                &checkout($fullname, $input{'rev'});
                    168:                exit;
                    169:        }
                    170:        if ($input{'r1'} && $input{'r2'}) {
                    171:                &dodiff($fullname, $input{'r1'}, $input{'tr1'},
                    172:                        $input{'r2'}, $input{'tr2'}, $input{'f'});
1.1       jfieber   173:                exit;
                    174:        }
                    175:        open(RCS, "rlog '$fullname'|") || &fatal("500 Internal Error",
                    176:                                                "Failed to spawn rlog");
                    177:        while (<RCS>) {
                    178:            print if ($verbose);
1.8       fenner    179:            if (/^branch:\s+([\d\.]+)/) {
                    180:                $curbranch = $1;
                    181:            }
1.1       jfieber   182:            if ($symnames) {
                    183:                if (/^\s+([^:]+):\s+([\d\.]+)/) {
                    184:                    $symrev{$1} = $2;
                    185:                    if ($revsym{$2}) {
                    186:                        $revsym{$2} .= ", ";
                    187:                    }
                    188:                    $revsym{$2} .= $1;
                    189:                } else {
                    190:                    $symnames = 0;
                    191:                }
                    192:            } elsif (/^symbolic names/) {
                    193:                $symnames = 1;
                    194:            } elsif (/^-----/) {
                    195:                last;
                    196:            }
                    197:        }
1.7       fenner    198:
                    199:        if ($onlyonbranch = $input{'only_on_branch'}) {
                    200:            ($onlyonbranch = $symrev{$onlyonbranch}) =~ s/\.0\././;
                    201:            ($onlybranchpoint = $onlyonbranch) =~ s/\.\d+$//;
                    202:        }
                    203:
1.1       jfieber   204: # each log entry is of the form:
                    205: # ----------------------------
                    206: # revision 3.7.1.1
                    207: # date: 1995/11/29 22:15:52;  author: fenner;  state: Exp;  lines: +5 -3
                    208: # log info
                    209: # ----------------------------
                    210:        logentry:
                    211:        while (!/^=========/) {
                    212:            $_ = <RCS>;
                    213:            print "R:", $_ if ($verbose);
                    214:            if (/^revision ([\d\.]+)/) {
                    215:                $rev = $1;
                    216:            } elsif (/^========/ || /^----------------------------$/) {
                    217:                next logentry;
                    218:            } else {
                    219:                &fatal("500 Internal Error","Error parsing RCS output: $_");
                    220:            }
                    221:            $_ = <RCS>;
                    222:            print "D:", $_ if ($verbose);
1.9     ! fenner    223:            if (m|^date:\s+(\d+)/(\d+)/(\d+)\s+(\d+):(\d+):(\d+);\s+author:\s+(\S+);\s+state:\s+(\S+);|) {
1.1       jfieber   224:                $yr = $1;
                    225:                # damn 2-digit year routines
                    226:                if ($yr > 100) {
                    227:                    $yr -= 1900;
                    228:                }
                    229:                $date{$rev} = &timelocal($6,$5,$4,$3,$2 - 1,$yr);
                    230:                $author{$rev} = $7;
1.9     ! fenner    231:                $state{$rev} = $8;
1.1       jfieber   232:            } else {
                    233:                &fatal("500 Internal Error", "Error parsing RCS output: $_");
                    234:            }
                    235:            line:
                    236:            while (<RCS>) {
                    237:                print "L:", $_ if ($verbose);
                    238:                next line if (/^branches:\s/);
                    239:                last line if (/^----------------------------$/ || /^=========/);
                    240:                $log{$rev} .= $_;
                    241:            }
                    242:            print "E:", $_ if ($verbose);
                    243:        }
                    244:        close(RCS);
                    245:        print "Done reading RCS file\n" if ($verbose);
                    246: #
                    247: # Sort the revisions into commit-date order.
                    248:        @revorder = sort {$date{$b} <=> $date{$a}} keys %date;
                    249:        print "Done sorting revisions\n" if ($verbose);
                    250: #
                    251: # HEAD is an artificial tag which is simply the highest tag number on the main
1.8       fenner    252: # branch, unless there is a branch tag in the RCS file in which case it's the
                    253: # highest revision on that branch.  Find it by looking through @revorder; it
                    254: # is the first commit listed on the appropriate branch.
                    255:        $headrev = $curbranch || "1";
1.1       jfieber   256:        revision:
                    257:        for ($i = 0; $i <= $#revorder; $i++) {
1.8       fenner    258:            if ($revorder[$i] =~ /^(\S*)\.\d+$/ && $headrev eq $1) {
1.1       jfieber   259:                if ($revsym{$revorder[$i]}) {
                    260:                    $revsym{$revorder[$i]} .= ", ";
                    261:                }
                    262:                $revsym{$revorder[$i]} .= "HEAD";
                    263:                $symrev{"HEAD"} = $revorder[$i];
                    264:                last revision;
                    265:            }
                    266:        }
                    267:        print "Done finding HEAD\n" if ($verbose);
                    268: #
                    269: # Now that we know all of the revision numbers, we can associate
                    270: # absolute revision numbers with all of the symbolic names, and
                    271: # pass them to the form so that the same association doesn't have
                    272: # to be built then.
                    273: #
                    274: # should make this a case-insensitive sort
                    275:        foreach (sort keys %symrev) {
                    276:            $rev = $symrev{$_};
                    277:            if ($rev =~ /^(\d+(\.\d+)+)\.0\.(\d+)$/) {
1.7       fenner    278:                push(@branchnames, $_);
1.1       jfieber   279:                #
                    280:                # A revision number of A.B.0.D really translates into
                    281:                # "the highest current revision on branch A.B.D".
                    282:                #
                    283:                # If there is no branch A.B.D, then it translates into
                    284:                # the head A.B .
                    285:                #
                    286:                $head = $1;
                    287:                $branch = $3;
                    288:                $regex = $head . "." . $branch;
                    289:                $regex =~ s/\./\./g;
                    290:                #             <
                    291:                #           \____/
                    292:                $rev = $head;
                    293:
                    294:                revision:
                    295:                foreach $r (@revorder) {
                    296:                    if ($r =~ /^${regex}/) {
                    297:                        $rev = $head . "." . $branch;
                    298:                        last revision;
                    299:                    }
                    300:                }
                    301:                $revsym{$rev} .= ", " if ($revsym{$rev});
                    302:                $revsym{$rev} .= $_;
1.8       fenner    303:                if ($rev ne $head) {
                    304:                    $branchpoint{$head} .= ", " if ($branchpoint{$head});
                    305:                    $branchpoint{$head} .= $_;
                    306:                }
1.1       jfieber   307:            }
                    308:            $sel .= "<OPTION VALUE=\"${rev}:${_}\">$_\n";
                    309:        }
                    310:        print "Done associating revisions with branches\n" if ($verbose);
1.2       jfieber   311:         print &html_header("CVS log for $where");
1.9     ! fenner    312:        ($upwhere = $where) =~ s|(Attic/)?[^/]+$||;
        !           313:        print "Up to ", &link($upwhere,$scriptname . "/" . $upwhere . $query);
1.1       jfieber   314:        print "<BR>\n";
                    315:        print "<A HREF=\"#diff\">Request diff between arbitrary revisions</A>\n";
1.2       jfieber   316:        print "<HR NOSHADE>\n";
1.8       fenner    317:        if ($curbranch) {
                    318:            print "Default branch is ";
                    319:            print ($revsym{$curbranch} || $curbranch);
                    320:        } else {
                    321:            print "No default branch";
                    322:        }
                    323:        print "<BR><HR NOSHADE>\n";
1.1       jfieber   324: # The other possible U.I. I can see is to have each revision be hot
                    325: # and have the first one you click do ?r1=foo
                    326: # and since there's no r2 it keeps going & the next one you click
                    327: # adds ?r2=foo and performs the query.
                    328: # I suppose there's no reason we can't try both and see which one
                    329: # people prefer...
                    330:
                    331:        for ($i = 0; $i <= $#revorder; $i++) {
                    332:            $_ = $revorder[$i];
1.7       fenner    333:            ($br = $_) =~ s/\.\d+$//;
                    334:            next if ($onlyonbranch && $br ne $onlyonbranch &&
                    335:                                            $_ ne $onlybranchpoint);
1.4       fenner    336:            print "<a NAME=\"rev$_\"></a>";
                    337:            foreach $sym (split(", ", $revsym{$_})) {
                    338:                print "<a NAME=\"$sym\"></a>";
                    339:            }
                    340:            if ($revsym{$br} && !$nameprinted{$br}) {
                    341:                foreach $sym (split(", ", $revsym{$br})) {
                    342:                    print "<a NAME=\"$sym\"></a>";
                    343:                }
                    344:                $nameprinted{$br}++;
                    345:            }
                    346:            print "\n";
1.1       jfieber   347:            print "<A HREF=\"$scriptwhere?rev=$_\"><b>$_</b></A>";
                    348:            if (/^1\.1\.1\.\d+$/) {
                    349:                print " <i>(vendor branch)</i>";
                    350:            }
                    351:            print " <i>" . &ctime($date{$_}) . "</i> by ";
                    352:            print "<i>" . $author{$_} . "</i>\n";
                    353:            if ($revsym{$_}) {
                    354:                print "<BR>CVS Tags: <b>$revsym{$_}</b>";
                    355:            }
1.4       fenner    356:            if ($revsym{$br})  {
1.1       jfieber   357:                if ($revsym{$_}) {
                    358:                    print "; ";
                    359:                } else {
                    360:                    print "<BR>";
                    361:                }
1.8       fenner    362:                print "Branch: <b>$revsym{$br}</b>\n";
                    363:            }
                    364:            if ($branchpoint{$_}) {
                    365:                if ($revsym{$br} || $revsym{$_}) {
                    366:                    print "; ";
                    367:                } else {
                    368:                    print "<BR>";
                    369:                }
                    370:                print "Branch point for: <b>$branchpoint{$_}</b>\n";
1.1       jfieber   371:            }
                    372:            # Find the previous revision on this branch.
                    373:            @prevrev = split(/\./, $_);
                    374:            if (--$prevrev[$#prevrev] == 0) {
                    375:                # If it was X.Y.Z.1, just make it X.Y
                    376:                if ($#prevrev > 1) {
                    377:                    pop(@prevrev);
                    378:                    pop(@prevrev);
                    379:                } else {
                    380:                    # It was rev 1.1 (XXX does CVS use revisions
                    381:                    # greater than 1.x?)
                    382:                    if ($prevrev[0] != 1) {
                    383:                        print "<i>* I can't figure out the previous revision! *</i>\n";
                    384:                    }
                    385:                }
                    386:            }
                    387:            if ($prevrev[$#prevrev] != 0) {
                    388:                $prev = join(".", @prevrev);
1.7       fenner    389:                print "<BR><A HREF=\"${scriptwhere}.diff?r1=$prev";
1.1       jfieber   390:                print "&r2=$_\">Diffs to $prev</A>\n";
                    391:                #
                    392:                # Plus, if it's on a branch, and it's not a vendor branch,
                    393:                # offer to diff with the immediately-preceding commit if it
                    394:                # is not the previous revision as calculated above
                    395:                # and if it is on the HEAD (or at least on a higher branch)
                    396:                # (e.g. change gets committed and then brought
                    397:                # over to -stable)
                    398:                if (!/^1\.1\.1\.\d+$/ && ($i != $#revorder) &&
                    399:                                        ($prev ne $revorder[$i+1])) {
                    400:                    @tmp1 = split(/\./, $revorder[$i+1]);
                    401:                    @tmp2 = split(/\./, $_);
                    402:                    if ($#tmp1 < $#tmp2) {
1.7       fenner    403:                        print "; <A HREF=\"${scriptwhere}.diff?r1=$revorder[$i+1]";
1.1       jfieber   404:                        print "&r2=$_\">Diffs to $revorder[$i+1]</A>\n";
                    405:                    }
                    406:                }
                    407:            }
1.9     ! fenner    408:            if ($state{$_} eq "dead") {
        !           409:                print "<BR><B><I>FILE REMOVED</I></B>\n";
        !           410:            }
1.1       jfieber   411:            print "<PRE>\n";
1.7       fenner    412:            print &htmlify($log{$_}, 1);
1.2       jfieber   413:            print "</PRE><HR NOSHADE>\n";
1.1       jfieber   414:        }
                    415:        print "<A NAME=diff>\n";
                    416:        print "This form allows you to request diff's between any two\n";
                    417:        print "revisions of a file.  You may select a symbolic revision\n";
                    418:        print "name using the selection box or you may type in a numeric\n";
                    419:        print "name using the type-in text box.\n";
                    420:        print "</A><P>\n";
1.7       fenner    421:        print "<FORM METHOD=\"GET\" ACTION=\"${scriptwhere}.diff\">\n";
1.1       jfieber   422:        print "Diffs between \n";
                    423:        print "<SELECT NAME=\"r1\">\n";
                    424:        print "<OPTION VALUE=\"text\" SELECTED>Use Text Field\n";
                    425:        print $sel;
                    426:        print "</SELECT>\n";
                    427:        print "<INPUT TYPE=\"TEXT\" NAME=\"tr1\" VALUE=\"$revorder[$#revorder]\">\n";
                    428:        print " and \n";
                    429:        print "<SELECT NAME=\"r2\">\n";
                    430:        print "<OPTION VALUE=\"text\" SELECTED>Use Text Field\n";
                    431:        print $sel;
                    432:        print "</SELECT>\n";
                    433:        print "<INPUT TYPE=\"TEXT\" NAME=\"tr2\" VALUE=\"$revorder[0]\">\n";
                    434:        print "<BR><INPUT TYPE=RADIO NAME=\"f\" VALUE=u CHECKED>Unidiff<br>\n";
                    435:        print "<INPUT TYPE=RADIO NAME=\"f\" VALUE=c>Context diff<br>\n";
1.7       fenner    436:        print "<INPUT TYPE=RADIO NAME=\"f\" VALUE=s>Side-by-Side<br>\n";
1.1       jfieber   437:        print "<INPUT TYPE=SUBMIT VALUE=\"Get Diffs\">\n";
                    438:        print "</FORM>\n";
1.7       fenner    439:        print "<HR noshade>\n";
                    440:        print "<A name=branch>\n";
                    441:        print "You may select to see revision information from only\n";
                    442:        print "a single branch.\n";
                    443:        print "</A><P>\n";
                    444:        print "<FORM METHOD=\"GET\" ACTION=\"$scriptwhere\">\n";
                    445:        print "Branch: \n";
                    446:        print "<SELECT NAME=\"only_on_branch\">\n";
1.9     ! fenner    447:        print "<OPTION VALUE=\"\"";
        !           448:        print " SELECTED" if ($input{"only_on_branch"} eq "");
        !           449:        print ">Show all branches\n";
1.7       fenner    450:        foreach (sort @branchnames) {
1.9     ! fenner    451:                print "<OPTION";
        !           452:                print " SELECTED" if ($input{"only_on_branch"} eq $_);
        !           453:                print ">${_}\n";
1.7       fenner    454:        }
                    455:        print "</SELECT>\n";
                    456:        print "<INPUT TYPE=SUBMIT VALUE=\"View Branch\">\n";
                    457:        print "</FORM>\n";
1.2       jfieber   458:         print &html_footer;
1.1       jfieber   459:        print "</BODY></HTML>\n";
1.7       fenner    460: } elsif ($fullname =~ s/\.diff$// && -f $fullname . ",v" &&
                    461:                                $input{'r1'} && $input{'r2'}) {
1.9     ! fenner    462:        # Allow diffs using the ".diff" extension
        !           463:        # so that browsers that default to the URL
        !           464:        # for a save filename don't save diff's as
        !           465:        # e.g. foo.c
1.7       fenner    466:        &dodiff($fullname, $input{'r1'}, $input{'tr1'},
                    467:                $input{'r2'}, $input{'tr2'}, $input{'f'});
1.9     ! fenner    468:        exit;
        !           469: } elsif (($newname = $fullname) =~ s|/([^/]+)$|/Attic/$1| &&
        !           470:                                 -f $newname . ",v") {
        !           471:        # The file has been removed and is in the Attic.
        !           472:        # Send a redirect pointing to the file in the Attic.
        !           473:        ($newplace = $scriptwhere) =~ s|/([^/]+)$|/Attic/$1|;
        !           474:        &redirect($newplace);
1.7       fenner    475:        exit;
                    476: } elsif (0 && (@files = &safeglob($fullname . ",v"))) {
                    477:        print "Content-type: text/plain\n\n";
                    478:        print "You matched the following files:\n";
                    479:        print join("\n", @files);
                    480:        # Find the tags from each file
                    481:        # Display a form offering diffs between said tags
1.1       jfieber   482: } else {
1.5       fenner    483:        # Assume it's a module name with a potential path following it.
1.7       fenner    484:        ($module = $where) =~ s|/.*||;
1.5       fenner    485:        $xtra = $&;
1.4       fenner    486:        # Is there an indexed version of modules?
                    487:        if (open(MODULES, "$cvsroot/CVSROOT/modules")) {
                    488:                while (<MODULES>) {
1.8       fenner    489:                        if (/^(\S+)\s+(\S+)/o && $module eq $1
                    490:                                && -d "${cvsroot}/$2" && $module ne $2) {
                    491:                                &redirect($scriptname . '/' . $2 . $xtra);
1.4       fenner    492:                        }
                    493:                }
                    494:        }
1.7       fenner    495:        &fatal("404 Not Found","$where: no such file or directory");
1.1       jfieber   496: }
                    497:
                    498: sub htmlify {
1.7       fenner    499:        local($string, $pr) = @_;
1.1       jfieber   500:
1.2       jfieber   501:        $string =~ s/&/&amp;/g;
1.1       jfieber   502:        $string =~ s/</&lt;/g;
                    503:        $string =~ s/>/&gt;/g;
                    504:
1.7       fenner    505:        if ($pr) {
                    506:                $string =~ s|\bpr(\W+[a-z]+/\W*)(\d+)|<A HREF=/cgi/query-pr.cgi?pr=$2>$&</A>|ig;
                    507:        }
                    508:
1.1       jfieber   509:        $string;
                    510: }
                    511:
                    512: sub link {
                    513:        local($name, $where) = @_;
                    514:
                    515:        "<A HREF=\"$where\">$name</A>\n";
                    516: }
                    517:
                    518: sub revcmp {
                    519:        local($rev1, $rev2) = @_;
                    520:        local(@r1) = split(/\./, $rev1);
                    521:        local(@r2) = split(/\./, $rev2);
                    522:        local($a,$b);
                    523:
1.5       fenner    524:        while (($a = shift(@r1)) && ($b = shift(@r2))) {
1.1       jfieber   525:            if ($a != $b) {
                    526:                return $a <=> $b;
                    527:            }
                    528:        }
                    529:        if (@r1) { return 1; }
                    530:        if (@r2) { return -1; }
                    531:        return 0;
                    532: }
                    533:
                    534: sub fatal {
                    535:        local($errcode, $errmsg) = @_;
                    536:        print "Status: $errcode\n";
1.7       fenner    537:        print &html_header("Error");
                    538: #      print "Content-type: text/html\n";
                    539: #      print "\n";
                    540: #      print "<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n";
                    541: #      print "<BODY>Error: $errmsg</BODY></HTML>\n";
                    542:        print "Error: $errmsg\n";
                    543:        print &html_footer;
1.4       fenner    544:        exit(1);
                    545: }
                    546:
                    547: sub redirect {
                    548:        local($url) = @_;
                    549:        print "Status: 301 Moved\n";
                    550:        print "Location: $url\n";
1.7       fenner    551:        print &html_header("Moved");
                    552: #      print "Content-type: text/html\n";
                    553: #      print "\n";
                    554: #      print "<HTML><HEAD><TITLE>Moved</TITLE></HEAD>\n";
                    555: #      print "<BODY>This document is located <A HREF=$url>here</A>.</BODY></HTML>\n";
                    556:        print "This document is located <A HREF=$url>here</A>.\n";
                    557:        print &html_footer;
1.1       jfieber   558:        exit(1);
1.7       fenner    559: }
                    560:
                    561: sub safeglob {
                    562:        local($filename) = @_;
                    563:        local($dirname);
                    564:        local(@results);
                    565:
                    566:        ($dirname = $filename) =~ s|/[^/]+$||;
                    567:        $filename =~ s|.*/||;
                    568:
                    569:        if (opendir(DIR, $dirname)) {
                    570:                $glob = $filename;
                    571:        #       transform filename from glob to regex.  Deal with:
                    572:        #       [, {, ?, * as glob chars
                    573:        #       make sure to escape all other regex chars
1.8       fenner    574:                $glob =~ s/([\.\(\)\|\+])/\\$1/g;
1.7       fenner    575:                $glob =~ s/\*/.*/g;
                    576:                $glob =~ s/\?/./g;
1.8       fenner    577:                $glob =~ s/{([^}]+)}/($t = $1) =~ s-,-|-g; "($t)"/eg;
1.7       fenner    578:                foreach (readdir(DIR)) {
                    579:                        if (/^${glob}$/) {
                    580:                                push(@results, $dirname . "/" .$_);
                    581:                        }
                    582:                }
                    583:        }
                    584:
                    585:        @results;
                    586: }
1.8       fenner    587:
1.7       fenner    588: sub checkout {
                    589:        local($fullname, $rev) = @_;
                    590:
                    591:        open(RCS, "co -p$rev '$fullname' 2>&1 |") ||
                    592:            &fail("500 Internal Error", "Couldn't co: $!");
                    593: # /home/ncvs/src/sys/netinet/igmp.c,v  -->  standard output
1.8       fenner    594: # or
                    595: # /home/ncvs/src/sys/netinet/igmp.c,v  -->  stdout
1.7       fenner    596: # revision 1.1.1.2
                    597: # /*
                    598:        $_ = <RCS>;
1.8       fenner    599:        if (/^(\S+),v\s+-->\s+st(andar)?d ?out(put)?\s*$/o && $1 eq $fullname) {
1.7       fenner    600:            # As expected
                    601:        } else {
                    602:            &fatal("500 Internal Error",
                    603:                "Unexpected output from co: $_");
                    604:        }
                    605:        $_ = <RCS>;
                    606:        if (/^revision\s+$rev\s*$/) {
                    607:            # As expected
                    608:        } else {
                    609:            &fatal("500 Internal Error",
                    610:                "Unexpected output from co: $_");
                    611:        }
                    612:        $| = 1;
                    613:        print "Content-type: text/plain\n\n";
                    614:        print <RCS>;
                    615:        close(RCS);
                    616: }
                    617:
                    618: sub dodiff {
                    619:        local($fullname, $r1, $tr1, $r2, $tr2, $f) = @_;
                    620:
                    621:        if ($r1 =~ /([^:]+)(:(.+))?/) {
                    622:            $rev1 = $1;
                    623:            $sym1 = $3;
                    624:        }
                    625:        if ($rev1 eq 'text') {
                    626:            $rev1 = $tr1;
                    627:        }
                    628:        if ($r2 =~ /([^:]+)(:(.+))?/) {
                    629:            $rev2 = $1;
                    630:            $sym2 = $3;
                    631:        }
                    632:        if ($rev2 eq 'text') {
                    633:            $rev2 = $tr2;
                    634:        }
                    635:        if (!($rev1 =~ /^[\d\.]+$/) || !($rev2 =~ /^[\d\.]+$/)) {
                    636:            &fatal("404 Not Found",
                    637:                    "Malformed query \"$ENV{'QUERY_STRING'}\"");
                    638:        }
                    639: #
                    640: # rev1 and rev2 are now both numeric revisions.
                    641: # Thus we do a DWIM here and swap them if rev1 is after rev2.
                    642: # XXX should we warn about the fact that we do this?
                    643:        if (&revcmp($rev1,$rev2) > 0) {
                    644:            ($tmp1, $tmp2) = ($rev1, $sym1);
                    645:            ($rev1, $sym1) = ($rev2, $sym2);
                    646:            ($rev2, $sym2) = ($tmp1, $tmp2);
                    647:        }
                    648: #
                    649: #      XXX Putting '-p' here is a personal preference
                    650:        if ($f eq 'c') {
                    651:            $difftype = '-p -c';
                    652:            $diffname = "Context diff";
                    653:        } elsif ($f eq 's') {
                    654:            $difftype = '--side-by-side --width=164';
                    655:            $diffname = "Side by Side";
                    656:        } else {
                    657:            $difftype = '-p -u';
                    658:            $diffname = "Unidiff";
                    659:        }
                    660: # XXX should this just be text/plain
                    661: # or should it have an HTML header and then a <pre>
                    662:        print "Content-type: text/plain\n\n";
                    663:        open(RCSDIFF, "rcsdiff $difftype -r$rev1 -r$rev2 '$fullname' 2>&1 |") ||
                    664:            &fail("500 Internal Error", "Couldn't rcsdiff: $!");
                    665: #
                    666: #===================================================================
                    667: #RCS file: /home/ncvs/src/sys/netinet/tcp_output.c,v
                    668: #retrieving revision 1.16
                    669: #retrieving revision 1.17
                    670: #diff -c -r1.16 -r1.17
                    671: #*** /home/ncvs/src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                    672: #--- /home/ncvs/src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17
                    673: #
                    674: # Ideas:
                    675: # - nuke the stderr output if it's what we expect it to be
                    676: # - Add "no differences found" if the diff command supplied no output.
                    677: #
                    678: #*** src/sys/netinet/tcp_output.c     1995/11/03 22:08:08     1.16
                    679: #--- src/sys/netinet/tcp_output.c     1995/12/05 17:46:35     1.17 RELENG_2_1_0
                    680: # (bogus example, but...)
                    681: #
                    682:        if ($difftype eq '-u') {
                    683:            $f1 = '---';
                    684:            $f2 = '\+\+\+';
                    685:        } else {
                    686:            $f1 = '\*\*\*';
                    687:            $f2 = '---';
                    688:        }
                    689:        while (<RCSDIFF>) {
                    690:            if (m|^$f1 $cvsroot|o) {
                    691:                s|$cvsroot/||o;
                    692:                if ($sym1) {
                    693:                    chop;
                    694:                    $_ .= " " . $sym1 . "\n";
                    695:                }
                    696:            } elsif (m|^$f2 $cvsroot|o) {
                    697:                s|$cvsroot/||o;
                    698:                if ($sym2) {
                    699:                    chop;
                    700:                    $_ .= " " . $sym2 . "\n";
                    701:                }
                    702:            }
                    703:            print $_;
                    704:        }
                    705:        close(RCSDIFF);
1.1       jfieber   706: }

CVSweb