2023-08-31 17:35:12 +02:00
|
|
|
/* Bi-directional popen.
|
2019-06-02 20:48:04 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <ext/stdio_filebuf.h>
|
|
|
|
|
|
|
|
#include <istream>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <map>
|
2019-06-02 20:48:04 +02:00
|
|
|
#include <memory>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
2023-09-08 10:08:52 +02:00
|
|
|
#include <vector>
|
2019-06-02 20:48:04 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace utils {
|
|
|
|
|
|
|
|
class Popen {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
using arg_list = std::vector<std::string>;
|
|
|
|
using env_map = std::map<std::string, std::string>;
|
|
|
|
|
|
|
|
using char_type = char;
|
|
|
|
using stdio_buf = __gnu_cxx::stdio_filebuf<char_type>;
|
|
|
|
|
|
|
|
Popen(const std::string &cmd, const arg_list &args = arg_list(),
|
|
|
|
const env_map &env = env_map(), const std::string &wd = std::string(),
|
|
|
|
bool shell = false);
|
|
|
|
~Popen();
|
|
|
|
|
|
|
|
void open();
|
|
|
|
int close();
|
|
|
|
void kill(int signal = SIGINT);
|
|
|
|
|
|
|
|
int getFdIn() { return fd_in; }
|
|
|
|
|
|
|
|
int getFdOut() { return fd_out; }
|
|
|
|
|
|
|
|
pid_t getPid() const { return pid; }
|
2019-06-05 18:58:56 +02:00
|
|
|
|
2019-06-02 20:48:04 +02:00
|
|
|
protected:
|
2023-09-07 13:19:19 +02:00
|
|
|
std::string command;
|
|
|
|
std::string working_dir;
|
|
|
|
arg_list arguments;
|
|
|
|
env_map environment;
|
|
|
|
bool shell;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
int fd_in, fd_out;
|
2021-11-24 08:54:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class PopenStream : public Popen {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 13:19:19 +02:00
|
|
|
PopenStream(const std::string &cmd, const arg_list &args = arg_list(),
|
|
|
|
const env_map &env = env_map(),
|
|
|
|
const std::string &wd = std::string(), bool shell = false);
|
|
|
|
~PopenStream();
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
std::istream &cin() { return *(input.stream); }
|
|
|
|
|
|
|
|
std::ostream &cout() { return *(output.stream); }
|
2021-11-24 08:54:55 -05:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void open();
|
|
|
|
int close();
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
protected:
|
|
|
|
struct {
|
|
|
|
std::unique_ptr<std::istream> stream;
|
|
|
|
std::unique_ptr<stdio_buf> buffer;
|
|
|
|
} input;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
std::unique_ptr<std::ostream> stream;
|
|
|
|
std::unique_ptr<stdio_buf> buffer;
|
|
|
|
} output;
|
2019-06-02 20:48:04 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace utils
|
|
|
|
} // namespace villas
|
2019-06-02 20:48:04 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
template <typename T>
|
|
|
|
std::istream &operator>>(villas::utils::PopenStream &po, T &value) {
|
|
|
|
return *(po.input.stream) >> value;
|
2019-06-02 20:48:04 +02:00
|
|
|
}
|