9.7 Hooks

The user may define functions to be called at certain occasions by the Prolog system. This is accomplished by passing the functions as arguments to the following set-hook-functions. The functions can be removed by passing a NULL.

typedef int (SP_ReadHookProc) (int fd)
SP_ReadHookProc SP_set_read_hook (SP_ReadHookProc *) obsolescent
The installed function is called before reading a character from fd provided it is associated with a terminal device. This function shall return nonzero when there is input available at fd. It is called repeatedly until it returns nonzero.

You should avoid the use of a read hook if possible. It is not possible to write a reliable read hook. The reason is that SICStus streams are typically based on C stdio streams (FILE*). Such streams can contain buffered data that is not accessible from the file descriptor. Using e.g. select() can therefore block even though fgetc on the underlying FILE* would not block.

typedef void (SP_VoidFun) (void)
SP_VoidFun * SP_set_reinit_hook (SP_VoidFun *)
The installed function is called by the development system upon startup. The call is made after SICStus Prolog's signal handler installation but before any initializations are run and the version banners are displayed. Calling Prolog from functions invoked through this hook is not supported. In recursive calls to Prolog from C, which includes all run-time systems, this hook is not used.
typedef void (SP_VoidFun) (void);
SP_VoidFun * SP_set_interrupt_hook (SP_VoidFun *)
The installed function is called on occasions like expansion of stacks, garbage collection and printouts, in order to yield control to special consoles etc. for interrupt checking. Calling Prolog from functions invoked through this hook is not supported.