Line data Source code
1 : // Copyright (c) 2013-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : #pragma once
20 :
21 : // self
22 : //
23 : #include <cppprocess/io.h>
24 :
25 :
26 : // eventdispatcher lib
27 : //
28 : #include <eventdispatcher/communicator.h>
29 : #include <eventdispatcher/pipe_connection.h>
30 : #include <eventdispatcher/signal_child.h>
31 :
32 :
33 : // snapdev lib
34 : //
35 : #include <snapdev/glob_to_list.h>
36 :
37 :
38 : // C++ lib
39 : //
40 : #include <list>
41 : #include <map>
42 : #include <memory>
43 :
44 :
45 :
46 : namespace cppprocess
47 : {
48 :
49 :
50 :
51 8 : class process
52 : {
53 : public:
54 : typedef std::shared_ptr<process> pointer_t;
55 : typedef std::list<pointer_t> list_t;
56 : typedef std::map<std::string, std::string> environment_map_t;
57 : typedef std::list<std::string> string_list_t;
58 : typedef snapdev::glob_to_list<string_list_t>
59 : argument_list_t;
60 : typedef std::function<void(ed::child_status status)>
61 : process_done_t;
62 :
63 : process(std::string const & name);
64 : process(process const & rhs) = delete;
65 : process & operator = (process const & rhs) = delete;
66 :
67 : std::string const & get_name() const;
68 :
69 : // setup the process
70 : //
71 : void set_forced_environment(bool forced = true);
72 : bool get_forced_environment() const;
73 :
74 : void set_working_directory(std::string const & name);
75 : std::string const & get_working_directory() const;
76 : void set_command(std::string const & name);
77 : std::string const & get_command() const;
78 : std::string get_command_line() const;
79 : bool add_argument(std::string const & arg, bool expand = false);
80 : argument_list_t & get_arguments();
81 : argument_list_t const & get_arguments() const;
82 : void add_environ(std::string const & name, std::string const & value);
83 : environment_map_t const & get_environ() const;
84 :
85 : void set_input_io(io::pointer_t input);
86 : io::pointer_t get_input_io() const;
87 : void set_output_io(io::pointer_t output);
88 : io::pointer_t get_output_io() const;
89 : void set_error_io(io::pointer_t output);
90 : io::pointer_t get_error_io() const;
91 :
92 : // what is received from the command stdout
93 : //
94 : void add_next_process(pointer_t next);
95 : void clear_next_process();
96 : list_t get_next_processes() const;
97 :
98 : // start/stop the process(es)
99 : //
100 : pid_t process_pid() const;
101 : int start();
102 : int wait();
103 : int exit_code() const;
104 : int kill(int sig);
105 : void set_process_done(process_done_t callback);
106 :
107 : private:
108 : int start_process(
109 : ed::pipe_connection::pointer_t output_fifo
110 : , int output_index
111 : , io::pointer_t input_fifo);
112 : void child_done(ed::child_status status);
113 : void execute_command(
114 : ed::pipe_connection::pointer_t output_fifo
115 : , int output_index
116 : , io::pointer_t input_fifo);
117 : void prepare_input(ed::pipe_connection::pointer_t output_fifo);
118 : void prepare_output();
119 : void prepare_error();
120 : void input_pipe_done();
121 : void output_pipe_done(ed::pipe_connection * p);
122 :
123 : ed::communicator::pointer_t f_communicator = ed::communicator::pointer_t();
124 : std::string const f_name = std::string();
125 : std::string f_working_directory = std::string();
126 : std::string f_command = std::string();
127 : argument_list_t f_arguments = argument_list_t();
128 : environment_map_t f_environment = environment_map_t();
129 : process_done_t f_process_done = process_done_t();
130 : bool f_forced_environment = false;
131 : bool f_running = false;
132 : bool f_capture_output = false;
133 : bool f_capture_error = false;
134 : io::pointer_t f_input = io::pointer_t();
135 : io::pointer_t f_output = io::pointer_t();
136 : io::pointer_t f_error = io::pointer_t();
137 : int f_prepared_input = -1;
138 : ed::pipe_connection::pointer_t f_intermediate_output_pipe = ed::pipe_connection::pointer_t();
139 : std::vector<int> f_prepared_output = {};
140 : int f_prepared_error = -1;
141 : list_t f_next = list_t();
142 : pid_t f_child = -1;
143 : int f_exit_code = -1;
144 : };
145 :
146 :
147 :
148 : } // namespace cppprocess
149 : // vim: ts=4 sw=4 et
|