123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- use IO::Socket;
- @languages = ("c", "cc", "java", "ml", "pascal", "ada", "lisp", "scheme", "haskell", "fortran", "ascii", "vhdl", "perl", "matlab", "python", "mips", "prolog", "spice", "vb", "csharp", "modula2", "a8086", "javascript", "plsql", "verilog");
- $server = 'moss.stanford.edu';
- $port = '7690';
- $noreq = "Request not sent.";
- $usage = "usage: moss [-x] [-l language] [-d] [-b basefile1] ... [-b basefilen] [-m #] [-c \"string\"] file1 file2 file3 ...";
- $userid=720082582;
- $opt_l = "c";
- $opt_m = 10;
- $opt_d = 0;
- $opt_x = 0;
- $opt_c = "";
- $opt_n = 250;
- $bindex = 0;
- while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
- ($first,$rest) = ($1,$2);
-
- shift(@ARGV);
- if ($first eq "d") {
- $opt_d = 1;
- next;
- }
- if ($first eq "b") {
- if($rest eq '') {
- die "No argument for option -b.\n" unless @ARGV;
- $rest = shift(@ARGV);
- }
- $opt_b[$bindex++] = $rest;
- next;
- }
- if ($first eq "l") {
- if ($rest eq '') {
- die "No argument for option -l.\n" unless @ARGV;
- $rest = shift(@ARGV);
- }
- $opt_l = $rest;
- next;
- }
- if ($first eq "m") {
- if($rest eq '') {
- die "No argument for option -m.\n" unless @ARGV;
- $rest = shift(@ARGV);
- }
- $opt_m = $rest;
- next;
- }
- if ($first eq "c") {
- if($rest eq '') {
- die "No argument for option -c.\n" unless @ARGV;
- $rest = shift(@ARGV);
- }
- $opt_c = $rest;
- next;
- }
- if ($first eq "n") {
- if($rest eq '') {
- die "No argument for option -n.\n" unless @ARGV;
- $rest = shift(@ARGV);
- }
- $opt_n = $rest;
- next;
- }
- if ($first eq "x") {
- $opt_x = 1;
- next;
- }
-
-
-
- if ($first eq "s") {
- $server = shift(@ARGV);
- next;
- }
-
-
-
- if ($first eq "p") {
- $port = shift(@ARGV);
- next;
- }
- die "Unrecognized option -$first. $usage\n";
- }
- print "Checking files . . . \n";
- $i = 0;
- while($i < $bindex)
- {
- die "Base file $opt_b[$i] does not exist. $noreq\n" unless -e "$opt_b[$i]";
- die "Base file $opt_b[$i] is not readable. $noreq\n" unless -r "$opt_b[$i]";
- die "Base file $opt_b is not a text file. $noreq\n" unless -T "$opt_b[$i]";
- $i++;
- }
- foreach $file (@ARGV)
- {
- die "File $file does not exist. $noreq\n" unless -e "$file";
- die "File $file is not readable. $noreq\n" unless -r "$file";
- die "File $file is not a text file. $noreq\n" unless -T "$file";
- }
- if ("@ARGV" eq '') {
- die "No files submitted.\n $usage";
- }
- print "OK\n";
- $sock = new IO::Socket::INET (
- PeerAddr => $server,
- PeerPort => $port,
- Proto => 'tcp',
- );
- die "Could not connect to server $server: $!\n" unless $sock;
- $sock->autoflush(1);
- sub read_from_server {
- $msg = <$sock>;
- print $msg;
- }
- sub upload_file {
- local ($file, $id, $lang) = @_;
- open(F,$file);
- $size = 0;
- while (<F>) {
- $size += length($_);
- }
- close(F);
- print "Uploading $file ...";
- open(F,$file);
- $file =~s/\s/\_/g;
- print $sock "file $id $lang $size $file\n";
- while (<F>) {
- print $sock $_;
- }
- close(F);
- print "done.\n";
- }
- print $sock "moss $userid\n";
- print $sock "directory $opt_d\n";
- print $sock "X $opt_x\n";
- print $sock "maxmatches $opt_m\n";
- print $sock "show $opt_n\n";
- print $sock "language $opt_l\n";
- $msg = <$sock>;
- chop($msg);
- if ($msg eq "no") {
- print $sock "end\n";
- die "Unrecognized language $opt_l.";
- }
- $i = 0;
- while($i < $bindex) {
- &upload_file($opt_b[$i++],0,$opt_l);
- }
- $setid = 1;
- foreach $file (@ARGV) {
- &upload_file($file,$setid++,$opt_l);
- }
- print $sock "query 0 $opt_c\n";
- print "Query submitted. Waiting for the server's response.\n";
- &read_from_server();
- print $sock "end\n";
- close($sock);
|