incorporate new class to support own asynchronous implementation of a "popen" system...
[enigma2.git] / lib / base / console.cpp
index 0a01094be3c153390623cd413591ede55696e159..9593dacbf2669a0575ba33f8fcfaaff1b5e3a71b 100644 (file)
@@ -59,28 +59,10 @@ eConsoleAppContainer::eConsoleAppContainer()
 :pid(-1), killstate(0), in(0), out(0), err(0)
 {
        for (int i=0; i < 3; ++i)
+       {
                fd[i]=-1;
-}
-
-static char brakets[][2] = {
-       { '\'','\'' },
-       {'"','"'},
-       {'`','`'},
-       {'(',')'},
-       {'{','}'},
-       {'[',']'},
-       {'<','>'}
-};
-
-static char *find_bracket(char ch)
-{
-       size_t idx=0;
-       while (idx < sizeof(brakets)/2) {
-               if (brakets[idx][0] == ch)
-                       return &brakets[idx][0];
-               ++idx;
+               filefd[i]=-1;
        }
-       return NULL;
 }
 
 int eConsoleAppContainer::setCWD( const char *path )
@@ -99,70 +81,13 @@ int eConsoleAppContainer::setCWD( const char *path )
 
 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;
-       memcpy(buf, cmd, slen+1);
-
-//     printf("cmd = %s, len %d\n", cmd, slen);
-
-       // kill spaces at beginning
-       while(path[0] == ' ') {
-               ++path;
-               ++cmds;
-               --slen;
-       }
-
-       // kill spaces at the end
-       while(slen && path[slen-1] == ' ') {
-               path[slen-1] = 0;
-               --slen;
-       }
-
-       if (!slen)
-               return -2;
-
-       tmp = strchr(path, ' ');
-       if (tmp) {
-               *tmp = 0;
-               cmds = tmp+1;
-               while(*cmds && *cmds == ' ')
-                       ++cmds;
-       }
-       else
-               cmds = path+slen;
-
-       memset(argv, 0, sizeof(argv));
-       argv[cnt++] = path;
-
-       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
-                       }
-                       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
-       }
+       int argc = 3;
+       const char *argv[argc + 1];
+       argv[0] = "/bin/sh";
+       argv[1] = "-c";
+       argv[2] = cmd;
+       argv[argc] = NULL;
 
-//     int tmp=0;
-//     while(argv[tmp])
-//             eDebug("%d is %s", tmp, argv[tmp++]);
        return execute(argv[0], argv);
 }
 
@@ -244,6 +169,12 @@ void 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()
@@ -259,6 +190,17 @@ void eConsoleAppContainer::sendCtrlC()
        }
 }
 
+void eConsoleAppContainer::sendEOF()
+{
+       if (out)
+               out->stop();
+       if (fd[1] != -1)
+       {
+               ::close(fd[1]);
+               fd[1]=-1;
+       }
+}
+
 void eConsoleAppContainer::closePipes()
 {
        if (in)
@@ -304,6 +246,9 @@ void eConsoleAppContainer::readyRead(int what)
                {
                        buf[rd]=0;
                        /*emit*/ dataAvail(buf);
+                       stdoutAvail(buf);
+                       if ( filefd[1] > 0 )
+                               ::write(filefd[1], buf, rd);
                        if (!hungup)
                                break;
                }
@@ -343,10 +288,29 @@ void eConsoleAppContainer::readyErrRead(int what)
                                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];
@@ -359,7 +323,7 @@ void eConsoleAppContainer::write( const char *data, int len )
 void eConsoleAppContainer::write( PyObject *data )
 {
        char *buffer;
-       int length;
+       Py_ssize_t length;
        if (PyString_AsStringAndSize(data, &buffer, &length))
                return;
        if (buffer && length)
@@ -370,7 +334,7 @@ void eConsoleAppContainer::readyWrite(int what)
 {
        if (what&eSocketNotifier::Write && outbuf.size() )
        {
-               queue_data d = outbuf.front();
+               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)");
@@ -380,9 +344,30 @@ void eConsoleAppContainer::readyWrite(int what)
                {
                        outbuf.pop();
                        delete [] d.data;
+                       if ( filefd[0] == -1 )
                        /* emit */ dataSent(0);
                }
        }
        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();
+       }
 }