File: ifclass

package info (click to toggle)
fai 6.5.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,084 kB
  • sloc: sh: 6,774; perl: 5,665; makefile: 138
file content (76 lines) | stat: -rwxr-xr-x 1,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /usr/bin/perl

# evaluate boolean expression of classes
# (c) Thomas Lange 2025

use strict;
use warnings;
use Eval::Logic;
use utf8;

sub usage {

  print << 'EOM';
ifclass, evaluate boolean expression of FAI classes

   Copyright (C) 2025 by Thomas Lange

Usage: ifclass 'EXPRESSION'
Usage: ifclass [-a|-o] CLASS1 [CLASSN]...

   -o         Evaluate a logical OR  of the given classes
   -a         Evaluate a logical AND of the given classes

Either an expression is given and will be evaluated or a list of classes
is combined with the OR or AND operator.
The list of defined FAI classes must be stored in the variable $classes.

Examples:

$ classes="DEMO AMD64 FAIBASE UBUNTU MINT"
$ ifclass 'UBUNTU && ! MINT'
$ ifclass 'DEMO || MINT'
$ ifclass 'AMD64 && ( ROCKY || ALMA )'

Note: You need to `export classes` in a login shell.

EOM
  exit 0;
}

&usage unless defined $ARGV[0];
&usage if ($ARGV[0] eq '-h');

warn "Variable \$classes is undefined \n" unless defined $ENV{classes};

# support old behaviour of ifclass -o and ifclass -a
  if ($ARGV[0] eq '-o') {
    # join all classes with logical OR
    shift;
    my $tmp = join(' || ',@ARGV);
    undef @ARGV;
    $ARGV[0] = $tmp;
  }
  if ($ARGV[0] eq '-a') {
    # join all classes with logical AND
    shift;
    my $tmp = join(' && ',@ARGV);
    undef @ARGV;
    $ARGV[0] = $tmp;
  }

if (defined $ARGV[2]) {
  die "Only two parameters are allowed. You may quote your boolean expression.\n";
}

# read the list of classes
my @classes = grep { s/-/Ö/g; !/^#|^\s*$/ } split(/[\s\n]+/,$ENV{classes});
my @clvar =  map { $_,1 } @classes; # make a list of defined variables for subroutine call

my $inp = shift;
$inp =~ s/-/Ö/g;

my $expr = Eval::Logic->new("$inp");
$expr->undef_default(0); # set default value for undefined classes
my $e = $expr->evaluate(@clvar);
exit ! $e;