From 37a6ffc5669870cb7724110a19d7dd7b29752b4d Mon Sep 17 00:00:00 2001 From: rudolfkoenig <> Date: Wed, 17 Jun 2020 13:53:32 +0000 Subject: [PATCH] fhem.pl: add CheckRegexp function (Forum #112168) git-svn-id: https://svn.fhem.de/fhem/trunk@22189 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/10_MQTT2_DEVICE.pm | 11 +++++------ fhem/fhem.pl | 13 +++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/fhem/FHEM/10_MQTT2_DEVICE.pm b/fhem/FHEM/10_MQTT2_DEVICE.pm index a017c361e..13307dd3e 100644 --- a/fhem/FHEM/10_MQTT2_DEVICE.pm +++ b/fhem/FHEM/10_MQTT2_DEVICE.pm @@ -477,8 +477,8 @@ MQTT2_DEVICE_Attr($$) my ($par1, $par2) = split(" ", $el, 2); next if(!$par1); return "$dev attr $attrName: more parameters needed" if(!$par2); - eval { "Hallo" =~ m/^$par1$/ }; - return "$dev $attrName regexp error: $@" if($@); + my $errMsg = CheckRegexp($par1, "bridgeRegexp attribute for $dev"); + return $errMsg if($errMsg); } if($init_done) { my $name = $hash->{NAME}; @@ -588,10 +588,9 @@ MQTT2_DEVICE_addReading($$) my $cid = $defs{$name}{CID}; foreach my $line (split("\n", $param)) { my ($re,$code) = split(" ", $line,2); - return "Bad line >$line< for $name" if(!defined($re) || !defined($code)); - return "Bad regexp >$re< for $name: $@" if($re =~ m/^[*+]/); - eval { "Hallo" =~ m/^$re$/ }; - return "Bad regexp >$re< for $name: $@" if($@); + return "Bad line >$line< for $name" if(!defined($code)); + my $errMsg = CheckRegexp($re, "readingList attribute for $name"); + return $errMsg if($errMsg); if($cid && $re =~ m/^$cid:/) { $modules{MQTT2_DEVICE}{defptr}{"re:$cid"}{$re}{"$name,$code"} = 1; } else { diff --git a/fhem/fhem.pl b/fhem/fhem.pl index bb942d09e..336a00755 100755 --- a/fhem/fhem.pl +++ b/fhem/fhem.pl @@ -56,6 +56,7 @@ sub Authenticate($$); sub CallFn(@); sub CallInstanceFn(@); sub CheckDuplicate($$@); +sub CheckRegexp($$); sub Debug($); sub DoSet(@); sub Dispatch($$;$$); @@ -6117,4 +6118,16 @@ execFhemTestFile() InternalTimer(1, sub { require $fhemTestFile }, 0 ) if($fhemTestFile); } +# return undef if ok or error. Prameter: regexp, error context +sub +CheckRegexp($$) +{ + my ($re,$context) = @_; + return "Empty regexp in $context" if(!defined($re)); + return "Bad regexp >$re< in $context" if($re =~ m/^[*+]/); + eval { "Hallo" =~ m/^$re$/ }; + return "Bad regexp >$re< in $context: $@" if($@); + return undef; +} + 1;