Skip to content

Commit

Permalink
libdeng2|CommandLine: Execute a process and wait
Browse files Browse the repository at this point in the history
Optionally can retrieve standard output from the process.
  • Loading branch information
skyjake committed Apr 7, 2014
1 parent 06971a4 commit 15caee4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doomsday/libdeng2/include/de/core/commandline.h
Expand Up @@ -92,6 +92,11 @@ class DENG2_PUBLIC CommandLine
*/
void append(String const &arg);

CommandLine &operator << (String const &arg) {
append(arg);
return *this;
}

/**
* Inserts a new argument to the list of arguments at index @a pos.
*
Expand Down Expand Up @@ -212,6 +217,16 @@ class DENG2_PUBLIC CommandLine
*/
bool execute() const;

/**
* Spawns a new process using the command line and blocks until it
* finishes running.
*
* @param output Output produced by the started process.
*
* @return @c true if successful, otherwise @c false.
*/
bool executeAndWait(String *output = 0) const;

private:
DENG2_PRIVATE(d)
};
Expand Down
23 changes: 23 additions & 0 deletions doomsday/libdeng2/src/core/commandline.cpp
Expand Up @@ -442,3 +442,26 @@ bool CommandLine::execute() const
LOG_DEBUG("Started detached process %i using \"%s\"") << pid << at(0);
return true;
}

bool CommandLine::executeAndWait(String *output) const
{
LOG_AS("CommandLine");

if(count() < 1) return false;

QStringList args;
for(int i = 1; i < count(); ++i) args << at(i);

LOG_DEBUG("Starting process \"%s\"") << at(0);

QProcess proc;
proc.start(at(0), args);
bool result = proc.waitForFinished();

if(output)
{
*output = String::fromUtf8(Block(proc.readAll()));
}

return result;
}
57 changes: 57 additions & 0 deletions doomsday/tests/test_commandline/main.cpp
@@ -0,0 +1,57 @@
/*
* The Doomsday Engine Project
*
* Copyright (c) 2014 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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 2 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 <de/TextApp>
#include <de/Log>

#include <QDebug>

using namespace de;

int main(int argc, char **argv)
{
try
{
TextApp app(argc, argv);
app.initSubsystems(App::DisablePlugins);

CommandLine cmd;
#ifdef UNIX
cmd << "/bin/ls";
cmd << "-l";
#endif

String output;
if(cmd.executeAndWait(&output))
{
LOG_MSG("Output from %s:\n") << cmd.at(0) << output;
}
else
{
LOG_WARNING("Failed to execute!");
}
}
catch(Error const &err)
{
qWarning() << err.asText();
}

qDebug() << "Exiting main()...";
return 0;
}
9 changes: 9 additions & 0 deletions doomsday/tests/test_commandline/test_commandline.pro
@@ -0,0 +1,9 @@
include(../config_test.pri)

TEMPLATE = app
TARGET = test_commandline

SOURCES += main.cpp

deployTest($$TARGET)

1 change: 1 addition & 0 deletions doomsday/tests/tests.pro
Expand Up @@ -7,6 +7,7 @@ TEMPLATE = subdirs

deng_tests: SUBDIRS += \
test_archive \
test_commandline \
test_bitfield \
test_glsandbox \
test_info \
Expand Down

0 comments on commit 15caee4

Please sign in to comment.