[comp.windows.x] Trouble using XtAddInput

keithh@atreus.uucp (Keith Hanlan) (06/03/89)

	I would like to use XtAddInput to trap output from a process and
	display it in an X widget. The following code fragment illustrates
	what I am doing:

	-----------------------------------------------------------------------
	static void Input_Handler(...)
	{
		...
		if((n_read = read( pipe_fd, buffer, BUFFER_SIZE )) == -1 ) {
		}

		printf( "Input_Handler: Read >%s<\n", buffer );
	} /* Input_Handler */

	void main( argc, argv )
	{
		top_level = XtInitialize( "trap_output", "trap_output", 0, 0,
									&argc, argv );

		pipe_fd = open( PIPE_NAME, O_RDONLY, 0600 );

		InputId = XtAddInput( pipe_fd, XtInputReadMask, Input_Handler, NULL );
		XtMainLoop();
	}/* main */
	-----------------------------------------------------------------------

	And to test, I simply "cat > fred_file".

	Everything works. Unfortunately it works too well! The Input_Handler
	is invoked continuously and the same contents are read() ad nauseum.

	It seems that the handler is invoked merely because select() says
	that it is ready for reading. There must be some way to use this
	facility to do what I want.

	There are two disturbing elements to this problem. The first is that
	the event is infinitely repetitive. The second is that successive
	calls to read() return the same text. Why isn't the text consumed
	normally?

	When I convert this to use pipes will the problem magically go away?
	That is, is the problem an artifact of using a file as opposed to a
	pipe?

	I would appreciate any input you might be able to offer. I am new to
	this list so I apologize if this was hashed out last month. I will
	be gone next week but I would still appreciate any answers that
	might pile up in the interim. My colleague will be looking for
	answers while I am gone.

	Thank you,
	Keith Hanlan		{uunet!attcan!}utgpu!bnr-vpa!bnr-fos!atreus!keithh
	Bell-Northern Research
	Ottawa, Canada

swick@ATHENA.MIT.EDU (Ralph R. Swick) (06/06/89)

>	When I convert this to use pipes will the problem magically go away?
>	That is, is the problem an artifact of using a file as opposed to a
>	pipe?

No, probably not and no, probably not.  Alternate input from both
files and pipes works just fine here.  Please make sure you have
fix8 installed; the symptoms you report could be due to a previously
fixed bug in Xt.