#include <poll.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <fcntl.h>
-int bidirpipe(int pfd[], char *cmd , char *argv[])
+int bidirpipe(int pfd[], const char *cmd , const char * const argv[], const char *cwd )
{
int pfdin[2]; /* from child to parent */
int pfdout[2]; /* from parent to child */
for (unsigned int i=3; i < 90; ++i )
close(i);
- execvp(cmd,argv);
+ if (cwd)
+ chdir(cwd);
+
+ execvp(cmd, (char * const *)argv);
+ /* the vfork will actually suspend the parent thread until execvp is called. thus it's ok to use the shared arg/cmdline pointers here. */
_exit(0);
}
if (close(pfdout[0]) == -1 || close(pfdin[1]) == -1 || close(pfderr[1]) == -1)
:pid(-1), killstate(0), in(0), out(0), err(0)
{
for (int i=0; i < 3; ++i)
+ {
fd[i]=-1;
+ filefd[i]=-1;
+ }
}
static char brakets[][2] = {
return NULL;
}
-int eConsoleAppContainer::execute( const char *cmd )
+int eConsoleAppContainer::setCWD( const char *path )
{
- if (running())
+ struct stat dir_stat;
+
+ if (stat(path, &dir_stat) == -1)
return -1;
- pid=-1;
- killstate=0;
+ if (!S_ISDIR(dir_stat.st_mode))
+ return -2;
+
+ m_cwd = path;
+ return 0;
+}
+
+int eConsoleAppContainer::execute( const char *cmd )
+{
int cnt=0, slen=strlen(cmd);
char buf[slen+1];
char *tmp=0, *argv[64], *path=buf, *cmds = buf;
return -2;
tmp = strchr(path, ' ');
- if (!tmp)
- return -3;
-
- *tmp = 0;
- cmds = tmp+1;
+ if (tmp) {
+ *tmp = 0;
+ cmds = tmp+1;
+ while(*cmds && *cmds == ' ')
+ ++cmds;
+ }
+ else
+ cmds = path+slen;
memset(argv, 0, sizeof(argv));
argv[cnt++] = path;
- if (slen) {
- char *argb=NULL;
- char *it = find_bracket(*cmds);
+ if (*cmds) {
+ char *argb=NULL, *it=NULL;
while ( (tmp = strchr(cmds, ' ')) ) {
+ if (!it && *cmds && (it = find_bracket(*cmds)) )
+ *cmds = 'X'; // replace open braket...
if (!argb) // not arg begin
argb = cmds;
if (it && *(tmp-1) == it[1]) {
+ *argb = it[0]; // set old char for open braket
it = 0;
}
if (!it) { // end of arg
+ *tmp = 0;
argv[cnt++] = argb;
argb=0; // reset arg begin
- it = find_bracket(*(tmp+1));
- *tmp = 0;
}
cmds = tmp+1;
+ while (*cmds && *cmds == ' ')
+ ++cmds;
}
argv[cnt++] = argb ? argb : cmds;
+ if (it)
+ *argv[cnt-1] = it[0]; // set old char for open braket
}
- // get one read ,one write and the err pipe to the prog..
-
// int tmp=0;
// while(argv[tmp])
// eDebug("%d is %s", tmp, argv[tmp++]);
+ return execute(argv[0], argv);
+}
- pid = bidirpipe(fd, argv[0], argv);
+int eConsoleAppContainer::execute(const char *cmdline, const char * const argv[])
+{
+ if (running())
+ return -1;
+
+ pid=-1;
+ killstate=0;
+
+ // get one read ,one write and the err pipe to the prog..
+ pid = bidirpipe(fd, cmdline, argv, m_cwd.length() ? m_cwd.c_str() : 0);
if ( pid == -1 )
return -3;
// eDebug("pipe in = %d, out = %d, err = %d", fd[0], fd[1], fd[2]);
+ ::fcntl(fd[1], F_SETFL, O_NONBLOCK);
+ ::fcntl(fd[2], F_SETFL, O_NONBLOCK);
in = new eSocketNotifier(eApp, fd[0], eSocketNotifier::Read|eSocketNotifier::Priority|eSocketNotifier::Hungup );
out = new eSocketNotifier(eApp, fd[1], eSocketNotifier::Write, false);
err = new eSocketNotifier(eApp, fd[2], eSocketNotifier::Read|eSocketNotifier::Priority );
return 0;
}
+int eConsoleAppContainer::execute( PyObject *cmdline, PyObject *args )
+{
+ if (!PyString_Check(cmdline))
+ return -1;
+ if (!PyList_Check(args))
+ return -1;
+ const char *argv[PyList_Size(args) + 1];
+ int i;
+ for (i = 0; i < PyList_Size(args); ++i)
+ {
+ PyObject *arg = PyList_GetItem(args, i); /* borrowed ref */
+ if (!arg)
+ return -1;
+ if (!PyString_Check(arg))
+ return -1;
+ argv[i] = PyString_AsString(arg); /* borrowed pointer */
+ }
+ argv[i] = 0;
+
+ return execute(PyString_AsString(cmdline), argv); /* borrowed pointer */
+}
+
eConsoleAppContainer::~eConsoleAppContainer()
{
kill();
delete out;
delete err;
in=out=err=0;
+
+ for (int i=0; i < 3; ++i)
+ {
+ if ( filefd[i] > 0 )
+ close(filefd[i]);
+ }
}
void eConsoleAppContainer::sendCtrlC()
if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
{
// eDebug("what = %d");
- char buf[2048];
+ char buf[2049];
int rd;
- while((rd = read(fd[0], buf, 2047)) > 0)
+ while((rd = read(fd[0], buf, 2048)) > 0)
{
-/* for ( int i = 0; i < rd; i++ )
- eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
buf[rd]=0;
/*emit*/ dataAvail(buf);
+ stdoutAvail(buf);
+ if ( filefd[1] > 0 )
+ ::write(filefd[1], buf, rd);
if (!hungup)
break;
}
}
+ readyErrRead(eSocketNotifier::Priority|eSocketNotifier::Read); /* be sure to flush all data which might be already written */
if (hungup)
{
eDebug("child has terminated");
if (what & (eSocketNotifier::Priority|eSocketNotifier::Read))
{
// eDebug("what = %d");
- char buf[2048];
+ char buf[2049];
int rd;
- while((rd = read(fd[2], buf, 2047)) > 0)
+ while((rd = read(fd[2], buf, 2048)) > 0)
{
/* for ( int i = 0; i < rd; i++ )
eDebug("%d = %c (%02x)", i, buf[i], buf[i] );*/
buf[rd]=0;
/*emit*/ dataAvail(buf);
+ stderrAvail(buf);
}
}
}
+void eConsoleAppContainer::dumpToFile( PyObject *py_filename )
+{
+ char *filename = PyString_AsString(py_filename);
+ filefd[1] = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ eDebug("eConsoleAppContainer::dumpToFile open(%s, O_WRONLY|O_CREAT|O_TRUNC, 0644)=%i", filename, filefd[1]);
+}
+
+void eConsoleAppContainer::readFromFile( PyObject *py_filename )
+{
+ char *filename = PyString_AsString(py_filename);
+ char readbuf[32*1024];
+ filefd[0] = open(filename, O_RDONLY);
+ int rsize = read(filefd[0], readbuf, 32*1024);
+ eDebug("eConsoleAppContainer::readFromFile open(%s, O_RDONLY)=%i, read: %i", filename, filefd[0], rsize);
+ if ( filefd[0] > 0 && rsize > 0 )
+ write(readbuf, rsize);
+}
+
void eConsoleAppContainer::write( const char *data, int len )
{
char *tmp = new char[len];
memcpy(tmp, data, len);
outbuf.push(queue_data(tmp,len));
- out->start();
+ if (out)
+ out->start();
+}
+
+void eConsoleAppContainer::write( PyObject *data )
+{
+ char *buffer;
+ int length;
+ if (PyString_AsStringAndSize(data, &buffer, &length))
+ return;
+ if (buffer && length)
+ write(buffer, length);
}
void eConsoleAppContainer::readyWrite(int what)
{
if (what&eSocketNotifier::Write && outbuf.size() )
{
- queue_data d = outbuf.front();
- outbuf.pop();
- if ( ::write( fd[1], d.data, d.len ) != d.len )
- {
- /* emit */ dataSent(-1);
-// eDebug("writeError");
- }
+ queue_data &d = outbuf.front();
+ int wr = ::write( fd[1], d.data+d.dataSent, d.len-d.dataSent );
+ if (wr < 0)
+ eDebug("eConsoleAppContainer write failed (%m)");
else
+ d.dataSent += wr;
+ if (d.dataSent == d.len)
{
+ outbuf.pop();
+ delete [] d.data;
+ if ( filefd[0] == -1 )
/* emit */ dataSent(0);
-// eDebug("write ok");
}
- delete [] d.data;
}
if ( !outbuf.size() )
- out->stop();
+ {
+ if ( filefd[0] > 0 )
+ {
+ char readbuf[32*1024];
+ int rsize = read(filefd[0], readbuf, 32*1024);
+ if ( rsize > 0 )
+ write(readbuf, rsize);
+ else
+ {
+ close(filefd[0]);
+ filefd[0] = -1;
+ ::close(fd[1]);
+ eDebug("readFromFile done - closing eConsoleAppContainer stdin pipe");
+ fd[1]=-1;
+ dataSent(0);
+ out->stop();
+ }
+ }
+ else
+ out->stop();
+ }
}