File: HerculesExecutor.cpp

package info (click to toggle)
herculesstudio 1.5.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,376 kB
  • sloc: cpp: 17,077; xml: 21; makefile: 13
file content (265 lines) | stat: -rw-r--r-- 6,705 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 *  File:       HerculesExecutor.cpp
 *
 *  Author:     Jacob Dekel
 *  Created on: Aug 7, 2009
 *
 *  Copyright (c) 2009-2013 Jacob Dekel
 *  $Id$
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "HerculesExecutor.h"
#include "HerculesStudio.h"
#include "Arguments.h"
#include "NamedPipe.h"

#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <iostream>
#ifdef UNISTD_INC
#include <unistd.h>
#endif

int pipeLog[2];
int pipeStatus[2];
int pipeCommand[2];

HerculesExecutor::HerculesExecutor(QMainWindow & mainWindow, int pid)
: mMainWindow(mainWindow), mProcess(NULL), mPid(pid)
{

}

HerculesExecutor::~HerculesExecutor()
{

}

#ifndef hFramework
int HerculesExecutor::run(std::string configName, std::string herculesPath)
{
	int pid;
	int rc =0;

	pid = fork();
	if (pid < 0)
		return -1;
	if (pid == 0)
	{
		std::string hercules = herculesPath;
		if (herculesPath.length() != 0 )
			hercules += "/";
		hercules += "hercules";
        hOutDebug(2,"hercules:" << hercules );

		FILE * fileOut = NamedPipe::getInstance().getHerculesStdout();
		rc = dup2(fileno(fileOut),fileno(stdout));

		FILE * fileErr = NamedPipe::getInstance().getHerculesStderr();
		rc = dup2(fileno(fileErr),fileno(stderr));

		FILE * fileIn = NamedPipe::getInstance().getHerculesStdin();
		if (fileIn == NULL) perror("fifo0");

		rc = dup2(fileno(fileIn),fileno(stdin));
		if (rc != 0) perror("stdin");

		if (herculesPath.length() != 0)
		{
			rc = chdir(herculesPath.c_str());
			if (rc != 0)
			{
				std::cout << "***************************************************************" << "\n"
						<< "hercules could not be started (" <<  rc << ")" << "\n"
		#ifndef Q_WS_MAC
						<< "check that the path specified in Edit/Preferences is correct." << "\n"
		#else
						<< "check that the path specified in HerculesStudio/Preferences is correct." << "\n"
		#endif
						<< "**************************************************************" << "\n";
				fflush(stdout);
				_exit(1);
			}
		}

		if (Arguments::getInstance().resourceFileName().length() > 0)
		{
			std::string resourceFile = "HERCULES_RC=" + Arguments::getInstance().resourceFileName();
			int stat = putenv(const_cast<char *>(resourceFile.c_str()));
			if (stat)
			{
             hOutDebug(0,"failed to define environment variable "<< stat);
			}
		}
		rc = execlp(hercules.c_str(),hercules.c_str(),"-d","-f",configName.c_str(),"EXTERNALGUI",NULL);
        std::cout << "***************************************************************" << "\n"
                << "hercules could not be started (" <<  rc << ")" << "\n"
                << "check that hercules is properly installed and is on the default path " << "\n"
#ifndef Q_WS_MAC
                << "and that the path specified in Edit/Preferences is correct." << "\n"
#else
                << "and that the path specified in HerculesStudio/Preferences is correct." << "\n"
#endif
                << "**************************************************************" << "\n";
        fflush(stdout);
		_exit(1);
	}

	mPid = pid;
	printf("pid=:%d\n",pid);
	return pid;
}

void HerculesExecutor::issueCommand(const char * command)
{
	if (mPid == 0)
		return;

    hOutDebug(2,"issue command:" << command);
	FILE * input = NamedPipe::getInstance().getHerculesCommandsFile();
	if (input)
	{
			fprintf(input,"%s\n", command);
			fflush(input);
			return;
	}
	else
	{
        hOutDebug(3, "input=" << input);
		return;
	}
}

void HerculesExecutor::issueFormattedCommand(const char * format, const char * arg1)
{
	std::vector<char> buffer(strlen(format) + strlen(arg1) + 2);
	sprintf(&buffer[0],format,arg1);
	issueCommand(&buffer[0]);
}

void HerculesExecutor::issueFormattedCommand(const char * format, int arg1)
{
	std::vector<char> buffer(strlen(format) + 64);
	sprintf(&buffer[0],format,arg1);
	issueCommand(&buffer[0]);
}


#else  // hFramework
int HerculesExecutor::run(std::string configName, std::string herculesPath)
{
	mProcess=NULL;
	std::string prog = herculesPath;
	if (prog.size() > 0) prog += "/";
	prog += "hercules";
	QString program = prog.c_str();
	QStringList arguments;
	arguments << "-d" << "-f" << configName.c_str() << "EXTERNALGUI" ;
	mProcess = new QProcess();
	mProcess->start(program,arguments);
	int iter=2;
	#ifndef  Q_WS_WIN
	while(mProcess->state() == QProcess::NotRunning && --iter > 0)
	{
		sleep(1);
	}
	#endif
	if (mProcess->state() != QProcess::Running && mProcess->state() != QProcess::Starting)
		return -1;
	Q_PID pid = mProcess->pid();
	if (pid != 0)
		return 0;
	else
		return -1;
}

void HerculesExecutor::issueCommand(const char * command)
{
	mProcess->write(command);
}

void HerculesExecutor::issueFormattedCommand(const char * format, const char * arg1)
{
	std::string buffer;
	buffer.resize(strlen(format)+strlen(arg1)+2);
	sprintf(&buffer[0],format,arg1);
	mProcess->write(buffer.c_str());
}

void HerculesExecutor::issueFormattedCommand(const char * format, int arg1)
{
	std::string buffer;
	buffer.resize(strlen(format)+64);
	sprintf(&buffer[0],format,arg1);
	mProcess->write(buffer.c_str());
}

bool HerculesExecutor::getLine(char * buff, int max)
{
	bool ready=false;
	while (!ready)
	{
		ready = mProcess->waitForReadyRead(3000);
		if (ready)
		{
			hOutDebug(1,"waiting for log");
			QByteArray output = mProcess->readLine(max);
			int len = output.length();
			hOutDebug(1,"log len=" << len << ":" << output.data());
			if (len > 0)
			{
				strncpy(buff, output.data(), (len>max? max : len));
				buff[len] = '\0';
			}
			else
			{
				ready=false;
			}
		}
	}
	return true;
}

bool HerculesExecutor::getStatusLine(char * buff, int max)
{
	bool ready=false;
	while (!ready)
	{
		ready = mProcess->waitForReadyRead(3000);
		if (ready)
		{
			hOutDebug(1,"waiting for status");
			QByteArray output = mProcess->readAllStandardError();
			int len = output.length();
			hOutDebug(1,"status len=" << len);
			if (len > 0)
			{
				strncpy(buff, output.data(), (len>max? max : len));
				buff[len] = '\0';
			}
			else
			{
				ready=false;
			}
		}
	}
	return true;
}
#endif