------------------------------------------------------------------------------ Xterm Slave Option -S That is connect xterms to a specific Psuedo-TTY For use in output from specialized applications. ------------------------------------------------------------------------------ From: meunier@blizzard.enst.fr (Sylvain Meunier) Reply-To: meunier@inf.enst.fr Subject: Re: -S option to xterm I had also the same problem there is a while. I had the answer and now I forward it. This is a part of my code to use the xterm with pseudo-device: --- Begin Here --- static char pseu[]="/dev/ptyXX"; int slave_fd; int openpseudo(tty) char **tty; { int gotpty = 0; int i; char c; int res; static char nm[4]; struct stat statbuf; static char hexa[]="0123456789abcdef"; for (c = 'p'; !gotpty && c <= 's'; c++) { pseu[sizeof "/dev/pty"-1 ] = c; pseu[sizeof "/dev/ptyX"-1 ] = hexa[i=0]; if (stat(pseu, &statbuf) < 0) break; for (i = 0; i < 16; i++) { pseu[sizeof "/dev/ptyX"-1 ] = hexa[i]; res = open(pseu, O_RDWR); if (res >= 0) { gotpty++; nm[0]=c; nm[1]=hexa[i]; break; } } } if (!gotpty) { fprintf(stderr,"All pseudo terminal in use\n"); return -1; } nm[2]='\0'; *tty=nm; return (res); } /* input: name of xterm; output: slave_fd is slave side of xterm (read from xterm and write to it) return pid of xterm or 0 for error */ int open_xterm(name) char *name; { int fils; char *nm; int master; char buf[20],c; if ((master=openpseudo(&nm))<0) { perror("master side"); return (0); } sprintf(buf,"/dev/tty%s",nm); if ((slave_fd=open(buf,O_RDWR))<0) { perror("slave side"); close(master); return (0); } if ((fils=fork())<0) { perror("fork xterm "); close(master); close(slave_fd); return (0); } if (fils==0) { close(0); close(slave_fd); dup2(master,1); if (master!=1) close(master); sprintf(buf,"-S%s1",nm); execlp("xterm", "teleinfo", "-name", name, "-geometry", "80x25", buf, NULL); perror("Erreur de lancement du terminal"); exit(-1); } close(master); /* xterm send info of it's state, skeep it */ do read(slave_fd, &c, 1); while (c!='\n'); return(fils); } ------------------------------------------------------------------------------ From: anderson@kzin.ColumbiaSC.NCR.COM (Stuart Anderson) Reply-To: Stuart.Anderson@ColumbiaSC.NCR.COM Subject: Re: using XTERM to display output {?} vinod@sommerfeld.WPI.EDU (Vinod K Nair) writes: |> Hello, I am writing a program which forks of a number of child processes |> and each of the children printing things on the screen (quite messy in |> one window)... |> |> The "xterm -Sxxd" option seems to be the solution to opening up |> slave windows only to display output and I use the following code to |> open up a pty (taken from Stevens)...and manage to open up an x-term |> successfully.... > |> The problem however is how do I write into this x-term ? Please help! We had to figure out how to get this to work. Below is a sample program that we wrote for SVR4 that does what you want. (A co-worker did this so real credit goes to him). Hope this helps. #include #include #include #include #include #include #include #include #include #include main() { int master, slave; char slavename[64]; char r_buffer[1024]; char slave_str[5]; struct stat slave_stat; char xterm_cmd[256]; int i; if((master = open("/dev/ptmx", O_RDWR)) == -1) { perror("open master"); exit(1); } grantpt(master); unlockpt(master); strncpy(slavename, ptsname(master), 64); if((slave = open(slavename, O_RDWR)) == -1) { perror("open slave"); exit(1); } slave_str[0] = slave_str[1] = slave_str[2] = slave_str[3] = '0'; slave_str[4] = 0; for(i=0;i<=3;i++) { if(slavename[strlen(slavename)-i-1] == '/') break; slave_str[3-i] = slavename[strlen(slavename)-i-1]; } /* * Set up slave stream */ if(ioctl(slave, I_PUSH, "ptem") < 0) { perror("I_PUSH ptem"); exit(1); } if(ioctl(slave, I_PUSH, "ldterm") < 0) { perror("I_PUSH ldterm"); exit(1); } if(ioctl(slave, I_PUSH, "ttcompat") < 0) { perror("I_PUSH ttcompat"); exit(1); } /* * Fork a child to execute the command. */ if(fork() == 0) { char arg[40]; sprintf(arg,"-S%s%d",slave_str, master); execl("/usr/bin/X11/xterm", "xterm", arg, 0); exit(0); } sleep(3); /* * Print something out to the xterm */ printf("About to write\n"); write(master, "This is output from doslave\n", strlen("This is output from doslave\n")); printf("About to read\n"); for(;;) { read(slave, &r_buffer, 1); printf("%c", r_buffer[0]); } } ------------------------------------------------------------------------------