]> git.lyx.org Git - lyx.git/blob - src/support/Systemcall.cpp
3d01683c96b8dfa765baed8724ae7817a326ce4a
[lyx.git] / src / support / Systemcall.cpp
1 /**
2  * \file Systemcall.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  *
8  * Interface cleaned up by
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "support/debug.h"
17 #include "support/qstring_helpers.h"
18 #include "support/Systemcall.h"
19 #include "support/os.h"
20
21 #include <cstdlib>
22 #include <iostream>
23
24 #include <QProcess>
25
26 #define DISABLE_EVALUATE_QPROCESS
27
28 using namespace std;
29
30 namespace lyx {
31 namespace support {
32
33 static void killProcess(QProcess * p)
34 {
35         p->closeReadChannel(QProcess::StandardOutput);
36         p->closeReadChannel(QProcess::StandardError);
37         p->close();
38         delete p;
39 }
40
41
42 // Reuse of instance
43 int Systemcall::startscript(Starttype how, string const & what)
44 {
45 #ifdef DISABLE_EVALUATE_QPROCESS
46         string command = what;
47
48         if (how == DontWait) {
49                 switch (os::shell()) {
50                 case os::UNIX:
51                         command += " &";
52                         break;
53                 case os::CMD_EXE:
54                         command = "start /min " + command;
55                         break;
56                 }
57         }
58
59         return ::system(command.c_str());
60 #else
61         QString cmd = QString::fromLocal8Bit(what.c_str());
62         QProcess * process = new QProcess;
63         cmd.replace("python", "python2.5");
64         cmd.prepend("/usr/bin/");
65         process->start(cmd);
66         if (!process->waitForStarted(1000)) {
67                 LYXERR0("Qprocess " << cmd << " did not start!");
68                 LYXERR0("error " << process->error());
69                 LYXERR0("state " << process->state());
70                 LYXERR0("status " << process->exitStatus());
71                 return 10;
72         }
73         if (how == DontWait)
74                 return 0;
75
76         if (!process->waitForFinished(30000)) {
77                 LYXERR0("Qprocess " << cmd << " did not finished!");
78                 LYXERR0("error " << process->error());
79                 LYXERR0("state " << process->state());
80                 LYXERR0("status " << process->exitStatus());
81                 return 20;
82         }
83         int const exit_code = process->exitCode();
84         if (exit_code) {
85                 LYXERR0("Qprocess " << cmd << " finished!");
86                 LYXERR0("exitCode " << process->exitCode());
87                 LYXERR0("error " << process->error());
88                 LYXERR0("state " << process->state());
89                 LYXERR0("status " << process->exitStatus());
90         }
91         cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl;
92         cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl;
93         killProcess(process);
94         return exit_code;
95 #endif
96 }
97
98 } // namespace support
99 } // namespace lyx