From 15caee491168765015a28b930141304ce77f5645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Ker=C3=A4nen?= Date: Mon, 7 Apr 2014 18:47:31 +0300 Subject: [PATCH] libdeng2|CommandLine: Execute a process and wait Optionally can retrieve standard output from the process. --- .../libdeng2/include/de/core/commandline.h | 15 +++++ doomsday/libdeng2/src/core/commandline.cpp | 23 ++++++++ doomsday/tests/test_commandline/main.cpp | 57 +++++++++++++++++++ .../test_commandline/test_commandline.pro | 9 +++ doomsday/tests/tests.pro | 1 + 5 files changed, 105 insertions(+) create mode 100644 doomsday/tests/test_commandline/main.cpp create mode 100644 doomsday/tests/test_commandline/test_commandline.pro diff --git a/doomsday/libdeng2/include/de/core/commandline.h b/doomsday/libdeng2/include/de/core/commandline.h index 85c63bfb67..de4f311460 100644 --- a/doomsday/libdeng2/include/de/core/commandline.h +++ b/doomsday/libdeng2/include/de/core/commandline.h @@ -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. * @@ -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) }; diff --git a/doomsday/libdeng2/src/core/commandline.cpp b/doomsday/libdeng2/src/core/commandline.cpp index 9068450dab..937bbb32c0 100644 --- a/doomsday/libdeng2/src/core/commandline.cpp +++ b/doomsday/libdeng2/src/core/commandline.cpp @@ -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; +} diff --git a/doomsday/tests/test_commandline/main.cpp b/doomsday/tests/test_commandline/main.cpp new file mode 100644 index 0000000000..294b613fb7 --- /dev/null +++ b/doomsday/tests/test_commandline/main.cpp @@ -0,0 +1,57 @@ +/* + * The Doomsday Engine Project + * + * Copyright (c) 2014 Jaakko Keränen + * + * 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 . + */ + +#include +#include + +#include + +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; +} diff --git a/doomsday/tests/test_commandline/test_commandline.pro b/doomsday/tests/test_commandline/test_commandline.pro new file mode 100644 index 0000000000..efea2efe34 --- /dev/null +++ b/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) + diff --git a/doomsday/tests/tests.pro b/doomsday/tests/tests.pro index 0f2a78716b..3f0fccab87 100644 --- a/doomsday/tests/tests.pro +++ b/doomsday/tests/tests.pro @@ -7,6 +7,7 @@ TEMPLATE = subdirs deng_tests: SUBDIRS += \ test_archive \ + test_commandline \ test_bitfield \ test_glsandbox \ test_info \