| Extending the language / C-Extensions | |
It is possible to call an BDL function from a C-Extension function.
fgl_call ( function-name, nb-params );
In this call, function-name is the name of the program function to call, and nb-params is the number of parameters pushed on the stack for the program function. The function-name must be written in lowercase letters; The fglcomp compiler converts all program functions names to lowercase.
The fgl_call() macro is converted to a function that returns the number of values returned on the stack.
The myprog.4gl BDL module defining the MAIN block and the display_item() function to be called from the C extension:
IMPORT mycext
MAIN
CALL c_fct()
END MAIN
FUNCTION display_item(name, size)
DEFINE name VARCHAR(30), size INTEGER
DISPLAY name, size
RETURN length(name), (size / 100)
END FUNCTION
The mycext.c C extension module calling the BDL function:
#include <stdlib.h>
#include <stdio.h>
#include "f2c/fglExt.h"
int c_fct( int n );
UsrFunction usrFunctions[]={
{"c_fct",c_fct,0,0},
{0,0,0,0}
};
int c_fct( int n )
{
int rc, len;
float size2;
if (n != 0) exit(1);
pushquote("Hand gloves", 11);
pushint(54);
rc = fgl_call( display_item, 2 );
if (rc != 2) exit(1);
popflo(&size2);
popint(&len);
printf(">> %d %f\n", len, size2);
return 0;
}
$ gcc -I $FGLDIR/include -shared -fPIC -o mycext.so mycext.c $ fglcomp myprog.4gl $ fglrun myprog.42m Hand gloves 54 >> 11 0.540000