3

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?

Impredicative
  • 5,039
  • 1
  • 17
  • 43

1 Answers1

1

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.

Community
  • 1
  • 1
Aaron Friel
  • 1,065
  • 5
  • 11
  • I did ultimately switch to using `hsc2hs`. However, the `c2hs` documentation explicitly references the `{#enum define ... }#` hook as being for precisely this case (see https://github.com/haskell/c2hs/wiki/Implementation%20of%20Haskell%20Binding%20Modules), so I cannot accept this answer as it is. – Impredicative Feb 24 '14 at 11:04
  • Sorry, you're right. It is in the manual but the use case isn't implemented: https://github.com/haskell/c2hs/blob/b18d890a3713ae9ea4404f6b767fce365b347d4f/src/C2HS/Gen/Bind.hs#L2057 I've had tremendous difficulty with the `{#enum ... #}` feature. Basically anything more advanced than an actual C `enum` hasn't worked for me. – Aaron Friel Feb 25 '14 at 00:18