refactor codes that run programs

This commit is contained in:
royqh1979 2021-10-25 09:16:00 +08:00
parent b2e88c4c00
commit 59ea699fb7
6 changed files with 69 additions and 36 deletions

View File

@ -10,6 +10,11 @@
#include "projectcompiler.h"
#include "../platform.h"
enum RunProgramFlag {
RPF_PAUSE_CONSOLE = 0x0001,
RPF_REDIRECT_INPUT = 0x0002
};
CompilerManager::CompilerManager(QObject *parent) : QObject(parent)
{
mCompiler = nullptr;
@ -197,26 +202,31 @@ void CompilerManager::run(const QString &filename, const QString &arguments, con
if (mRunner!=nullptr) {
return;
}
QChar redirectChar = '0';
QString redirectInputFilename;
bool redirectInput=false;
if (pSettings->executor().redirectInput()
&& !pSettings->executor().inputFilename().isEmpty()) {
redirectInput =true;
redirectChar = '1';
redirectInputFilename = pSettings->executor().inputFilename();
}
if (pSettings->executor().pauseConsole() && programHasConsole(filename)) {
if (programHasConsole(filename)) {
int consoleFlag=0;
if (redirectInput)
consoleFlag |= RPF_REDIRECT_INPUT;
if (pSettings->executor().pauseConsole())
consoleFlag |= RPF_PAUSE_CONSOLE;
QString newArguments = QString(" %1 \"%2\" %3")
.arg(redirectChar)
.arg(consoleFlag)
.arg(toLocalPath(filename)).arg(arguments);
mRunner = new ExecutableRunner(includeTrailingPathDelimiter(pSettings->dirs().app())+"ConsolePauser.exe",newArguments,workDir);
if (redirectInput)
mRunner->setRedirectConsoleProgram(true);
mRunner->setStartConsole(true);
} else {
mRunner = new ExecutableRunner(filename,arguments,workDir);
}
mRunner->setRedirectInputFilename(redirectInputFilename);
if (redirectInput) {
mRunner->setRedirectInput(true);
mRunner->setRedirectInputFilename(redirectInputFilename);
}
connect(mRunner, &ExecutableRunner::finished, this ,&CompilerManager::onRunnerTerminated);
connect(mRunner, &ExecutableRunner::finished, pMainWindow ,&MainWindow::onRunFinished);
connect(mRunner, &ExecutableRunner::runErrorOccurred, pMainWindow ,&MainWindow::onRunErrorOccured);

View File

@ -13,7 +13,8 @@ ExecutableRunner::ExecutableRunner(const QString &filename, const QString &argum
mArguments(arguments),
mWorkDir(workDir),
mStop(false),
mRedirectConsoleProgram(false)
mRedirectInput(false),
mStartConsole(false)
{
}
@ -23,14 +24,24 @@ void ExecutableRunner::stop()
mStop = true;
}
bool ExecutableRunner::redirectConsoleProgram() const
bool ExecutableRunner::startConsole() const
{
return mRedirectConsoleProgram;
return mStartConsole;
}
void ExecutableRunner::setRedirectConsoleProgram(bool newRedirectConsoleProgram)
void ExecutableRunner::setStartConsole(bool newStartConsole)
{
mRedirectConsoleProgram = newRedirectConsoleProgram;
mStartConsole = newStartConsole;
}
bool ExecutableRunner::redirectInput() const
{
return mRedirectInput;
}
void ExecutableRunner::setRedirectInput(bool isRedirect)
{
mRedirectInput = isRedirect;
}
const QString &ExecutableRunner::redirectInputFilename() const
@ -69,28 +80,26 @@ void ExecutableRunner::run()
}
env.insert("PATH",path);
process.setProcessEnvironment(env);
if (redirectConsoleProgram()) {
process.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments * args){
args->flags |= CREATE_NEW_CONSOLE;
});
} else {
process.setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments * args){
process.setCreateProcessArgumentsModifier([this](QProcess::CreateProcessArguments * args){
if (mStartConsole) {
args->flags |= CREATE_NEW_CONSOLE;
}
if (!mRedirectInput) {
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
});
}
}
});
process.connect(&process, &QProcess::errorOccurred,
[&](){
errorOccurred= true;
});
// qDebug() << mFilename;
// qDebug() << QProcess::splitCommand(mArguments);
if (!redirectConsoleProgram()) {
if (!redirectInput()) {
process.closeWriteChannel();
}
process.start();
process.waitForStarted(5000);
if (process.state()==QProcess::Running && redirectConsoleProgram()) {
if (process.state()==QProcess::Running && redirectInput()) {
process.write(ReadFileToByteArray(redirectInputFilename()));
process.closeWriteChannel();
}

View File

@ -12,8 +12,11 @@ public:
const QString &redirectInputFilename() const;
void setRedirectInputFilename(const QString &newDataFile);
bool redirectConsoleProgram() const;
void setRedirectConsoleProgram(bool newRedirectConsoleProgram);
bool redirectInput() const;
void setRedirectInput(bool isRedirect);
bool startConsole() const;
void setStartConsole(bool newStartConsole);
signals:
void started();
@ -29,7 +32,8 @@ private:
QString mWorkDir;
bool mStop;
QString mRedirectInputFilename;
bool mRedirectConsoleProgram;
bool mRedirectInput;
bool mStartConsole;
// QThread interface
protected:

View File

@ -352,14 +352,14 @@ void ProjectCompiler::writeMakeObjFilesRules(QFile &file)
if (mOnlyCheckSyntax) {
if (unit->compileCpp())
writeln(file, "\t$(CPP) -c " + genMakePath1(shortFileName) + " $(CXXFLAGS) " + encodingStr);
writeln(file, "\t$(CPP) -c " + genMakePath1(unit->fileName()) + " $(CXXFLAGS) " + encodingStr);
else
writeln(file, "\t(CC) -c " + genMakePath1(shortFileName) + " $(CFLAGS) " + encodingStr);
writeln(file, "\t(CC) -c " + genMakePath1(unit->fileName()) + " $(CFLAGS) " + encodingStr);
} else {
if (unit->compileCpp())
writeln(file, "\t$(CPP) -c " + genMakePath1(shortFileName) + " -o " + ObjFileName2 + " $(CXXFLAGS) " + encodingStr);
writeln(file, "\t$(CPP) -c " + genMakePath1(unit->fileName()) + " -o " + ObjFileName2 + " $(CXXFLAGS) " + encodingStr);
else
writeln(file, "\t$(CC) -c " + genMakePath1(shortFileName) + " -o " + ObjFileName2 + " $(CFLAGS) " + encodingStr);
writeln(file, "\t$(CC) -c " + genMakePath1(unit->fileName()) + " -o " + ObjFileName2 + " $(CFLAGS) " + encodingStr);
}
}
}

