-rw-r--r-- | libopie/oprocess.cpp | 94 |
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 @@ -33,144 +33,164 @@ // e9025461@student.tuwien.ac.at // // Changes: // // March 2nd, 1998: Changed parameter list for KShellProcess: // Arguments are now placed in a single string so that // <shell> -c <commandstring> is passed to the shell // to make the use of "operator<<" consistent with KProcess // // // Ported by Holger Freyther // <zekce> Harlekin: oprocess and say it was ported to Qt by the Opie developers an Qt 2 #include "oprocess.h" #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 OProcess::runPrivileged() const { return keepPrivs; } OProcess::~OProcess() { // destroying the OProcess instance sends a SIGKILL to the // child process (if it is running) after removing it from the // list of valid processes (if the process is not started as // "DontCare") OProcessController::theOProcessController->removeOProcess(this); // this must happen before we kill the child // TODO: block the signal while removing the current process from the process list @@ -411,128 +431,142 @@ bool OProcess::start(RunMode runmode, Communication comm) bool OProcess::kill(int signo) { bool rv=false; if (0 != pid_) rv= (-1 != ::kill(pid_, signo)); // probably store errno somewhere... return rv; } bool OProcess::isRunning() const { return runs; } 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); delete errnot; errnot = 0; close(err[0]); rv = true; } else rv = false; return rv; } ///////////////////////////// // protected slots // ///////////////////////////// @@ -827,96 +861,66 @@ void OProcess::commClose() } void OProcess::setUseShell(bool useShell, const char *shell) { if (!d) d = new OProcessPrivate; d->useShell = useShell; d->shell = shell; if (d->shell.isEmpty()) d->shell = searchShell(); } QString OProcess::quote(const QString &arg) { QString res = arg; res.replace(QRegExp(QString::fromLatin1("\'")), 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" |