#!/usr/bin/perl -w use Socket; use Carp; use Getopt::Std; # # sub logmsg: for logging activity # sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n"; } # # options: # d: debugging (no arg) # s: socket name (filename arg, default '/tmp/mgsock') # q: query text (no default, use quoted string) # l: logfile name (default '/tmp/mgqlog') getopts('ds:q:l:o:'); $ENV{MGDATA}='/usr2/people/lis429/public_html/mgdata'; $ENV{MG_GETRC}='/usr2/people/lis429/public_html/.mg_getrc'; my $logfile = $opt_l || "/tmp/mgqlog"; open(LOG, ">>$logfile"); select(LOG); my $address = $opt_s || "/tmp/mgsock"; logmsg "Client started"; socket(MG, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!"; connect(MG, sockaddr_un($address)) or die "connect: $!"; select(MG); $|++; select(LOG); # srand(time()); # do { # $num=rand(700); # $tmpfile = "/tmp/mgqtmp".$num; # } while (-x "$tmpfile"); # from option if ($opt_o) { $tmpfile = $opt_o; } else { print LOG "$0: puking cuz no output specified.\n"; die "No output file.\n"; } open(TMP,">$tmpfile"); if ($opt_q) { # # if we're provided a query on the command line, send it: # my ($nl) = "\n"; logmsg "query: $opt_q"; print MG $opt_q, $nl; print MG '.quit',$nl; logmsg "query sent" if $opt_d; # # read the socket's response, parsing for 'filename': # while ($line = ) { logmsg "received $line" if $opt_d; if ($line =~ m|\Filename\<\/B\>\s*(\S+?)\s*\|i) { print TMP "$1\n"; } } logmsg "finished receiving" if $opt_d; } else { # # if no query, simple echoback testing: # print MG "test\n"; print MG "q\n"; while ($line = ) { print TMP "output: $line\n"; } } # # close socket and close our temp file # close (MG); close (TMP); # # report name of temp file back to calling process # # print STDOUT "$tempfile\n"; exit(0);