file: WishIntfc.cxx
/*--
Description:
Methods for Class WishIntfc is used to interface to a 'wish' shell
It has functions that 'spawn' a wish shell and establish
pipes that communicate with the wish shell
Last Modified: 18 Jan, 1999 by Prakash Gopalakrishnan (prakashg@ece.cmu.edu)
--*/
#include <signal.h>
#include <sys/wait.h>
#include <iostream.h>
#include "WishIntfc.hxx"
void
WishIntfc::Initialize(void){
// currently does nothing
}
/*-
This spawns the wish shell and makes it source the TCL script from tcl_fname
-*/
int
WishIntfc::Start(char* tcl_fname)
{
assert(strlen(path) > 0);
// file descriptors These will be set by the pipes
int fdTo[2], fdFrom[2];
if(pipe(fdTo) == ERROR){
perror("pipe");
return (ERROR);
}
if(pipe(fdFrom) == ERROR){
perror("pipe");
return (ERROR);
}
if((childPid = fork()) == ERROR){
perror("fork");
return (ERROR);
}
if(childPid == 0){ // child process
if(dup2(fdFrom[1], STDOUT_FILENO) != STDOUT_FILENO){
perror("dup2");
return (ERROR);
}
close(fdFrom[0]);
close(fdFrom[1]);
if(dup2(fdTo[0], STDIN_FILENO) != STDIN_FILENO){
perror("dup2");
return (ERROR);
}
close(fdTo[0]);
close(fdTo[1]);
const char *pathPtr = path;
execl((char *) pathPtr, "wish", NULL);
perror("execl");
return (ERROR);
}
close(fdFrom[1]);
close(fdTo[0]);
toWish = fdTo[1];
fromWish = fdFrom[0];
// give the child some time to exec
// sleep(0.5);
/*
source tcl_fname ..gives the basic look and feel of the window
*/
sprintf(TCLcommand,"source %s \n", tcl_fname);
SendCommand(TCLcommand);
return (NOERROR);
}
int
WishIntfc::End(void)
{
return (SendCommand("exit\n"));
}
int
WishIntfc::SendCommand(const char* cmd)
{
// set SIGPIPE so we can recover if there is no reader
if(signal(SIGPIPE, wish_catch_sig_pipe) == SIG_ERR){
return ERROR;
}
if (writen(toWish,(char*) cmd, strlen(cmd)) == ERROR)
return(ERROR);
return(NOERROR);
}
int
WishIntfc::Kill(void)
{
int ReturnValue = kill(childPid, SIGKILL);
// wait for the child to go away
waitpid(childPid, NULL, 0);
close(toWish);
close(fromWish);
return (ReturnValue);
}
void
wish_catch_sig_alrm(int Signal)
{
cerr << "SIGALRM Generated.No response from WISH..."
<< endl;
}
void
wish_catch_sig_pipe(int Signal)
{
cerr << "SIGPIPE Generated. No reader on WISH pipe....."
<< endl;
}
C++ to HTML Conversion by ctoohtml