]> ruin.nu Git - popboot.git/blob - action.cpp
Spelling fix in inittab
[popboot.git] / action.cpp
1 #include "action.h"
2 #include <cstdlib>
3 #include <unistd.h>
4 #include <sys/wait.h>
5 #include <iostream>
6 using namespace std;
7 using namespace __gnu_cxx;
8
9 const Literals Action::_empty;
10
11 Action::Action(std::string name,const Preconditions& preconditions, std::string executable,const EffectsMap& effects){
12         _name = name;
13         _executable = executable;
14         _preconditions = preconditions;
15         _effects = effects;
16 }
17
18 Action::Action(const Action& action){
19         _name = action._name;
20         _preconditions = action._preconditions;
21         _effects = action._effects;
22         _executable = action._executable;
23 }
24
25 const Literals& Action::effects(int value) const{
26         EffectsMap::const_iterator effects = _effects.find(value);
27         if (effects != _effects.end())
28                 return effects->second;
29         return _empty;
30 }
31
32 const Preconditions& Action::preconditions() const{
33         return _preconditions;
34 }
35
36 int Action::execute() const{
37         cout << "Executing: " << _name << endl;
38         //return system(_executable.c_str());
39         pid_t proc = fork();
40         if (proc == -1) return proc;
41
42         int retval;
43         if (proc == 0) {
44                 //execl("/bin/sh",  "-c", _executable.c_str(), (char*) NULL);
45                 retval = system(_executable.c_str());
46                 _exit(WEXITSTATUS(retval));
47         }
48         waitpid(proc,&retval,0);
49         cout << "Done executing: " << _name << ", returnvalue: " << WEXITSTATUS(retval) << endl;
50         return WEXITSTATUS(retval);
51 }
52
53 const string& Action::name() const{
54         return _name;
55 }