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