Nnamdi Kohn wrote:
haven’t managed to get it running. Here are some questioins:
pterm_pipe = popen( “pterm -N1”, “r” );
opens a terminal window but how do I know if the file_descriptor “1” is the
right one?
It’s popen()'s job to make sure that pterm’s file descriptor 1 is the
writable end of the pipe.
fgets( namebuf, sizeof(namebuf), pterm_pipe );
fgets() waits until ‘\n’ or MAX_SIZE-1, but “pterm -N1” does not close the
output with an ‘\n’. So fgets() seems to wait forever.
Oh yeah, I forgot about one detail: popen() spawns a shell, and the
shell spawns pterm, and even though pterm closes its fd, the shell keeps
its fd open, which prevents your fgets() from getting an EOF.
Change the command in the popen() call to “exec pterm -N1”. I just made
a little experiment that confirmed that that fixed the problem. If you
replace the popen() with your own pipe() and spawn() calls like I
suggested before, spawn pterm directly rather than a shell.
fp1 = fopen( namebuf, “w” );
fp2 = fopen( namebuf, “r” );
when I set namebuf to “/dev/ttypa” manually then the file pointers fp1 and
fp2 are set correctly to the opened window (opened with
open( “pterm…”)… ). But when I then fprintf( fp1,…) the output in the
screen is at the same time directed to fd2. So when I read a line with
fgets( …, fd2) it has no effect because the input buffer is already filled
with all the output lines.
I’m not sure if I understand you correctly, but it doesn’t sound like
what should happen.
Is there an other (maybe simpler) way to do it, so that the output is NOT
piped into the input again?
Here’s a little program that works for me. Does it not work for you?
#include <stdio.h>
int main( void ) {
char buf[ 120 ];
FILE *pterm_pipe, *fp1, *fp2;
if ( ( pterm_pipe = popen( “exec pterm -N1”, “r” ) ) == NULL
|| fgets( buf, sizeof(buf), pterm_pipe ) == NULL
|| ( fp1 = fopen( buf, “w” ) ) == NULL
|| ( fp2 = fopen( buf, “r” ) ) == NULL
) {
perror( “Something went wrong” );
return 1;
}
printf( “Opened ‘%s’\n”, buf );
fputs( “Type stuff into this window\n”, fp1 );
while ( fgets( buf, sizeof(buf), fp2 ) ) {
printf( “You typed ‘%s’\n”, buf );
fputs( “I got it, type more or hit Ctrl-D\n”, fp1 );
}
puts( “Got an EOF, exiting” );
return 0;
}