[plt-scheme] tools and practises to derice FFI types from C type declarations?

From: Ernie Smith (esmith at acanac.net)
Date: Sun Oct 5 16:03:11 EDT 2008

I'm hoping this isn't the wrong place to be asking this kind of
question, if so please redirect me to the right place.

I'm trying to access the 'lstat' function within libgc
In order to perform some straightforward file attribute tests for a script
which hinges on the 'fold-files' function from file.ss.

In order to do so I must interface to a stat function and a 'stat' 
declared in include files for that function call. <sys/stat.h>.

The foreign interface manual explains well enough how to interact
with c structures once you know the primitive types they use.

However typical library functions, rather than declare structures
using primitive types, tend to use intermediate sub-types, and how
these sub-types boil down to  primitives is partly dependent on compile
time switch conditions.  So in short, what primitives they boil down to
is installation dependent.

It's easy enough to trace things these back to primitives on a given
system, (the C preprocessor cpp helps a lot), but that only really
supplies that information on a given installation.

It really should be computed wherever a PLT scheme script is
installed.  At this stage I'm starting to feel like I'm reinventing the
wheel. Again!.   And this time only to accomplish a script.

Someone else has already had to cope with this issue and
written some useful tools for generating compatible
scheme code from C structure declarations.
If I had been responsible for any part of PLT scheme, I know
I would have done something along those lines.  Surely someone
else has?

For perspective, here is the output of the cpp preprocessor's
version of the stat structure on my system.

struct stat
    __dev_t st_dev;
    unsigned short int __pad1;
    __ino_t st_ino;
    __mode_t st_mode;
    __nlink_t st_nlink;
    __uid_t st_uid;
    __gid_t st_gid;
    __dev_t st_rdev;
    unsigned short int __pad2;
    __off_t st_size;
    __blksize_t st_blksize;
    __blkcnt_t st_blocks;
    struct timespec st_atim;
    struct timespec st_mtim;
    struct timespec st_ctim;
    unsigned long int __unused4;
    unsigned long int __unused5;

In the interest of brevity, I clipped the upper part of the preprocessor
output where for instance these translations can be gleaned:

__dev_t      to 'unsigned long long int'
__ino_t       to 'unsigned long int'
__mode_t   to 'unsigned int'

Is there an existing PLT recommended practise for dealing with these 
sorts of
installation issues?

Are there existing recommended tools for deriving necessary scheme interface
data from stand library include files?

If not are there existing workaround tools and practices being used until
recommended ones emerge?

Posted on the users mailing list.