1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
#include <sys/types.h>
|
1999-10-12 21:37:10 +00:00
|
|
|
|
|
|
|
#include "LString.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma interface
|
|
|
|
#endif
|
|
|
|
|
1999-10-12 21:37:10 +00:00
|
|
|
/**
|
|
|
|
This class can be used to start child processes.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-10-12 21:37:10 +00:00
|
|
|
An instance of the class represents a child process.
|
1999-09-27 18:44:28 +00:00
|
|
|
You should use this class if you need to start an external program in LyX.
|
1999-10-12 21:37:10 +00:00
|
|
|
If you wish, you can have a callback function executed when the process
|
|
|
|
finishes.
|
|
|
|
You can chose between three kinds of child processes:
|
|
|
|
1) System processes, which are initiated with the "system" call,
|
|
|
|
where the main thread waits for the system call to return.
|
|
|
|
2) Wait for child process, which are forked, but the main thread waits for
|
|
|
|
the child to end.
|
|
|
|
3) Don't wait, which are forked, but the main thread is not stopped.
|
|
|
|
The process can be considered a background process.
|
|
|
|
A timer will make sure that any callback function is called when
|
|
|
|
the child process ends.
|
|
|
|
|
|
|
|
Notice that any callback associated with a process is called whatever
|
|
|
|
the kind of child process.
|
1999-09-27 18:44:28 +00:00
|
|
|
*/
|
|
|
|
class Systemcalls {
|
|
|
|
public:
|
|
|
|
///
|
|
|
|
enum Starttype {
|
1999-10-12 21:37:10 +00:00
|
|
|
System, // Uses system() which uses /bin/sh
|
|
|
|
Wait, // Uses fork() and execvp()
|
|
|
|
DontWait // Uses fork() and execvp()
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
|
|
|
|
1999-10-12 21:37:10 +00:00
|
|
|
/// Callback function gets commandline and return value from child
|
1999-10-02 16:21:10 +00:00
|
|
|
typedef void (*Callbackfct)(string cmd, int retval);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
Systemcalls();
|
|
|
|
|
1999-10-12 21:37:10 +00:00
|
|
|
/** Generate instance and start child process.
|
1999-09-27 18:44:28 +00:00
|
|
|
The string "what" contains a commandline with arguments separated
|
|
|
|
by spaces.
|
|
|
|
When the requested program finishes, the callback-function is
|
1999-10-12 21:37:10 +00:00
|
|
|
called with the commandline and the return value from the program.
|
|
|
|
The instance is automatically added to a timer check if starttype
|
|
|
|
is DontWait (i.e. background execution). When a background child
|
|
|
|
finishes, the timer check will automatically call the callback
|
1999-09-27 18:44:28 +00:00
|
|
|
function.
|
|
|
|
*/
|
1999-10-12 21:37:10 +00:00
|
|
|
Systemcalls(Starttype how, string const & what, Callbackfct call = 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
~Systemcalls();
|
|
|
|
|
1999-10-12 21:37:10 +00:00
|
|
|
/** Start childprocess. "what" contains a command at system level.
|
|
|
|
* This is for reuse of the Systemcalls instance.
|
1999-09-27 18:44:28 +00:00
|
|
|
*/
|
1999-10-12 21:37:10 +00:00
|
|
|
int startscript(Starttype how, string const & what,
|
|
|
|
Callbackfct call = 0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/** gets PID of childprocess. Used by timer */
|
1999-10-12 21:37:10 +00:00
|
|
|
pid_t getpid() { return pid; }
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/// Start callback
|
1999-10-12 21:37:10 +00:00
|
|
|
void callback() { if (cbk) cbk(command, retval); }
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/** Set return value. Used by timer */
|
1999-10-12 21:37:10 +00:00
|
|
|
void setRetValue(int r) { retval = r; }
|
|
|
|
|
|
|
|
/** Kill child prematurely.
|
|
|
|
First, a SIGHUP is sent to the child.
|
|
|
|
If that does not end the child process within "tolerance"
|
|
|
|
seconds, the SIGKILL signal is sent to the child.
|
|
|
|
When the child is dead, the callback is called.
|
|
|
|
*/
|
|
|
|
void kill(int tolerance = 5);
|
1999-09-27 18:44:28 +00:00
|
|
|
private:
|
|
|
|
/// Type of execution: system, wait for child or background
|
1999-10-12 21:37:10 +00:00
|
|
|
Starttype start;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Callback function
|
1999-10-12 21:37:10 +00:00
|
|
|
Callbackfct cbk;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Commmand line
|
1999-10-12 21:37:10 +00:00
|
|
|
string command;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Process ID of child
|
1999-10-12 21:37:10 +00:00
|
|
|
pid_t pid;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
/// Return value from child
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
///
|
1999-10-12 21:37:10 +00:00
|
|
|
int startscript();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
///
|
1999-10-12 21:37:10 +00:00
|
|
|
pid_t fork();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/// Wait for child process to finish. Updates returncode from child.
|
|
|
|
void waitForChild();
|
|
|
|
};
|