This commit was manufactured by cvs2svn to create branch 'LyX-Team'.
[lyx.git] / src / syscontr.C
blobd4bdf7f1db607d6bf9a82e74aee44f4a31c690f2
1 #include <config.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6 #include "syscontr.h"
7 #include "syscall.h"
9 #ifdef __GNUG__
10 #pragma implementation
11 #endif
13 //----------------------------------------------------------------------
14 // Controller-Implementation
15 //----------------------------------------------------------------------
18 // default contstructor
21 SystemcallsSingletoncontroller::SystemcallsSingletoncontroller() 
23        SysCalls = NULL;
27 // destructor
28 // 
29 // destroy structs for leaving program
30 // open question: should we stop here childs?
31 // Asger says no: I like to have my xdvi open after closing LyX. Maybe
32 // I want to print or something.
34 SystemcallsSingletoncontroller::~SystemcallsSingletoncontroller()
36        ControlledCalls *next;
37        while (SysCalls)
38        {
39                next = SysCalls->next;
40                delete SysCalls;
41                SysCalls = next;
42        }
43        
47 // Add childprocessinformation into controlled list
50 void 
51 SystemcallsSingletoncontroller::AddCall(Systemcalls const &newcall)
53 // not yet implemented
54        ControlledCalls *newCall = new ControlledCalls;
55        if (newCall == NULL) // sorry, no idea
56                return;
57        newCall->next = SysCalls;
58        newCall->call = new Systemcalls(newcall);
59        SysCalls = newCall;
62 // 
63 // Timer-call
64 // 
65 // Check list, if there is a stopped child. If yes, call-back.
68 void 
69 SystemcallsSingletoncontroller::Timer()
71         // check each entry of our list, if it's finished
72         ControlledCalls *prev = NULL;
73         for (ControlledCalls *actCall=SysCalls; actCall; actCall=actCall->next) 
74                 {
75                         pid_t pid=actCall->call->Getpid();
76                         int stat_loc;
77                         waitpid(pid, &stat_loc, WNOHANG);
78                         if (WIFEXITED(stat_loc) || WIFSIGNALED(stat_loc)) {
79                                 // Ok, the return value goes into retval.
80                                 if (WIFEXITED(stat_loc)) {
81                                         actCall->call->setRetValue(WEXITSTATUS(stat_loc));
82                                 } else {
83                                         // Child died, so pretend it returned 1
84                                         actCall->call->setRetValue(1);
85                                 }
86                                 // callback and release
87                                 actCall->call->Callback();
88                                 if (actCall == SysCalls) {
89                                         SysCalls = actCall->next;
90                                 } else {
91                                         prev->next = actCall->next;
92                                 }
93                                 delete actCall;
94                                 actCall = prev;
95                         }
96                         prev = actCall;
97                 }