]> git.tdb.fi Git - builder.git/blob - source/externalaction.cpp
Adjust requires to library changes
[builder.git] / source / externalaction.cpp
1 #include <sys/wait.h>
2 #include <iostream>
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(builder.get_dry_run())
12         {
13                 signal_done.emit();
14                 return 0;
15         }
16
17         if(!pid)
18                 return exit_code;
19
20         int status;
21         if(waitpid(pid, &status, WNOHANG)==pid)
22         {
23                 signal_done.emit();
24                 if(WIFEXITED(status))
25                         exit_code=WEXITSTATUS(status);
26                 else
27                         exit_code=254;
28                 pid=0;
29                 return exit_code;
30         }
31         else
32                 return -1;
33 }
34
35 /**
36 Starts the external program.  Fill in argv before calling this.
37 */
38 void ExternalAction::launch()
39 {
40         if(builder.get_verbose()>=2)
41         {
42                 for(StringList::const_iterator i=argv.begin(); i!=argv.end(); ++i)
43                 {
44                         if(i!=argv.begin())
45                                 cout<<' ';
46                         cout<<*i;
47                 }
48                 cout<<'\n';
49         }
50
51         if(builder.get_dry_run())
52                 pid=-1;
53         else
54         {
55                 pid=fork();
56                 if(pid==0)
57                 {
58                         char *argv_[argv.size()+1];
59
60                         unsigned j=0;
61                         for(StringList::iterator i=argv.begin(); i!=argv.end(); ++i)
62                                 argv_[j++]=strdup(i->c_str());
63                         argv_[j]=0;
64
65                         execvp(argv_[0], argv_);
66                         cout<<"Couldn't execute "<<argv.front()<<'\n';
67                         exit(1);
68                 }
69                 else if(pid<0)
70                         pid=0;
71         }
72 }