[Haskell-cafe] Intercept stdin in Haskell

Andrew Gibiansky andrew.gibiansky at gmail.com
Sun Jan 5 20:03:31 UTC 2014


I am using the GHC API extensively. The entire application is built on the
GHC API, with the occasional bit of code practically stolen from GHCi :)

However, I don't know of a way to intercept stdin even if I am using the
GHC API. Given a block of code, there is no sane static analysis than can
be done to detect whether it reads from stdin, as far as I know.

Is there any way to check whether a process is blocked when reading from a
Handle or an Fd? Maybe there are some low-level hacks with unsafeCoerce or
something that would let me pass a non-handle as a Handle, and do it that
way?

The only other option is just disabling all uses of stdin, or perhaps
reimplementing myself the Prelude functions that use stdin. (But this would
still break libraries that have already compiled in the Prelude versions.)


On Sun, Jan 5, 2014 at 2:52 PM, Carter Schonwald <carter.schonwald at gmail.com
> wrote:

> Hrm.  Have you considered using the ghc API? Ghci itself is written using
> it, and I imagine you could adapt that code for your purposes perhaps?
>
>
> On Sunday, January 5, 2014, Andrew Gibiansky wrote:
>
>> I cannot modify the Haskell code.
>>
>> I am working on IHaskell - it is effectively a Haskell interpreter. It
>> reads some code from the user, which may contain something like `getLine`.
>> However, the frontend to the interpreter is not a shell but is a GUI in the
>> web browser (IPython!). In order to do input, the interpreter sends a
>> message to the frontend via the network saying "give me input", the
>> frontend reads some input, and then sends things back.
>>
>> In order to do this communication, I need to know when the getLine is
>> called so that I know I need to send the message to the frontend.
>>
>> Ideas? The thing is, getLine needs to not actually read from any shell -
>> it will read from a pipe I create, and I simply need to know when to put
>> stuff into that pipe.
>>
>> -- Andrew
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140105/bd8e046f/attachment.html>


More information about the Haskell-Cafe mailing list