c2hs: Binding constants

253 Views Asked by At

I'm trying to write a quick binding to some ioctl functions (in particular, getting and setting the window size) using c2hs. Here's the relevant part of what I have:

{-# LANGUAGE ForeignFunctionInterface #-}

#include <sys/ttycom.h>
#include <sys/ioctl.h>

module A where
  import Foreign.Storable
  import Foreign.Ptr
  import Foreign.C

  {#enum define TIO {TIOCGWINSZ as GetWinsz, TIOCSWINSZ as SetWinsz} deriving (Eq) #}

(full code available at https://gist.github.com/nc6/8977936)

When I try to compile this, I get:

c2hs: Feature not yet implemented: GenBind.evalConstCExpr: Casts are not implemented yet.

I'm guessing the cause of this problem is that the C consts are defined using some helper functions (_IOW and _IOR) which the Haskell preprocessor is unable to deal with. However, it's not clear how best to fix this. I've tried defining an enum in a #c ... #endc section and using the straight enum hook, but this gives precisely the same problem.

Should I give up using c2hs and use something else? Is there a sensible way around this problem rather than simply hard-coding constant values?

1

There are 1 best solutions below

2
On

The {#enum ... #} feature of c2hs only really handles integral enum values, as defined by an enum in C/C++. You're dealing with these #defines and c2hs just doesn't handle that case.

You might want to look into hsc2hs and bindings-dsl, as this answer from 2011 suggests are suited for handling more complex FFI examples.