14 #include "support/lstrings.h"
16 //----------------------------------------------------------------------
17 // Class, which controlls a system-call
18 //----------------------------------------------------------------------
21 Systemcalls::Systemcalls()
23 pid = (pid_t) 0; // yet no child
29 Systemcalls::Systemcalls(Starttype how, string what, Callbackfct cback)
34 pid = (pid_t) 0; // no child yet
40 // not yet implemented (?)
41 Systemcalls::~Systemcalls()
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=
71 starter.GetController();
73 contr->AddCall(*this);
77 //default: // error();
84 // Wait for child process to finish. Returns returncode from child.
85 void Systemcalls::waitForChild()
87 // We'll pretend that the child returns 1 on all errorconditions.
92 pid_t waitrpid = waitpid(pid, &status, WUNTRACED);
94 perror("LyX: Error waiting for child");
96 } else if (WIFEXITED(status)) {
97 // Child exited normally. Update return value.
98 retval = WEXITSTATUS(status);
100 } else if (WIFSIGNALED(status)) {
101 fprintf(stderr,"LyX: Child didn't catch signal %d "
102 "and died. Too bad.\n", WTERMSIG(status));
104 } else if (WIFSTOPPED(status)) {
105 fprintf(stderr,"LyX: Child (pid: %ld) stopped on "
106 "signal %d. Waiting for child to finish.\n",
107 (long) pid, WSTOPSIG(status));
109 fprintf(stderr,"LyX: Something rotten happened while "
110 "waiting for child %ld\n", (long) pid);
117 // generate child in background
119 pid_t Systemcalls::Fork()
122 if (cpid == 0) { // child
123 string childcommand(command); // copy
124 string rest = split(command, childcommand, ' ');
125 const int MAX_ARGV = 255;
127 char *argv[MAX_ARGV];
132 syscmd = new char[childcommand.length() + 1];
133 childcommand.copy(syscmd, childcommand.length());
134 syscmd[childcommand.length()] = '\0';
136 char * tmp = new char[childcommand.length() + 1];
137 childcommand.copy(tmp, childcommand.length());
138 tmp[childcommand.length()] = '\0';
141 Abbruch = !rest.empty();
143 rest = split(rest, childcommand, ' ');
146 // replace by command. Expand using PATH-environment-var.
147 execvp(syscmd, argv);
148 // If something goes wrong, we end up here:
149 perror("LyX: execvp failed");
150 } else if (cpid < 0) { // error
151 perror("LyX: Could not fork");
161 int Systemcalls::Startscript(Starttype how, string what, Callbackfct cback)
166 pid = (pid_t) 0; // yet no child
168 return Startscript();
174 // Mini-Test-environment for script-classes
181 void back(string cmd, int retval)
183 printf("Done: %s gave %d\n", cmd.c_str(), retval);
188 int main(int, char**)
191 SystemcallsSingletoncontroller::Startcontroller starter;
192 SystemcallsSingletoncontroller *contr=starter.GetController();
194 Systemcalls one(Systemcalls::System, "ls -ltag", back);
195 Systemcalls two(Systemcalls::Wait, "ls -ltag", back);
197 Systemcalls three(Systemcalls::DontWait , "ls -ltag", back);
198 // Simulation of timer
199 while (SimulateTimer)