Next: Loops, Previous: Statements, Up: MFL [Contents][Index]
Conditional expressions, or conditionals for short, test some conditions and alter the control flow depending on the result. There are two kinds of conditional statements: if-else branches and switch statements.
The syntax of an if-else branching construct is:
if condition then-body [else else-body] fi
Here, condition is an expression that governs control flow
within the statement. Both then-body and else-body are
lists of mailfromd
statements. If condition is
true, then-body is executed, if it is false, else-body is
executed. The ‘else’ part of the statement is optional. The
condition is considered false if it evaluates to zero, otherwise it is
considered true. For example:
if $f = "" accept else reject fi
This will accept the message if the value of the Sendmail
macro $f
is an empty string, and reject it otherwise. Both
then-body and else-body can be compound statements
including other if
statements. Nesting level of
conditional statements is not limited.
To facilitate writing complex conditional statements, the elif
keyword can be used to introduce alternative conditions, for example:
if $f = "" accept elif $f = "root" echo "Mail from root!" else reject fi
Another type of branching instruction is switch
statement:
switch condition do case x1 [or x2 …]: stmt1 case y1 [or y2 …]: stmt2 . . . [default: stmt] done
Here, x1, x2, y1, y2 are literal expressions;
stmt1, stmt2 and stmt are arbitrary
mailfromd
statements (possibly compound); condition is
the controlling expression. The vertical dotted row represent another
eventual ‘case’ branches.
This statement is executed as follows: the condition
expression is evaluated and if its value equals x1 or x2
(or any other x from the first case
), then
stmt1 is executed. Otherwise, if condition evaluates
to y1 or y2 (or any other y from the second
case
), then stmt2 is executed. Other case
branches are tried in turn. If none of them matches, stmt
(called the default branch) is executed.
There can be as many case
branches as you wish. The
default
branch is optional. There can be at most one
default
branch.
An example of switch
statement follows:
switch x do case 1 or 3: add "X-Branch" "1" accept case 2 or 4 or 6: add "X-Branch" "2" default: reject done
If the value of mailfromd
variable x
is 2 or 3,
it will accept the message immediately, and add a ‘X-Branch: 1’
header to it. If x
equals 2 or 4 or 6, this code will add
‘X-Branch: 2’ header to the message and will continue processing
it. Otherwise, it will reject the message.
The controlling condition of a switch
statement may evaluate
to numeric or string type. The type of the condition governs the
type of comparisons used in case
branches: for numeric types,
numeric equality will be used, whereas for string types, string
equality is used.
Next: Loops, Previous: Statements, Up: MFL [Contents][Index]