]> git.tdb.fi Git - builder.git/blob - source/externalaction.cpp
Add comments
[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 exit_code;
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                 pid=0;
30                 return exit_code;
31         }
32         else
33                 return -1;
34 }
35
36 /**
37 Starts the external program.  Fill in argv before calling this.
38 */
39 void ExternalAction::launch()
40 {
41         if(builder.get_verbose()>=2)
42         {
43                 for(StringList::const_iterator i=argv.begin(); i!=argv.end(); ++i)
44                 {
45                         if(i!=argv.begin())
46                                 cout<<' ';
47                         cout<<*i;
48                 }
49                 cout<<'\n';
50         }
51         
52         if(builder.get_dry_run())
53                 pid=-1;
54         else
55         {
56                 pid=fork();
57                 if(pid==0)
58                 {
59                         char *argv_[argv.size()+1];
60                         for(CountingIterator<string, StringList::iterator> i=argv.begin(); i!=argv.end(); ++i)
61                                 argv_[i.count()]=strdup(i->c_str());
62                         argv_[argv.size()]=0;
63                         execvp(argv_[0], argv_);
64                         exit(1);
65                 }
66                 else if(pid<0)
67                         pid=0;
68         }
69 }