]> git.tdb.fi Git - builder.git/blob - source/externalaction.cpp
Builder can build itself now.
[builder.git] / source / externalaction.cpp
1 #include <sys/wait.h>
2 #include <msp/iter.h>
3 #include "builder.h"
4 #include "externalaction.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 int ExternalAction::check()
10 {
11         if(!pid)
12                 return 255;
13
14         int status;
15         if(waitpid(pid, &status, WNOHANG)==pid)
16         {
17                 signal_done.emit();
18                 if(WIFEXITED(status))
19                         return WEXITSTATUS(status);
20                 else
21                         return 254;
22         }
23         else
24                 return -1;
25 }
26
27 void ExternalAction::launch()
28 {
29         if(builder.get_verbose()>=1)
30         {
31                 for(list<string>::const_iterator i=argv.begin(); i!=argv.end(); ++i)
32                 {
33                         if(i!=argv.begin())
34                                 cout<<' ';
35                         cout<<*i;
36                 }
37                 cout<<'\n';
38         }
39         
40         pid=fork();
41         if(pid==0)
42         {
43                 char *argv_[argv.size()+1];
44                 for(CountingIterator<string, list<string>::iterator> i=argv.begin(); i!=argv.end(); ++i)
45                         argv_[i.count()]=strdup(i->c_str());
46                 argv_[argv.size()]=0;
47                 execvp(argv_[0], argv_);
48                 exit(1);
49         }
50         else if(pid<0)
51                 pid=0;
52 }