]> git.lyx.org Git - lyx.git/blob - src/support/SystemcallPrivate.h
More requires --> required, for C++2a.
[lyx.git] / src / support / SystemcallPrivate.h
1 // -*- C++ -*-
2 /**
3  * \file SystemcallPrivate.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef SYSTEMCALLPRIVATE_H
13 #define SYSTEMCALLPRIVATE_H
14
15 #include <QObject>
16 #include <QProcess>
17
18 #include <string>
19
20 namespace lyx {
21 namespace support {
22
23 class Systemcall;
24
25 /**
26  * Outputs to the console terminal the line buffered standard output and
27  * error of a spawned process when there is a controlling terminal and
28  * stdout/stderr have not been redirected.
29  */
30 class SystemcallPrivate : public QObject
31 {
32         Q_OBJECT
33
34 public:
35         SystemcallPrivate(std::string const & infile, std::string const & outfile,
36                           std::string const & errfile);
37         ~SystemcallPrivate();
38
39         enum State {
40                 Starting,
41                 Running,
42                 Finished,
43                 Error,
44                 Killed
45         };
46         State state;
47
48         bool waitWhile(State, bool processEvents, int timeout = -1);
49         void startProcess(QString const & cmd, std::string const & path,
50                           std::string const & lpath, bool detach);
51
52         int exitCode();
53
54         QString errorMessage() const;
55         QString exitStatusMessage() const;
56
57         QProcess* releaseProcess();
58
59         static void killProcess(QProcess * p);
60
61         // when true, kill any running script ASAP
62         static bool kill_script;
63
64
65 public Q_SLOTS:
66         void stdOut();
67         void stdErr();
68         void processError(QProcess::ProcessError);
69         void processStarted();
70         void processFinished(int, QProcess::ExitStatus status);
71
72
73 private:
74         /// Pointer to the process to monitor.
75         QProcess * process_;
76
77         /// Index to the standard output buffer.
78         size_t out_index_;
79         /// Index to the standard error buffer.
80         size_t err_index_;
81         ///
82         std::string in_file_;
83         ///
84         std::string out_file_;
85         ///
86         std::string err_file_;
87
88         /// Size of buffers.
89         static size_t const buffer_size_ = 200;
90         /// Standard output buffer.
91         char out_data_[buffer_size_];
92         /// Standard error buffer.
93         char err_data_[buffer_size_];
94
95         QString cmd_;
96         bool process_events_;
97
98         void waitAndProcessEvents();
99         void processEvents();
100         void killProcess();
101
102         /// returns false if we killed the process
103         /// actually returns Systemcall::ReturnValue
104         bool waitAndCheck();
105 };
106
107 } // namespace support
108 } // namespace lyx
109
110 #endif // SYSTEMCALLPRIVATE_H