02.03.2014 Views

Tornado

Tornado

Tornado

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5<br />

Shell<br />

Function Calls<br />

Shell expressions may contain calls to C functions (or C-compatible functions)<br />

whose names have been entered in the system symbol table; they may also contain<br />

function calls to WindSh commands that execute on the host.<br />

The shell executes such function calls in tasks spawned for the purpose, with the<br />

specified arguments and default task parameters; if the task parameters make a<br />

difference, you can call taskSpawn( ) instead of calling functions from the shell<br />

directly. The value of a function call is the 4-byte integer value returned by the<br />

function. The shell assumes that all functions return integers. If a function returns<br />

a value other than an integer, the shell must know the data type being returned<br />

before the function is invoked. This requires a slightly unusual syntax because you<br />

must cast the function, not its return value. For example:<br />

5<br />

-> floatVar = ( float ()) funcThatReturnsAFloat (x,y)<br />

The shell can pass up to ten arguments to a function. In fact, the shell always passes<br />

exactly ten arguments to every function called, passing values of zero for any<br />

arguments not specified. This is harmless because the C function-call protocol<br />

handles passing of variable numbers of arguments. However, it allows you to omit<br />

trailing arguments of value zero from function calls in shell expressions.<br />

Function calls can be nested. That is, a function call can be an argument to another<br />

function call. In the following example, myFunc()takes two arguments: the return<br />

value from yourFunc( ) and myVal. The shell displays the value of the overall<br />

expression, which in this case is the value returned from myFunc( ).<br />

myFunc (yourFunc (yourVal), myVal);<br />

Shell expressions can also contain references to function addresses instead of<br />

function invocations. As in C, this is indicated by the absence of parentheses after<br />

the function name. Thus the following expression evaluates to the result returned<br />

by the function myFunc2( ) plus 4:<br />

4 + myFunc2 ( )<br />

However, the following expression evaluates to the address of myFunc2( ) plus 4:<br />

4 + myFunc2<br />

An important exception to this occurs when the function name is the very first item<br />

encountered in a statement. This is discussed in Arguments to Commands, p.180.<br />

Shell expressions can also contain calls to functions that do not have a symbolic<br />

name in the symbol table, but whose addresses are known to you. To do this,<br />

179

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!