15 //----------------------------------------------------------------------
16 // Class, which controlls a system-call
17 //----------------------------------------------------------------------
20 Systemcalls::Systemcalls()
22 pid = (pid_t) 0; // yet no child
28 Systemcalls::Systemcalls(Starttype how, LString what, Callbackfct cback)
33 pid = (pid_t) 0; // no child yet
39 // not yet implemented (?)
40 Systemcalls::~Systemcalls()
44 // Start a childprocess
46 // if child runs in background, add information to global controller.
48 int Systemcalls::Startscript()
53 retval = system(command.c_str());
58 if (pid>0) { // Fork succesful. Wait for child
67 // Now integrate into Controller
68 SystemcallsSingletoncontroller::Startcontroller starter;
69 SystemcallsSingletoncontroller *contr=
70 starter.GetController();
72 contr->AddCall(*this);
76 //default: // error();
83 // Wait for child process to finish. Returns returncode from child.
84 void Systemcalls::waitForChild()
86 // We'll pretend that the child returns 1 on all errorconditions.
91 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
93 perror("LyX: Error waiting for child");
95 } else if (WIFEXITED(status)) {
96 // Child exited normally. Update return value.
97 retval = WEXITSTATUS(status);
99 } else if (WIFSIGNALED(status)) {
100 fprintf(stderr,"LyX: Child didn't catch signal %d "
101 "and died. Too bad.\n", WTERMSIG(status));
103 } else if (WIFSTOPPED(status)) {
104 fprintf(stderr,"LyX: Child (pid: %ld) stopped on "
105 "signal %d. Waiting for child to finish.\n",
106 (long) pid, WSTOPSIG(status));
108 fprintf(stderr,"LyX: Something rotten happened while "
109 "waiting for child %ld\n", (long) pid);
116 // generate child in background
118 pid_t Systemcalls::Fork()
121 if (cpid == 0) { // child
122 LString childcommand(command); // copy
123 LString rest = command.split(childcommand, ' ');
124 const int MAX_ARGV = 255;
126 char *argv[MAX_ARGV];
130 if (syscmd == NULL) {
131 syscmd = childcommand.copy();
133 argv[index++] = childcommand.copy();
135 Abbruch = !rest.empty();
137 rest = rest.split(childcommand, ' ');
140 // replace by command. Expand using PATH-environment-var.
141 execvp(syscmd, argv);
142 // If something goes wrong, we end up here:
143 perror("LyX: execvp failed");
144 } else if (cpid < 0) { // error
145 perror("LyX: Could not fork");
155 int Systemcalls::Startscript(Starttype how, LString what, Callbackfct cback)
160 pid = (pid_t) 0; // yet no child
162 return Startscript();
168 // Mini-Test-environment for script-classes
175 void back(LString cmd, int retval)
177 printf("Done: %s gave %d\n", cmd.c_str(), retval);
182 int main(int, char**)
185 SystemcallsSingletoncontroller::Startcontroller starter;
186 SystemcallsSingletoncontroller *contr=starter.GetController();
188 Systemcalls one(Systemcalls::System, "ls -ltag", back);
189 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
191 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
192 // Simulation of timer
193 while (SimulateTimer)