]> git.tdb.fi Git - builder.git/blob - source/externalaction.cpp
Add Id tag to all files
[builder.git] / source / externalaction.cpp
1 /* $Id$
2
3 This file is part of builder
4 Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <sys/wait.h>
9 #include <iostream>
10 #include "builder.h"
11 #include "externalaction.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 int ExternalAction::check()
17 {
18         if(builder.get_dry_run())
19         {
20                 signal_done.emit();
21                 return 0;
22         }
23
24         if(!pid)
25                 return exit_code;
26
27         int status;
28         if(waitpid(pid, &status, WNOHANG)==pid)
29         {
30                 signal_done.emit();
31                 if(WIFEXITED(status))
32                         exit_code=WEXITSTATUS(status);
33                 else
34                         exit_code=254;
35                 pid=0;
36                 return exit_code;
37         }
38         else
39                 return -1;
40 }
41
42 /**
43 Starts the external program.  Fill in argv before calling this.
44 */
45 void ExternalAction::launch()
46 {
47         if(builder.get_verbose()>=2)
48         {
49                 for(StringList::const_iterator i=argv.begin(); i!=argv.end(); ++i)
50                 {
51                         if(i!=argv.begin())
52                                 cout<<' ';
53                         cout<<*i;
54                 }
55                 cout<<'\n';
56         }
57
58         if(builder.get_dry_run())
59                 pid=-1;
60         else
61         {
62                 pid=fork();
63                 if(pid==0)
64                 {
65                         char *argv_[argv.size()+1];
66
67                         unsigned j=0;
68                         for(StringList::iterator i=argv.begin(); i!=argv.end(); ++i)
69                                 argv_[j++]=strdup(i->c_str());
70                         argv_[j]=0;
71
72                         execvp(argv_[0], argv_);
73                         cout<<"Couldn't execute "<<argv.front()<<'\n';
74                         exit(1);
75                 }
76                 else if(pid<0)
77                         pid=0;
78         }
79 }