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