[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klfbackend/klfblockprocess.cpp

00001 /***************************************************************************
00002  *   file klfblockprocess.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2011 by Philippe Faist
00005  *   philippe.faist@bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /* $Id: klfblockprocess.cpp 862 2013-11-23 11:10:54Z phfaist $ */
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; // exited
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   //  klfDbg("Running: "<<cmd<<", stdindata/size="<<stdindata.size());
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   // ** epstopdf bug in ubuntu: peek into executable, see if it is script. if it is, run with 'sh' on *nix's.
00082   // this is a weird bug with QProcess that will not execute some script files like epstopdf.
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       //klfDbg("cmd[0]="<<cmd[0]<<", Can't peek into file "<<fn<<"!") ;
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         // explicitely run with /usr/bin/env (we're on *nix, so OK)
00107         cmd.prepend("/usr/bin/env");
00108       }
00109     }
00110   }
00111     
00112 
00113 #endif
00114 
00115   QString program = cmd[0];
00116 
00117   //klfDbg("Running cmd="<<cmd);
00118   //klfDbg("env="<<env<<", curenv="<<environment());
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   //klfDbg("Starting "<<program<<", "<<args) ;
00128   start(program, args);
00129   if ( ! waitForStarted() ) {
00130     //klfDbg("Can't wait for started! Error="<<error()) ;
00131     return false;
00132   }
00133 
00134   write(stdindata.constData(), stdindata.size());
00135   closeWriteChannel();
00136 
00137   //klfDbg("wrote input data (size="<<stdindata.size()<<")") ;
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   // slot ourProcGotOutStdinData() should be called, which closes input
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) { // some error occurred somewhere
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 }

Generated by doxygen 1.7.3