2020-05-02 21:01:21 +00:00
|
|
|
#ifndef __NCPP_SUBPROC_HH
|
|
|
|
#define __NCPP_SUBPROC_HH
|
|
|
|
|
|
|
|
#include <notcurses/notcurses.h>
|
|
|
|
|
|
|
|
#include "Root.hh"
|
|
|
|
#include "Plane.hh"
|
2020-09-30 21:42:42 +00:00
|
|
|
#include "Widget.hh"
|
|
|
|
#include "Utilities.hh"
|
2020-05-02 21:01:21 +00:00
|
|
|
|
|
|
|
namespace ncpp
|
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
class NCPP_API_EXPORT Subproc : public Widget
|
2020-05-02 21:01:21 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static ncsubproc_options default_options;
|
|
|
|
|
|
|
|
public:
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Subproc (Plane* plane, const char* bin, bool use_path = true,
|
2020-05-02 21:01:21 +00:00
|
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
2020-09-30 21:42:42 +00:00
|
|
|
: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
2020-05-02 21:01:21 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Subproc (Plane* plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
2020-05-24 12:41:18 +00:00
|
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
2020-09-30 21:42:42 +00:00
|
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
2020-05-02 21:01:21 +00:00
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
ensure_valid_plane (plane);
|
|
|
|
create_subproc (*plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
|
|
take_plane_ownership (plane);
|
2020-05-02 21:01:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Subproc (Plane& plane, const char* bin, bool use_path = true,
|
2020-05-02 21:01:21 +00:00
|
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
2020-09-30 21:42:42 +00:00
|
|
|
: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
|
2020-05-24 12:41:18 +00:00
|
|
|
{}
|
|
|
|
|
2020-09-30 21:42:42 +00:00
|
|
|
explicit Subproc (Plane& plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
|
2020-05-24 12:41:18 +00:00
|
|
|
char* const arg[] = nullptr, char* const env[] = nullptr,
|
|
|
|
ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
|
2020-09-30 21:42:42 +00:00
|
|
|
: Widget (Utilities::get_notcurses_cpp (plane))
|
2020-05-02 21:01:21 +00:00
|
|
|
{
|
2020-09-30 21:42:42 +00:00
|
|
|
ensure_valid_plane (plane);
|
|
|
|
create_subproc (plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
|
|
|
|
take_plane_ownership (plane);
|
2020-05-02 21:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~Subproc ()
|
|
|
|
{
|
|
|
|
if (is_notcurses_stopped ())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ncsubproc_destroy (subproc);
|
|
|
|
}
|
|
|
|
|
|
|
|
Plane* get_plane () const noexcept
|
|
|
|
{
|
|
|
|
return Plane::map_plane (ncsubproc_plane (subproc));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void create_subproc (Plane& n, const char* bin, const ncsubproc_options* opts, bool use_path,
|
|
|
|
char* const arg[], char* const env[],
|
|
|
|
ncfdplane_callback cbfxn, ncfdplane_done_cb donecbfxn)
|
|
|
|
{
|
|
|
|
if (bin == nullptr)
|
|
|
|
throw invalid_argument ("'bin' must be a valid pointer");
|
|
|
|
|
|
|
|
if (opts == nullptr)
|
|
|
|
opts = &default_options;
|
|
|
|
|
|
|
|
if (use_path) {
|
|
|
|
if (env != nullptr) {
|
|
|
|
subproc = ncsubproc_createvpe (
|
|
|
|
n, opts, bin, arg, env, cbfxn, donecbfxn
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
subproc = ncsubproc_createvp (
|
|
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
subproc = ncsubproc_createv (
|
|
|
|
n, opts, bin, arg, cbfxn, donecbfxn
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subproc == nullptr)
|
2021-09-27 08:48:26 +00:00
|
|
|
throw init_error ("Notcurses failed to create ncsubproc instance");
|
2020-05-02 21:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ncsubproc *subproc;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif // __NCPP_SUBPROC_HH
|