]> git.lyx.org Git - features.git/blob - src/support/Systemcall.cpp
Fix problem with the nul device on Windows.
[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                 // Check whether we have to simply throw away the output.
103                 if (outfile != os::nulldev())
104                         process->setStandardOutputFile(toqstr(outfile));
105         } else if (os::is_terminal(os::STDOUT))
106                 process->setStandardOutputFile(toqstr(os::stdoutdev()));
107         if (os::is_terminal(os::STDERR))
108                 process->setStandardErrorFile(toqstr(os::stderrdev()));
109
110         process->start(cmd);
111         if (!process->waitForStarted(3000)) {
112                 LYXERR0("Qprocess " << cmd << " did not start!");
113                 LYXERR0("error " << process->error());
114                 LYXERR0("state " << process->state());
115                 LYXERR0("status " << process->exitStatus());
116                 return 10;
117         }
118         if (how == DontWait)
119                 return 0;
120
121         if (!process->waitForFinished(180000)) {
122                 LYXERR0("Qprocess " << cmd << " did not finished!");
123                 LYXERR0("error " << process->error());
124                 LYXERR0("state " << process->state());
125                 LYXERR0("status " << process->exitStatus());
126                 return 20;
127         }
128         int const exit_code = process->exitCode();
129         if (exit_code) {
130                 LYXERR0("Qprocess " << cmd << " finished!");
131                 LYXERR0("exitCode " << process->exitCode());
132                 LYXERR0("error " << process->error());
133                 LYXERR0("state " << process->state());
134                 LYXERR0("status " << process->exitStatus());
135         }
136
137         // If the output has been redirected, we write it all at once.
138         // Even if we are not running in a terminal, the output could go
139         // to some log file, for example ~/.xsession-errors on *nix.
140         if (!os::is_terminal(os::STDOUT) && outfile.empty())
141                 cout << fromqstr(QString::fromLocal8Bit(
142                             process->readAllStandardOutput().data()));
143         if (!os::is_terminal(os::STDERR))
144                 cerr << fromqstr(QString::fromLocal8Bit(
145                             process->readAllStandardError().data()));
146
147         killProcess(process);
148         return exit_code;
149 }
150 #endif
151
152 } // namespace support
153 } // namespace lyx