This commit was manufactured by cvs2svn to create tag
[lyx.git] / src / support / syscall.h
blob11c28fc58de935829d950730879bf511c9340864
1 // -*- C++ -*-
2 #include <sys/types.h>
4 #include "LString.h"
6 #ifdef __GNUG__
7 #pragma interface
8 #endif
10 /**
11 This class can be used to start child processes.
13 An instance of the class represents a child process.
14 You should use this class if you need to start an external program in LyX.
15 If you wish, you can have a callback function executed when the process
16 finishes.
17 You can chose between three kinds of child processes:
18 1) System processes, which are initiated with the "system" call,
19 where the main thread waits for the system call to return.
20 2) Wait for child process, which are forked, but the main thread waits for
21 the child to end.
22 3) Don't wait, which are forked, but the main thread is not stopped.
23 The process can be considered a background process.
24 A timer will make sure that any callback function is called when
25 the child process ends.
27 Notice that any callback associated with a process is called whatever
28 the kind of child process.
30 class Systemcalls {
31 public:
32 ///
33 enum Starttype {
34 System, // Uses system() which uses /bin/sh
35 Wait, // Uses fork() and execvp()
36 DontWait // Uses fork() and execvp()
39 /// Callback function gets commandline and return value from child
40 typedef void (*Callbackfct)(string cmd, int retval);
42 ///
43 Systemcalls();
45 /** Generate instance and start child process.
46 The string "what" contains a commandline with arguments separated
47 by spaces.
48 When the requested program finishes, the callback-function is
49 called with the commandline and the return value from the program.
50 The instance is automatically added to a timer check if starttype
51 is DontWait (i.e. background execution). When a background child
52 finishes, the timer check will automatically call the callback
53 function.
55 Systemcalls(Starttype how, string const & what, Callbackfct call = 0);
57 ///
58 ~Systemcalls();
60 /** Start childprocess. "what" contains a command at system level.
61 * This is for reuse of the Systemcalls instance.
63 int startscript(Starttype how, string const & what,
64 Callbackfct call = 0);
66 /** gets PID of childprocess. Used by timer */
67 pid_t getpid() { return pid; }
69 /// Start callback
70 void callback() { if (cbk) cbk(command, retval); }
72 /** Set return value. Used by timer */
73 void setRetValue(int r) { retval = r; }
75 /** Kill child prematurely.
76 First, a SIGHUP is sent to the child.
77 If that does not end the child process within "tolerance"
78 seconds, the SIGKILL signal is sent to the child.
79 When the child is dead, the callback is called.
81 void kill(int tolerance = 5);
82 private:
83 /// Type of execution: system, wait for child or background
84 Starttype start;
86 /// Callback function
87 Callbackfct cbk;
89 /// Commmand line
90 string command;
92 /// Process ID of child
93 pid_t pid;
95 /// Return value from child
96 int retval;
98 ///
99 int startscript();
102 pid_t fork();
104 /// Wait for child process to finish. Updates returncode from child.
105 void waitForChild();