Debian Codegraph @

HercStudio/HerculesExecutor.cpp

raw symbols SCIP
1 /*
2 * File: HerculesExecutor.cpp
3 *
4 * Author: Jacob Dekel
5 * Created on: Aug 7, 2009
6 *
7 * Copyright (c) 2009-2013 Jacob Dekel
8 * $Id$
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25 #include "HerculesExecutor.h"
26 #include "HerculesStudio.h"
27 #include "Arguments.h"
28 #include "NamedPipe.h"
29
30 #include <cstdio>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <cerrno>
35 #include <iostream>
36 #ifdef UNISTD_INC
37 #include <unistd.h>
38 #endif
39
40 int pipeLog[2];
41 int pipeStatus[2];
42 int pipeCommand[2];
43
44 HerculesExecutor::HerculesExecutor(QMainWindow & mainWindow, int pid)
45 : mMainWindow(mainWindow), mProcess(NULL), mPid(pid)
46 {
47
48 }
49
50 HerculesExecutor::~HerculesExecutor()
51 {
52
53 }
54
55 #ifndef hFramework
56 int HerculesExecutor::run(std::string configName, std::string herculesPath)
57 {
58 int pid;
59 int rc =0;
60
61 pid = fork();
62 if (pid < 0)
63 return -1;
64 if (pid == 0)
65 {
66 std::string hercules = herculesPath;
67 if (herculesPath.length() != 0 )
68 hercules += "/";
69 hercules += "hercules";
70 hOutDebug(2,"hercules:" << hercules );
71
72 FILE * fileOut = NamedPipe::getInstance().getHerculesStdout();
73 rc = dup2(fileno(fileOut),fileno(stdout));
74
75 FILE * fileErr = NamedPipe::getInstance().getHerculesStderr();
76 rc = dup2(fileno(fileErr),fileno(stderr));
77
78 FILE * fileIn = NamedPipe::getInstance().getHerculesStdin();
79 if (fileIn == NULL) perror("fifo0");
80
81 rc = dup2(fileno(fileIn),fileno(stdin));
82 if (rc != 0) perror("stdin");
83
84 if (herculesPath.length() != 0)
85 {
86 rc = chdir(herculesPath.c_str());
87 if (rc != 0)
88 {
89 std::cout << "***************************************************************" << "\n"
90 << "hercules could not be started (" << rc << ")" << "\n"
91 #ifndef Q_WS_MAC
92 << "check that the path specified in Edit/Preferences is correct." << "\n"
93 #else
94 << "check that the path specified in HerculesStudio/Preferences is correct." << "\n"
95 #endif
96 << "**************************************************************" << "\n";
97 fflush(stdout);
98 _exit(1);
99 }
100 }
101
102 if (Arguments::getInstance().resourceFileName().length() > 0)
103 {
104 std::string resourceFile = "HERCULES_RC=" + Arguments::getInstance().resourceFileName();
105 int stat = putenv(const_cast<char *>(resourceFile.c_str()));
106 if (stat)
107 {
108 hOutDebug(0,"failed to define environment variable "<< stat);
109 }
110 }
111 rc = execlp(hercules.c_str(),hercules.c_str(),"-d","-f",configName.c_str(),"EXTERNALGUI",NULL);
112 std::cout << "***************************************************************" << "\n"
113 << "hercules could not be started (" << rc << ")" << "\n"
114 << "check that hercules is properly installed and is on the default path " << "\n"
115 #ifndef Q_WS_MAC
116 << "and that the path specified in Edit/Preferences is correct." << "\n"
117 #else
118 << "and that the path specified in HerculesStudio/Preferences is correct." << "\n"
119 #endif
120 << "**************************************************************" << "\n";
121 fflush(stdout);
122 _exit(1);
123 }
124
125 mPid = pid;
126 printf("pid=:%d\n",pid);
127 return pid;
128 }
129
130 void HerculesExecutor::issueCommand(const char * command)
131 {
132 if (mPid == 0)
133 return;
134
135 hOutDebug(2,"issue command:" << command);
136 FILE * input = NamedPipe::getInstance().getHerculesCommandsFile();
137 if (input)
138 {
139 fprintf(input,"%s\n", command);
140 fflush(input);
141 return;
142 }
143 else
144 {
145 hOutDebug(3, "input=" << input);
146 return;
147 }
148 }
149
150 void HerculesExecutor::issueFormattedCommand(const char * format, const char * arg1)
151 {
152 std::vector<char> buffer(strlen(format) + strlen(arg1) + 2);
153 sprintf(&buffer[0],format,arg1);
154 issueCommand(&buffer[0]);
155 }
156
157 void HerculesExecutor::issueFormattedCommand(const char * format, int arg1)
158 {
159 std::vector<char> buffer(strlen(format) + 64);
160 sprintf(&buffer[0],format,arg1);
161 issueCommand(&buffer[0]);
162 }
163
164
165 #else // hFramework
166 int HerculesExecutor::run(std::string configName, std::string herculesPath)
167 {
168 mProcess=NULL;
169 std::string prog = herculesPath;
170 if (prog.size() > 0) prog += "/";
171 prog += "hercules";
172 QString program = prog.c_str();
173 QStringList arguments;
174 arguments << "-d" << "-f" << configName.c_str() << "EXTERNALGUI" ;
175 mProcess = new QProcess();
176 mProcess->start(program,arguments);
177 int iter=2;
178 #ifndef Q_WS_WIN
179 while(mProcess->state() == QProcess::NotRunning && --iter > 0)
180 {
181 sleep(1);
182 }
183 #endif
184 if (mProcess->state() != QProcess::Running && mProcess->state() != QProcess::Starting)
185 return -1;
186 Q_PID pid = mProcess->pid();
187 if (pid != 0)
188 return 0;
189 else
190 return -1;
191 }
192
193 void HerculesExecutor::issueCommand(const char * command)
194 {
195 mProcess->write(command);
196 }
197
198 void HerculesExecutor::issueFormattedCommand(const char * format, const char * arg1)
199 {
200 std::string buffer;
201 buffer.resize(strlen(format)+strlen(arg1)+2);
202 sprintf(&buffer[0],format,arg1);
203 mProcess->write(buffer.c_str());
204 }
205
206 void HerculesExecutor::issueFormattedCommand(const char * format, int arg1)
207 {
208 std::string buffer;
209 buffer.resize(strlen(format)+64);
210 sprintf(&buffer[0],format,arg1);
211 mProcess->write(buffer.c_str());
212 }
213
214 bool HerculesExecutor::getLine(char * buff, int max)
215 {
216 bool ready=false;
217 while (!ready)
218 {
219 ready = mProcess->waitForReadyRead(3000);
220 if (ready)
221 {
222 hOutDebug(1,"waiting for log");
223 QByteArray output = mProcess->readLine(max);
224 int len = output.length();
225 hOutDebug(1,"log len=" << len << ":" << output.data());
226 if (len > 0)
227 {
228 strncpy(buff, output.data(), (len>max? max : len));
229 buff[len] = '\0';
230 }
231 else
232 {
233 ready=false;
234 }
235 }
236 }
237 return true;
238 }
239
240 bool HerculesExecutor::getStatusLine(char * buff, int max)
241 {
242 bool ready=false;
243 while (!ready)
244 {
245 ready = mProcess->waitForReadyRead(3000);
246 if (ready)
247 {
248 hOutDebug(1,"waiting for status");
249 QByteArray output = mProcess->readAllStandardError();
250 int len = output.length();
251 hOutDebug(1,"status len=" << len);
252 if (len > 0)
253 {
254 strncpy(buff, output.data(), (len>max? max : len));
255 buff[len] = '\0';
256 }
257 else
258 {
259 ready=false;
260 }
261 }
262 }
263 return true;
264 }
265 #endif