Appendix C

PAWS Service Program

#!/ncsu/csc255_info/bin/sybperl
#PAWS Service program.  The serial line must be conditioned with another
#program called settty before this program will function properly.
#sybperl must be reachable for this program to work.  Currently on Eos, it is
#sufficiant to
#setenv DSCONSOLE CSC
#setenv DSQUERY CSC
#add csc255_info
#add sybase
#Warning:  this is evil but functional code.  Hack at your own risk!
#This code services the PAWS machine through the serial port
#and calls upon databases residing on eos.  

$logfile = "paws\.log";
#main
while(){
  open(LOG,">>$logfile");                                  #open the log file for appending

  #Get the SSN from PAWS and command string 
  #(ii or ll for increase and lookup)
  $commandstring = &getpawsdata();
  print LOG "\nNEW RECORD on ";
  open(DATE,"/bin/date|");
  @dateandtime = ;
  print LOG @dateandtime;
  print LOG $commandstring," is what we got from PAWS\n";
  $SSN = (substr($commandstring,0,9));                     #chop out offending command and return SSN
  #print LOG $SSN,"\n";
if ((substr($commandstring,9,2) eq "ll")||
    (substr($commandstring,9,2) eq "ii")){
  if (substr($commandstring,9,2) eq "ll"){                 #ll is lookup command
    $loginID = &SSNlookup($SSN);                             #lookup the login ID for user
    #print LOG $loginID,"\n";
    if ($loginID eq "BOGUS"){$quotaneeded = "FBad ID    Q";}else{
      $quotaneeded = &getquotadata($loginID);
    }
    print LOG $quotaneeded," is how much $loginID needs to print\n";
    &givepawsdata($quotaneeded);                             #send data to PAWS
  }

  if (substr($commandstring,9,2) eq "ii"){                 #ii  increase quota $5.00
  $quotaneeded = &increasequota($loginID);
  print LOG $quotaneeded," is how much $loginID needs to print\n";
  &givepawsdata($quotaneeded);                             #send data to PAWS
  }
  }else{
  $quotaneeded = "FBad CommandQ";
  &givepawsdata($quotaneeded);
  print LOG $quotaneeded,"\n";
  }
  close(LOG);
}

#Read the data from the serial port.  See program commin.  Leaves data in $_
sub getpawsdata{
  $serdevice = "\/dev\/tty00";                              #name of serial device
  $i = 0;

  open(SERIN,"+<$serdevice");                               #open for read and write
  $inputstring = "";
  while ($i < 11) {
    sysread(SERIN,$_,1);                                      #read 1 byte from dev
    #hard Echo#
    #syswrite(STDOUT,$_,1);                                  
    $inputstring .= $_;                                       #build 11 char string in the form: 555052299ii
    #print LOG $inputstring,"is the current input string\n";
    $i++;
  }
  close(SERIN);
  $commandstring = $inputstring;
  #print $commandstring;
  return($commandstring);
}

sub SSNlookup{
  #Look up users  login ID from SSN and leaves it in $loginID
  #would like to use sybperl for this and use an SQL server.  For now, 
  #we read a flat file
  $SSNinfile = "\.\/SSNnames";                       #file with names and SSNs in it

  $loginID = "BOGUS";                                 #Default login ID is BOGUS

  #Sorts it out and spits out the good stuff in sybase 8-)
$searchname = $SSN;
$dbproc = &dblogin("paws","12345");

if ($dbproc==-1) {
    die "Could not login to Sybase server\n";
}
$querystring = "where p.ssn = \'$searchname\'\n";
#print $querystring;

&dbcmd($dbproc,"select p.ssn, p.username\n");
&dbcmd($dbproc,"from paws_table p\n");
&dbcmd($dbproc, $querystring);
&dbsqlexec;

$i=0;

while (&dbresults != $NO_MORE_RESULTS ) {
    while (@data = &dbnextrow)
       {
       $i++;
       ($ssn, $username) = @data;
       #print "$username\n";
       $loginID = $username;
       }
}

&dbexit;



#     print LOG $loginID,"in function";
      return($loginID);
}

sub getquotadata{                                  #we want to parse out the info from 
                                                   #lpquota on the user and tell us how
                                                   #much money is in their account
open(LPQUOTA,"/afs/eos.ncsu.edu/users/s/spkorb/ECE481/lpquote -P100lez $loginID|"); #fake lpquota
    $i = 0;
    $j = 0;
@limitstuff = ;
         $quotadata = @limitstuff[2] ;              #third line of lpquota
         $usage = substr($quotadata,8,7);           #gets usage outof substr
         $limitstart = index($quotadata,"Limit:");  #finds word "Limit"
         $limit = substr($quotadata,$limitstart + 8,8);
         if ($usage eq ""){
         $quotaneeded = "FBad ID    Q";
          return($quotaneeded);
         close(LPQUOTA);
         }else{
$quotaneeded = $usage-$limit;                       #positive if in the hole
return($quotaneeded);
close(LPQUOTA);
}
}


