Parser module is based on a scripting language T-Script which primary purpose is to generate text files. It can be useful for processing templates with some additional data retrieved from data sources like SQL databases or text files. In lmsd's module contents of scripts are stored in database, so they can be edited via LMS-UI. In the future parser should replace almost all lmsd modules.
Before compilation ensure that you have in your system packages bison (at least 1.875 version) and flex.
Parser has following options:
script
Contents of script. Default: empty.
Example: script = '{var=1}variable var={var}'
file
Location of output file. Default: empty.
Example: file = /tmp/parser.out
command
Shell command to execute after script compilation. Default: empty
Example: command = "sh /tmp/parser.out"
T-Script language syntax is based on other popular languages like C or JavaScript with little changes made to make writting templates simpler. Additional commands should be written inside { } parenthesis.
Supported expressions:
Literal inside quotes
"some literal"
Number
1234
Value of variable "var"
var
N-th element of array "var"
var[n]
Subvariable "n" of variable "var"
var.n
Value of expression inside parenthesis
( <expression> )
1 if value of expression is false, otherwise 0
! <expression>
Evaluates to 1 if expression1 value equals expression2 value or 0 if not
<expression1> == <expression2>
Evaluates to 1 if expression1 value doesn't equal expression2 value or 0 if it does
<expression1> != <expression2>
Evaluates to 1 if expression1 value is less than expression2 value
<expression1> < <expression2>
Evaluates to 1 if expression1 value is greater than expression2 value
<expression1> > <expression2>
Evaluates to 1 if expression1 value or expression2 value are true
<expression1> || <expression2>
Evaluates to 1 if expression1 value and expression2 value are true
<expression1> && <expression2>
When both expression values have numeric type evaluates to result of arithmetic operation. In other situation treats expressions as strings and performs string concatenation
<expression1> + <expression2>
Evaluates to result of arithmetic operation on two expression values
<expression1> - <expression2> <expression1> * <expression2> <expression1> / <expression2> <expression1> % <expression2>
Basic commands:
Assigns expression value to specified variable
<variable_name> = <expression>
Executes command only if expression is true. Second form executes command1 if expression is true or command2 if expression is false
if ( <expression> ) <commands> /if if ( <expression> ) <commands1> else <commands2> /if
Text between command blocks is treated as a command so the following example is correct:
Some text {if (a==1)} a equals 1 {else} a doesn't equal 1 {/if}You can put backslash between command block and end of line to eat \n symbol and keep normal text flow. For example:
Some text {if (a==1)}\ a equals 1 {else}\ a doesn't equal 1 {/if}\
Executes first command1 as loop initialization command. Then executes commands and command2 while expression is true
for ( <command1> ; <expression> ; <command2> ) <commands> /for
Basic functions:
Converts numeric variable to string value
string(<variable_name>)
Converts string variable to numeric value
number(<variable_name>)
Extensions:
SQL commands (case insensitive)
SELECT <the_rest_of_query> INSERT <the_rest_of_query> DELETE <the_rest_of_query> UPDATE <the_rest_of_query> CREATE <the_rest_of_query> DROP <the_rest_of_query>
SQL result rows count (case insensitive)
ROWS <sql_query>
Shell command executing
exec <command>
Let's begin from simple script creating file /etc/hosts with list of computers (and devices) IPs and names list.