Legato
Legato

GoFiler Legato Script Reference

 

Legato v 1.5b

Application v 5.24b

  

 

Chapter FourFlow Control (continued)

4.3 Entry Point

4.3.1 Overview

The execution of a script must start at some defined or default point. Execution is generally started from one of four points: from the IDE (the Run function), from an application hook, an event, or from a command line (e.g., API call).

In the case of the IDE or command line, if a function name is not specified by the caller, the script will attempt to find “main” or “entry” function names. If neither of these functions are defined, the first global statement prior to any defined function body is executed.

From a hook for event, the entry point is either specified as a function name for the hook or a predefined function name.

Example default run, no defined entry point:

int a, b;
a = 5;
b = a * 10;
MessageBox('I',"My answer is %d", b);

The first executable statement is a = 5;. During program flow, if the script engine encounters a function it will cause the default global code to end.

Example with a main entry point function.

int main() {
    int a, b;
    a = 5;
    b = a * 10;
    MessageBox('I',"My answer is %d", b);
    return 0;
    } 

In the later mode, any global statements will not ever be executed and should be avoided. The “main” can also be called “entry”.

Many code examples in this manual are in free form without an entry point and therefore execution begins at the first statement.

4.3.2 How Does a Script Start

Scripts always start in the following sequence:

1.  Internal Script Engine Class is Initialized. On the first script run for the application, the engine will perform keyword and DLL initializations.

2.  The Script File or Files are Parsed.

3.  All global definitions/declarations are processed by defining the variable or function in the Global Variable Pool.

4.  The entry point is found by searching for the specified entry point or the function names “main” or “entry”.

5.  The script engine is started by calling that specific function or the first free flowing line of code.

Depending on how a script was executed, after the actual program or function exits, cleanup will be performed. For certain functions such as hooks, the script can be initialized, run and exited and then later re-entered leaving all global data intact.

4.3.3 How Entry Points Are Used

Example of the second general entry point (also main example above):

int entry() {
    int a, b;
    a = 5;
    b = a * 10;
    MessageBox('I',"My answer is %d", b);
    return 0;
    } 

Below is an example of a hook. The main is run when the script is loaded and each time the EDGAR_SUBMIT_TEST function is invoked, the hook test_file_hook is called at the start of the application function and at the end of the application function. Note that each class of hook will have certain parameters passed to the script.

int main() {
    MenuSetHook("EDGAR_SUBMIT_TEST", "", "test_file_hook");
    return 0;
    } 

int test_file_hook(int f_id, string mode) {
    (. . . hook code . . .)
    return ERROR_NONE;
    }

Events can also be used as entry points when a script is attached to a specific edit view such as Forms View. In this case, the on_function event hook is called each time a menu function is invoked for the particular view:

int on_function(int f_id, dword lParam) {
    if (f_id == fid_col_hide_empty) {    
      columns_hide_empty();              
      return ERROR_EXIT;                 
      }                                  
    if (f_id == fid_col_selector) {      
      columns_selector();                
      return ERROR_EXIT;                 
      }                                  
    if (f_id == fid_col_show_all) {      
      columns_show_all();                
      return ERROR_EXIT;                 
      }                                  
    return ERROR_NONE;                   
    }

Dialog procedures also reenter a script.

4.3.4 Reentrant Scripts

In certain environments, a script will be loaded and reentered repetitively. For example, hooks to a menu function will leave the processing script in memory. Reentrant scripts keep their global variables intact.

4.3.5 Unexecutable Code

Aside from code outside of the main function, unreachable or unexecutable is code outside of a function or after a break or return is not reported by the script engine. Further, the engine will not warn and does not check for always true or uncalled functions.

Example with main entry point function.

MessageBox('X',"This code will not be run");

int main() {
    int a, b;
    a = 5;
    b = a * 10;
    MessageBox('I',"My answer is %d", b);
    return 0;
    }

MessageBox('X',"This code will not be run");

This will result in warnings placed in the IDE log (if run from the IDE):

Legato 1.0f Startup at 2017-01-10T12:53:17 for: (untitled)

Warning 1264 line 1: Global statement(s) will never be executed (additional warnings not displayed).

Warning 1264 line 1: Global statement(s) will never be executed (additional warnings not displayed).

Total no errors, 2 warnings Total Time: 1625ms, Execute: 0ms 

In the later mode, any global statements will not ever be executed and should be avoided. The “main” can also be called “entry”.