16 #include "support/lstrings.h"
17 #include "support/lyxlib.h"
22 Systemcalls::Systemcalls() {
23 pid = 0; // No child yet
26 Systemcalls::Systemcalls(Starttype how, string const & what, Callbackfct cback)
31 pid = static_cast<pid_t>(0);
36 Systemcalls::~Systemcalls() {
38 // If the child is alive, we have to brutally kill it
40 lyx::kill(getpid(), SIGKILL);
45 // Start a childprocess
47 // if child runs in background, add information to global controller.
49 int Systemcalls::startscript() {
54 retval = system(command.c_str());
59 if (pid>0) { // Fork succesful. Wait for child
68 // Now integrate into Controller
69 SystemcallsSingletoncontroller::Startcontroller starter;
70 SystemcallsSingletoncontroller * contr = starter.getController();
71 // Add this to controller
72 contr->addCall(*this);
80 void Systemcalls::kill(int /*tolerance*/) {
82 lyxerr << "LyX: Can't kill non-existing process." << endl;
85 int ret = lyx::kill(getpid(), SIGHUP);
86 bool wait_for_death = true;
89 // The process is already dead!
90 wait_for_death = false;
92 // Something is rotten - maybe we lost permissions?
96 // Here, we should add the PID to a list of
97 // waiting processes to kill if they are not
98 // dead without tolerance seconds
100 // CHECK Implement this using the timer of
101 // the singleton systemcontroller (Asger)
107 // Wait for child process to finish. Returns returncode from child.
108 void Systemcalls::waitForChild() {
109 // We'll pretend that the child returns 1 on all errorconditions.
114 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
115 if (waitrpid == -1) {
116 lyxerr << "LyX: Error waiting for child:" << strerror(errno) << endl;
118 } else if (WIFEXITED(status)) {
119 // Child exited normally. Update return value.
120 retval = WEXITSTATUS(status);
122 } else if (WIFSIGNALED(status)) {
123 lyxerr << "LyX: Child didn't catch signal "
125 << "and died. Too bad." << endl;
127 } else if (WIFSTOPPED(status)) {
128 lyxerr << "LyX: Child (pid: " << pid
129 << ") stopped on signal "
131 << ". Waiting for child to finish." << endl;
133 lyxerr << "LyX: Something rotten happened while "
134 "waiting for child " << pid << endl;
141 // generate child in background
143 pid_t Systemcalls::fork()
145 pid_t cpid= ::fork();
146 if (cpid == 0) { // child
147 string childcommand(command); // copy
148 string rest = split(command, childcommand, ' ');
149 const int MAX_ARGV = 255;
151 char *argv[MAX_ARGV];
155 childcommand = frontStrip(childcommand);
157 syscmd = new char[childcommand.length() + 1];
158 childcommand.copy(syscmd, childcommand.length());
159 syscmd[childcommand.length()] = '\0';
161 if (!childcommand.empty()) {
162 char * tmp = new char[childcommand.length() + 1];
163 childcommand.copy(tmp, childcommand.length());
164 tmp[childcommand.length()] = '\0';
169 more = !rest.empty();
171 rest = split(rest, childcommand, ' ');
174 // replace by command. Expand using PATH-environment-var.
175 execvp(syscmd, argv);
176 // If something goes wrong, we end up here:
177 lyxerr << "LyX: execvp failed: " << strerror(errno) << endl;
178 } else if (cpid < 0) { // error
179 lyxerr << "LyX: Could not fork: " << strerror(errno) << endl;
189 int Systemcalls::startscript(Starttype how, string const & what,
195 pid = static_cast<pid_t>(0); // yet no child
198 if (how == SystemDontWait) {
202 // OS/2 cmd.exe has another use for '&'
203 // This is not NLS safe, but it's OK, I think.
204 string sh = OnlyFilename(GetEnvPath("EMXSHELL"));
206 // COMSPEC is set, unless user unsets
207 sh = OnlyFilename(GetEnvPath("COMSPEC"));
212 if (contains(sh, "cmd.exe")
213 || contains(sh, "4os2.exe"))
214 command = "start /min/n " + command;
220 return startscript();
226 // Mini-Test-environment for script-classes
233 void back(string cmd, int retval)
235 printf("Done: %s gave %d\n", cmd.c_str(), retval);
240 int main(int, char **)
243 SystemcallsSingletoncontroller::Startcontroller starter;
244 SystemcallsSingletoncontroller *contr= starter.GetController();
246 Systemcalls one(Systemcalls::System, "ls -ltag", back);
247 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
249 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
250 // Simulation of timer
251 while (SimulateTimer)