diff --git a/example/args.cpp b/example/args.cpp index c4b62b30..efad00d1 100644 --- a/example/args.cpp +++ b/example/args.cpp @@ -11,12 +11,19 @@ #include #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[args - child c("test.exe", "--foo", "/bar"); -//] + bp::child c("test.exe", "--foo", "/bar"); + + //or explicit + + bp::child c2( + bp::exe="test.exe", + bp::args={"--foo", "/bar"} + ); + c.wait(); + c2.wait(); } diff --git a/example/async_io.cpp b/example/async_io.cpp index 0a0c1ee9..cf0448ef 100644 --- a/example/async_io.cpp +++ b/example/async_io.cpp @@ -11,27 +11,19 @@ #include #include #include -#if defined(BOOST_WINDOWS_API) -# include -#endif -using namespace boost::process; +namespace bp = boost::process; int main() { -//[async_io boost::asio::io_service ios; - boost::process::async_pipe p(ios); - - child c( - "test.exe", - std_out > p, ios - ); - boost::asio::streambuf buffer; - boost::asio::async_read_until(p, buffer, '\n', - [](const boost::system::error_code&, std::size_t){}); + + + bp::child c( + "test.exe", + bp::std_out > buffer + ); ios.run(); -//] } diff --git a/example/cleanup.cpp b/example/cleanup.cpp deleted file mode 100644 index 5f43864f..00000000 --- a/example/cleanup.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2006, 2007 Julio M. Merino Vidal -// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling -// Copyright (c) 2009 Boris Schaeling -// Copyright (c) 2010 Felipe Tanus, Boris Schaeling -// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#if defined(BOOST_POSIX_API) -# include -#endif - -using namespace boost::process; - -int main() -{ -//[cleanup - { - child c("test.exe"); - //wait for exit - } -//] - -//[cleanup_detach_short - - spawn("test.exe"); -//] - -//[cleanup_system - { - system("test.exe"); - } -//] -} diff --git a/example/env.cpp b/example/env.cpp index 40574955..cb9a609b 100644 --- a/example/env.cpp +++ b/example/env.cpp @@ -9,15 +9,16 @@ #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[modifiy_env - boost::process::environment my_env = boost::this_process::environment(); //empty env, that would fail. - system("test.exe", my_env); -//] -//[inherit_env - system("test.exe", env["PATH"]+="/tmp"); -//] + bp::environment my_env = boost::this_process::environment(); + + my_env["PATH"] += "/foo"; + bp::system("test.exe", my_env); + + + + bp::system("test.exe", bp::env["PATH"]+="/bar"); } diff --git a/example/error_handling.cpp b/example/error_handling.cpp index a293a2f5..1a0dad00 100644 --- a/example/error_handling.cpp +++ b/example/error_handling.cpp @@ -10,16 +10,15 @@ #include #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[set_on_error - std::error_code ec; - child c1("test.exe", ec); -//] -//[ignore_error - child c2("test.exe", ignore_error); -//] + std::error_code ec; + bp::child c1("test.exe", ec); + + + bp::child c2("test.exe", bp::ignore_error); + } diff --git a/example/execute.cpp b/example/execute.cpp deleted file mode 100644 index b18922f8..00000000 --- a/example/execute.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) 2006, 2007 Julio M. Merino Vidal -// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling -// Copyright (c) 2009 Boris Schaeling -// Copyright (c) 2010 Felipe Tanus, Boris Schaeling -// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include - -using namespace boost::process; - -int main() -{ -//[execute - child c1("test.exe"); -//] - -//[execute_path - boost::filesystem::path exe = "../test.exe"; - child c2(exe); -//] -} diff --git a/example/intro.cpp b/example/intro.cpp deleted file mode 100644 index 6cd9e687..00000000 --- a/example/intro.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2006, 2007 Julio M. Merino Vidal -// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling -// Copyright (c) 2009 Boris Schaeling -// Copyright (c) 2010 Felipe Tanus, Boris Schaeling -// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -//[intro -#include - -using namespace boost::process; - -int main() -{ - ipstream pipe_stream; - system("gcc.exe", "--version", std_out > pipe_stream); - - - while (pipe_stream) - { - std::string value; - std::getline(pipe_stream, value); - std::cerr << value << std::endl; - } -} -//] diff --git a/example/io.cpp b/example/io.cpp index 9f77f0d2..08ef243a 100644 --- a/example/io.cpp +++ b/example/io.cpp @@ -8,101 +8,84 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include -#include #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[close - system( + // + bp::system( "test.exe", - std_out.close(), - std_err.close(), - std_in.close() + bp::std_out > stdout, //forward + bp::std_err.close(), //close + bp::std_in > bp::null //null in ); -//] -//[null - system( - "test.exe", - (std_out & std_err) > null, - std_in < null - ); -//] -//[redirect - boost::filesystem::path p = "input.txt"; - system( - "test.exe", - std_err > "log.txt", - std_out > stderr, - std_in < p - ); -//] - { -//[pipe - pipe p1; - pipe p2; - system( - "test.exe", - std_out > p2, - std_in < p1 - ); - auto text = "my_text"; - p1.write(text, 8); - boost::iostreams::stream istr(p2.source()); - - int i = 0; - p2 >> i; -//] + boost::filesystem::path p = "input.txt"; + + bp::system( + "test.exe", + (bp::std_out & bp::std_err) > "output.txt", //redirect both to one file + bp::std_in < p //read input from file + ); + + { + bp::opstream p1; + bp::ipstream p2; + bp::system( + "test.exe", + bp::std_out > p2, + bp::std_in < p1 + ); + p1 << "my_text"; + int i = 0; + p2 >> i; + } { -//[async_pipe - boost::asio::io_service io_service; - async_pipe p1(io_service); - async_pipe p2(io_service); - system( - "test.exe", - std_out > p2, - std_in < p1, - io_service, - on_exit([&](int exit, const std::error_code& ec_in) - { - p1.async_close(); - p2.async_close(); - }) - ); - std::vector in_buf; - std::string value = "my_string"; - boost::asio::async_write(p1, boost::asio::buffer(value), []( const boost::system::error_code&, std::size_t){}); - boost::asio::async_read (p2, boost::asio::buffer(in_buf), []( const boost::system::error_code&, std::size_t){}); -//] - }{ -//[async_pipe_simple boost::asio::io_service io_service; - std::vector in_buf; - std::string value = "my_string"; - system( - "test.exe", - std_out > buffer(in_buf), - std_in < buffer(value) + bp::async_pipe p1(io_service); + bp::async_pipe p2(io_service); + bp::system( + "test.exe", + bp::std_out > p2, + bp::std_in < p1, + io_service, + bp::on_exit([&](int exit, const std::error_code& ec_in) + { + p1.async_close(); + p2.async_close(); + }) ); -//] - }{ -//[async_pipe_future - boost::asio::io_service io_service; - std::future> in_buf; - std::future write_fut; - std::string value = "my_string"; - system( + std::vector in_buf; + std::string value = "my_string"; + boost::asio::async_write(p1, boost::asio::buffer(value), []( const boost::system::error_code&, std::size_t){}); + boost::asio::async_read (p2, boost::asio::buffer(in_buf), []( const boost::system::error_code&, std::size_t){}); + } + { + boost::asio::io_service io_service; + std::vector in_buf; + std::string value = "my_string"; + bp::system( "test.exe", - std_out > in_buf, - std_in < buffer(value) > write_fut + bp::std_out > bp::buffer(in_buf), + bp::std_in < bp::buffer(value) ); + } - write_fut.get(); - in_buf.get(); -//] + { + boost::asio::io_service io_service; + std::future> in_buf; + std::future write_fut; + std::string value = "my_string"; + bp::system( + "test.exe", + bp::std_out > in_buf, + bp::std_in < bp::buffer(value) > write_fut + ); + + write_fut.get(); + in_buf.get(); } } diff --git a/example/posix.cpp b/example/posix.cpp index 623a8e3a..b2294e91 100644 --- a/example/posix.cpp +++ b/example/posix.cpp @@ -18,21 +18,19 @@ using namespace boost::process; int main() { -//[bind_fd + //duplicate our pipe descriptor into literal position 4 pipe p; system("test", posix::fd.bind(4, p.native_sink()) ); -//] -//[close_fd + + //close file-descriptor from explicit integral value system("test", posix::fd.close(STDIN_FILENO)); -//] -//[close_fds + //close file-descriptors from explicit integral values system("test", posix::fd.close({STDIN_FILENO, STDOUT_FILENO})); -//] -//[fork_execve + //add custom handlers const char *env[2] = { 0 }; env[0] = "LANG=de"; system("test", @@ -44,5 +42,5 @@ int main() posix::on_exec_error([](auto&) { std::ofstream ofs("log.txt"); if (ofs) ofs << errno; }) ); -//] + } diff --git a/example/work_dir.cpp b/example/start_dir.cpp similarity index 80% rename from example/work_dir.cpp rename to example/start_dir.cpp index ea54d37a..896ef8cc 100644 --- a/example/work_dir.cpp +++ b/example/start_dir.cpp @@ -10,22 +10,18 @@ #include #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[work_dir - system( + bp::system( "test.exe", - start_dir="../foo" + bp::start_dir="../foo" ); -//] -//[work_dir_abs boost::filesystem::path exe = "test.exe"; - system( + bp::system( boost::filesystem::absolute(exe), - start_dir="../foo" + bp::start_dir="../foo" ); -//] } diff --git a/example/streams.cpp b/example/streams.cpp deleted file mode 100644 index 86875265..00000000 --- a/example/streams.cpp +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2006, 2007 Julio M. Merino Vidal -// Copyright (c) 2008 Ilya Sokolov, Boris Schaeling -// Copyright (c) 2009 Boris Schaeling -// Copyright (c) 2010 Felipe Tanus, Boris Schaeling -// Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include - -using namespace boost::process; -using namespace boost::iostreams; - -int main() -{ -//[stdout - system("test.exe", std_out > "log.txt"); -//] - -//[close_in_err - system("test.exe", - std_out > null, - std_in < null, - std_err.close()); -//] -} diff --git a/example/sync_io.cpp b/example/sync_io.cpp index 85c35ed4..a861e7ab 100644 --- a/example/sync_io.cpp +++ b/example/sync_io.cpp @@ -10,22 +10,19 @@ #include #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[sync_io - boost::process::ipstream p; + bp::ipstream p; - child c( + bp::child c( "test.exe", - std_out > p + bp::std_out > p ); - std::string s; std::getline(p, s); c.wait(); -//] } diff --git a/example/terminate.cpp b/example/terminate.cpp index aab828f1..7de2d830 100644 --- a/example/terminate.cpp +++ b/example/terminate.cpp @@ -9,12 +9,10 @@ #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[c_terminate - child c("test.exe"); + bp::child c("test.exe"); c.terminate(); -//] } diff --git a/example/wait.cpp b/example/wait.cpp index 73fca1b1..3cafe01b 100644 --- a/example/wait.cpp +++ b/example/wait.cpp @@ -9,36 +9,26 @@ #include #include -#if defined(BOOST_WINDOWS_API) -# include -#elif defined(BOOST_POSIX_API) -# include -# include -#endif -using namespace boost::process; +namespace bp = boost::process; int main() { { -//[sync - child c("test.exe"); - c.wait(); - auto exit_code = c.exit_code(); -//] + bp::child c("test.exe"); + c.wait(); + auto exit_code = c.exit_code(); } { -//[async - boost::asio::io_service io_service; + boost::asio::io_service io_service; - child c( - "test.exe", - io_service, - on_exit([&](int exit, const std::error_code& ec_in){}) - ); + bp::child c( + "test.exe", + io_service, + bp::on_exit([&](int exit, const std::error_code& ec_in){}) + ); - io_service.run(); -//] + io_service.run(); } } diff --git a/example/windows.cpp b/example/windows.cpp index 702df791..98a838c8 100644 --- a/example/windows.cpp +++ b/example/windows.cpp @@ -13,21 +13,18 @@ #include -using namespace boost::process; +namespace bp = boost::process; int main() { -//[show_window - system("test.exe", - windows::show); -//] + bp::system("test.exe", + bp::windows::show); -//[create_process - system("test.exe", - on_setup([](auto &e) + + bp::system("test.exe", + bp::on_setup([](auto &e) { e.startup_info.dwFlags = STARTF_RUNFULLSCREEN; }), - on_error([](auto&, const std::error_code & ec) + bp::on_error([](auto&, const std::error_code & ec) { std::cerr << ec.message() << std::endl; }) - ); -//] + ); } diff --git a/include/boost/process/child.hpp b/include/boost/process/child.hpp index 6976f4d1..3802794b 100644 --- a/include/boost/process/child.hpp +++ b/include/boost/process/child.hpp @@ -84,7 +84,7 @@ class child /** Get the native handle for the child process. */ native_handle_t native_handle() const; - /** Get the exit_code. The return value is without any meaning if the child wasn't waited for. */ + /** Get the exit_code. The return value is without any meaning if the child wasn't waited for or if it was terminated. */ int exit_code() const; /** Get the Process Identifier. */ pid_t id() const; diff --git a/test/args_cmd.cpp b/test/args_cmd.cpp index c9c3e641..11700d21 100644 --- a/test/args_cmd.cpp +++ b/test/args_cmd.cpp @@ -35,8 +35,8 @@ BOOST_AUTO_TEST_CASE(args, *boost::unit_test::timeout(2)) bp::child c( master_test_suite().argv[1], "test", "--echo-argv", - bp::args+={"hello thingy", "\"stuff\""}, - bp::args+=" spa ce ", + bp::args+={"hello thingy", "\"stuff\""}, + bp::args+=" spa ce ", bp::std_out>is, ec ); diff --git a/test/env.cpp b/test/env.cpp index 5d709897..2e1546cf 100644 --- a/test/env.cpp +++ b/test/env.cpp @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(inherit_env, *boost::unit_test::timeout(2)) ); } else - BOOST_CHECK(boost::starts_with(s, "************** empty environment **************")); + BOOST_CHECK(boost::starts_with(s, "************** empty environment **************")); c.wait(); }