]> git.lyx.org Git - lyx.git/blob - src/support/SystemcallPrivate.h
3f00f0c3ba79fbdaf5b1094df1ee3f1e9654618f
[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
62 public Q_SLOTS:
63         void stdOut();
64         void stdErr();
65         void processError(QProcess::ProcessError);
66         void processStarted();
67         void processFinished(int, QProcess::ExitStatus status);
68
69
70 private:
71         /// Pointer to the process to monitor.
72         QProcess * process_;
73
74         /// Index to the standard output buffer.
75         size_t out_index_;
76         /// Index to the standard error buffer.
77         size_t err_index_;
78         ///
79         std::string in_file_;
80         ///
81         std::string out_file_;
82         ///
83         std::string err_file_;
84
85         /// Size of buffers.
86         static size_t const buffer_size_ = 200;
87         /// Standard output buffer.
88         char out_data_[buffer_size_];
89         /// Standard error buffer.
90         char err_data_[buffer_size_];
91
92         QString cmd_;
93         bool process_events_;
94
95         void waitAndProcessEvents();
96         void processEvents();
97         void killProcess();
98
99         /// returns false if we killed the process
100         /// actually returns Systemcall::ReturnValue
101         bool waitAndCheck();
102 };
103
104 } // namespace support
105 } // namespace lyx
106
107 #endif // SYSTEMCALLPRIVATE_H