next up previous contents index
Next: Predefined Events Up: Events Previous: Shared Clients

Script Clients

       

In addition to running separate programs or shell commands as clients, you can also use Glish scripts as clients in other Glish scripts. We refer to such scripts as ``script clients''. You create script clients using the client function, much as you might expect:

    sc := client("glish myscript.g")
In general you use the same argument syntax as when running a script directly (see § 11.1, page gif).

In every Glish script, whether run as a script client or not, Glish provides a global variable called script (§ 9.11, page gif). If you run the script directly (not as a script client), then script has the boolean value F. If however you run the script as a script client then script is an agent record (§ 7.2.2, page gif), and can be used in whenever and event-send statements to receive and send events. You can also determine whether a script is being run as a script client by checking whether ``system.is_script_client'' is T or F (see § 9.11, page gif for further description of the system global variable).

For example, the following script computes a ``timestamp'' string (perhaps to be used in constructing archive file names). If invoked directly the script prints the current timestamp and exits. But if invoked as a script client, it waits for get_timestamp events. Upon receiving one it generates a timestamp event with the current timestamp and goes back to waiting for the next get_timestamp event.  

    # Script to compute timestamps.
    # Run independently, prints current timestamp
    # and exits. Run as a script client, responds to
    # "get_timestamp" events by fetching the current
    # timestamp and sending it out in an "timestamp"
    # event.

    if ( system.is_script_client )
        {
        whenever script->get_timestamp do
            script->timestamp( current_timestamp() )
        }

    else
        # Run independently.
        print "Current timestamp:", current_timestamp()


    func current_timestamp()
        shell("date +%a-%h-%d-%T")

If this script were in a file timestamp.g, we could then use it in another script as follows:

    timestamp := client("glish timestamp.g")
    ...
    # Return current timestamp.
    func stamp()
        {
        timestamp->get_timestamp()
        await timestamp->timestamp
        return $value
        }
 

Script clients behave in general identically to regular clients. In particular, they respond to and generate the predefined events defined in § 7.11, page gif below.

      Script clients are can also be shared by multiple users. This is done by simply adding one of the following statements to the beginning of the script:

The shared script processes events as usual; the only difference is that the events are comming from multiple interpreters. Currently, there is no way for the script to select a specific interpreter to send the event to. This will be fixed in a future release.          


next up previous contents index
Next: Predefined Events Up: Events Previous: Shared Clients

Thu Nov 13 16:44:05 EST 1997