2016-06-15 03:32:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2016-06-15 03:32:35 +00:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "components/logger.hpp"
|
2016-12-05 19:41:00 +00:00
|
|
|
#include "errors.hpp"
|
2016-12-09 08:02:47 +00:00
|
|
|
#include "utils/factory.hpp"
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "utils/functional.hpp"
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
DEFINE_ERROR(command_error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper used to execute command in a subprocess.
|
|
|
|
* In-/output streams are opened to enable ipc.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
2018-11-04 15:08:54 +00:00
|
|
|
* \code cpp
|
2016-12-20 04:05:43 +00:00
|
|
|
* auto cmd = command_util::make_command("cat /etc/rc.local");
|
|
|
|
* cmd->exec();
|
|
|
|
* cmd->tail([](string s) { std::cout << s << std::endl; });
|
2018-11-04 15:08:54 +00:00
|
|
|
* \endcode
|
2016-12-20 04:05:43 +00:00
|
|
|
*
|
2018-11-04 15:08:54 +00:00
|
|
|
* \code cpp
|
2016-12-20 04:05:43 +00:00
|
|
|
* auto cmd = command_util::make_command(
|
|
|
|
* "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 15:08:54 +00:00
|
|
|
* \endcode
|
2016-12-20 04:05:43 +00:00
|
|
|
*
|
2018-11-04 15:08:54 +00:00
|
|
|
* \code cpp
|
2016-12-20 04:05:43 +00:00
|
|
|
* auto cmd = command_util::make_command("for i in 1 2 3; do echo $i; done");
|
|
|
|
* cmd->exec();
|
|
|
|
* cout << cmd->readline(); // 1
|
|
|
|
* cout << cmd->readline() << cmd->readline(); // 23
|
2018-11-04 15:08:54 +00:00
|
|
|
* \endcode
|
2016-12-20 04:05:43 +00:00
|
|
|
*/
|
|
|
|
class command {
|
|
|
|
public:
|
|
|
|
explicit command(const logger& logger, string cmd);
|
|
|
|
|
|
|
|
~command();
|
|
|
|
|
|
|
|
int exec(bool wait_for_completion = true);
|
|
|
|
void terminate();
|
|
|
|
bool is_running();
|
|
|
|
int wait();
|
|
|
|
|
|
|
|
void tail(callback<string> cb);
|
|
|
|
int writeline(string data);
|
|
|
|
string readline();
|
|
|
|
|
|
|
|
int get_stdout(int c);
|
|
|
|
int get_stdin(int c);
|
|
|
|
pid_t get_pid();
|
|
|
|
int get_exit_status();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const logger& m_log;
|
|
|
|
|
|
|
|
string m_cmd;
|
|
|
|
|
|
|
|
int m_stdout[2]{};
|
|
|
|
int m_stdin[2]{};
|
|
|
|
|
|
|
|
pid_t m_forkpid{};
|
|
|
|
int m_forkstatus{};
|
|
|
|
|
2016-12-21 07:00:09 +00:00
|
|
|
std::mutex m_pipelock{};
|
2016-12-20 04:05:43 +00:00
|
|
|
};
|
2016-06-15 03:32:35 +00:00
|
|
|
|
2016-12-20 04:05:43 +00:00
|
|
|
namespace command_util {
|
2016-06-15 03:32:35 +00:00
|
|
|
template <typename... Args>
|
2016-12-05 19:41:00 +00:00
|
|
|
unique_ptr<command> make_command(Args&&... args) {
|
2016-12-09 08:02:47 +00:00
|
|
|
return factory_util::unique<command>(logger::make(), forward<Args>(args)...);
|
2016-06-15 03:32:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|