From f88cd790dee58a4c35df5fba80e1f558bf75cf6a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 1 Jul 2010 10:32:27 +0200 Subject: [PATCH] new functions @eq, @ne, @not --- NEWS | 1 + doc/ferm.pod | 14 ++++++++++++++ src/ferm | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/NEWS b/NEWS index af21582..a9ee16d 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,7 @@ v2.0.8 - not yet released - added automatic variable $DIRNAME - implement confirmation/rollback for --shell --interactive - added the "type" parameter to @resolve() + - new functions @eq, @ne, @not v2.0.7 - 2 Jan 2010 diff --git a/doc/ferm.pod b/doc/ferm.pod index b9ec488..9660a96 100644 --- a/doc/ferm.pod +++ b/doc/ferm.pod @@ -1603,6 +1603,20 @@ hooks. There are several built-in functions which you might find useful. +=head2 @eq(a,b) + +Tests two values for equality. Example: + + @if @eq($DOMAIN, ip6) DROP; + +=head2 @ne(a,b) + +Similar to @eq, this tests for non-equality. + +=head2 @not(x) + +Negates a boolean value. + =head2 @resolve((hostname1 hostname2 ...), [type]) Usually, host names are resolved by iptables. To let ferm resolve diff --git a/src/ferm b/src/ferm index 26752e5..5af275f 100755 --- a/src/ferm +++ b/src/ferm @@ -943,6 +943,10 @@ sub negate_value($$;$) { return bless [ $value ], $class || 'negated'; } +sub format_bool($) { + return $_[0] ? 1 : 0; +} + sub resolve($\@$) { my ($resolver, $names, $type) = @_; @@ -1090,6 +1094,18 @@ sub getvalues { my $resolver = new Net::DNS::Resolver; my @params = to_array($params[0]); return [resolve($resolver, @params, $type)]; + } elsif ($token eq '@eq') { + my @params = get_function_params(); + error('Usage: @eq(a, b)') unless @params == 2; + return format_bool($params[0] eq $params[1]); + } elsif ($token eq '@ne') { + my @params = get_function_params(); + error('Usage: @ne(a, b)') unless @params == 2; + return format_bool($params[0] ne $params[1]); + } elsif ($token eq '@not') { + my @params = get_function_params(); + error('Usage: @eq(a, b)') unless @params == 1; + return format_bool(not $params[0]); } else { error("unknown ferm built-in function"); } -- 2.11.4.GIT