coLinux日記

coLinuxはフリーソフトを種として、よろずのシステムとぞなれりける。

sendmail.cf ルールセット呼び出しと arith

2007-04-26 22:31:24 | sendmail
sendmail.cf でルールセットからルールセットの呼び出しに挑戦してみます。

sendmail.mc で以下の行を追加します。

LOCAL_RULESETS
Slocal_test=9
# Test of R command
R$-+$-          $: $>PlusNum <$1><$2>
R$*             $@ RESULT : $1
SPlusNum
R<1><4>         $@ 5
R<2><4>         $@ 6
R<3><4>         $@ 7
R<4><4>         $@ 8
R<5><4>         $@ 9
R$*             $@ ERROR
#

早速テストしてみます。
$ ./sendmail -bt -C./sendmail.cf
.............
> 9 3+4
local_test         input: 3 + 4
PlusNum            input: < 3 > < 4 >
PlusNum          returns: 7
local_test       returns: RESULT : 7
> 9 4+4
local_test         input: 4 + 4
PlusNum            input: < 4 > < 4 >
PlusNum          returns: 8
local_test       returns: RESULT : 8
> 9 4+6
local_test         input: 4 + 6
PlusNum            input: < 4 > < 6 >
PlusNum          returns: ERROR
local_test       returns: RESULT : ERROR
> /quit
$

うまく変換されていますね。実際に計算が必要な場合は、Kコマンドでタイプ arith を使えばできます。

通常 sendmail.mc で作成されたものは、
Karith arith

が含まれています。これを使うには、sendmail.mc に以下の行を指定します。
Slocal_test=9
# Test of R command
R$+ $+ $+     $: $(arith $2 $@ $1 $@ $3 $: EXCEPTION $)
R$*             $@ RESULT : $1
#

sendmail.cf を作成し確かめてみます。
$ ./sendmail -bt -C ./sendmail.cf
..............
> 9 25+28
local_test         input: 25 + 28
local_test       returns: RESULT : 53
> 9 12*3
local_test         input: 12*3
local_test       returns: RESULT : 12*3
> 9 12 * 3
local_test         input: 12 * 3
local_test       returns: RESULT : 36
> 9 25 + 28
local_test         input: 25 + 28
local_test       returns: RESULT : 53
> 9 12 * 3
local_test         input: 12 * 3
local_test       returns: RESULT : 36
> 9 -12 / 3
local_test         input: -12 / 3
local_test       returns: RESULT : -4
> /quit
$

これは便利。マイナスも使えますね。それ以外に比較演算子みたいなのもあるので、IF文もどきのようなこともできるようです。

Kコマンドは、データベース・マップコマンドですから、/etc/passwd のようなテキスト型のものなど文字列の変換に色々利用できます。指定方法は、

Kname type switches file_or_map

で、定義された name は Rコマンドのrhs だけで使用できます。ちなみにクラス・マクロは Rコマンドの lhs だけでしか使用できず、定義マクロは lhs と rhs の両方で使用できます。

参照は、以下のように、$( と $) を使うそうです。

$(name key $: default $)
や
$(name key $@ text1 $@ text2 $: default $)

実際にテストする場合は、直接 sendmail.cf を修正すればよいのですが、今回はわざと sendmail.mc を使ってみました。だいぶテストも慣れてきましたので、メール転送関連の設定も調べてみたいと思います。
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする