1 // common/SubProcess.hh 2 // This file is part of AnyTerm; see http://anyterm.org/ 3 // (C) 2006 Philip Endecott 4 5 // This program is free software; you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation; either version 2 of the License, or 8 // any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 #ifndef SubProcess_hh 20 #define SubProcess_hh 21 22 #include "Activity.hh" 23 24 #include <string> 25 26 #include "FileDescriptor.hh" 27 28 29 // This base class is just so that fd can be initialised before Activity. 30 struct SubProcess_base { 31 pbe::FileDescriptor fd; 32 bool fd_open; 33 ::pid_t pid; SubProcess_baseSubProcess_base34 SubProcess_base(std::pair<int,::pid_t> fd_pid): 35 fd(fd_pid.first, false), fd_open(true), pid(fd_pid.second) 36 {} ~SubProcess_baseSubProcess_base37 ~SubProcess_base() { 38 if (fd_open) { 39 // Normally fd is closed by ~SubProcess; this is here in case 40 // e.g. Activity's ctor throws and ~SubProcess is not executed. 41 fd.close(); 42 } 43 } 44 }; 45 46 47 class SubProcess: private SubProcess_base, public Activity { 48 49 public: 50 SubProcess(Activity::onOutput_t onOutput_, 51 Activity::onError_t onError_, 52 std::string command, 53 int pty_rows=25, int pty_cols=80); 54 55 ~SubProcess(); 56 }; 57 58 59 #endif 60