Hi!

I wrote something which does this for some internal applikation which           
                                                                                
                                                                       
uses FAI_CLASSES to decide which things to do.                                  
                                                                                
                                                                       
                                                                                
                                                                                
                                                                       
The tool read /etc/fai/FAI_CLASSES and checks where the expr specified          
                                                                                
                                                                       
as cmdline parameter matches for these classes.                                 
                                                                                
                                                                       
                                                                                
                                                                                
                                                                       
See the perl script attached.

Ciao
Max
-- 
        Follow the white penguin.
#!/usr/bin/perl -w
#
# Copyright 2007 Maximilian Wihelm <[EMAIL PROTECTED]>
#

use strict;

if (! @ARGV) {
	die "Usage: $0 class_expr\n";
}

my $expr = $ARGV[0];

# The set of control chars we know of
my $control_chars = '[[:space:]()!&|]';
my $control_chars_without_exclamation_mark = '[()&|]';

my $FAI_CLASSES = "/etc/fai/FAI_CLASSES";

my $debug = 0;

#
# Keep your fingers of of this...

# Hashref to store class names
my $class = {};

# Scalar to store the logical expression
my $check_expr = "";

#
# Read CLASSES of this host and initialize them as '1' (set)
open (LIST, "<", $FAI_CLASSES)
	or die "Could not open list file\n";
while (my $class_name = <LIST>) {
	# Strip CR/LF
	chomp $class_name;

	print STDERR "Setting \$class->{$class_name} = 1\n" if ($debug);

	$class->{$class_name} = 1;
}
close (LIST);

#
# Compatibility glue
if (! grep { /$control_chars_without_exclamation_mark/  } $expr) {
	$expr =~ s/[[:space:]]+/ & /g;
}


#
# Read CALSSES used in expression and initialize them as '0' (unset) IFF
# not initialized before!
foreach my $word (split /$control_chars/, $expr) {
	next if ($word eq "");

	if (!  $class->{$word}) {
		print STDERR "Setting \$$word = 0\n" if ($debug);

		$class->{$word} = 0;
	}
}

#
# Build up a logical expression which can be eval'uated
my $word_start = 1;
my $reading_word = 0;
foreach my $char (split ('', $expr)) {
	if ($char =~ m/$control_chars/) {
		if ($reading_word) {
			$check_expr .= "}";
			$reading_word = 0;
		}

		$check_expr .= $char;
		$word_start = 1;
	} else {
		if ($word_start) {
			$check_expr .= "\$class->{$char";
			$word_start = 0;
			$reading_word = 1;
		} else {
			$check_expr .= $char;
		}
	}
}
if ($reading_word) {
	$check_expr .= "}";
}

print STDERR "check_expr = \"$check_expr\"\n" if($debug);

#
# Evaluate expression and decide fate.
if (eval $check_expr) {
#	print "Jo man\n";
	exit 0;
} else {
#	print "Noe.\n";
	exit 1;
}

Reply via email to