View File

@ -955,7 +955,7 @@
<x>0</x>
<y>0</y>
<width>946</width>
<height>25</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">

View File

@ -9,6 +9,11 @@ using std::string;
#define MAX_COMMAND_LENGTH 32768
#define MAX_ERROR_LENGTH 2048
enum RunProgramFlag {
RPF_PAUSE_CONSOLE = 0x0001,
RPF_REDIRECT_INPUT = 0x0002
};
HANDLE hJob;
LONGLONG GetClockTick() {
@ -87,9 +92,11 @@ void PauseExit(int exitcode, bool reInp) {
exit(exitcode);
}
string GetCommand(int argc,char** argv,bool &reInp) {
string GetCommand(int argc,char** argv,bool &reInp,bool &pauseAfterExit) {
string result;
reInp = (strcmp(argv[1],"0")!=0) ;
int flags = atoi(argv[1]);
reInp = flags & RPF_REDIRECT_INPUT;
pauseAfterExit = flags & RPF_PAUSE_CONSOLE;
for(int i = 2;i < argc;i++) {
/*
// Quote the first argument in case the path name contains spaces
@ -183,8 +190,9 @@ int main(int argc, char** argv) {
}
bool reInp;
bool pauseAfterExit;
// Then build the to-run application command
string command = GetCommand(argc,argv,reInp);
string command = GetCommand(argc,argv,reInp, pauseAfterExit);
HANDLE hOutput = NULL;
if (reInp) {
SECURITY_ATTRIBUTES sa;
@ -197,9 +205,9 @@ int main(int argc, char** argv) {
SetStdHandle(STD_OUTPUT_HANDLE, hOutput);
SetStdHandle(STD_ERROR_HANDLE, hOutput);
freopen("CONOUT$","w+",stdout);
freopen("CONOUT$","w+",stderr);
freopen("CONOUT$","w+",stderr);
}
// Save starting timestamp
LONGLONG starttime = GetClockTick();
@ -213,5 +221,7 @@ int main(int argc, char** argv) {
// Done? Print return value of executed program
printf("\n--------------------------------");
printf("\nProcess exited after %.4g seconds with return value %lu\n",seconds,returnvalue);
PauseExit(returnvalue,reInp);
if (pauseAfterExit)
PauseExit(returnvalue,reInp);
return 0;
}