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