diff --git a/src/Hooks.cpp b/src/Hooks.cpp index 87601d6ab..ca2671b10 100644 --- a/src/Hooks.cpp +++ b/src/Hooks.cpp @@ -24,10 +24,13 @@ // //////////////////////////////////////////////////////////////////////////////// +#include // TODO Remove #include #include +#include #include #include +#include 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; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/Hooks.h b/src/Hooks.h index d77d1b64f..6ed630f86 100644 --- a/src/Hooks.h +++ b/src/Hooks.h @@ -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 _scripts; };