Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

Popen.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 2001 Pascal Vincent, Yoshua Bengio and University of Montreal 00005 // 00006 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 00036 00037 00038 /* ******************************************************* 00039 * $Id: Popen.cc,v 1.7 2004/07/21 16:30:54 chrish42 Exp $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include <sys/types.h> 00044 // norman: added win32 00045 #if !defined(_MSC_VER) && !defined(_MINGW_) && !defined(WIN32) 00046 #include <sys/wait.h> 00047 #include <unistd.h> 00048 #endif 00049 #include <plearn/base/stringutils.h> 00050 #include "Popen.h" 00051 #include <errno.h> 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 #if defined(_MINGW_) || defined(WIN32) 00057 void Popen::launch(const string& command) 00058 { PLERROR("Popen - Not implemented for this platform"); } 00059 00060 void Popen::launch(const string& command, const vector<string>& commandoptions) 00061 { PLERROR("Popen - Not implemented for this platform"); } 00062 00063 int Popen::wait() 00064 { PLERROR("Popen - Not implemented for this platform"); return 0; } 00065 00066 Popen::~Popen() 00067 {} 00068 00069 vector<string> execute(const string& command) 00070 { vector<string> results; PLERROR("Popen - Not implemented for this platform"); return results; } 00071 00072 #else 00073 00074 void Popen::launch(const string& command) 00075 { 00076 /*vector<string> args = split(command); 00077 vector<string> commandoptions(args.begin()+1, args.end()); 00078 launch(args[0], commandoptions);*/ 00079 if (verbose) 00080 cout << "Popen launches:" << endl << command << endl; 00081 int tocommand[2]; 00082 int fromcommand[2]; 00083 00084 pipe(tocommand); 00085 pipe(fromcommand); 00086 pid = fork(); 00087 if(pid==0) // in child process 00088 { 00089 close(tocommand[1]); 00090 close(fromcommand[0]); 00091 dup2(tocommand[0],0); 00092 dup2(fromcommand[1],1); 00093 if (verbose) 00094 { 00095 ofstream dbg("dbg1"); 00096 dbg << command << endl; 00097 dbg.close(); 00098 } 00099 execl("/bin/sh", "sh", "-c", command.c_str(), 0); 00100 ofstream dbg2("dbg2"); 00101 dbg2 << "exec failed: " << errno << " : " << strerror(errno) 00102 << endl; 00103 dbg2.close(); 00104 } 00105 else // in parent 00106 { 00107 process_alive=true; 00108 close(tocommand[0]); 00109 close(fromcommand[1]); 00110 fdout = tocommand[1]; 00111 fdin = fromcommand[0]; 00112 in.attach(fdin); 00113 out.attach(fdout); 00114 } 00115 } 00116 00117 void Popen::launch(const string& command, const vector<string>& commandoptions) 00118 { 00119 int tocommand[2]; 00120 int fromcommand[2]; 00121 pipe(tocommand); 00122 pipe(fromcommand); 00123 pid = fork(); 00124 if(pid==0) // in child process 00125 { 00126 close(tocommand[1]); 00127 close(fromcommand[0]); 00128 dup2(tocommand[0],0); 00129 dup2(fromcommand[1],1); 00130 char **argv = new char*[commandoptions.size()+2]; 00131 argv[0] = (char*)command.c_str(); 00132 //ofstream out("dbg.out"); //commented out by xsm (??? what is this line for ???) 00133 for(unsigned int i=0; i<commandoptions.size(); i++) 00134 argv[i+1] = (char*)commandoptions[i].c_str(); 00135 argv[commandoptions.size()+1] = 0; 00136 execvp(command.c_str(),argv); 00137 } 00138 else // in parent 00139 { 00140 process_alive=true; 00141 close(tocommand[0]); 00142 close(fromcommand[1]); 00143 fdout = tocommand[1]; 00144 fdin = fromcommand[0]; 00145 in.attach(fdin); 00146 out.attach(fdout); 00147 } 00148 } 00149 00150 int Popen::wait() 00151 { 00152 int status; 00153 if(process_alive) 00154 waitpid(pid, &status, 0); 00155 process_alive=false; 00156 // the following will return 1 IF THE PROGRAM RETURNED NORMALLY 00157 return WIFEXITED(status); 00158 } 00159 00160 Popen::~Popen() 00161 { 00162 // cerr << "Entering Popen destructor" << endl; 00163 close(fdout); 00164 close(fdin); 00165 // cerr << "Popen in waitpid()...." << endl; 00166 if(process_alive) 00167 waitpid(pid, 0, 0); 00168 // cerr << "Popen destructor completed." << endl; 00169 } 00170 00171 vector<string> execute(const string& command) 00172 { 00173 Popen p(command); 00174 vector<string> result; 00175 while(p.in) 00176 { 00177 string line = p.in.getline(); 00178 //cout << line << endl; 00179 result.push_back(line); 00180 } 00181 return result; 00182 } 00183 00184 #endif 00185 } // end of namespace PLearn 00186 00187 00188

Generated on Tue Aug 17 16:02:26 2004 for PLearn by doxygen 1.3.7