00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
#include <sys/types.h>
00044
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
00077
00078
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)
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
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)
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
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
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
00157
return WIFEXITED(status);
00158 }
00159
00160 Popen::~Popen()
00161 {
00162
00163 close(
fdout);
00164 close(
fdin);
00165
00166
if(
process_alive)
00167 waitpid(
pid, 0, 0);
00168
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
00179 result.push_back(line);
00180 }
00181
return result;
00182 }
00183
00184
#endif
00185
}
00186
00187
00188