rllib  1
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
Public Member Functions | Public Attributes | Private Attributes | List of all members
rlSpawn Class Reference

#include <rlspawn.h>

Public Member Functions

 rlSpawn ()
 
virtual ~rlSpawn ()
 
int spawn (const char *command)
 
const char * readLine ()
 
int getchar ()
 
int select (int timeout=50)
 
int writeString (const char *buf)
 
int write (const char *buf, int len)
 
int printf (const char *format,...)
 
void printAll ()
 

Public Attributes

int pid
 

Private Attributes

void * toChild
 
void * fromChild
 
char line [4096]
 

Detailed Description

Spawn an external program.
Redirect <stdin> <stdout> <stderr> of external program to this class
Now you can communicate with this external program over a pipe.
Attention: This class is only available on unix like systems.

Definition at line 32 of file rlspawn.h.

Constructor & Destructor Documentation

rlSpawn::rlSpawn ( )

Definition at line 33 of file rlspawn.cpp.

{
toChild = fromChild = NULL;
pid = 0;
}
rlSpawn::~rlSpawn ( )
virtual

Definition at line 39 of file rlspawn.cpp.

{
if(toChild != NULL) ::fclose((FILE*) toChild);
if(fromChild != NULL) ::fclose((FILE*) fromChild);
}

Member Function Documentation

int rlSpawn::getchar ( )
 Read a char from the spawned command.
 When the command terminates EOF is returned.
 

Definition at line 111 of file rlspawn.cpp.

{
if(fromChild == NULL) return EOF;
return ::fgetc((FILE*) fromChild);
}
void rlSpawn::printAll ( )
 Print all outputs from spawned command to <stdout>
 

Definition at line 146 of file rlspawn.cpp.

{
const char *cptr;
while((cptr=readLine()) != NULL) ::printf("%s",cptr);
}
int rlSpawn::printf ( const char *  format,
  ... 
)
 similar to printf
 Return: number of bytes written
         -1 error
 

Definition at line 127 of file rlspawn.cpp.

{
int ret;
char message[rl_PRINTF_LENGTH]; // should be big enough
va_list ap;
va_start(ap,format);
ret = rlvsnprintf(message, rl_PRINTF_LENGTH - 1, format, ap);
va_end(ap);
if(ret < 0) return ret;
return write(message,strlen(message));
}
const char * rlSpawn::readLine ( )
 Read a line from the spawned command.
 When the command terminates NULL is returned.
 

Definition at line 93 of file rlspawn.cpp.

{
if(fromChild == NULL) return NULL;
if(::fgets(line,sizeof(line)-1,(FILE*) fromChild) == NULL)
{
#ifdef RLUNIX
if(pid != 0)
{
int status;
waitpid(pid, &status, 0);
kill(pid,SIGHUP);
}
#endif
return NULL;
}
return line;
}
int rlSpawn::select ( int  timeout = 50)
 Wait for characters.
 return = 0 // timeout
 return = 1 // characters available
 

Definition at line 152 of file rlspawn.cpp.

{
#ifdef RLWIN32
return -1;
#else
struct timeval timout;
fd_set wset,rset,eset;
int ret,maxfdp1,s;
if(fromChild == NULL) return -1;
s = fileno((FILE *) fromChild);
/* setup sockets to read */
maxfdp1 = s+1;
FD_ZERO(&rset);
FD_SET (s,&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
timout.tv_sec = timeout / 1000;
timout.tv_usec = (timeout % 1000) * 1000;
ret = ::select(maxfdp1,&rset,&wset,&eset,&timout);
if(ret == 0) return 0; /* timeout */
return 1;
#endif
}
int rlSpawn::spawn ( const char *  command)
 Start an operating system command.
 The output from the command can be read with readLine()
 You can write to the program with writeString() or write()
 Return: 0=success -1=error
 

Definition at line 45 of file rlspawn.cpp.

{
#ifdef RLWIN32
return -1;
#else
int to_child[2],from_child[2],ret;
if(toChild != NULL) ::fclose((FILE*) toChild);
if(fromChild != NULL) ::fclose((FILE*) fromChild);
toChild = fromChild = NULL;
ret = ::pipe(to_child);
if(ret == -1) return -1;
ret = ::pipe(from_child);
if(ret == -1) return -1;
if((pid = ::fork()) == 0)
{
if(to_child[0] != 0) // stdin
{
::dup2(to_child[0],0);
::close(to_child[0]);
}
if(from_child[1] != 2) // stderr
{
::dup2(from_child[1] ,2);
}
if(from_child[1] != 1) // stdout
{
::dup2(from_child[1],1);
::close(from_child[1]);
}
::close(to_child[1]);
::close(from_child[0]);
::rlexec(command);
::exit(0);
}
::close(to_child[0]);
::close(from_child[1]);
toChild = (void*) ::fdopen(to_child[1],"w");
if(toChild == NULL) { return -1; }
fromChild = (void*) ::fdopen(from_child[0],"r");
if(fromChild == NULL) { ::fclose((FILE*) toChild); return -1; }
return pid;
#endif
}
int rlSpawn::write ( const char *  buf,
int  len 
)
 Write buf to <stdin> of spawned command
 Return: number of bytes written
         -1 error
 

Definition at line 117 of file rlspawn.cpp.

{
#ifdef RLWIN32
return -1;
#else
if(toChild == NULL) return -1;
return ::write(fileno((FILE*)toChild),buf,len);
#endif
}
int rlSpawn::writeString ( const char *  buf)
 Write buf to <stdin> of spawned command
 buf must be 0 terminated
 Return: number of bytes written
         -1 error
 

Definition at line 140 of file rlspawn.cpp.

{
if(toChild == NULL) return -1;
return fprintf((FILE*)toChild,"%s",buf);
}

Member Data Documentation

void * rlSpawn::fromChild
private

Definition at line 95 of file rlspawn.h.

char rlSpawn::line[4096]
private

Definition at line 96 of file rlspawn.h.

int rlSpawn::pid

Definition at line 92 of file rlspawn.h.

void* rlSpawn::toChild
private

Definition at line 95 of file rlspawn.h.


The documentation for this class was generated from the following files: