| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# evaluate boolean expression of classes |
| 4 |
# (c) Thomas Lange 2025 |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
use Eval::Logic; |
| 9 |
use utf8; |
| 10 |
|
| 11 |
sub usage { |
| 12 |
|
| 13 |
print << 'EOM'; |
| 14 |
ifclass, evaluate boolean expression of FAI classes |
| 15 |
|
| 16 |
Copyright (C) 2025 by Thomas Lange |
| 17 |
|
| 18 |
Usage: ifclass 'EXPRESSION' |
| 19 |
Usage: ifclass [-a|-o] CLASS1 [CLASSN]... |
| 20 |
|
| 21 |
-o Evaluate a logical OR of the given classes |
| 22 |
-a Evaluate a logical AND of the given classes |
| 23 |
|
| 24 |
Either an expression is given and will be evaluated or a list of classes |
| 25 |
is combined with the OR or AND operator. |
| 26 |
The list of defined FAI classes must be stored in the variable $classes. |
| 27 |
|
| 28 |
Examples: |
| 29 |
|
| 30 |
$ classes="DEMO AMD64 FAIBASE UBUNTU MINT" |
| 31 |
$ ifclass 'UBUNTU && ! MINT' |
| 32 |
$ ifclass 'DEMO || MINT' |
| 33 |
$ ifclass 'AMD64 && ( ROCKY || ALMA )' |
| 34 |
|
| 35 |
Note: You need to `export classes` in a login shell. |
| 36 |
|
| 37 |
EOM |
| 38 |
exit 0; |
| 39 |
} |
| 40 |
|
| 41 |
&usage unless defined $ARGV[0]; |
| 42 |
&usage if ($ARGV[0] eq '-h'); |
| 43 |
|
| 44 |
warn "Variable \$classes is undefined \n" unless defined $ENV{classes}; |
| 45 |
|
| 46 |
# support old behaviour of ifclass -o and ifclass -a |
| 47 |
if ($ARGV[0] eq '-o') { |
| 48 |
# join all classes with logical OR |
| 49 |
shift; |
| 50 |
my $tmp = join(' || ',@ARGV); |
| 51 |
undef @ARGV; |
| 52 |
$ARGV[0] = $tmp; |
| 53 |
} |
| 54 |
if ($ARGV[0] eq '-a') { |
| 55 |
# join all classes with logical AND |
| 56 |
shift; |
| 57 |
my $tmp = join(' && ',@ARGV); |
| 58 |
undef @ARGV; |
| 59 |
$ARGV[0] = $tmp; |
| 60 |
} |
| 61 |
|
| 62 |
if (defined $ARGV[2]) { |
| 63 |
die "Only two parameters are allowed. You may quote your boolean expression.\n"; |
| 64 |
} |
| 65 |
|
| 66 |
# read the list of classes |
| 67 |
my @classes = grep { s/-/Ö/g; !/^#|^\s*$/ } split(/[\s\n]+/,$ENV{classes}); |
| 68 |
my @clvar = map { $_,1 } @classes; # make a list of defined variables for subroutine call |
| 69 |
|
| 70 |
my $inp = shift; |
| 71 |
$inp =~ s/-/Ö/g; |
| 72 |
|
| 73 |
my $expr = Eval::Logic->new("$inp"); |
| 74 |
$expr->undef_default(0); # set default value for undefined classes |
| 75 |
my $e = $expr->evaluate(@clvar); |
| 76 |
exit ! $e; |