The EXEC command allows externally defined subroutines to be accessed from the sort. It allows different sorting and storage algorithms to be used via the sort language.
function-name
is a string by which the new external routine is known.
The arguments associated with function-name
are specific to the corresponding routine.
function-name
will be forced to be LOWER-case, see example below.
The return code of the routine is tested for success. A non-zero return code will stop processing the current event at that point.
There are two sets of arguments, associated with the two routines defined below in the C language ...
The function-name
_init
routine is optional, and will only be executed if the
init
section of the exec command is specified.
If present, the init routine will be executed once only before
any events are processed.
The current maximum number of arguments that may be passed to an
exec routine may be found in Appendix A.
Example
... exec printit sortword ...
will require the following C code ...
int printit(short *sortword) { printf("Sortword value = %d\n",*sortword); return 0; }
Example
To output some information to a file, then the following code can be used to place the file in the sort directory. Note: Be aware that outputting every event may produce a large file.
... exec fileit sortword ...
will require the following C code ...
extern char basedir[]; static char filename[128]; static FILE *fp; int fileit_init() { strcpy(filename,basedir); strcat(filename,"/myfile"); fp = fopen (filename, "w")); return 0; } int fileit(short *sortword) { fprintf(fp, "%d\n", *sortword); return 0; }
If it is necessary to execute a routine to tidy up after the sort has finished, but not yet exited, a hook mechanism is provided for that purpose. This allows a user-provided routine to be executed at the sort program flushing stage immediately before exit. The mechanism to connect the routine into the sort program is to execute a routine called flush_hook_add() in the function-name_init routine.
Such a mechanism could be useful if the current state or a set of variables needs to be saved.
Example
... exec calculate sortword init ...
will require the following C code ...
calculate_tidy() { /* Tidy up calculate before exit */ ... return 0; } calculate_init() { flush_hook_add( calculate_tidy() ); return 0; }