sub increasequota{
if ($loginID eq "BOGUS"){$quotaneeded = "FBad ID    Q";}else{
  open(LPQUOTA,"/afs/eos.ncsu.edu/users/s/spkorb/ECE481/lpquote -P100lez $loginID|"); #look at quota
      $i = 0;
      $j = 0;
  @limitstuff = ;
         $quotadata = @limitstuff[2] ;
         $usage = substr($quotadata,8,7);
         $limitstart = index($quotadata,"Limit:");
         $limit = substr($quotadata,$limitstart + 8,8);
  $quotaneeded = $usage-$limit;
  close(LPQUOTA);
  if (($quotaneeded - 5.0) >  0) {   #we want to increase quota, but we don't want to turn on printing
  open(LPQUOTA,"/afs/eos.ncsu.edu/users/s/spkorb/ECE481/lpquote -f0 -P100lez -i 500 $loginID|");
  }else{                             #we want to increase quota, and we want to turn on printing
  open(LPQUOTA,"/afs/eos.ncsu.edu/users/s/spkorb/ECE481/lpquote -f1 -P100lez -i 500 $loginID|");
  }
  close(LPQUOTA);
  $quotaneeded = $quotaneeded - 5.0; #quota is increased $5.00
}
  return($quotaneeded);

}


sub givepawsdata{                  #process and send the data back to PAWS
  if (($quotaneeded eq "FBad ID    Q")||($quotaneeded eq "FBad CommandQ"))
  {#do nothing.  Send BOGUS to the serial port
  }else{                             #assemble output string
    if ($quotaneeded <= 0) {$printing = "on";}else{$printing = "off";}
    if (($quotaneeded > 0)){
       if($quotaneeded < 10.0){
	 $quotaneededstr = &roundup($quotaneeded);
	 $quotaneeded = "P-".$quotaneededstr."  ".$printing." Q";
       }else{
	 $quotaneededstr = &roundup($quotaneeded);
	 $quotaneeded = "P-".$quotaneededstr." ".$printing." Q";
      }
    }
    else{#quotaneeded is less than zero
      if($quotaneeded > -10.0){
        $quotaneededstr = &roundup($quotaneeded);
        $quotaneeded ="P+".$quotaneededstr."  ".$printing." Q";
      }else{
        $quotaneededstr = &roundup($quotaneeded);
        $quotaneeded = "P+".$quotaneededstr." ".$printing." Q";
      }
    }
  }

  #Squirt it out the serial port
  $infile = "\/dev\/tty00";
  $i = 10;
  #Byte is of the form:
  #-20.00 off          
  #if printing is turned off and
#+5.00  on
  #if printing is turned on
  ###########################
  open(SEROUT,">$infile");
  syswrite(SEROUT,$quotaneeded,14);
  print LOG $quotaneeded," is the line given to PAWS\n";
  close(SEROUT);
}

sub roundup{
  if ($quotaneeded < 0.0){$quotaneeded = $quotaneeded * -1.0;}#abs value
  if ($quotaneeded !~ /[\.]+/){$quotaneeded = $quotaneeded."\.00";}
  else{
  @splitstr = split(/[\.]+/, $quotaneeded);
  @splitstr[1] = substr(@splitstr[1],0,3);
  if (substr(@splitstr[1],2,1) >= 5.0){
  @splitstr[1] = @splitstr[1] + 5.0;
  if (@splitstr[1] > 999.0) {@splitstr[0]++;}
  }
  @splitstr[1] = substr(@splitstr[1],0,2);
  
  $quotaneeded = @splitstr[0]."\.".@splitstr[1];
  }
return($quotaneeded);
}


#!/usr/local/bin/perl
#settty program.
#This program  conditions the serial line once a second such that communication
#with PAWS is possible.
$infile = "\/dev\/tty00";

while()
{
#open(OUT,">>$outfile");
#print OUT "sleeping";
system("stty raw 9600 > $infile"); #Raw data at 9600 baud
system("stty -evenp > $infile");   #8 bits, no parity
system("stty -cstopb > $infile");  #1 stop bit
system("stty echo > $infile");     #echo on
sleep(1);
#close(OUT);
}