
#include "bacula_template.inc" title="Variable Expansion" num=5.5

<h1>Variable Expansion</h1>
Variable expansion is somewhat similar to Unix shell variable 
expansion.  Currently (version 1.31), it is used only in format
labels, but in the future, it will most likely be used in more
places.

<h2>General Functionality</h2>
This is basically a string expansion capability that permits 
referencing variables, indexing arrays, conditional replacement
of variables, case conversion, substring selection, regular expression
matching and replacement, character class replacement, padding strings,
repeated expansion in a user controlled loop, support of arithmetic
expressions in the loop start, step and end conditions, and
recursive expansion.
<p>
For example, <b>${HOME}</b> will be replaced by your home
directory as defined in the environment. If you have
defined the variable <b>xxx</b> to be <b>Test</b>, then
the reference <b>${xxx:p/7/Y/r}</b> will right pad the contents
of <b>xxx</b> to a length of seven characters filling with 
the character <b>Y</b> giving <b>YYYTest</b>.

<h2>Bacula Variables</h2>
Within Bacula, there are three main classes of variables with
some minor variations within the classes.
The classes are:
<dl>
  <dt>Counters</dt><dd>Counters are defined by the <b>Counter</b>
     resources in the Director's conf file. The counter can either
     be a temporary counter that lasts for the duration of Bacula's
     execution, or it can be a variable that is stored in
     the catalog, and thus retains its value from one Bacula execution
     to another. Counter variables may be incremented by postfixing
     a plus sign (<b>+</b> after the variable name). </dd>

  <dt>Internal Variables</dt><dd>Internal variables are read-only,
     and may be related to the current job (i.e. Job name), or
     may be special variables such as the date and time.
     The following variables are available:
     <p>
     <ul>
       <li><b>Year</b>      -- the full year</li>
       <li><b>Month</b>     -- the current month 1-12</li>
       <li><b>Day</b>       -- the day of the month 1-31</li>
       <li><b>Hour</b>      -- the hour 0-24</li>
       <li><b>Minute</b>    -- the current minute 0-59</li>
       <li><b>Second</b>    -- the current second 0-59</li>
       <li><b>WeekDay</b>   -- the current day of the week 0-6 with 0 being Sunday</li>
       <li><b>Job</b>       -- the job name</li>
       <li><b>Dir</b>       -- the Director's name</li>
       <li><b>Level</b>     -- the Job Level </li>
       <li><b>Type</b>      -- the Job type </li>
       <li><b>JobId</b>     -- the JobId </li>
       <li><b>JobName</b>   -- the unique job name composed of Job and date</li>
       <li><b>Storage</b>   -- the Storage daemon's name</li>
       <li><b>Client</b>    -- the Client's name</li>
       <li><b>NumVols</b>   -- the current number of Volumes in the Pool</li>
       <li><b>Pool</b>      -- the Pool name</li>
       <li><b>Catalog</b>   -- the Catalog name</li>
       <li><b>MediaType</b> -- the Media Type</li>
     </ul>
</dd>

  <dt>Environment Variables</dt><dd>Environment variables are read-only,
     and must be defined in the environment prior to executing Bacula.
     Environment variables may be either scalar or an array, where the
     elements of the array are referenced by subscripting the variable
     name (e.g. <b>${Months[3]}</b>). Environment variable arrays are
     defined by separating the elements with a vertical bar (<b>|</b>),
     thus <b>set Months=&quot;Jan|Feb|Mar|Apr|...&quot;</b> defines
     an environment variable named <b>Month</b> that will be
     treated as an array, and the reference <b>${Months[3]}</b> will
     yield <b>Mar</b>. The elements of the array can have
     differing lengths.</dd>
</dl>
   
<h2>Full Syntax</h2>
Since the syntax is quite extensive, below, you will find the
pseudo BNF. The special characters have the following meaning:
<pre>
 ::=     definition
 ( )     grouping if the parens are not quoted
 |       separates alternatives
 '/'     literal / (or any other character)
 CAPS    a character or character sequence
 *       preceding item can be repeated zero or more times
 ?       preceding item can appear zero or one time
 +       preceding item must appear one or more times
