home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / SATAN11.ZIP / PERL / INFER_FA.PL < prev    next >
Text File  |  1995-04-11  |  2KB  |  80 lines

  1. #
  2. # Usage: &infer_facts();
  3. # Apply the rules in $infer_fact_files to the global $target..$text variables
  4. # (and to any other globals that happen to be available) and generate new
  5. # facts.
  6. # Standalone usage: perl infer_fact.pl [satan_record_files...]
  7.  
  8. $infer_fact_files = "rules/facts";
  9.  
  10. sub build_infer_facts {
  11.     local($files) = @_;
  12.     local($code, $cond, $fact);
  13.  
  14.     $code = "sub infer_facts {\n";
  15.  
  16.     foreach $file (split(/\s+/, $files)) {
  17.     open(RULES, $file) || die "cannot open $file: $!";
  18.     while (<RULES>) {
  19.         chop;
  20.         while (/\\$/) {
  21.         chop;
  22.         $_ .= <RULES>;
  23.         chop;
  24.         }
  25.         s/#.*$//;
  26.         s/\s+$//;
  27.         next if /^$/;
  28.         s/@/\\@/g;
  29.         ($cond, $fact) = split(/\t+/, $_, 2);
  30.         $code .= "\tif ($cond) {\n\t\t&add_fact(\"$fact\");\n\t}\n";
  31.     }
  32.     close(RULES);
  33.     }
  34.  
  35.     $code .= "}\n";
  36.  
  37.     return $code;
  38. }
  39.  
  40. #
  41. # Some scaffolding for stand-alone operation
  42. #
  43. if ($running_under_satan) {
  44.     eval &build_infer_facts($infer_fact_files);
  45.     die "error in $infer_fact_files: $@" if $@;
  46. } else {
  47.     $running_under_satan = -1;
  48.  
  49.     require 'perl/misc.pl';
  50.  
  51.     eval "sub add_fact { local(\$fact) = \@_; print \"Add-fact: \$fact\n\"; }";
  52.     die "error in $infer_fact_files: $@" if $@;
  53.  
  54.     #
  55.     # Build satan rules and include them into the running code.
  56.     #
  57.     $code = &build_infer_facts($infer_fact_files);
  58.     print "Code generated from $infer_fact_files:\n\n";
  59.     print $code;
  60.     eval $code;
  61.  
  62.     #
  63.     # Apply rules.
  64.     #
  65.     print "\nApplying rules to all SATAN records...\n";
  66.     while (<>) {
  67.     chop;
  68.     if (&satan_split($_)) {
  69.         warn "Ill-formed fact: $_\n";
  70.     } else {
  71.         &infer_facts();
  72.     }
  73.     }
  74. }
  75.  
  76. 1;
  77.