00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <ctype.h>
00025
00026 #include <qprocess.h>
00027 #include <qapplication.h>
00028 #include <qeventloop.h>
00029 #include <qfileinfo.h>
00030
00031 #include "klfblockprocess.h"
00032
00033 KLFBlockProcess::KLFBlockProcess(QObject *p) : QProcess(p)
00034 {
00035 #ifdef KLFBACKEND_QT4
00036 mProcessAppEvents = true;
00037 connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ourProcExited()));
00038 #else
00039 connect(this, SIGNAL(wroteToStdin()), this, SLOT(ourProcGotOurStdinData()));
00040 connect(this, SIGNAL(processExited()), this, SLOT(ourProcExited()));
00041 #endif
00042 }
00043
00044
00045 KLFBlockProcess::~KLFBlockProcess()
00046 {
00047 }
00048
00049 void KLFBlockProcess::ourProcGotOurStdinData()
00050 {
00051 #ifndef KLFBACKEND_QT4
00052 closeStdin();
00053 #endif
00054 }
00055
00056 void KLFBlockProcess::ourProcExited()
00057 {
00058 _runstatus = 1;
00059 }
00060 #ifndef KLFBACKEND_QT4
00061 bool KLFBlockProcess::startProcess(QStringList cmd, QCString str, QStringList env)
00062 {
00063 return startProcess(cmd, QByteArray().duplicate(str.data(), str.length()), env);
00064 }
00065 #endif
00066 bool KLFBlockProcess::startProcess(QStringList cmd, QStringList env)
00067 {
00068 return startProcess(cmd, QByteArray(), env);
00069 }
00070
00071 bool KLFBlockProcess::startProcess(QStringList cmd, QByteArray stdindata, QStringList env)
00072 {
00073
00074
00075 _runstatus = 0;
00076
00077 KLF_ASSERT_CONDITION(cmd.size(), "Empty command list given.", return false;) ;
00078
00079 #if defined(Q_OS_UNIX) && defined(KLFBACKEND_QT4)
00080
00081
00082
00083
00084 {
00085 QString fn = cmd[0];
00086 if (!QFile::exists(fn))
00087 fn = klfSearchPath(cmd[0]);
00088 QFile fpeek(fn);
00089 if (!fpeek.open(QIODevice::ReadOnly)) {
00090
00091 } else {
00092 QByteArray line;
00093 int n = 0, j;
00094 bool isbinary = false;
00095 while (n++ < 3 && (line = fpeek.readLine()).size()) {
00096 for (j = 0; j < line.size(); ++j) {
00097 if ( ! isascii(line[j]) ) {
00098 isbinary = true;
00099 break;
00100 }
00101 }
00102 if (isbinary)
00103 break;
00104 }
00105 if (!isbinary) {
00106
00107 cmd.prepend("/usr/bin/env");
00108 }
00109 }
00110 }
00111
00112
00113 #endif
00114
00115 QString program = cmd[0];
00116
00117
00118
00119
00120 #ifdef KLFBACKEND_QT4
00121 if (env.size() > 0) {
00122 setEnvironment(env);
00123 }
00124
00125 QStringList args = cmd;
00126 args.erase(args.begin());
00127
00128 start(program, args);
00129 if ( ! waitForStarted() ) {
00130
00131 return false;
00132 }
00133
00134 write(stdindata.constData(), stdindata.size());
00135 closeWriteChannel();
00136
00137
00138
00139 #else
00140 setArguments(cmd);
00141 QStringList *e = &env;
00142 if (e->size() == 0)
00143 e = 0;
00144
00145 if (! start(e) )
00146 return false;
00147
00148 writeToStdin(stdindata);
00149
00150 #endif
00151
00152 #ifdef KLFBACKEND_QT4
00153 if (mProcessAppEvents) {
00154 while (_runstatus == 0) {
00155 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
00156 }
00157 } else {
00158 if (!waitForFinished()) {
00159 klfDbg("Can't wait for finished!");
00160 return false;
00161 }
00162 }
00163 klfDbg("Process should have finished now.");
00164 #else
00165 while (_runstatus == 0) {
00166 qApp->processEvents(QEventLoop::ExcludeUserInput);
00167 }
00168 #endif
00169
00170 if (_runstatus < 0) {
00171 klfDbg("some error occurred, _runstatus="+QString("%1").arg(_runstatus)) ;
00172 return false;
00173 }
00174
00175 return true;
00176 }
00177
00178
00179
00180 KLF_EXPORT QStringList klf_cur_environ()
00181 {
00182 QStringList curenvironment;
00183 #ifdef KLFBACKEND_QT4
00184 curenvironment = QProcess::systemEnvironment();
00185 #else
00186 extern char ** environ;
00187 int k;
00188 for (k = 0; environ[k] != NULL; ++k) {
00189 curenvironment.append(QString::fromLocal8Bit(environ[k]));
00190 }
00191 #endif
00192 return curenvironment;
00193 }