views:

13

answers:

1

How can I get control of the stdin, stdout and stderr streams in gcc.??

We can redirect the error and output to separate files for sure, but is there a way that i can control the stdin such that whenever my program is executing and there is a moment when the stdin waits for a input(either from the user keyboard or any file), it sets a signal / changes the value of a variable or something like that so that i can get to know that now the stdin is waiting for the input, so that i can proceed accordingly and do something (e.g.show user a message or anything)

The other way i can think of doing it is continously polling the stdin stream somehow and whenever it waits for input, do my action. But again I don't know how to do this.?

Let me know if the question is not clear enough.

+1  A: 

The simplest way I can think of, is to split "your program" into two. One reads from stdin as it is doing now. The other spawns the first program with its stdin connected to a pseudoterminal. Whenever the first program blocks waiting for input, then, the second program will unblock from waiting for output to the pseudoterminal. (You want a pseudoterminal rather than a pipe to avoid buffering headaches.)

There doesn't seem to be a good online explication of how to do this, but W. Richard Stevens' Advanced Programming in the UNIX Environment covers it quite well.

Zack
@Zack :- A lot of what you said went over my head. I have no experience of working with pseudo-terminals. Thanks for the link. I will read it and then get back to you if I face any problem. For now, can you explain why prefer a pseudoterminal to a pipe and that avoiding buffering headaches.
Silver Spoon
With a pipe, the C library (on both sides) rebuffers everything into big blocks, which is not what you want in this case. You could also avoid that problem by using only the OS primitives to transfer data (read(2) and write(2)) or by using write(2) on the sender side and setting stdin to unbuffered on the receiver side; that might be easier, but I'm less sure it would work.
Zack