[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfblockprocess.cpp
1 /***************************************************************************
2  * file klfblockprocess.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist@bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfblockprocess.cpp 748 2012-01-01 15:06:40Z phfaist $ */
23 
24 #include <qprocess.h>
25 #include <qapplication.h>
26 #include <qeventloop.h>
27 #include <qfileinfo.h>
28 
29 #include "klfblockprocess.h"
30 
32 {
33 #ifdef KLFBACKEND_QT4
34  mProcessAppEvents = true;
35  connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ourProcExited()));
36 #else
37  connect(this, SIGNAL(wroteToStdin()), this, SLOT(ourProcGotOurStdinData()));
38  connect(this, SIGNAL(processExited()), this, SLOT(ourProcExited()));
39 #endif
40 }
41 
42 
44 {
45 }
46 
47 void KLFBlockProcess::ourProcGotOurStdinData()
48 {
49 #ifndef KLFBACKEND_QT4
50  closeStdin();
51 #endif
52 }
53 
54 void KLFBlockProcess::ourProcExited()
55 {
56  _runstatus = 1; // exited
57 }
58 #ifndef KLFBACKEND_QT4
59 bool KLFBlockProcess::startProcess(QStringList cmd, QCString str, QStringList env)
60 {
61  return startProcess(cmd, QByteArray().duplicate(str.data(), str.length()), env);
62 }
63 #endif
65 {
66  return startProcess(cmd, QByteArray(), env);
67 }
68 
70 {
71  klfDbg("Running: "<<cmd<<", stdindata/size="<<stdindata.size());
72 
73  _runstatus = 0;
74 
75  KLF_ASSERT_CONDITION(cmd.size(), "Empty command list given.", return false;) ;
76 
77 #if defined(Q_OS_UNIX) && defined(KLFBACKEND_QT4)
78 
79  // ** epstopdf bug in ubuntu: peek into executable, see if it is script. if it is, run with 'sh' on *nix's.
80  // this is a weird bug with QProcess that will not execute some script files like epstopdf.
81 
82  { QString fn = cmd[0];
83  if (!QFile::exists(fn))
84  fn = klfSearchPath(cmd[0]);
85  QFile fpeek(fn);
86  if (!fpeek.open(QIODevice::ReadOnly)) {
87  klfDbg("cmd[0]="<<cmd[0]<<", Can't peek into file "<<fn<<"!") ;
88  } else {
89  QByteArray line;
90  int n = 0, j;
91  bool isbinary = false;
92  while (n++ < 3 && (line = fpeek.readLine()).size()) {
93  for (j = 0; j < line.size(); ++j) {
94  if ((int)line[j] > 127 || (int)line[j] < 0) {
95  isbinary = true;
96  break;
97  }
98  }
99  if (isbinary)
100  break;
101  }
102  if (!isbinary) {
103  // explicitely add the shell (we're on *nix, so OK)
104  cmd.prepend("sh");
105  }
106  }
107  }
108 
109 
110 #endif
111 
112  QString program = cmd[0];
113 
114  klfDbg("Running cmd="<<cmd);
115  klfDbg("env="<<env<<", curenv="<<environment());
116 
117 #ifdef KLFBACKEND_QT4
118  if (env.size() > 0) {
119  setEnvironment(env);
120  }
121 
122  QStringList args = cmd;
123  args.erase(args.begin());
124  klfDbg("Starting "<<program<<", "<<args) ;
125  start(program, args);
126  if ( ! waitForStarted() ) {
127  klfDbg("Can't wait for started! Error="<<error()) ;
128  return false;
129  }
130 
131  write(stdindata.constData(), stdindata.size());
133 
134  klfDbg("wrote input data (size="<<stdindata.size()<<")") ;
135 
136 #else
137  setArguments(cmd);
138  QStringList *e = &env;
139  if (e->size() == 0)
140  e = 0;
141 
142  if (! start(e) )
143  return false;
144 
145  writeToStdin(stdindata);
146  // slot ourProcGotOutStdinData() should be called, which closes input
147 #endif
148 
149 #ifdef KLFBACKEND_QT4
150  if (mProcessAppEvents) {
151  while (_runstatus == 0) {
152  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
153  }
154  } else {
155  if (!waitForFinished()) {
156  klfDbg("Can't wait for finished!");
157  return false;
158  }
159  }
160  klfDbg("Process should have finished now.");
161 #else
162  while (_runstatus == 0) {
163  qApp->processEvents(QEventLoop::ExcludeUserInput);
164  }
165 #endif
166 
167  if (_runstatus < 0) { // some error occurred somewhere
168  klfDbg("some error occurred, _runstatus="<<_runstatus) ;
169  return false;
170  }
171 
172  return true;
173 }
174 
175 
176 
177 KLF_EXPORT QStringList klf_cur_environ()
178 {
179  QStringList curenvironment;
180 #ifdef KLFBACKEND_QT4
181  curenvironment = QProcess::systemEnvironment();
182 #else
183  extern char ** environ;
184  int k;
185  for (k = 0; environ[k] != NULL; ++k) {
186  curenvironment.append(QString::fromLocal8Bit(environ[k]));
187  }
188 #endif
189  return curenvironment;
190 }

Generated by doxygen 1.8.2