2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
#include <mutex>
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2019-03-07 00:22:00 -05:00
|
|
|
#include "components/types.hpp"
|
2016-12-05 14:41:00 -05:00
|
|
|
#include "errors.hpp"
|
2016-12-09 03:02:47 -05:00
|
|
|
#include "utils/factory.hpp"
|
2016-11-20 17:04:31 -05:00
|
|
|
#include "utils/functional.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
DEFINE_ERROR(command_error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper used to execute command in a subprocess.
|
|
|
|
* In-/output streams are opened to enable ipc.
|
2019-03-07 00:22:00 -05:00
|
|
|
* If the command is created using command_util::make_command<output_policy::REDIRECTED>, the child streams are
|
|
|
|
* redirected and you can read the program output or write into the program input.
|
|
|
|
*
|
|
|
|
* If the command is created using command_util::make_command<output_policy::IGNORED>, the output is not redirected and
|
|
|
|
* you can't communicate with the child program.
|
2016-12-19 23:05:43 -05:00
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
2018-11-04 10:08:54 -05:00
|
|
|
* \code cpp
|
2019-03-07 00:22:00 -05:00
|
|
|
* auto cmd = command_util::make_command<output_policy::REDIRECTED>("cat /etc/rc.local");
|
2016-12-19 23:05:43 -05:00
|
|
|
* cmd->exec();
|
|
|
|
* cmd->tail([](string s) { std::cout << s << std::endl; });
|
2018-11-04 10:08:54 -05:00
|
|
|
* \endcode
|
2016-12-19 23:05:43 -05:00
|
|
|
*
|
2018-11-04 10:08:54 -05:00
|
|
|
* \code cpp
|
2019-03-07 00:22:00 -05:00
|
|
|
* auto cmd = command_util::make_command<output_policy::REDIRECTED>(
|
2016-12-19 23:05:43 -05:00
|
|
|
* "while read -r line; do echo data from parent process: $line; done");
|
|
|
|
* cmd->exec(false);
|
|
|
|
* cmd->writeline("Test");
|
|
|
|
* cout << cmd->readline();
|
|
|
|
* cmd->wait();
|
2018-11-04 10:08:54 -05:00
|
|
|
* \endcode
|
2016-12-19 23:05:43 -05:00
|
|
|
*
|
2018-11-04 10:08:54 -05:00
|
|
|
* \code cpp
|
2019-03-07 00:22:00 -05:00
|
|
|
* auto cmd = command_util::make_command<output_policy::REDIRECTED>("for i in 1 2 3; do echo $i; done");
|
2016-12-19 23:05:43 -05:00
|
|
|
* cmd->exec();
|
|
|
|
* cout << cmd->readline(); // 1
|
|
|
|
* cout << cmd->readline() << cmd->readline(); // 23
|
2018-11-04 10:08:54 -05:00
|
|
|
* \endcode
|
2019-03-07 00:22:00 -05:00
|
|
|
*
|
|
|
|
* \code cpp
|
|
|
|
* auto cmd = command_util::make_command<output_policy::IGNORED>("ping kernel.org");
|
|
|
|
* int status = cmd->exec();
|
|
|
|
* \endcode
|
2016-12-19 23:05:43 -05:00
|
|
|
*/
|
2019-03-07 00:22:00 -05:00
|
|
|
template <output_policy>
|
|
|
|
class command;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class command<output_policy::IGNORED> {
|
2016-12-19 23:05:43 -05:00
|
|
|
public:
|
|
|
|
explicit command(const logger& logger, string cmd);
|
2019-03-07 00:22:00 -05:00
|
|
|
command(const command&) = delete;
|
2016-12-19 23:05:43 -05:00
|
|
|
~command();
|
|
|
|
|
2019-03-07 00:22:00 -05:00
|
|
|
command& operator=(const command&) = delete;
|
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
int exec(bool wait_for_completion = true);
|
|
|
|
void terminate();
|
|
|
|
bool is_running();
|
|
|
|
int wait();
|
|
|
|
|
|
|
|
pid_t get_pid();
|
|
|
|
int get_exit_status();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const logger& m_log;
|
|
|
|
|
|
|
|
string m_cmd;
|
|
|
|
|
|
|
|
pid_t m_forkpid{};
|
2019-03-07 01:42:10 -05:00
|
|
|
int m_forkstatus = - 1;
|
2019-03-07 00:22:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class command<output_policy::REDIRECTED> : private command<output_policy::IGNORED> {
|
|
|
|
public:
|
|
|
|
explicit command(const logger& logger, string cmd);
|
|
|
|
command(const command&) = delete;
|
|
|
|
~command();
|
|
|
|
|
|
|
|
command& operator=(const command&) = delete;
|
|
|
|
|
|
|
|
int exec(bool wait_for_completion = true);
|
|
|
|
using command<output_policy::IGNORED>::terminate;
|
|
|
|
using command<output_policy::IGNORED>::is_running;
|
|
|
|
using command<output_policy::IGNORED>::wait;
|
|
|
|
|
|
|
|
using command<output_policy::IGNORED>::get_pid;
|
|
|
|
using command<output_policy::IGNORED>::get_exit_status;
|
|
|
|
|
|
|
|
void tail(callback<string> cb);
|
|
|
|
int writeline(string data);
|
|
|
|
string readline();
|
|
|
|
|
|
|
|
int get_stdout(int c);
|
|
|
|
int get_stdin(int c);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int m_stdout[2]{};
|
|
|
|
int m_stdin[2]{};
|
2016-12-19 23:05:43 -05:00
|
|
|
|
2016-12-21 02:00:09 -05:00
|
|
|
std::mutex m_pipelock{};
|
2016-12-19 23:05:43 -05:00
|
|
|
};
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-12-19 23:05:43 -05:00
|
|
|
namespace command_util {
|
2019-03-07 00:22:00 -05:00
|
|
|
template <output_policy OutputType, typename... Args>
|
|
|
|
unique_ptr<command<OutputType>> make_command(Args&&... args) {
|
|
|
|
return factory_util::unique<command<OutputType>>(logger::make(), forward<Args>(args)...);
|
2016-06-14 23:32:35 -04:00
|
|
|
}
|
2019-03-07 00:22:00 -05:00
|
|
|
} // namespace command_util
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|