Next: , Previous: , Up: Tutorial   [Contents][Index]


3.17 Run Mode

Mailfromd provides a special option that allows to run arbitrary MFL scripts.

When given the --run command line option, mailfromd loads the script given in its command line, looks for the function called ‘main’, and runs it.

This function must be declared as:

func main(...) returns number

Mailfromd passes all command line arguments that follow the script name as arguments to that function. When the function returns, its return value is used by mailfromd as exit code.

As an example, suppose the file script.mfl contains the following:

func main (...)
  returns number
do
  loop for number i 1,
       while i <= $#,
       set i i + 1
  do
    echo "arg %i=" . $(i)
  done
done

This function prints all its arguments (See variadic functions, for a detailed description of functions with variable number of arguments). Now running:

$ mailfromd --run script.mfl 1 file dest

displays the following:

arg 1=1
arg 2=file
arg 3=dest

You can direct the script output to the standard output by using the --echo, as described above, e.g.:

$ mailfromd --echo --run script.mfl 1 file dest

Note, that MFL does not have a direct equivalent of shell’s $0 argument. If your function needs to know the name of the script that is being executed, use __file__ built-in constant instead (see __file__).

The name main is not hard-coded. You can use the --run option to run any function, provided that its definition is as discussed above. Just give the name of this function as the argument to the option. This argument is optional, therefore it must be separated from the option by an equals sign (with no whitespace from either side). For example, given the command line below, mailfromd will load the file script.mfl and execute the function ‘start’:

$ mailfromd --run=start script.mfl

If you need to define sendmail macros (see Sendmail Macros) for use in the run mode, place the macro=value assignments before the script name, e.g.:

$ mailfromd --run=start i=feedbeef client_addr=::1 script.mfl

To summarize, the command line when using the run mode is:

mailfromd [options] --run [macro=value] file args...

Finally, notice that file together with args... can be omitted. In this case the default script file will be used (see default script file).

The ‘macro=value’ assignments define Sendmail macros, args... are passed as arguments to the main function defined in file, and option stands for any other mailfromd options that might be needed.


Next: , Previous: , Up: Tutorial   [Contents][Index]