]> git.lyx.org Git - lyx.git/blob - src/support/forkedcall.h
fix reading the author field.
[lyx.git] / src / support / forkedcall.h
1 // -*- C++ -*-
2 /**
3  * \file forkedcall.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Asger Alstrup
8  *
9  * Interface cleaned up by
10  * \author Angus Leeming
11  *
12  * Full author contact details are available in file CREDITS.
13  *
14  * An instance of Class Forkedcall represents a single child process.
15  *
16  * Class Forkedcall uses fork() and execvp() to lauch the child process.
17  *
18  * Once launched, control is returned immediately to the parent process
19  * but a Signal can be emitted upon completion of the child.
20  *
21  * The child process is not killed when the Forkedcall instance goes out of
22  * scope, but it can be killed by an explicit invocation of the kill() member
23  * function.
24  */
25
26 #ifndef FORKEDCALL_H
27 #define FORKEDCALL_H
28
29 #include <boost/shared_ptr.hpp>
30 #include <boost/signals/signal2.hpp>
31 #include <boost/function/function0.hpp>
32
33 #include <sys/types.h>
34
35 namespace lyx {
36 namespace support {
37
38 class ForkedProcess {
39 public:
40         ///
41         enum Starttype {
42                 ///
43                 Wait,
44                 ///
45                 DontWait
46         };
47
48         ///
49         ForkedProcess();
50         ///
51         virtual ~ForkedProcess() {}
52         ///
53         virtual boost::shared_ptr<ForkedProcess> clone() const = 0;
54
55         /** A SignalType signal is can be emitted once the forked process
56          *  has finished. It passes:
57          *  the PID of the child and;
58          *  the return value from the child.
59          *
60          *  We use a signal rather than simply a callback function so that
61          *  we can return easily to C++ methods, rather than just globally
62          *  accessible functions.
63          */
64         typedef boost::signal2<void, pid_t, int> SignalType;
65
66         /** The signal is connected in the calling routine to the desired
67          *  slot. We pass a shared_ptr rather than a reference to the signal
68          *  because it is eminently possible for the instance of the calling
69          *  class (and hence the signal) to be destructed before the forked
70          *  call is complete.
71          *
72          *  It doesn't matter if the slot disappears, SigC takes care of that.
73          */
74         typedef boost::shared_ptr<SignalType> SignalTypePtr;
75
76         /** Invoking the following methods makes sense only if the command
77          *  is running asynchronously!
78          */
79
80         /** gets the PID of the child process.
81          *  Used by the timer.
82          */
83         pid_t pid() const { return pid_; }
84
85         /** Emit the signal.
86          *  Used by the timer.
87          */
88         void emitSignal();
89
90         /** Set the return value of the child process.
91          *  Used by the timer.
92          */
93         void setRetValue(int r) { retval_ = r; }
94
95         /// Returns the identifying command (for display in the GUI perhaps).
96         std::string const & command() const { return command_; }
97
98         /// is the process running ?
99         bool running() const;
100
101         /** Kill child prematurely.
102          *  First, a SIGHUP is sent to the child.
103          *  If that does not end the child process within "tolerance"
104          *  seconds, the SIGKILL signal is sent to the child.
105          *  When the child is dead, the callback is called.
106          */
107         void kill(int tolerance = 5);
108
109 protected:
110         /** Spawn the child process.
111          *  Returns returncode from child.
112          */
113         int run(Starttype type);
114
115         /// Callback function
116         SignalTypePtr signal_;
117
118         /// identifying command (for display in the GUI perhaps).
119         std::string command_;
120
121         /// Process ID of child
122         pid_t pid_;
123
124         /// Return value from child
125         int retval_;
126 private:
127         /// generate child in background
128         virtual int generateChild() = 0;
129
130         /// Wait for child process to finish. Updates returncode from child.
131         int waitForChild();
132 };
133
134
135 class Forkedcall : public ForkedProcess {
136 public:
137         ///
138         virtual boost::shared_ptr<ForkedProcess> clone() const {
139                 return boost::shared_ptr<ForkedProcess>(new Forkedcall(*this));
140         }
141
142         /** Start the child process.
143          *
144          *  The command "what" is passed to execvp() for execution.
145          *
146          *  There are two startscript commands available. They differ in that
147          *  the second receives a signal that is executed on completion of
148          *  the command. This makes sense only for a command executed
149          *  in the background, ie DontWait.
150          *
151          *  The other startscript command can be executed either blocking
152          *  or non-blocking, but no signal will be emitted on finishing.
153          */
154         int startscript(Starttype, std::string const & what);
155
156         ///
157         int startscript(std::string const & what, SignalTypePtr);
158
159 private:
160         ///
161         virtual int generateChild();
162 };
163
164 } // namespace support
165 } // namespace lyx
166
167 #endif // FORKEDCALL_H