File: Subproc.hh

package info (click to toggle)
notcurses 3.0.17%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,780 kB
  • sloc: ansic: 50,375; cpp: 17,808; python: 1,123; sh: 230; makefile: 35
file content (99 lines) | stat: -rw-r--r-- 3,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef __NCPP_SUBPROC_HH
#define __NCPP_SUBPROC_HH

#include <notcurses/notcurses.h>

#include "Root.hh"
#include "Plane.hh"
#include "Widget.hh"
#include "Utilities.hh"

namespace ncpp
{
	class NCPP_API_EXPORT Subproc : public Widget
	{
	public:
		static ncsubproc_options default_options;

	public:
		explicit Subproc (Plane* plane, const char* bin, bool use_path = true,
		                  char* const arg[] = nullptr, char* const env[] = nullptr,
		                  ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
			: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
		{}

		explicit Subproc (Plane* plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
		                  char* const arg[] = nullptr, char* const env[] = nullptr,
		                  ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
			: Widget (Utilities::get_notcurses_cpp (plane))
		{
			ensure_valid_plane (plane);
			create_subproc (*plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
			take_plane_ownership (plane);
		}

		explicit Subproc (Plane& plane, const char* bin, bool use_path = true,
		                  char* const arg[] = nullptr, char* const env[] = nullptr,
		                  ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
			: Subproc (plane, bin, nullptr, use_path, arg, env, cbfxn, donecbfxn)
		{}

		explicit Subproc (Plane& plane, const char* bin, const ncsubproc_options* opts, bool use_path = true,
		                  char* const arg[] = nullptr, char* const env[] = nullptr,
		                  ncfdplane_callback cbfxn = nullptr, ncfdplane_done_cb donecbfxn = nullptr)
			: Widget (Utilities::get_notcurses_cpp (plane))
		{
			ensure_valid_plane (plane);
			create_subproc (plane, bin, opts, use_path, arg, env, cbfxn, donecbfxn);
			take_plane_ownership (plane);
		}

		~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)
				throw init_error ("Notcurses failed to create ncsubproc instance");
		}

	private:
		ncsubproc *subproc;
	};
}
#endif // __NCPP_SUBPROC_HH