From cce605ccb956efdfa7eb0ef59a625bc5a65232e7 Mon Sep 17 00:00:00 2001 From: rudolfkoenig Date: Sun, 20 Oct 2013 11:18:52 +0000 Subject: [PATCH] new module: cmdalias git-svn-id: https://svn.fhem.de/fhem/trunk@4075 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 1 + fhem/FHEM/98_cmdalias.pm | 134 +++++++++++++++++++++++++++++ fhem/docs/commandref_frame.html | 1 + fhem/docs/commandref_frame_DE.html | 1 + 4 files changed, 137 insertions(+) create mode 100644 fhem/FHEM/98_cmdalias.pm diff --git a/fhem/CHANGED b/fhem/CHANGED index 7985fc22f..829bba8dc 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,6 +1,7 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. - SVN + - feature: cmdalias added - feature: rain Module by baumrasen - feature: endPlotNow attribute for SVG-Plots (ChrisD) - feature: new module 51_BBB_BMP180.pm added (betateilchen) diff --git a/fhem/FHEM/98_cmdalias.pm b/fhem/FHEM/98_cmdalias.pm new file mode 100644 index 000000000..b9ec46607 --- /dev/null +++ b/fhem/FHEM/98_cmdalias.pm @@ -0,0 +1,134 @@ +############################################## +# $Id: 98_cmdalias.pm 3738 2013-08-18 14:13:59Z rudolfkoenig $ +# Avarage computing + +package main; +use strict; +use warnings; + +my %cmdalias; + +########################## +sub +cmdalias_Initialize($) +{ + my ($hash) = @_; + $hash->{DefFn} = "cmdalias_Define"; + $hash->{UndefFn} = "cmdalias_Undefine"; + $hash->{AttrList} = "disable:0,1"; +} + + +########################## +sub +cmdalias_Define($$$) +{ + my ($hash, $def) = @_; + + if($def !~ m/^([^ ]*) cmdalias ([^ ]*)(.*) AS (.*)$/) { + my $msg = + "wrong syntax: define cmdalias [parameter] AS command..."; + return $msg; + } + my ($name, $alias, $param, $newcmd) = ($1, $2, $3, $4); + $param =~ s/^ *//; + # Checking for misleading regexps + eval { "Hallo" =~ m/^$param$/ }; + return "$name: Bad regexp in $param: $@" if($@); + $hash->{ALIAS} = lc($alias); + $hash->{PARAM} = $param; + $hash->{NEWCMD} = $newcmd; + $hash->{STATE} = "defined"; + + $cmdalias{$alias}{Alias}{$name} = $hash; + $cmdalias{$alias}{OrigFn} = $cmds{$alias}{Fn} + if($cmds{$alias} && $cmds{$alias}{Fn} ne "CommandCmdAlias"); + $cmds{$alias}{Fn} = "CommandCmdAlias"; + + return undef; +} + +sub +cmdalias_Undefine($$) +{ + my ($hash, $arg) = @_; + + my $alias = $hash->{ALIAS}; + delete $cmdalias{$alias}{Alias}{$hash->{NAME}}; + + if(! keys %{$cmdalias{$alias}{Alias}}) { + if($cmdalias{$alias}{OrigFn}) { + $cmds{$alias}{Fn} = $cmdalias{$alias}{OrigFn}; + } else { + delete($cmds{$alias}); + } + delete($cmdalias{$alias}); + } + + return undef; +} + +sub +CommandCmdAlias($$$) +{ + my ($cl, $param, $alias) = @_; + my $a = $cmdalias{lc($alias)}; + return "Unknown command $a, internal error" if(!$a); + foreach my $n (sort keys %{$a->{Alias}}) { + my $h = $a->{Alias}{$n}; + next if($h->{InExec}); + if($param =~ m/$h->{PARAM}/) { + my %specials= ("%EVENT" => $param); + my $exec = EvalSpecials($h->{NEWCMD}, %specials); + $h->{InExec} = 1; + my $r = AnalyzeCommandChain(undef, $exec); + delete $h->{InExec}; + return $r; + } + } + no strict "refs"; + return &{$a->{OrigFn} }($cl, $param, $alias); + use strict "refs"; +} + +1; + +=pod +=begin html + + +

cmdalias

+
    + create new commands or replace internal ones. +
    + + + Define +
      + define <name> cmdalias <cmd> [parameter] + AS newcommand..."
      +
      +
        + parameter is optional and is a regexp which must match the command + entered. + If it matches, then the specified newcommand will be executed, which is + a fhem command (see Fhem command types for + details). Like in the notify commands, $EVENT or + $EVTPART may be used, in this case representing the command arguments as + whole or the unique words entered.
        + Notes:
          +
        • newcommand may contain cmd, but recursion is not allowed.
        • +
        • if there are multiple definitions, they are checked/executed in + alphabetically sorted name oder.
        • +
        + Examples: +
          + define s1 cmdalias shutdown update AS save;;shutdown
          + define s2 cmdalias set lamp .* AS { Log 1, "$EVENT";; fhem("set $EVENT") } +
        +
      +
    +
+ +=end html +=cut diff --git a/fhem/docs/commandref_frame.html b/fhem/docs/commandref_frame.html index bf697c67f..95833fbdb 100644 --- a/fhem/docs/commandref_frame.html +++ b/fhem/docs/commandref_frame.html @@ -36,6 +36,7 @@ attr   backup   CULflash   + cmdalias   createlog   define   delete   diff --git a/fhem/docs/commandref_frame_DE.html b/fhem/docs/commandref_frame_DE.html index 2fb035d3e..c6d13cb20 100644 --- a/fhem/docs/commandref_frame_DE.html +++ b/fhem/docs/commandref_frame_DE.html @@ -36,6 +36,7 @@ attr   backup   CULflash   + cmdalias   createlog   define   delete