This is automatically generated documentation. Edit after the "COMMENTS" heading; changes to the main body will be lost.
Script -- Click element; script a Click router configuration
Script(INSTRUCTIONS...)
Ports: normally none
Package: standard (core)
The Script element implements a simple scripting language useful for controlling Click configurations. Scripts can set variables, call handlers, wait for prodding from other elements, and stop the router.
Each configuration argument is an instruction (except for optional keywords; see below). Script generally processes these instructions sequentially.
In all cases, text arguments are subject to substitutions; see below. Many instructions come in two forms, as in set and setq, read and readq, and write and writeq. The non-q forms perform substitutions on the text, but do not remove any quotes from the result, while the q forms perform substitutions and then remove a layer of quoting. For example, assuming the 'c.count' read handler returns 0:
set x $(c.count) print $x => 0 set x "$(c.count)" print $x => "0" setq x "$(c.count)" print $x => 0
set VAR TEXT', 'setq VAR TEXT'init VAR TEXT', 'initq VAR TEXT'print [>FILE | >>FILE] [TEXT | HANDLER]'print's argument starts with a letter, '@', or '_', then it is treated as a read handler. Otherwise, a layer of quotes is removed and the result is printed. For example, assuming the 'c.count' read handler returns "0": print c.count => 0 print "c.count" => c.count print '"c.count"' => "c.count" set x c.count print $x => c.count print $($x) => 0
printn [>FILE | >>FILE] [TEXT | HANDLER]'print, but does not append a newline.read HANDLER [ARGS]', 'readq HANDLER [ARGS]'print' instruction.
write HANDLER [ARGS]', 'writeq HANDLER [ARGS]'
pause [COUNT]'wait TIME'
label LABEL'goto LABEL [CONDITION]'goto exit [CONDITION]' and 'goto end [CONDITION]' end execution of the script, like 'exit' and 'end' respectively. 'goto begin
[CONDITION]' transfers control to the first instruction, like 'loop'. 'goto error [CONDITION]' ends execution of the script with an error, like 'error'.loop'end'end' causes the script to be reinstalled as a signal handler. In packet scripts, 'end' emits the packet on output 0.exit'return [VALUE]', 'returnq [VALUE]'run handler. In packet scripts, VALUE is the port on which the packet should be emitted.error [MSG]', 'errorq [MSG]'Scripts come in several types, including active scripts, which start running as soon as the configuration is loaded; passive scripts, which run only when prodded; signal scripts, which run in response to a signal; and driver scripts, which are active scripts that also control when the driver stops. The optional TYPE keyword argument is used to select a script type. The types are:
ACTIVEPASSIVErun handler. Passive scripts can help build complex handlers from existing simple ones; for example, here's a passive script whose s.run read handler returns the sum of two Counter handlers. ... c1 :: Counter ... c2 :: Counter ... s :: Script(TYPE PASSIVE, return $(add $(c1.count) $(c2.count))) Within the script, the $args variable equals the run handler's arguments. $1, $2, etc. equal the first, second, etc. space-separated portions of $args, and $# equals the number of space-separated arguments.
PACKET$input variable equals the packet input port. The script's return value is used as the output port number.PROXY$0 variable equals the handler's name. Also, the $write variable is "true" if the handler was called as a write handler. For example, consider: s :: Script(TYPE PROXY, goto nota $(ne $0 a), returnq "you called 'a'", label nota, goto notb $(ne $0 b), returnq "you called 'b'", label notb, error bad handler); Calling the read handler "s.a" will return "you called 'a'", calling "s.b" will return "you called 'b'", and anything else will produce a "bad handler" error.
DRIVERSIGNAL SIGNO...
Text in most Script instructions undergoes variable substitution. References to script variables, such as '$x', are replaced by the variable text. Additionally, the form '$(HANDLER [ARG...])' can be used to interpolate a read handler's value. Variable and handler references can be nested inside a '$(...)' block. For example, the following script will print 0, 1, 2, 3, and 4 on separate lines, then exit. Note the use of Script's arithmetic handlers.
s :: Script(set x 0,
label begin_loop,
print $x,
set x $(s.add $x 1),
goto begin_loop $(s.lt $x 5),
stop);
This can be further shortened since local handler references do not require the element name. Thus, "$(s.add ...)" can be written "$(add ...)", as below.
Script(set x 0,
label begin_loop,
print $x,
set x $(add $x 1),
goto begin_loop $(lt $x 5),
stop);
pause or wait). A numeric argument will step past that many blocking instructions.return' instruction, then the handler returns with that value.add 10 5 2' returns "17". (At user level, the arithmetic and comparison operators can parse floating-point numbers as well as integers.)sub 10 5 2' returns "3".idiv' handler truncates its result to an integer and returns that, whereas the 'div' handler returns a floating-point number; in the kernel, 'idiv' and 'div' both perform integer division.mod 7 3' returns "1". 'mod' expects integer operands and returns the integer modulus. At user level, 'rem' implements floating-point remainder; in the kernel, it is the same as 'mod'.eq 10 0xA' returns "true", and 'le 9 8' returns "false". If either parameter cannot be interpreted as a number, performs a string comparison in bytewise lexicographic order. For example, 'eq 10x 10x' returns "true".in foo bar foo' returns "true".sprintf "%05x" 127' returns "0007F".Generated by 'click-elem2man' from '../elements/standard/script.hh' on 7/Mar/2009.