]> git.tdb.fi Git - builder.git/blobdiff - source/externalaction.cpp
Builder can build itself now.
[builder.git] / source / externalaction.cpp
diff --git a/source/externalaction.cpp b/source/externalaction.cpp
new file mode 100644 (file)
index 0000000..d4a48d7
--- /dev/null
@@ -0,0 +1,52 @@
+#include <sys/wait.h>
+#include <msp/iter.h>
+#include "builder.h"
+#include "externalaction.h"
+
+using namespace std;
+using namespace Msp;
+
+int ExternalAction::check()
+{
+       if(!pid)
+               return 255;
+
+       int status;
+       if(waitpid(pid, &status, WNOHANG)==pid)
+       {
+               signal_done.emit();
+               if(WIFEXITED(status))
+                       return WEXITSTATUS(status);
+               else
+                       return 254;
+       }
+       else
+               return -1;
+}
+
+void ExternalAction::launch()
+{
+       if(builder.get_verbose()>=1)
+       {
+               for(list<string>::const_iterator i=argv.begin(); i!=argv.end(); ++i)
+               {
+                       if(i!=argv.begin())
+                               cout<<' ';
+                       cout<<*i;
+               }
+               cout<<'\n';
+       }
+       
+       pid=fork();
+       if(pid==0)
+       {
+               char *argv_[argv.size()+1];
+               for(CountingIterator<string, list<string>::iterator> i=argv.begin(); i!=argv.end(); ++i)
+                       argv_[i.count()]=strdup(i->c_str());
+               argv_[argv.size()]=0;
+               execvp(argv_[0], argv_);
+               exit(1);
+       }
+       else if(pid<0)
+               pid=0;
+}