libshevek
|
00001 /* process.hh - fork/exec made userfriendly 00002 * Copyright 2005-2008 Bas Wijnen <wijnen@debian.org> 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef SHEVEK_PROCESS_HH 00019 #define SHEVEK_PROCESS_HH 00020 00021 #include <string> 00022 #include "refbase.hh" 00023 #include "fd.hh" 00024 00025 namespace shevek 00026 { 00028 class process : public shevek::refbase 00029 { 00030 public: 00032 static Glib::RefPtr <process> create (std::string const &command, 00033 std::list <std::string> &argv, 00034 bool pipe_stdin = true, 00035 bool pipe_stdout = true, 00036 bool pipe_stderr = true); 00038 static Glib::RefPtr <process> create (std::string const &command, 00039 std::list <std::string> &argv, 00040 std::list <std::string> const &envp, 00041 bool pipe_stdin = true, 00042 bool pipe_stdout = true, 00043 bool pipe_stderr = true); 00045 00047 static Glib::RefPtr <process> shell (std::string const &command, 00048 bool pipe_stdin = true, 00049 bool pipe_stdout = true, 00050 bool pipe_stderr = true, 00051 std::string const &sh = "/bin/sh") 00052 { 00053 std::list <std::string> args; 00054 args.push_back (sh); 00055 args.push_back ("-c"); 00056 args.push_back (command); 00057 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00058 } 00060 Glib::RefPtr <shevek::fd> in (); 00062 Glib::RefPtr <shevek::fd> out (); 00064 Glib::RefPtr <shevek::fd> err (); 00066 pid_t pid (); 00068 ~process (); 00070 00073 static std::string run (std::string const &command, std::string const &sh); 00075 static Glib::RefPtr <process> create (std::string const &command, 00076 bool pipe_stdin = true, 00077 bool pipe_stdout = true, 00078 bool pipe_stderr = true) 00079 { 00080 std::list <std::string> args; 00081 args.push_back (command); 00082 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00083 } 00085 static Glib::RefPtr <process> create (std::string const &command, 00086 std::string const &a1, 00087 bool pipe_stdin = true, 00088 bool pipe_stdout = true, 00089 bool pipe_stderr = true) 00090 { 00091 std::list <std::string> args; 00092 args.push_back (command); 00093 args.push_back (a1); 00094 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00095 } 00097 static Glib::RefPtr <process> create (std::string const &command, 00098 std::string const &a1, 00099 std::string const &a2, 00100 bool pipe_stdin = true, 00101 bool pipe_stdout = true, 00102 bool pipe_stderr = true) 00103 { 00104 std::list <std::string> args; 00105 args.push_back (command); 00106 args.push_back (a1); 00107 args.push_back (a2); 00108 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00109 } 00111 static Glib::RefPtr <process> create (std::string const &command, 00112 std::string const &a1, 00113 std::string const &a2, 00114 std::string const &a3, 00115 bool pipe_stdin = true, 00116 bool pipe_stdout = true, 00117 bool pipe_stderr = true) 00118 { 00119 std::list <std::string> args; 00120 args.push_back (command); 00121 args.push_back (a1); 00122 args.push_back (a2); 00123 args.push_back (a3); 00124 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00125 } 00127 static Glib::RefPtr <process> create (std::string const &command, 00128 std::string const &a1, 00129 std::string const &a2, 00130 std::string const &a3, 00131 std::string const &a4, 00132 bool pipe_stdin = true, 00133 bool pipe_stdout = true, 00134 bool pipe_stderr = true) 00135 { 00136 std::list <std::string> args; 00137 args.push_back (command); 00138 args.push_back (a1); 00139 args.push_back (a2); 00140 args.push_back (a3); 00141 args.push_back (a4); 00142 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00143 } 00145 static Glib::RefPtr <process> create (std::string const &command, 00146 std::string const &a1, 00147 std::string const &a2, 00148 std::string const &a3, 00149 std::string const &a4, 00150 std::string const &a5, 00151 bool pipe_stdin = true, 00152 bool pipe_stdout = true, 00153 bool pipe_stderr = true) 00154 { 00155 std::list <std::string> args; 00156 args.push_back (command); 00157 args.push_back (a1); 00158 args.push_back (a2); 00159 args.push_back (a3); 00160 args.push_back (a4); 00161 args.push_back (a5); 00162 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00163 } 00165 static Glib::RefPtr <process> create (std::string const &command, 00166 std::string const &a1, 00167 std::string const &a2, 00168 std::string const &a3, 00169 std::string const &a4, 00170 std::string const &a5, 00171 std::string const &a6, 00172 bool pipe_stdin = true, 00173 bool pipe_stdout = true, 00174 bool pipe_stderr = true) 00175 { 00176 std::list <std::string> args; 00177 args.push_back (command); 00178 args.push_back (a1); 00179 args.push_back (a2); 00180 args.push_back (a3); 00181 args.push_back (a4); 00182 args.push_back (a5); 00183 args.push_back (a6); 00184 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00185 } 00187 static Glib::RefPtr <process> create (std::string const &command, 00188 std::string const &a1, 00189 std::string const &a2, 00190 std::string const &a3, 00191 std::string const &a4, 00192 std::string const &a5, 00193 std::string const &a6, 00194 std::string const &a7, 00195 bool pipe_stdin = true, 00196 bool pipe_stdout = true, 00197 bool pipe_stderr = true) 00198 { 00199 std::list <std::string> args; 00200 args.push_back (command); 00201 args.push_back (a1); 00202 args.push_back (a2); 00203 args.push_back (a3); 00204 args.push_back (a4); 00205 args.push_back (a5); 00206 args.push_back (a6); 00207 args.push_back (a7); 00208 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00209 } 00211 static Glib::RefPtr <process> create (std::string const &command, 00212 std::string const &a1, 00213 std::string const &a2, 00214 std::string const &a3, 00215 std::string const &a4, 00216 std::string const &a5, 00217 std::string const &a6, 00218 std::string const &a7, 00219 std::string const &a8, 00220 bool pipe_stdin = true, 00221 bool pipe_stdout = true, 00222 bool pipe_stderr = true) 00223 { 00224 std::list <std::string> args; 00225 args.push_back (command); 00226 args.push_back (a1); 00227 args.push_back (a2); 00228 args.push_back (a3); 00229 args.push_back (a4); 00230 args.push_back (a5); 00231 args.push_back (a6); 00232 args.push_back (a7); 00233 args.push_back (a8); 00234 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00235 } 00237 static Glib::RefPtr <process> create (std::string const &command, 00238 std::string const &a1, 00239 std::string const &a2, 00240 std::string const &a3, 00241 std::string const &a4, 00242 std::string const &a5, 00243 std::string const &a6, 00244 std::string const &a7, 00245 std::string const &a8, 00246 std::string const &a9, 00247 bool pipe_stdin = true, 00248 bool pipe_stdout = true, 00249 bool pipe_stderr = true) 00250 { 00251 std::list <std::string> args; 00252 args.push_back (command); 00253 args.push_back (a1); 00254 args.push_back (a2); 00255 args.push_back (a3); 00256 args.push_back (a4); 00257 args.push_back (a5); 00258 args.push_back (a6); 00259 args.push_back (a7); 00260 args.push_back (a8); 00261 args.push_back (a9); 00262 return create (command, args, pipe_stdin, pipe_stdout, pipe_stderr); 00263 } 00264 private: 00265 Glib::RefPtr <shevek::fd> m_in, m_out, m_err; 00266 pid_t m_pid; 00267 process (std::string const &command, char **argv, char **envp, bool pipe_stdin, bool pipe_stdout, bool pipe_stderr); 00268 static char **make_pointers (std::list <std::string> const &source); 00269 static void clean (char **pointers); 00270 static char *dup (char const *str); 00271 }; 00272 } 00273 00274 #endif