summaryrefslogtreecommitdiff
path: root/libopie/oprocess.cpp
Side-by-side diff
Diffstat (limited to 'libopie/oprocess.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/oprocess.cpp94
1 files changed, 49 insertions, 45 deletions
diff --git a/libopie/oprocess.cpp b/libopie/oprocess.cpp
index f3e52bd..5db2b6c 100644
--- a/libopie/oprocess.cpp
+++ b/libopie/oprocess.cpp
@@ -49,112 +49,132 @@
#define _MAY_INCLUDE_KPROCESSCONTROLLER_
#include "oprocctrl.h"
//#include <config.h>
#include <qfile.h>
#include <qsocketnotifier.h>
#include <qregexp.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_INITGROUPS
#include <grp.h>
#endif
#include <pwd.h>
#include <qapplication.h>
#include <qmap.h>
//#include <kdebug.h>
/////////////////////////////
// public member functions //
/////////////////////////////
class OProcessPrivate {
public:
OProcessPrivate() : useShell(false) { }
bool useShell;
QMap<QString,QString> env;
QString wd;
QCString shell;
};
-OProcess::OProcess()
- : QObject(),
- run_mode(NotifyOnExit),
- runs(false),
- pid_(0),
- status(0),
- keepPrivs(false),
- innot(0),
- outnot(0),
- errnot(0),
- communication(NoCommunication),
- input_data(0),
- input_sent(0),
- input_total(0),
- d(0)
+OProcess::OProcess(QObject *parent, const char *name)
+ : QObject(parent, name)
{
+ init ( );
+}
+
+OProcess::OProcess(const QString &arg0, QObject *parent, const char *name)
+ : QObject(parent, name)
+{
+ init ( );
+ *this << arg0;
+}
+
+OProcess::OProcess(const QStringList &args, QObject *parent, const char *name)
+ : QObject(parent, name)
+{
+ init ( );
+ *this << args;
+}
+
+void OProcess::init ( )
+{
+ run_mode = NotifyOnExit;
+ runs = false;
+ pid_ = 0;
+ status = 0;
+ keepPrivs = false;
+ innot = 0;
+ outnot = 0;
+ errnot = 0;
+ communication = NoCommunication;
+ input_data = 0;
+ input_sent = 0;
+ input_total = 0;
+ d = 0;
+
if (0 == OProcessController::theOProcessController) {
(void) new OProcessController();
CHECK_PTR(OProcessController::theOProcessController);
}
OProcessController::theOProcessController->addOProcess(this);
out[0] = out[1] = -1;
in[0] = in[1] = -1;
err[0] = err[1] = -1;
}
void
OProcess::setEnvironment(const QString &name, const QString &value)
{
if (!d)
d = new OProcessPrivate;
d->env.insert(name, value);
}
void
OProcess::setWorkingDirectory(const QString &dir)
{
if (!d)
d = new OProcessPrivate;
d->wd = dir;
}
void
OProcess::setupEnvironment()
{
if (d)
{
QMap<QString,QString>::Iterator it;
for(it = d->env.begin(); it != d->env.end(); ++it)
setenv(QFile::encodeName(it.key()).data(),
QFile::encodeName(it.data()).data(), 1);
if (!d->wd.isEmpty())
chdir(QFile::encodeName(d->wd).data());
}
}
void
OProcess::setRunPrivileged(bool keepPrivileges)
{
keepPrivs = keepPrivileges;
}
bool
@@ -427,96 +447,110 @@ bool OProcess::isRunning() const
pid_t OProcess::pid() const
{
return pid_;
}
bool OProcess::normalExit() const
{
int _status = status;
return (pid_ != 0) && (!runs) && (WIFEXITED((_status)));
}
int OProcess::exitStatus() const
{
int _status = status;
return WEXITSTATUS((_status));
}
bool OProcess::writeStdin(const char *buffer, int buflen)
{
bool rv;
// if there is still data pending, writing new data
// to stdout is not allowed (since it could also confuse
// kprocess...
if (0 != input_data)
return false;
if (runs && (communication & Stdin)) {
input_data = buffer;
input_sent = 0;
input_total = buflen;
slotSendData(0);
innot->setEnabled(true);
rv = true;
} else
rv = false;
return rv;
}
+void OProcess::flushStdin ( )
+{
+ if ( !input_data || ( input_sent == input_total ))
+ return;
+
+ int d1, d2;
+
+ do {
+ d1 = input_total - input_sent;
+ slotSendData ( 0 );
+ d2 = input_total - input_sent;
+ } while ( d2 <= d1 );
+}
+
void OProcess::suspend()
{
if ((communication & Stdout) && outnot)
outnot->setEnabled(false);
}
void OProcess::resume()
{
if ((communication & Stdout) && outnot)
outnot->setEnabled(true);
}
bool OProcess::closeStdin()
{
bool rv;
if (communication & Stdin) {
communication = (Communication) (communication & ~Stdin);
delete innot;
innot = 0;
close(in[1]);
rv = true;
} else
rv = false;
return rv;
}
bool OProcess::closeStdout()
{
bool rv;
if (communication & Stdout) {
communication = (Communication) (communication & ~Stdout);
delete outnot;
outnot = 0;
close(out[0]);
rv = true;
} else
rv = false;
return rv;
}
bool OProcess::closeStderr()
{
bool rv;
if (communication & Stderr) {
communication = static_cast<Communication>(communication & ~Stderr);
@@ -843,80 +877,50 @@ QString OProcess::quote(const QString &arg)
QString::fromLatin1("'\"'\"'"));
res.prepend('\'');
res.append('\'');
return res;
}
QCString OProcess::searchShell()
{
QCString tmpShell = QCString(getenv("SHELL")).stripWhiteSpace();
if (!isExecutable(tmpShell))
{
tmpShell = "/bin/sh";
}
return tmpShell;
}
bool OProcess::isExecutable(const QCString &filename)
{
struct stat fileinfo;
if (filename.isEmpty()) return false;
// CC: we've got a valid filename, now let's see whether we can execute that file
if (-1 == stat(filename.data(), &fileinfo)) return false;
// CC: return false if the file does not exist
// CC: anyway, we cannot execute directories, block/character devices, fifos or sockets
if ( (S_ISDIR(fileinfo.st_mode)) ||
(S_ISCHR(fileinfo.st_mode)) ||
(S_ISBLK(fileinfo.st_mode)) ||
#ifdef S_ISSOCK
// CC: SYSVR4 systems don't have that macro
(S_ISSOCK(fileinfo.st_mode)) ||
#endif
(S_ISFIFO(fileinfo.st_mode)) ||
(S_ISDIR(fileinfo.st_mode)) ) {
return false;
}
// CC: now check for permission to execute the file
if (access(filename.data(), X_OK) != 0) return false;
// CC: we've passed all the tests...
return true;
}
-void OProcess::virtual_hook( int, void* )
-{ /*BASE::virtual_hook( id, data );*/ }
-
-
-///////////////////////////
-// CC: Class KShellProcess
-///////////////////////////
-
-KShellProcess::KShellProcess(const char *shellname):
- OProcess()
-{
- setUseShell(true, shellname);
-}
-
-
-KShellProcess::~KShellProcess() {
-}
-
-QString KShellProcess::quote(const QString &arg)
-{
- return OProcess::quote(arg);
-}
-
-bool KShellProcess::start(RunMode runmode, Communication comm)
-{
- return OProcess::start(runmode, comm);
-}
-void KShellProcess::virtual_hook( int id, void* data )
-{ OProcess::virtual_hook( id, data ); }
-//#include "kprocess.moc"