]> git.lyx.org Git - features.git/blob - src/support/Systemcall.cpp
Take into account output redirection when spawning a program.
[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/lstrings.h"
18 #include "support/qstring_helpers.h"
19 #include "support/Systemcall.h"
20 #include "support/os.h"
21
22 #include <cstdlib>
23 #include <iostream>
24
25 #include <QProcess>
26
27 #define USE_QPROCESS
28
29 using namespace std;
30
31 namespace lyx {
32 namespace support {
33
34 static void killProcess(QProcess * p)
35 {
36         p->closeReadChannel(QProcess::StandardOutput);
37         p->closeReadChannel(QProcess::StandardError);
38         p->close();
39         delete p;
40 }
41
42
43 // Reuse of instance
44 #ifndef USE_QPROCESS
45 int Systemcall::startscript(Starttype how, string const & what)
46 {
47         string command = what;
48
49         if (how == DontWait) {
50                 switch (os::shell()) {
51                 case os::UNIX:
52                         command += " &";
53                         break;
54                 case os::CMD_EXE:
55                         command = "start /min " + command;
56                         break;
57                 }
58         }
59
60         return ::system(command.c_str());
61 }
62
63 #else
64
65 namespace {
66
67 string const parsecmd(string const & cmd, string & outfile)
68 {
69         bool inquote = false;
70         bool escaped = false;
71
72         for (size_t i = 0; i < cmd.length(); ++i) {
73                 char c = cmd[i];
74                 if (c == '"' && !escaped)
75                         inquote = !inquote;
76                 else if (c == '\\' && !escaped)
77                         escaped = !escaped;
78                 else if (c == '>' && !(inquote || escaped)) {
79                         outfile = trim(cmd.substr(i + 1), " \"");
80                         return trim(cmd.substr(0, i));
81                 } else
82                         escaped = false;
83         }
84         outfile.erase();
85         return cmd;
86
87 }
88
89 } // namespace anon
90
91
92 int Systemcall::startscript(Starttype how, string const & what)
93 {
94         string outfile;
95         QString cmd = toqstr(parsecmd(what, outfile));
96         QProcess * process = new QProcess;
97
98         // Qt won't start the process if we redirect stdout/stderr in
99         // this way and they are not connected to a terminal (maybe
100         // because we were launched from some desktop GUI).
101         if (!outfile.empty())
102                 process->setStandardOutputFile(toqstr(outfile));
103         else if (os::is_terminal(os::STDOUT))
104                 process->setStandardOutputFile(toqstr(os::stdoutdev()));
105         if (os::is_terminal(os::STDERR))
106                 process->setStandardErrorFile(toqstr(os::stderrdev()));
107
108         process->start(cmd);
109         if (!process->waitForStarted(3000)) {
110                 LYXERR0("Qprocess " << cmd << " did not start!");
111                 LYXERR0("error " << process->error());
112                 LYXERR0("state " << process->state());
113                 LYXERR0("status " << process->exitStatus());
114                 return 10;
115         }
116         if (how == DontWait)
117                 return 0;
118
119         if (!process->waitForFinished(180000)) {
120                 LYXERR0("Qprocess " << cmd << " did not finished!");
121                 LYXERR0("error " << process->error());
122                 LYXERR0("state " << process->state());
123                 LYXERR0("status " << process->exitStatus());
124                 return 20;
125         }
126         int const exit_code = process->exitCode();
127         if (exit_code) {
128                 LYXERR0("Qprocess " << cmd << " finished!");
129                 LYXERR0("exitCode " << process->exitCode());
130                 LYXERR0("error " << process->error());
131                 LYXERR0("state " << process->state());
132                 LYXERR0("status " << process->exitStatus());
133         }
134
135         // If the output has been redirected, we write it all at once.
136         // Even if we are not running in a terminal, the output could go
137         // to some log file, for example ~/.xsession-errors on *nix.
138         if (!os::is_terminal(os::STDOUT) && outfile.empty())
139                 cout << fromqstr(QString::fromLocal8Bit(
140                             process->readAllStandardOutput().data()));
141         if (!os::is_terminal(os::STDERR))
142                 cerr << fromqstr(QString::fromLocal8Bit(
143                             process->readAllStandardError().data()));
144
145         killProcess(process);
146         return exit_code;
147 }
148 #endif
149
150 } // namespace support
151 } // namespace lyx