- Implemented ::execute to run a command, feed it input, read it's output and
  return the exit status.
This commit is contained in:
Paul Beckingham 2014-05-14 00:03:02 -04:00
parent 68436d0d50
commit d30fb54a62
2 changed files with 41 additions and 1 deletions

View file

@ -24,10 +24,13 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream> // TODO Remove
#include <cmake.h>
#include <algorithm>
#include <stdio.h>
#include <Context.h>
#include <Hooks.h>
#include <text.h>
extern Context context;
@ -61,7 +64,7 @@ void Hooks::initialize ()
// Data fed to stdin: None
// Exit code: 0: Success, proceed
// !0: Failure, terminate
// Output handled: 0: context.footnote ()
// Output handled: 0: context.header ()
// !0: context.error ()
void Hooks::onLaunch ()
{
@ -196,3 +199,37 @@ void Hooks::onModify (const Task& before, Task& after)
}
////////////////////////////////////////////////////////////////////////////////
int Hooks::execute (
const std::string& command,
const std::string& input,
std::string& output)
{
FILE* fp = popen (command.c_str (), "r+");
if (fp)
{
// Write input to fp.
if (input != "")
{
fputs (input.c_str (), fp);
fflush (fp);
}
// Read output from fp.
output = "";
char* line = NULL;
size_t len = 0;
while (getline (&line, &len, fp) != -1)
{
output += line;
free (line);
line = NULL;
}
fflush (fp);
return pclose (fp);
}
return -1;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -45,6 +45,9 @@ public:
void onAdd (Task&);
void onModify (const Task&, Task&);
private:
int execute (const std::string&, const std::string&, std::string&);
private:
std::vector <std::string> _scripts;
};