[KLF Backend][KLF Tools][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$ */
23
24#include <QProcess>
25#include <QCoreApplication>
26#include <QEventLoop>
27#include <QFile>
28#include <QThread>
29
30#include <klfutil.h>
31#include <klfsysinfo.h>
32#include "klfblockprocess.h"
33
34static bool is_binary_file(QString fn)
35{
36 klfDbg("is_binary_file("<<fn<<")") ;
37 if (!QFile::exists(fn)) {
39 klfDbg("is_binary_file: file doesn't exist directly, path search gave "<<fn) ;
40 }
41 QFile fpeek(fn);
42 if (!fpeek.open(QIODevice::ReadOnly)) {
43 klfDbg("fn="<<fn<<", Can't peek into file "<<fn<<"!") ;
44 return true; // assumption by default
45 }
47 int n = 0, j;
48 while (n++ < 5 && (line = fpeek.readLine(1024)).size()) {
49 for (j = 0; j < line.size(); ++j) {
50 if ((int)line[j] >= 127 || (int)line[j] <= 0) {
51 klfDbg("fn="<<fn<<" found binary char '"<<(char)line[j]<<"'=="<<(int)line[j]
52 <<" on line "<<n<<", column "<< j) ;
53 return true;
54 }
55 }
56 }
57 klfDbg("fn="<<fn<<", file seems to be ascii based on the first few lines") ;
58 return false;
59}
60
61#if defined(Q_OS_WIN)
62const static QString script_extra_paths = QString("C:\\Python27;C:\\Python*");
63const static QString exe_suffix = ".exe";
64#elif defined(Q_OS_MAC)
65const static QString script_extra_paths =
66 "/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/local/sbin" // general unix
67 "/usr/local/opt/*/bin:/opt/local/bin:" // homebrew
68 "/opt/local/sbin" // macports
69 "/Library/TeX/texbin:/usr/texbin"; // mactex binaries
70const static QString exe_suffix = "";
71#else
72const static QString script_extra_paths =
73 "/usr/bin:/bin:/usr/local/bin:/usr/sbin:/sbin:/usr/local/sbin"; // general unix paths
74const static QString exe_suffix = "";
75#endif
76
77
78// static
79QString KLFBlockProcess::detectInterpreterPath(const QString& interp, const QStringList & addpaths)
80{
81 QString search_paths = script_extra_paths;
82 search_paths += addpaths.join(KLF_PATH_SEP);
83 // first, try exact name (python.exe, bash.exe)
84 QString s = klfSearchPath(interp+exe_suffix, script_extra_paths);
85 if (!s.isEmpty()) {
86 return s;
87 }
88 // otherwise, try name with some suffix (e.g. python2.exe) -- dont directly try with
89 // wildcard, because we want the exact name if it exists (and not some other program,
90 // such as 'bashbug', which happened to be found first)
91 return klfSearchPath(interp+"*"+exe_suffix, script_extra_paths);
92}
93
94
95
96
98 : QProcess(p)
99{
100 mProcessAppEvents = true;
101 connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ourProcExited()));
102}
103
104
108
109void KLFBlockProcess::ourProcGotOurStdinData()
110{
111}
112
113void KLFBlockProcess::ourProcExited()
114{
115 _runstatus = 1; // exited
116}
117
119{
121 klfDbg("ext = " << ext) ;
122 if (ext == "py") {
123 return detectInterpreterPath("python");
124 } else if (ext == "sh") {
125 return detectInterpreterPath("bash");
126 } else if (ext == "rb") {
127 return detectInterpreterPath("ruby");
128 }
129 return QString();
130}
131
136
138{
140
141 klfDbg("Running: "<<cmd<<", stdindata/size="<<stdindata.size());
142
143 _runstatus = 0;
144
145 KLF_ASSERT_CONDITION(cmd.size(), "Empty command list given.", return false;) ;
146
147 // For scripts, use the interpreter explicitly. This so that the script doesn't have to
148 // be executable, and also for an old bug on Ubuntu with epstopdf.
149 //
150 // We peek into executable to see if it is script. If it is, use the correct interpreter.
151
152 if (!is_binary_file(cmd[0])) {
153 // check what script type it is and invoke the corresponding interpreter.
154 QString ext = cmd[0].split('.').last();
156 if (exec_proc.size()) {
157 cmd.prepend(exec_proc);
158 }
159 }
160
161 QString program = cmd[0];
162
163 klfDbg("Running cmd="<<cmd);
164 klfDbg("env="<<env<<", curenv="<<environment());
165
166 if (env.size() > 0) {
168 }
169
171 args.erase(args.begin());
172 klfDbg("Starting "<<program<<", "<<args) ;
174 if ( ! waitForStarted() ) {
175 klfDbg("Can't wait for started! Error="<<error()) ;
176 return false;
177 }
178
179 write(stdindata.constData(), stdindata.size());
181
182 klfDbg("wrote input data (size="<<stdindata.size()<<")") ;
183
184 if (mProcessAppEvents) {
185 klfDbg("letting current thread (="<<QThread::currentThread()<<") process events ...") ;
186 while (_runstatus == 0) {
187 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents, 1000);
188 klfDbg("events processed, maybe more?") ;
189 }
190 } else {
191 if (!waitForFinished()) {
192 klfDbg("Can't wait for finished!");
193 return false;
194 }
195 }
196 klfDbg("Process should have finished now.");
197
198 if (_runstatus < 0) { // some error occurred somewhere
199 klfDbg("some error occurred, _runstatus="<<_runstatus) ;
200 return false;
201 }
202
203 return true;
204}
205
KLFBlockProcess(QObject *parent=0)
bool startProcess(QStringList cmd, QByteArray stdindata, QStringList env=QStringList())
virtual QString getInterpreterPath(const QString &ext)
The interpter path to use for the given extension.
Defines the KLFBlockProcess class.
#define KLF_DEBUG_BLOCK(msg)
#define KLF_ASSERT_CONDITION(expr, msg, failaction)
#define klfDbg(streamableItems)
KLF_EXPORT QString klfSearchPath(const QString &programName, const QString &extra_path)
void processEvents(QEventLoop::ProcessEventsFlags flags)
bool exists() const
qint64 write(const char *data, qint64 maxSize)
iterator begin()
iterator erase(iterator pos)
T & last()
int size() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringList environment() const
void finished(int exitCode)
void setEnvironment(const QStringList &environment)
void closeWriteChannel()
QProcess::ProcessError error() const
QString program() const
void start(const QString &program, const QStringList &arguments, OpenMode mode)
bool waitForFinished(int msecs)
bool waitForStarted(int msecs)
bool isEmpty() const
QThread * currentThread()

Generated by doxygen 1.10.0