15 #include "support/lstrings.h"
17 //----------------------------------------------------------------------
18 // Class, which controlls a system-call
19 //----------------------------------------------------------------------
22 Systemcalls::Systemcalls()
24 pid = (pid_t) 0; // yet no child
30 Systemcalls::Systemcalls(Starttype how, string what, Callbackfct cback)
35 pid = (pid_t) 0; // no child yet
41 // not yet implemented (?)
42 Systemcalls::~Systemcalls()
46 // Start a childprocess
48 // if child runs in background, add information to global controller.
50 int Systemcalls::Startscript()
55 retval = system(command.c_str());
60 if (pid>0) { // Fork succesful. Wait for child
69 // Now integrate into Controller
70 SystemcallsSingletoncontroller::Startcontroller starter;
71 SystemcallsSingletoncontroller *contr=
72 starter.GetController();
74 contr->AddCall(*this);
78 //default: // error();
85 // Wait for child process to finish. Returns returncode from child.
86 void Systemcalls::waitForChild()
88 // We'll pretend that the child returns 1 on all errorconditions.
93 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
95 perror("LyX: Error waiting for child");
97 } else if (WIFEXITED(status)) {
98 // Child exited normally. Update return value.
99 retval = WEXITSTATUS(status);
101 } else if (WIFSIGNALED(status)) {
102 lyxerr << "LyX: Child didn't catch signal "
104 <<" and died. Too bad." << endl;
106 } else if (WIFSTOPPED(status)) {
107 lyxerr << "LyX: Child (pid: " << pid
108 << ") stopped on signal "
110 << ". Waiting for child to finish." << endl;
112 lyxerr << "LyX: Something rotten happened while "
121 // generate child in background
123 pid_t Systemcalls::Fork()
126 if (cpid == 0) { // child
127 string childcommand(command); // copy
128 string rest = split(command, childcommand, ' ');
129 const int MAX_ARGV = 255;
131 char *argv[MAX_ARGV];
136 syscmd = new char[childcommand.length() + 1];
137 childcommand.copy(syscmd, childcommand.length());
138 syscmd[childcommand.length()] = '\0';
140 char * tmp = new char[childcommand.length() + 1];
141 childcommand.copy(tmp, childcommand.length());
142 tmp[childcommand.length()] = '\0';
145 Abbruch = !rest.empty();
147 rest = split(rest, childcommand, ' ');
150 // replace by command. Expand using PATH-environment-var.
151 execvp(syscmd, argv);
152 // If something goes wrong, we end up here:
153 perror("LyX: execvp failed");
154 } else if (cpid < 0) { // error
155 perror("LyX: Could not fork");
165 int Systemcalls::Startscript(Starttype how, string what, Callbackfct cback)
170 pid = (pid_t) 0; // yet no child
172 return Startscript();
178 // Mini-Test-environment for script-classes
185 void back(string cmd, int retval)
187 printf("Done: %s gave %d\n", cmd.c_str(), retval);
192 int main(int, char**)
195 SystemcallsSingletoncontroller::Startcontroller starter;
196 SystemcallsSingletoncontroller *contr=starter.GetController();
198 Systemcalls one(Systemcalls::System, "ls -ltag", back);
199 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
201 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
202 // Simulation of timer
203 while (SimulateTimer)