</pre>

<p>
And the pseudo BNF describing the syntax is:

<pre>
 input       ::= ( TEXT
                 | variable 
                 | INDEX_OPEN input INDEX_CLOSE (loop_limits)?
                 )*

 variable    ::= DELIM_INIT (name|expression)

 name        ::= (NAME_CHARS)+

 expression  ::= DELIM_OPEN 
                 (name|variable)+
                 (INDEX_OPEN num_exp INDEX_CLOSE)? 
                 (':' command)*
                 DELIM_CLOSE

 command     ::= '-' (TEXT_EXP|variable)+
               | '+' (TEXT_EXP|variable)+
               | 'o' NUMBER ('-'|',') (NUMBER)?
               | '#'
               | '*' (TEXT_EXP|variable)+
               | 's' '/' (TEXT_PATTERN)+ 
                     '/' (variable|TEXT_SUBST)* 
                     '/' ('m'|'g'|'i'|'t')*
               | 'y' '/' (variable|TEXT_SUBST)+ 
                     '/' (variable|TEXT_SUBST)* 
                     '/'
               | 'p' '/' NUMBER 
                     '/' (variable|TEXT_SUBST)* 
                     '/' ('r'|'l'|'c')
               | '%' (name|variable)+ 
                     ('(' (TEXT_ARGS)? ')')?
               | 'l'
               | 'u'

 num_exp     ::= operand
               | operand ('+'|'-'|'*'|'/'|'%') num_exp

 operand     ::= ('+'|'-')? NUMBER
               | INDEX_MARK
               | '(' num_exp ')'
               | variable

 loop_limits ::= DELIM_OPEN 
                 (num_exp)? ',' (num_exp)? (',' (num_exp)?)? 
                 DELIM_CLOSE

 NUMBER      ::= ('0'|...|'9')+

 TEXT_PATTERN::= (^('/'))+
 TEXT_SUBST  ::= (^(DELIM_INIT|'/'))+
 TEXT_ARGS   ::= (^(DELIM_INIT|')'))+
 TEXT_EXP    ::= (^(DELIM_INIT|DELIM_CLOSE|':'|'+'))+
 TEXT        ::= (^(DELIM_INIT|INDEX_OPEN|INDEX_CLOSE))+

 DELIM_INIT  ::= '$'                    
 DELIM_OPEN  ::= '{'   
 DELIM_CLOSE ::= '}' 
 INDEX_OPEN  ::= '['    
 INDEX_CLOSE ::= ']'
 INDEX_MARK  ::= '#'
 NAME_CHARS  ::= 'a'|...|'z'|'A'|...|'Z'|'0'|...|'9'
</pre>

<h2>Semantics</h2>  
The items listed in <b>command</b> above, which always follow
a colon (<b>:</b>) have the following meanings:
<pre>
 -    perform substitution if variable is empty
 +    perform substitution if variable is not empty
 o    cut out substring of the variable value
 \#    length of the variable value
 *    substitute empty string if the variable value is not empty,
      otherwise substitute the trailing parameter
 s    regular expression search and replace. The trailing
      options are: m = multiline, i = case insensitive,
                   g = global,    t = plain text (no regexp)
 y    transpose characters from class A to class B
 p    pad variable to l = left, r = right or c = center,
      with second value.
 %    special function call (none implemented)
 l    lower case the variable value
 u    upper case the variable value
</pre>
<p>
The <b>loop_limits</b> are start, step, and end values.
<p>
A counter variable name followed immediately by a plus (<b>+</b>)
will cause the counter to be incremented by one.

<h2>Examples</h2>
To create an ISO date:
<pre>
  DLT-${Year}-${Month:p/2/0/r}-${Day:p/2/0/r}
</pre>
on 20 June 2003 would give <b>DLT-2003-06-20</b>
<p>
If you set the environment variable <b>mon</b> to
<b>January|February|March|April|May|...</b>
<pre>
   File-${mon[${Month}]}/${Day}/${Year}
</pre>
on the first of March would give <b>File-March/1/2003</b>

<page_prev name="Console Configuration" url="consoleconf.html">
<page_next name="Running Bacula" url="running.html">
