]> git.lyx.org Git - lyx.git/blob - src/support/syscall.h
merge from the string-switch branch and ready for a prelease.
[lyx.git] / src / support / syscall.h
1 // -*- C++ -*-
2 #include <sys/types.h>
3 #include <LString.h>
4
5 #ifdef __GNUG__
6 #pragma interface
7 #endif
8
9
10 /*@Doc:
11   Instance starts and represents childprocesses.
12   You should use this class if you need to start an external program in LyX.
13   You can start a child in the background and have a callback function
14   executed when the child finishes by using the DontWait starttype.
15   */
16 class Systemcalls {
17 public:
18         ///
19         enum Starttype {
20                 System,
21                 Wait,
22                 DontWait
23         };
24         
25         /// Callback function gets commandline and returnvalue from child
26         typedef void (*Callbackfct)(string cmd, int retval);
27         
28         ///
29         Systemcalls();
30         
31         /** Geberate instance and start childprocess 
32           The string "what" contains a commandline with arguments separated 
33           by spaces.
34           When the requested program finishes, the callback-function is 
35           called with the commandline and the returnvalue from the program.
36           The instance is automatically added to a timercheck if starttype is
37           DontWait (i.e. background execution). When a background child
38           finishes, the timercheck will automatically call the callback
39           function.
40           */
41         Systemcalls(Starttype how, string what, Callbackfct call = 0);
42         
43         
44         ///
45         ~Systemcalls();
46         
47         /** Start childprocess. what contains a command on systemlevel. 
48          */
49         int Startscript(Starttype how, string what, Callbackfct call = 0); // for reuse
50         
51         /** gets PID of childprocess. Used by timer */
52         inline pid_t Getpid() { return pid; }
53         
54         /// Start callback
55         inline void Callback() { if (cbk) cbk(command, retval); }
56         
57         /** Set return value. Used by timer */
58         inline void setRetValue(int r) { retval = r; }
59 private:
60         /// Type of execution: system, wait for child or background
61         Starttype    start;
62         /// Callback function
63         Callbackfct  cbk;
64         /// Commmand line
65         string      command;
66         /// Process ID of child
67         pid_t        pid;
68         /// Return value from child
69         int retval;
70         
71         ///
72         int Startscript();
73         
74         ///
75         pid_t Fork();
76         
77         /// Wait for child process to finish. Updates returncode from child.
78         void waitForChild();
79 };