3
votes

I am trying to write Haskell FFI binding for some C structs. An example is below:

typedef struct s0{int a; 
                  union{unsigned char b; 
                        struct s0*c; 
                        struct{unsigned char d[1];
                         }; };}*S;

My question is how to write the binding for it in chs (for c2hs) or hsc (for hsc2hs) format? I looked into the tutorials for c2hs but either didn't get enough information, or didn't understand it in the way, that would have helped me write the chs file for above definition.

I can generate haskell bindings using HSFFIG tool but it uses custom access method HSFFIG.FieldAccess.FieldAccess to define bindings. I prefer to write bindings that use core haskell FFI libraries, not third-party libraries.

Hence, this question about how to write a binding for recursive struct above in hsc format, or chs format that uses only core FFI libraries.

The actual definition is more complex, but once I figure out how to write above struct definition for c2hs or hsc2hs tools, I can go from there. I know Storable instances need to be defined for inner union and struct as well, but I don't know how to write wrappers for recursive definition like above. Especially, how do you access inside struct/union from outer struct? I looked into HSFFIG definitions, but the access methods are HSFFIG defined access methods. So, I was unable to figure out how to translate it to chs definition that uses only core FFI libraries.

The questions I have seen in StackOverflow seem to be about simpler definitions. If there is a similar answer somewhere else, I will appreciate pointers.

1
Do you actually need to do anything with the data? It's possible you could get away with using a void data typealternative
Yes, I do need to deal with the data. The pointer to above struct is passed to another FFI C function as one of the arguments, and a struct value is returned by that function.Sal
doesn't sound like you care about the data.alternative
The struct value that is returned, will be used within haskell code, after retrieving the relevant value, and after using relevant accessor function. So, it does matter. The FFI performance (and so, amount of data being marshalled) doesn't really matter.Sal
Do you want to read and write the C data directly, or to marshal to/from Haskell data? Marshaling copies the entire data structure, but is easier to use.Heatsink

1 Answers

1
votes

You can't magic up an equiv data structure in either c2hs or hsc2hs. However, you can do your own marshaling in c2hs with only a little work.

data MyType = Next MyType | MyChar Char | MyString String | MyEnd

Then use hsc2hs' newtype pointer functionality to declare a pointer for MyType (ie. s0). Then, write an explicit function using hsc2hs' accessors to recursively walk your structure and build up your Haskell structure. At each step you test if you've hit a null pointer and if so return a MyEnd (or, depending on the data encoding, just check if the int indicating the type in the union is negative one or whatever), otherwise proceed to parse out whatever you have, and if that thing is a pointer, proceed recursively.

You could do nearly the same thing with hsc2hs as well.