|
Home > Archive > Unix Programming > June 2005 > Problem piping to an xterm
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem piping to an xterm
|
|
| Lionel B 2005-06-15, 7:48 am |
| I want to start an xterm from a program and then be able to pipe commands to it - can't seem to figure out how to do
this. My current attempt (minimal example in C++ under Win32, Cygwin) looks like:
int main()
{
int pfd[2];
if (pipe(pfd)) {
cerr << "failed to create pipe\n";
return EXIT_FAILURE;
}
const int& pipe_in = pfd[STDIN_FILENO];
const int& pipe_out = pfd[STDOUT_FILENO];
pid_t ppid = fork();
if (ppid<0) {
cerr << "failed to fork\n";
return EXIT_FAILURE;
}
else if (ppid>0) {
cout << "This is the parent" << endl;
if (close(pipe_in)<0) {
cerr << "failed to close pipe (stdin)\n";
return EXIT_FAILURE;
}
const char* cmd = "ls\n";
if (write(pipe_out,cmd,strlen(cmd))<0) {
cerr << "failed to write to pipe\n";
return EXIT_FAILURE;
}
if (fsync(pipe_out)<0) {
cerr << "failed to sync pipe\n";
return EXIT_FAILURE;
}
int status;
if (waitpid(ppid,&status,0) != ppid) {
cerr << "wait for child process failed\n";
return EXIT_FAILURE;
}
cout << "Child returned status " << status << endl;
return EXIT_SUCCESS;
}
else {
cout << "This is the child" << endl;
if (close(pipe_out)<0) {
cerr << "failed to close pipe (stdout)\n";
return EXIT_FAILURE;
}
if (dup2(pipe_in,STDIN_FILENO)<0) {
cerr << "failed to duplicate stdin\n";
return EXIT_FAILURE;
}
char* argv[] = {"xterm",NULL};
execv("/usr/bin/xterm",argv);
cerr << "exec failed\n";
_exit(EXIT_FAILURE);
}
}
This pops up an xterm ok, but the write to the pipe never returns (until the xterm itself terminates, at which point of
course the fsync fails) and the xterm doesn't appear to receive any input on stdin.
Something tells me the problem is with redirecting stdin for an X program... is there perhaps a flag for xterm to enable
redirection of its stdin? I have looked at the -S flag (which I frankly don't understand). If I exec xterm with "-Sab0",
say, then the xterm does appear to receive input from the pipe, but doesn't actually respond to the input.
Any assistance appreciated,
--
Lionel B
| |
| Thomas Maier-Komor 2005-06-15, 6:10 pm |
| Lionel B wrote:
> I want to start an xterm from a program and then be able to pipe commands to it - can't seem to figure out how to do
> this. My current attempt (minimal example in C++ under Win32, Cygwin) looks like:
>
[ code removed ]
>
> This pops up an xterm ok, but the write to the pipe never returns (until the xterm itself terminates, at which point of
> course the fsync fails) and the xterm doesn't appear to receive any input on stdin.
>
> Something tells me the problem is with redirecting stdin for an X program... is there perhaps a flag for xterm to enable
> redirection of its stdin? I have looked at the -S flag (which I frankly don't understand). If I exec xterm with "-Sab0",
> say, then the xterm does appear to receive input from the pipe, but doesn't actually respond to the input.
>
> Any assistance appreciated,
>
The xterm does not read stdin. It contacts the X server and reads the
keyboard events that are generated by the X server, which even does
not mean that those events are generated by a physical existend
keyboard...
Tom
| |
| Lionel B 2005-06-15, 6:10 pm |
| Thomas Maier-Komor wrote:
> Lionel B wrote:
>
> [snip]
>
> The xterm does not read stdin. It contacts the X server and reads the
> keyboard events that are generated by the X server, which even does
> not mean that those events are generated by a physical existend
> keyboard...
Yes, I've just realised that. But it seems like it can be an I/O "channel" using the -Scnn switch to prescribe a pseudo
terminal to use. Am reading up on that. Will post code if I get it working (might be useful to someone...)
Cheers,
--
Lionel B
|
|
|
|
|