# The interpreter hooks

- The Main interpreter hook
- The Loop interpreter hook
- The Wait interpreter hook
- The Watch interpreter hook
- The Post interpreter hook
- The Quit interpreter hook
- The Error interpreter hook

## The Main interpreter hook

This hook is used for parsing the command line arguments. Its prototype must be :

    void MainHook ( int * argc , char ** argv )

- argc points at a variable that contains the number of arguments on the command line.
- argv points at an array containing the arguments.

Use this function to interpret some of the command line arguments. If you want to remove the interpreted arguments from the argv array, don't forget to modify the number of arguments referenced by argc accordingly.

Every remaining parameters are passed to the interpreted Gambas program, that can get them with the `Application.Args` array.

## The Loop interpreter hook

This hook is used for managing the event loop. Its prototype must be :

    void LoopHook ( void )

This hook is called just after the startup method of the Gambas program has returned.

The hook must return when the program is ended. The return value of the hook will be the return value of the program.

If no component provided such a hook, then the interpreter will supply itw own event loop. This event loop can just manage watched file descriptors.

## The Wait interpreter hook

This hook is used for implementing the WAIT instruction. Its prototype must be :

    void WaitHook ( long duration )

- duration is the number of milliseconds to wait.

The hook must wait until duration milliseconds elapse, and must call the event loop during this wait.

The hook can return before the specified duration is elapsed. In that case, the hook will be called again and again until the duration specified in the `WAIT` instruction really elapse.

## The Watch interpreter hook

This hook is called when the interpreter wants to watch a specific file descriptor. Its prototype must be:

    void WatchHook ( int fd , int type , void * callback , long param )

- fd is the file descriptor to watch.
- type is one of the following constants:
  - GB_WATCH_NONE to stop watching the file descritor.
  - GB_WATCH_READ to watch it for reading.
  - GB_WATCH_WRITE to watch it for writing.
  - GB_WATCH_READ_WRITE to watch it for bot reading and writing.
- callback is the callback called when the there is something to read or write. its prototype is the one described in GB_WATCH_CALLBACK.
- param is a value passed to the callback.

## The Post interpreter hook

This hook is called each time a callback routine is posted with the `GB.Post` function. Its prototype must be :

    void PostHook ( void )

The aim of this hook is to let the event loop management call each posted callbacks at the end of the event loop, with the `GB.CheckPost` function.

For example, the Post hook of the `gb.qt` component initializes a one-shot QTimer that is known to be called at the end of the loop. When this one-shot QTimer raises its signal, a slot is called that calls `GB.CheckPost` in return.

## The Quit interpreter hook

This hook is called when the QUIT instruction is used. Its prototype must be :

    void QuitHook ( void )

Use this hook to release as much objects as possible, so that the interpreter can quit the more cleanly possible.

## The Error interpreter hook

This hook is called there is an error message to display. Its prototype must be

    void ErrorHook ( int code , char * error , char * where )

- code is the error code.
- error is the error message.
- where is a string that describe where the error occured.