]> git.lyx.org Git - features.git/blob - src/support/Systemcall.cpp
Allow to separately redirect stdout and stderr.
[features.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
64         // Qt won't start the process if we redirect stdout/stderr in
65         // this way and they are not connected to a terminal (maybe
66         // because we were launched from some desktop GUI).
67         if (os::is_terminal(os::STDOUT))
68                 process->setStandardOutputFile(toqstr(os::stdoutdev()));
69         if (os::is_terminal(os::STDERR))
70                 process->setStandardErrorFile(toqstr(os::stderrdev()));
71
72         process->start(cmd);
73         if (!process->waitForStarted(3000)) {
74                 LYXERR0("Qprocess " << cmd << " did not start!");
75                 LYXERR0("error " << process->error());
76                 LYXERR0("state " << process->state());
77                 LYXERR0("status " << process->exitStatus());
78                 return 10;
79         }
80         if (how == DontWait)
81                 return 0;
82
83         if (!process->waitForFinished(180000)) {
84                 LYXERR0("Qprocess " << cmd << " did not finished!");
85                 LYXERR0("error " << process->error());
86                 LYXERR0("state " << process->state());
87                 LYXERR0("status " << process->exitStatus());
88                 return 20;
89         }
90         int const exit_code = process->exitCode();
91         if (exit_code) {
92                 LYXERR0("Qprocess " << cmd << " finished!");
93                 LYXERR0("exitCode " << process->exitCode());
94                 LYXERR0("error " << process->error());
95                 LYXERR0("state " << process->state());
96                 LYXERR0("status " << process->exitStatus());
97         }
98
99         // If the output has been redirected, we write it all at once.
100         // Even if we are not running in a terminal, the output could go
101         // to some log file, for example ~/.xsession-errors on *nix.
102         if (!os::is_terminal(os::STDOUT))
103                 cout << fromqstr(QString::fromLocal8Bit(
104                             process->readAllStandardOutput().data()));
105         if (!os::is_terminal(os::STDERR))
106                 cerr << fromqstr(QString::fromLocal8Bit(
107                             process->readAllStandardError().data()));
108
109         killProcess(process);
110         return exit_code;
111 #endif
112 }
113
114 } // namespace support
115 } // namespace lyx