]> git.lyx.org Git - lyx.git/blob - src/support/Systemcall.cpp
bed1f45f500df712b5dd11e5eb17b563760c274d
[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         process->start(cmd);
64         if (!process->waitForStarted(3000)) {
65                 LYXERR0("Qprocess " << cmd << " did not start!");
66                 LYXERR0("error " << process->error());
67                 LYXERR0("state " << process->state());
68                 LYXERR0("status " << process->exitStatus());
69                 return 10;
70         }
71         if (how == DontWait)
72                 return 0;
73
74         if (!process->waitForFinished(180000)) {
75                 LYXERR0("Qprocess " << cmd << " did not finished!");
76                 LYXERR0("error " << process->error());
77                 LYXERR0("state " << process->state());
78                 LYXERR0("status " << process->exitStatus());
79                 return 20;
80         }
81         int const exit_code = process->exitCode();
82         if (exit_code) {
83                 LYXERR0("Qprocess " << cmd << " finished!");
84                 LYXERR0("exitCode " << process->exitCode());
85                 LYXERR0("error " << process->error());
86                 LYXERR0("state " << process->state());
87                 LYXERR0("status " << process->exitStatus());
88         }
89         cout << fromqstr(QString::fromLocal8Bit(process->readAllStandardOutput().data())) << endl;
90         cerr << fromqstr(QString::fromLocal8Bit(process->readAllStandardError().data())) << endl;
91         killProcess(process);
92         return exit_code;
93 #endif
94 }
95
96 } // namespace support
97 } // namespace lyx