]> git.tdb.fi Git - builder.git/blobdiff - source/externalaction.cpp
Migrate from msppath to mspfs
[builder.git] / source / externalaction.cpp
index b86d8028da25636e5677f00e5eb1763df6a17a94..5d01f4f701071fd4b330b26618b897899a59110d 100644 (file)
@@ -1,6 +1,15 @@
+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
 #include <sys/wait.h>
 #include <iostream>
-#include <msp/iter.h>
+#include <cstring>
+#include <cstdlib>
+#include <msp/fs/dir.h>
 #include "builder.h"
 #include "externalaction.h"
 
@@ -14,9 +23,9 @@ int ExternalAction::check()
                signal_done.emit();
                return 0;
        }
-       
+
        if(!pid)
-               return 255;
+               return exit_code;
 
        int status;
        if(waitpid(pid, &status, WNOHANG)==pid)
@@ -26,12 +35,16 @@ int ExternalAction::check()
                        exit_code=WEXITSTATUS(status);
                else
                        exit_code=254;
+               pid=0;
                return exit_code;
        }
        else
                return -1;
 }
 
+/**
+Starts the external program.  Fill in argv before calling this.
+*/
 void ExternalAction::launch()
 {
        if(builder.get_verbose()>=2)
@@ -44,7 +57,7 @@ void ExternalAction::launch()
                }
                cout<<'\n';
        }
-       
+
        if(builder.get_dry_run())
                pid=-1;
        else
@@ -53,10 +66,16 @@ void ExternalAction::launch()
                if(pid==0)
                {
                        char *argv_[argv.size()+1];
-                       for(CountingIterator<string, StringList::iterator> i=argv.begin(); i!=argv.end(); ++i)
-                               argv_[i.count()]=strdup(i->c_str());
-                       argv_[argv.size()]=0;
+
+                       unsigned j=0;
+                       for(StringList::iterator i=argv.begin(); i!=argv.end(); ++i)
+                               argv_[j++]=strdup(i->c_str());
+                       argv_[j]=0;
+
+                       if(!work_dir.empty())
+                               FS::chdir(work_dir);
                        execvp(argv_[0], argv_);
+                       cout<<"Couldn't execute "<<argv.front()<<'\n';
                        exit(1);
                }
                else if(pid<0)