| 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"cxx . . $ `<file>/HercStudio/HerculesExecutor.h`/ |
| 26 | #include "HerculesStudio.h"cxx . . $ `<file>/HercStudio/HerculesStudio.h`/ |
| 27 | #include "Arguments.h"cxx . . $ `<file>/HercStudio/Arguments.h`/ |
| 28 | #include "NamedPipe.h"cxx . . $ `<file>/HercStudio/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_INCcxx . . $ `HercStudio/HerculesStudio.h:38:9`! |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
| 40 | int pipeLogcxx . . $ pipeLog.[2]; |
| 41 | int pipeStatuscxx . . $ pipeStatus.[2]; |
| 42 | int pipeCommandcxx . . $ pipeCommand.[2]; |
| 43 | |
| 44 | HerculesExecutorcxx . . $ HerculesExecutor#::HerculesExecutorcxx . . $ HerculesExecutor#HerculesExecutor(d9bdacaaf48540d5).(QMainWindowcxx . . $ QMainWindow# & mainWindow, int pid) |
| 45 | : mMainWindowcxx . . $ HerculesExecutor#mMainWindow.(mainWindow), mProcesscxx . . $ HerculesExecutor#mProcess.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!), mPidcxx . . $ HerculesExecutor#mPid.(pid) |
| 46 | { |
| 47 | |
| 48 | } |
| 49 | |
| 50 | HerculesExecutorcxx . . $ HerculesExecutor#::~cxx . . $ HerculesExecutor#`~HerculesExecutor`(49f6e7a06ebc5aa8).HerculesExecutorcxx . . $ HerculesExecutor#() |
| 51 | { |
| 52 | |
| 53 | } |
| 54 | |
| 55 | #ifndef hFramework |
| 56 | int HerculesExecutorcxx . . $ HerculesExecutor#::runcxx . . $ HerculesExecutor#run(6649784b9cb66156).(stdcxx . . $ std/::stringcxx . . $ std/string# configName, stdcxx . . $ std/::stringcxx . . $ 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 | stdcxx . . $ std/::stringcxx . . $ std/string# hercules = herculesPathcxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5).; |
| 67 | if (herculesPath.lengthcxx . . $ std/__cxx11/basic_string#length(48278e53df7b52a5).() != 0 ) |
| 68 | hercules +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "/"; |
| 69 | hercules +=cxx . . $ std/__cxx11/basic_string#`operator+=`(978ca1133b5f6e74). "hercules"; |
| 70 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(2,"hercules:" << hercules ); |
| 71 | |
| 72 | FILEcxx . . $ FILE# * fileOut = NamedPipecxx . . $ NamedPipe#::getInstancecxx . . $ NamedPipe#getInstance(464fad7f4e589b3).().getHerculesStdoutcxx . . $ NamedPipe#getHerculesStdout(3637adb0f06093ef).(); |
| 73 | rc = dup2(fileno(fileOut),fileno(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!)); |
| 74 | |
| 75 | FILEcxx . . $ FILE# * fileErr = NamedPipecxx . . $ NamedPipe#::getInstancecxx . . $ NamedPipe#getInstance(464fad7f4e589b3).().getHerculesStderrcxx . . $ NamedPipe#getHerculesStderr(3637adb0f06093ef).(); |
| 76 | rc = dup2(fileno(fileErr),fileno(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!)); |
| 77 | |
| 78 | FILEcxx . . $ FILE# * fileIn = NamedPipecxx . . $ NamedPipe#::getInstancecxx . . $ NamedPipe#getInstance(464fad7f4e589b3).().getHerculesStdincxx . . $ NamedPipe#getHerculesStdin(3637adb0f06093ef).(); |
| 79 | if (fileIn == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) perror("fifo0"); |
| 80 | |
| 81 | rc = dup2(fileno(fileIn),fileno(stdincxx . . $ `<external>/232824719582829767/stdio.h:153:9`!)); |
| 82 | if (rc != 0) perror("stdin"); |
| 83 | |
| 84 | if (herculesPath.lengthcxx . . $ std/__cxx11/basic_string#length(48278e53df7b52a5).() != 0) |
| 85 | { |
| 86 | rc = chdir(herculesPath.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).()); |
| 87 | if (rc != 0) |
| 88 | { |
| 89 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "***************************************************************" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 90 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "hercules could not be started (" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). rc <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ")" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 91 | #ifndef Q_WS_MAC |
| 92 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "check that the path specified in Edit/Preferences is correct." <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 93 | #else |
| 94 | << "check that the path specified in HerculesStudio/Preferences is correct." << "\n" |
| 95 | #endif |
| 96 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "**************************************************************" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 97 | fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 98 | _exit(1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (Argumentscxx . . $ Arguments#::getInstancecxx . . $ Arguments#getInstance(86e8a5b8d0f95c3c).().resourceFileNamecxx . . $ Arguments#resourceFileName(f4fff0029955a25f).().lengthcxx . . $ std/__cxx11/basic_string#length(48278e53df7b52a5).() > 0) |
| 103 | { |
| 104 | stdcxx . . $ std/::stringcxx . . $ std/string# resourceFile = "HERCULES_RC="cxx . . $ std/__cxx11/basic_string#basic_string(3bc0f77cdfbaa67f). +cxx . . $ std/operator+(42c652dfebe55ad7). Argumentscxx . . $ Arguments#::getInstancecxx . . $ Arguments#getInstance(86e8a5b8d0f95c3c).().resourceFileNamecxx . . $ Arguments#resourceFileName(f4fff0029955a25f).(); |
| 105 | int stat = putenv(const_cast<char *>(resourceFile.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).())); |
| 106 | if (stat) |
| 107 | { |
| 108 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(0,"failed to define environment variable "<< stat); |
| 109 | } |
| 110 | } |
| 111 | rc = execlp(hercules.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),hercules.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"-d","-f",configName.c_strcxx . . $ std/__cxx11/basic_string#c_str(1016ee5406d91ab9).(),"EXTERNALGUI",NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 112 | stdcxx . . $ std/::coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "***************************************************************" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 113 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "hercules could not be started (" <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). rc <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ")" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 114 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "check that hercules is properly installed and is on the default path " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 115 | #ifndef Q_WS_MAC |
| 116 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "and that the path specified in Edit/Preferences is correct." <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" |
| 117 | #else |
| 118 | << "and that the path specified in HerculesStudio/Preferences is correct." << "\n" |
| 119 | #endif |
| 120 | <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "**************************************************************" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n"; |
| 121 | fflush(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!); |
| 122 | _exit(1); |
| 123 | } |
| 124 | |
| 125 | mPidcxx . . $ HerculesExecutor#mPid. = pid; |
| 126 | printf("pid=:%d\n",pid); |
| 127 | return pid; |
| 128 | } |
| 129 | |
| 130 | void HerculesExecutorcxx . . $ HerculesExecutor#::issueCommandcxx . . $ HerculesExecutor#issueCommand(85c52e162fed56f9).(const char * command) |
| 131 | { |
| 132 | if (mPidcxx . . $ HerculesExecutor#mPid. == 0) |
| 133 | return; |
| 134 | |
| 135 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(2,"issue command:" << command); |
| 136 | FILEcxx . . $ FILE# * input = NamedPipecxx . . $ NamedPipe#::getInstancecxx . . $ NamedPipe#getInstance(464fad7f4e589b3).().getHerculesCommandsFilecxx . . $ NamedPipe#getHerculesCommandsFile(3637adb0f06093ef).(); |
| 137 | if (input) |
| 138 | { |
| 139 | fprintf(input,"%s\n", command); |
| 140 | fflush(input); |
| 141 | return; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | hOutDebugcxx . . $ `HercStudio/HerculesStudio.h:49:9`!(3, "input=" << input); |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void HerculesExecutorcxx . . $ HerculesExecutor#::issueFormattedCommandcxx . . $ HerculesExecutor#issueFormattedCommand(2fa786934fe80e24).(const char * format, const char * arg1) |
| 151 | { |
| 152 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> buffercxx . . $ std/vector#vector(83fdeebbe0501a83).(strlen(format) + strlen(arg1) + 2); |
| 153 | sprintf(&buffer[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).0],format,arg1); |
| 154 | issueCommandcxx . . $ HerculesExecutor#issueCommand(85c52e162fed56f9).(&buffer[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).0]); |
| 155 | } |
| 156 | |
| 157 | void HerculesExecutorcxx . . $ HerculesExecutor#::issueFormattedCommandcxx . . $ HerculesExecutor#issueFormattedCommand(34d26762b42c433a).(const char * format, int arg1) |
| 158 | { |
| 159 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<char> buffercxx . . $ std/vector#vector(83fdeebbe0501a83).(strlen(format) + 64); |
| 160 | sprintf(&buffer[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).0],format,arg1); |
| 161 | issueCommandcxx . . $ HerculesExecutor#issueCommand(85c52e162fed56f9).(&buffer[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).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 |