Thanks, this approach works very well. I've copied 2 small test programs at
the bottom if anyone's interested. (solaris 2.6)
-rev3
>> I have an application which needs to spawn a separate window (xterm,
>> shelltool, whatever, I don't care) and use that window to pass data in
and
>> out of the parent process. The parent's window is too busy with other
status
>> and info msgs. The parent can poll the stdin pipe with a non-blocking
>> read().
>If I understand you correctly you have two ways to go that I can
>think of.
>Option 1. Use a terminal emulator (e.g. xterm) by:
> a) Write a buddy program to work with your parent which passes information
> to/from your server using IPC (e.g. a fifo).
> b) Have your parent parent fork/exec a terminal emulator with the to
> run the buddy program. For example,
> xterm -e buddy /tmp/myfifo
>The buddy interacts with the screen/user via the emulator and reports to
the parent
>via the fifo.
>Option 2. Roll your own emulator using ptys. In this case your buddy
>program creates it's own window and does it's own terminal emulation
>via pesudo terminals. You can shorten development time on this approach
>by leveraging the functions in libexpect. See http://expect.nist.gov/.
>HTH,
>ced
>--
>Chuck Dillon
>Senior Software Engineer
>Genetics Computer Group, a subsidiary of Oxford Molecular
do a 'mkfifo /tmp/fifo' before running.
=====================Program
1==================================================
define _POSIX_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* main routine to test all this */
int main(int argc, char **argv) {
pid_t Child_id;
int rc;
int n;
int i;
int fifoInput;
int bufsize = 200;
char buf[200];
Child_id = fork();
if(Child_id == -1) {
fprintf(stderr,"Fork failed\n");
exit(0);
}
if (Child_id) { /* Parent */
if ((fifoInput = open("/tmp/fifo",(O_RDWR | O_NONBLOCK))) < 0) {
fprintf(stderr,"Fifo input open failed \n");
exit(0);
}
while (1) {
if ( (n = read(fifoInput, buf, bufsize)) > 0) {
buf[n] = '\0';
printf("Saw this: ");
printf("%s\n",buf);
}
sleep(1);
}
} else { /* child */
rc = execl("/tools/bin/xterm" ,"xterm", "-bg", "white", "-fg", "black",
"-title", "Terminal Window", "-e", "/path/uart_buddy", "/tmp/fifo", (char
*)0);
fprintf(stderr,"child: execl failed: %d\n",rc);
exit(0);
}
Quote:}
======================Program 2========================================
/* uart_buddy */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int fifoOutput;
char buf[200];
int n;
int bufsize = 200;
fprintf(stderr,"Using fifo at %s\n",argv[1]);
if ((fifoOutput = open (argv[1],O_WRONLY)) < 0) {
fprintf(stderr,"Fifo output open failed \n");
exit(0);
}
while (1) {
putchar('>');
gets(buf);
if ( (n = write(fifoOutput, buf, bufsize)) < 0) {
fprintf(stderr,"Couldn't write to /tmp/fifo\n");
exit(0);
}
}
Quote:}