### Example Usage of Show Window Flags Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to use the `process_show_window` flags when creating a process. This example shows how to launch a process with its window minimized. ```cpp process p{"C:\\not-a-virus.exe", {}, process::windows::show_window_minimized}; ``` -------------------------------- ### Process Starting Directory Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Configuration for the starting directory of a subprocess. ```APIDOC ## `start_dir.hpp` ```cpp /// Initializer for the starting directory of a subprocess to be launched. struct process_start_dir { filesystem::path start_dir; process_start_dir(filesystem::path start_dir); { } }; ``` ``` -------------------------------- ### Example Usage Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to launch a process using `basic_popen`, write data to its stdin, and read from its stdout. ```APIDOC ## Example Usage This example shows how to use the `basic_popen` class to execute a command, send input to it, and capture its output. ```cpp // Assuming 'executor' is an asio::io_context or similar, // and 'find_executable' is a utility to locate executables. // Launch the 'addr2line' process with the current executable as an argument. popen proc(executor, find_executable("addr2line"), {argv[0]}); // Write data to the process's standard input. asio::write(proc.get_stdin(), asio::buffer("main\n")); // Read data from the process's standard output until a newline character. std::string line; asio::read_until(proc.get_stdout(), asio::dynamic_buffer(line), '\n'); // 'line' now contains the output from the addr2line process. ``` ``` -------------------------------- ### Define Process Start Directory Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Initializes the starting directory for a subprocess. This struct holds the path that the new process will use as its current working directory. ```cpp /// Initializer for the starting directory of a subprocess to be launched. struct process_start_dir { filesystem::path start_dir; process_start_dir(filesystem::path start_dir); { } }; ``` -------------------------------- ### Example Usage of Windows Process Creation Flags Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to use process creation flags, such as `create_new_console`, when launching a process on Windows. ```cpp process p{"C:\\not-a-virus.exe", {}, process::windows::create_new_console}; ``` -------------------------------- ### Example Usage of Windows As User Launcher Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to use the `windows::as_user_launcher` to create a process. Requires an `asio::io_context` and a `boost::system::error_code`. ```cpp auto l = windows::as_user_launcher((HANDLE)0xDEADBEEF); asio::io_context ctx; boost::system::error_code ec; auto proc = l(ctx, ec, "C:\\User\\boost\\Downloads\\totally_not_a_virus.exe", {}); ``` -------------------------------- ### Create a Process with Default Launcher Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates the standard way of creating a process using `default_launcher`. This example shows a basic process creation with an executor, path, and arguments. ```cpp asio::io_context ctx; process proc(ctx.get_executor(), "test", {}); // equivalent to process prod = default_launcher()(ctx.get_executor(), "test", {}); ``` -------------------------------- ### Using Boost.Process popen Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html The `boost::process::popen` class starts a process and connects pipes for stdin and stdout, allowing it to be used as a stream. ```cpp asio::io_context ctx; boost::process::popen proc(ctx, "/usr/bin/addr2line", {argv[0]}); asio::write(proc, asio::buffer("main\n")); std::string line; asio::read_until(proc, asio::dynamic_buffer(line), '\n'); ``` -------------------------------- ### Custom Initializer for POSIX Launchers Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Defines a `custom_initializer` struct with methods to hook into the process launch lifecycle on POSIX systems. These methods are called before setup, on error, on success, on fork error, before exec setup, and on exec error. ```cpp struct custom_initializer { // called before a call to fork. A returned error will cancel the launch. template error_code on_setup(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called for every initializer if an error occurred during setup or process creation template void on_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line), const error_code & ec); // called after successful process creation template void on_success(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called for every initializer if an error occurred when forking, in addition to on_error. template void on_fork_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line), const error_code & ec); // called before a call to execve. A returned error will cancel the launch. Called from the child process. template error_code on_exec_setup(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); // called after a failed call to execve from the child process. template void on_exec_error(Launcher & launcher, const filesystem::path &executable, const char * const * (&cmd_line)); }; ``` -------------------------------- ### Wait for Process Completion Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Launches a process and waits for its completion, asserting that the exit code is 0. This is a basic example of process execution and synchronization. ```cpp process proc(ctx, "/bin/ls", {}); assert(proc.wait() == 0); ``` -------------------------------- ### Start Process in Specific Directory Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Use `process_start_dir` to specify the working directory for a new process. Relative paths may fail on POSIX systems if the directory is changed before `execve`. ```cpp asio::io_context ctx; process ls(ctx.get_executor(), "/ls", {}, process_start_dir("/home")); ls.wait(); ``` -------------------------------- ### Inherit Current Environment for Process Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Obtain the current environment using `environment::current` and pass it to a process. This example demonstrates using a vector of key-value pairs. ```cpp asio::io_context ctx; auto c = environment::current(); // we need to use a value, since windows needs wchar_t. std::vector my_env{c.begin(), c.end()}; my_env.push_back("SECRET=THIS_IS_A_TEST"); auto exe = environment::find_executable("g++", my_env); process proc(ctx, exe, {"main.cpp"}, process_environment(my_env)); ``` -------------------------------- ### Custom Initializer for Windows Launchers Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Defines a `custom_initializer` struct with methods to hook into the process launch lifecycle on Windows systems. These methods are called before setup, on error, and on success. ```cpp struct custom_initializer { // called before a call to CreateProcess. A returned error will cancel the launch. template error_code on_setup(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line); // called for every initializer if an error occurred during setup or process creation template void on_error(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line, const error_code & ec); // called after successful process creation template void on_success(Launcher & launcher, const filesystem::path &executable, std::wstring &cmd_line); }; ``` -------------------------------- ### Start Asynchronous Write Operation Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Initiates an asynchronous write operation to the child process. Uses a completion token to handle the result. Ensure buffers are valid and accessible. ```cpp template > auto async_write_some(const ConstBufferSequence& buffers, WriteToken && token = net::default_completion_token_t()); ``` -------------------------------- ### Tuple-like getters for key_value_pair_view Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Specialized get functions for accessing key and value by index, enabling structured bindings. ```cpp template inline auto get() const -> typename conditional::type; template<> key_view key_value_pair_view::get<0u>() const; template<> value_view key_value_pair_view::get<1u>() const; ``` -------------------------------- ### Getting C-style string and data pointers Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides access to the null-terminated C-style string and raw data pointer of a value_view. ```cpp const char_type * c_str(); const value_type * data() const; ``` -------------------------------- ### Get Current Process Environment View Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Obtain a view object representing the current environment of the process. This view allows iteration over environment variables. ```cpp inline current_view current() {return current_view();} ``` -------------------------------- ### Getting C-style string and data pointers for key_value_pair_view Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides access to the null-terminated C-style string and raw data pointer of a key_value_pair_view. ```cpp const value_type * c_str() const noexcept; const value_type * data() const; ``` -------------------------------- ### Get Process Environment Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the environment variables of a process. Functions are provided for `basic_process_handle` and `pid_type`. An `error_code` can be passed to capture errors. ```cpp template env_view cwd(basic_process_handle & handle, error_code & ec); template env_view cwd(basic_process_handle & handle) env_view env(pid_type pid, error_code & ec); env_view env(pid_type pid); ``` -------------------------------- ### Get Process Command Line Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the command line used to launch a process. Overloads are available for `basic_process_handle` and `pid_type`. An error code can be provided to handle potential failures. ```cpp template shell cmd(basic_process_handle & handle, error_code & ec); template shell cmd(basic_process_handle & handle); shell cmd(pid_type pid, error_code & ec); shell cmd(pid_type pid); ``` -------------------------------- ### Get Process Executable Path Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Fetches the path to the executable of a process. This function can be called with a `basic_process_handle` or a `pid_type`. Error handling is supported via an `error_code` parameter. ```cpp template filesystem::path exe(basic_process_handle & handle, error_code & ec); template filesystem::path exe(basic_process_handle & handle) filesystem::path exe(pid_type pid, error_code & ec); filesystem::path exe(pid_type pid); ``` -------------------------------- ### Start Asynchronous Read Operation Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Initiates an asynchronous read operation from the child process's stdout pipe. Uses a completion token to handle the result. Ensure buffers are valid and accessible. ```cpp template > auto async_read_some(const MutableBufferSequence& buffers, BOOST_ASIO_MOVE_ARG(ReadToken) token = net::default_completion_token_t()); ``` -------------------------------- ### Configure Subprocess stdio (Explicit Null Error Stream) Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Configures the standard error stream of a subprocess to be a null device. This example shows explicit initialization for C++17 and older standards. ```cpp asio::io_context ctx; /// C++17 v2::process proc17(ctx, "/bin/bash", {}, v2::process_stdio{.err=nullptr}); /// C++11 & C++14 v2::process proc17(ctx, "/bin/bash", {}, v2::process_stdio{ {}, {}, nullptr}); ``` -------------------------------- ### Redirect Subprocess I/O with FILE* Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Use `FILE*` streams, such as `stdout`, for redirecting subprocess I/O. This example forwards both stderr and stdout to the parent process's stdout. ```cpp asio::io_context ctx; // forward both stderr & stdout to stdout of the parent process process proc(ctx, "/usr/bin/g++", {"--version"}, process_stdio{{ /* in to default */}, stdout, stdout}); proc.wait(); ``` -------------------------------- ### Redirect Subprocess I/O with Native Handles Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Utilize native handles (like `int` on POSIX or `HANDLE` on Windows) or objects with a `native_handle()` method for I/O redirection. This example ignores stderr using a socket pair. ```cpp asio::io_context ctx; // ignore stderr asio::local::stream_protocol::socket sock{ctx}, other{ctx}; asio::local::connect_pair(sock, other); process proc(ctx, "~/not-a-virus", {}, process_stdio{sock, sock, nullptr}); proc.wait(); ``` -------------------------------- ### Launch a Process with Boost.Process Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates launching a process with an executor, executable path, arguments, and initializers. Ensure the executor, path, and arguments are correctly provided. ```cpp process proc(ctx.get_executor(), "/usr/bin/cp", {"source.txt", "target.txt"} ); ``` -------------------------------- ### Getting the size of a value_view Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Returns the number of characters in the value_view. ```cpp std::size_t size() const; ``` -------------------------------- ### Get Process ID Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the unique identifier (PID) of the process. ```cpp pid_type id() const; ``` -------------------------------- ### Environment Management with Boost.Process Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to access and modify the current environment for launching processes. ```APIDOC ## Inheriting an environment The current environment can be obtained by calling `environment::current` which returns a forward range of `environment::key_value_pair_view`. ### Example 1: Using `std::vector` for environment ```cpp asio::io_context ctx; auto c = environment::current(); // we need to use a value, since windows needs wchar_t. std::vector my_env{c.begin(), c.end()}; my_env.push_back("SECRET=THIS_IS_A_TEST"); auto exe = environment::find_executable("g++", my_env); process proc(ctx, exe, {"main.cpp"}, process_environment(my_env)); ``` ### Example 2: Using `std::unordered_map` for environment ```cpp asio::io_context ctx; std::unordered_map my_env; for (const auto & kv : environment::current()) if (kv.key().string() != "SECRET") my_env[kv.key()] = kv.value(); auto exe = environment::find_executable("g++", my_env); process proc(ctx, exe, {"main.cpp"}, process_environment(my_env)); ``` ``` -------------------------------- ### Getting the native string representation Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the native string representation of a value_view. ```cpp string_type native_string() const; ``` -------------------------------- ### Getting the native string view of a key_value_pair_view Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the native string view representation of the key_value_pair_view. ```cpp string_view_type native() const noexcept; ``` -------------------------------- ### Default Launcher Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html The standard way of creating a process using Boost.Process. ```APIDOC ## `default_launcher.hpp` The `default_launcher` is the standard way of creating a process. ### Basic Usage ```cpp asio::io_context ctx; process proc(ctx.get_executor(), "test", {}); // equivalent to process prod = default_launcher()(ctx.get_executor(), "test", {}); ``` ### Overloads It has four overloads: ```cpp (ExecutionContext &, filesystem::path, Args && args, Inits && ... inits) -> basic_process (Executor &, filesystem::path, Args && args, Inits && ... inits) -> basic_process; (ExecutionContext &, error_code&, filesystem::path, Args && args, Inits && ... inits) -> basic_process; (Executor &, error_code&, filesystem::path, Args && args, Inits && ... inits) -> basic_process ``` ``` -------------------------------- ### Bind Launcher Utilities Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Utilities for on-the-fly construction of a launcher with bound initializers. ```APIDOC ## `bind_launcher.hpp` The `bind_launcher` utlitities allow on the fly construction of a launcher with bound initializers. ### Function Overloads ```cpp // Creates a new launcher with the bound initializer. template auto bind_launcher(Launcher && launcher, Init && ... init); // Calls bind_launcher with the default_launcher as the first parameter. // The new launcher with bound parameters template auto bind_default_launcher(Init && ... init); ``` ``` -------------------------------- ### Get Process Exit Code Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the evaluated exit code of the process. This method is available for completed processes. ```cpp int exit_code() const; ``` -------------------------------- ### Boost.Process V1 Child Process Initialization Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Illustrates the DSL-like syntax used in Boost.Process V1 for defining child process settings, including executable, arguments, standard input redirection, and environment variables. ```cpp child c{exe="test", args+="--help", std_in < null(), env["FOO"] += "BAR"}; ``` -------------------------------- ### Launch Process and Interact with Pipes Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Launches a process, writes data to its stdin, and reads from its stdout. Ensure the executor and executable path are correctly provided. ```cpp popen proc(executor, find_executable("addr2line"), {argv[0]}); asio::write(proc, asio::buffer("main\n")); std::string line; asio::read_until(proc, asio::dynamic_buffer(line), '\n'); ``` -------------------------------- ### Get Current Process ID Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Returns the process ID of the currently executing process. This is a simple utility function for self-identification. ```cpp // Get the process id of the current process. pid_type current_pid(); ``` -------------------------------- ### Boost.Process V2 Process Initialization Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Shows the simplified interface for creating a process in Boost.Process V2. It uses explicit initializers for components like stdio and environment, requiring an `asio::io_context`. ```cpp extern std::unordered_map my_env; extern asio::io_context ctx; process proc(ctx, "./test", {"--help"}, process_stdio{nullptr, {}, {}}, process_environment(my_env)); ``` -------------------------------- ### Process Environment Management Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates how to set the environment for a child process using `process_environment` and its reusability. ```APIDOC ## Process Environment Management ### Description `process_environment` is used to set the environment variables for a child process. The environment initializer maintains its state, allowing it to be used multiple times. Note that the operating system may modify the internal state. ### Example Usage ```cpp // Setting environment for a single process process proc1{executor, find_executable("printenv"), {"foo"}, process_environment{"foo=bar"}}; // Reusing environment initializer for multiple processes auto exe = find_executable("printenv"); process_environment env = {"FOO=BAR", "BAR=FOO"}; process proc2(executor, exe, {"FOO"}, env); process proc3(executor, exe, {"BAR"}, env); ``` ``` -------------------------------- ### Process Information (`ext`) Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides functions to obtain information about third-party processes, such as command line, current working directory, environment, and executable path. ```APIDOC ## Process Information (`ext`) ### Description Provides functions to obtain information about third-party processes. ### Functions #### `cmd` Get the command line used to launch the process. - `template shell cmd(basic_process_handle & handle, error_code & ec);` - `template shell cmd(basic_process_handle & handle);` - `shell cmd(pid_type pid, error_code & ec);` - `shell cmd(pid_type pid);` #### `cwd` Get the current working directory of the process. - `template filesystem::path cwd(basic_process_handle & handle, error_code & ec);` - `template filesystem::path cwd(basic_process_handle & handle);` - `filesystem::path cwd(pid_type pid, error_code & ec);` - `filesystem::path cwd(pid_type pid);` #### `env` Get the current environment of the process. - `template env_view env(basic_process_handle & handle, error_code & ec);` - `template env_view env(basic_process_handle & handle);` - `env_view env(pid_type pid, error_code & ec);` - `env_view env(pid_type pid);` #### `exe` Get the executable of the process. - `template filesystem::path exe(basic_process_handle & handle, error_code & ec);` - `template filesystem::path exe(basic_process_handle & handle);` - `filesystem::path exe(pid_type pid, error_code & ec);` - `filesystem::path exe(pid_type pid);` ### Notes - The function may fail with "operation_not_supported" on some niche platforms. - On Windows, overloads taking a `HANDLE` are also available. ``` -------------------------------- ### Constructing a key object Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Constructors for the key class, accepting various string types, raw pointers, and iterators. ```cpp key(); key( const key& p ) = default; key( key&& p ) noexcept = default; key( const string_type& source ); key( string_type&& source ); key( const value_type * raw ); key( value_type * raw ); explicit key(key_view kv); template< class Source > key( const Source& source); key(const typename conditional::value, wchar_t, char>::type * raw); template< class InputIt > key( InputIt first, InputIt last); ``` -------------------------------- ### Get Environment Variable Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the value of an environment variable from the current process. Overloads are provided for different key types and error handling. ```cpp value get(const key & k, error_code & ec); value get(const key & k); value get(basic_cstring_ref> k, error_code & ec); value get(basic_cstring_ref> k); value get(const char_type * c, error_code & ec); value get(const char_type * c); ``` -------------------------------- ### key Constructors Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Details the various constructors available for the `key` class, which represents a key within an environment. ```APIDOC ## key Constructors ### Description This section outlines the constructors for the `key` class, used to represent a key in an environment. It supports construction from various string types and views. ### Constructors - `key();` - `key( const key& p ) = default;` - `key( key&& p ) noexcept = default;` - `key( const string_type& source );` - `key( string_type&& source );` - `key( const value_type * raw );` - `key( value_type * raw );` - `explicit key(key_view kv);` - `template< class Source > key( const Source& source);` - `key(const typename conditional::value, wchar_t, char>::type * raw);` - `template< class InputIt > key( InputIt first, InputIt last); ``` -------------------------------- ### Execute Process and Get Exit Code Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Uses the `execute` function to run a process and directly obtain its exit code. This is a synchronous operation. ```cpp assert(execute(process(ctx, "/bin/ls", {})) == 0); ``` -------------------------------- ### Constructing key_value_pair_view from various sources Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Constructors for key_value_pair_view, accepting different source types including raw pointers and string views. ```cpp key_value_pair_view( const char_type * p); key_value_pair_view( char_type * p); ``` -------------------------------- ### Process Window Show Flags Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Defines templated initializers for setting `wShowWindow` flags when creating processes on Windows. These flags control how the process window is displayed upon creation. ```cpp /// A templated initializer to set wShowWindow flags. template struct process_show_window; //Hides the window and activates another window. constexpr static process_show_window show_window_hide; //Activates the window and displays it as a maximized window. constexpr static process_show_window show_window_maximized; //Activates the window and displays it as a minimized window. constexpr static process_show_window show_window_minimized; //Displays the window as a minimized window. This value is similar to `minimized`, except the window is not activated. constexpr static process_show_window show_window_minimized_not_active; //Displays a window in its most recent size and position. This value is similar to show_normal`, except that the window is not activated. constexpr static process_show_window show_window_not_active; //Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. constexpr static process_show_window show_window_normal; ``` -------------------------------- ### Get Native Exit Code Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves the native representation of the process's exit code. This might be undefined on POSIX systems that only support signals. ```cpp native_exit_code_type native_exit_code() const; ``` -------------------------------- ### Process Creation Flags (`windows/creation_flags.hpp`) Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Allows explicitly setting `dwFlags` for process creation on Windows. ```APIDOC ## Process Creation Flags (`windows/creation_flags.hpp`) ### Description Creation flags allows explicitly setting `dwFlags` for process creation on Windows. ### Flags - `template struct process_creation_flags;` An initializer to add to the dwFlags in the startup-info. - `constexpr static process_creation_flags create_new_process_group;` A flag to create a new process group. Necessary to allow interrupts for the subprocess. - `constexpr static process_creation_flags create_breakaway_from_job;` Breakaway from the current job object. - `constexpr static process_creation_flags create_new_console;` Allocate a new console. ### Usage The flags can be used like this: ``` process p{"C:\\not-a-virus.exe", {}, process::windows::create_new_console}; ``` ``` -------------------------------- ### Asynchronous Process Execution with Cancellation Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Demonstrates `async_execute` with multiple cancellation strategies. `asio::cancellation_type::partial` maps to `request_exit`, and `asio::cancellation_type::terminal` maps to `terminate`. ```cpp async_execute(process(ctx, "/usr/bin/g++", {"hello_world.cpp"})) (asio::cancel_after(std::chrono::seconds(10), asio::cancellation_type::partial)) (asio::cancel_after(std::chrono::seconds(10), asio::cancellation_type::terminal)) (asio::detached); ``` -------------------------------- ### Find Home Directory in Environment Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Locates the home directory within a given environment. The environment must be a range supporting key-value pairs where the key is comparable to `key_view` and the value is convertible to `filesystem::path`. Defaults to the current process environment. ```cpp template inline filesystem::path home(Environment && env = current()); ``` -------------------------------- ### Inherit Current Environment using std::unordered_map Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html An alternative method to inherit the current environment using `std::unordered_map`. This example filters out a specific environment variable. ```cpp asio::io_context ctx; std::unordered_map my_env; for (const auto & kv : environment::current()) if (kv.key().string() != "SECRET") my_env[kv.key()] = kv.value(); auto exe = environment::find_executable("g++", my_env); process proc(ctx, exe, {"main.cpp"}, process_environment(my_env)); ``` -------------------------------- ### Constructing a basic_string from a value_view Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Creates a std::basic_string from a value_view, optionally specifying an allocator. ```cpp template< class CharT, class Traits = std::char_traits, class Alloc = std::allocator > std::basic_string basic_string( const Alloc& alloc = Alloc() ) const; ``` -------------------------------- ### basic_popen Class Overview Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html The basic_popen class allows for launching child processes and managing their input/output streams via pipes. It supports various constructors for flexibility in process creation and provides access to stdin and stdout pipes. ```APIDOC ## basic_popen Class ### Description Launches a process and connects its stdin and stderr to pipes. This class provides a way to interact with child processes by reading from their standard output and writing to their standard input. ### Template Parameters * `typename Executor`: The type of the executor used for asynchronous operations. ### Member Types * `executor_type`: The executor of the process. * `rebind_executor`: Rebinds the popen type to another executor. * `other`: The pipe type when rebound to the specified executor. * `stdin_type`: The type used for stdin on the parent process side. * `stdout_type`: The type used for stdout on the parent process side. ### Constructors Several constructors are available to create and launch a `basic_popen` instance: * `explicit basic_popen(executor_type exec)`: Creates a closed process handle. * `template explicit basic_popen(ExecutionContext & context)`: Creates a closed process handle using an execution context. * `template explicit basic_popen(executor_type executor, const filesystem::path& exe, std::initializer_list args, Inits&&... inits)`: Constructs a child from a property list and launches it. * `template explicit basic_popen(Launcher && launcher, executor_type executor, const filesystem::path& exe, std::initializer_list args, Inits&&... inits)`: Constructs a child using a specific launcher. * `template explicit basic_popen(executor_type executor, const filesystem::path& exe, Args&& args, Inits&&... inits)`: Constructs a child with arguments. * `template explicit basic_popen(Launcher && launcher, executor_type executor, const filesystem::path& exe, Args&& args, Inits&&... inits)`: Constructs a child using a specific launcher and arguments. * `template explicit basic_popen(ExecutionContext & context, const filesystem::path& exe, std::initializer_list args, Inits&&... inits)`: Constructs a child using an execution context and arguments. * `template explicit basic_popen(Launcher && launcher, ExecutionContext & context, const filesystem::path& exe, std::initializer_list args, Inits&&... inits)`: Constructs a child using a specific launcher, execution context, and arguments. * `template explicit basic_popen(ExecutionContext & context, const filesystem::path& exe, Args&& args, Inits&&... inits)`: Constructs a child using an execution context and arguments. * `template explicit basic_popen(Launcher && launcher, ExecutionContext & context, const filesystem::path& exe, Args&& args, Inits&&... inits)`: Constructs a child using a specific launcher, execution context, and arguments. ### Move Operations * `basic_popen(basic_popen &&) = default`: Move constructs a `basic_popen`. * `basic_popen& operator=(basic_popen &&) = default`: Move assigns a `basic_popen`. * `template basic_popen(basic_popen&& lhs)`: Move constructs a `basic_popen` and changes the executor type. ### Member Functions * `stdin_type & get_stdin()`: Get the stdin pipe. * `const stdin_type & get_stdin() const`: Get the stdin pipe (const version). * `stdout_type & get_stdout()`: Get the stdout pipe. * `const stdout_type & get_stdout() const`: Get the stdout pipe (const version). * `template std::size_t write_some(const ConstBufferSequence& buffers)`: Write some data to the stdin pipe. * `template std::size_t write_some(const ConstBufferSequence& buffers, ...)`: Write some data to the stdin pipe (overload). ``` -------------------------------- ### Comparing key_value_pair_view objects Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Comparison methods for key_value_pair_view objects against other key_value_pair_view, strings, and C-style strings. ```cpp int compare( key_value_pair_view p ) const noexcept; int compare( const string_type& str ) const; int compare( string_view_type str ) const; int compare( const value_type* s ) const; ``` -------------------------------- ### Get Process Executable Path Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieve the path to the executable file of a process. This functionality is available for both process handles and PIDs. An error_code can be supplied to catch potential errors, such as 'operation_not_supported'. ```cpp template filesystem::path exe(basic_process_handle & handle, error_code & ec); template filesystem::path exe(basic_process_handle & handle) filesystem::path exe(pid_type pid, error_code & ec); filesystem::path exe(pid_type pid); ``` -------------------------------- ### Get Parent Process ID Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Returns the process ID of the parent process for a given process ID. An overload is available to handle errors using `boost::system::error_code`. ```cpp // return parent pid of pid. pid_type parent_pid(pid_type pid, boost::system::error_code & ec); pid_type parent_pid(pid_type pid); ``` -------------------------------- ### Get Process Current Working Directory Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Obtains the current working directory of a process. Supports both `basic_process_handle` and `pid_type` arguments. An optional `error_code` parameter can be used for error handling. ```cpp template filesystem::path cwd(basic_process_handle & handle, error_code & ec); template filesystem::path cwd(basic_process_handle & handle) filesystem::path cwd(pid_type pid, error_code & ec); filesystem::path cwd(pid_type pid); ``` -------------------------------- ### value_view Methods and Operators Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides an overview of the methods and overloaded operators available for the `value_view` class, used for representing values in an environment. ```APIDOC ## value_view Methods and Operators ### Description This section details the available methods and comparison operators for the `value_view` class, which represents a view of a value within an environment. ### Methods - `int compare( string_view_type str ) const;` - `int compare( const value_type* s ) const;` - `template< class CharT, class Traits = std::char_traits, class Alloc = std::allocator > std::basic_string basic_string( const Alloc& alloc = Alloc() ) const;` - `std::string string() const;` - `std::wstring wstring() const;` - `string_type native_string() const;` - `bool empty() const;` - `value_iterator begin() const;` - `value_iterator end() const;` - `const char_type * c_str();` - `const value_type * data() const;` - `std::size_t size() const;` ### Operators - `friend bool operator==(value_view l, value_view r);` - `friend bool operator!=(value_view l, value_view r);` - `friend bool operator<=(value_view l, value_view r);` - `friend bool operator>=(value_view l, value_view r);` - `friend bool operator< (value_view l, value_view r);` - `friend bool operator> (value_view l, value_view r);` - `template< class CharT, class Traits > friend std::basic_ostream& operator<<( std::basic_ostream& os, const value_view& p );` - `template< class CharT, class Traits > friend std::basic_istream& operator>>( std::basic_istream& is, value_view& p ); ``` -------------------------------- ### Converting key_value_pair_view to basic_string Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Constructs a std::basic_string from a key_value_pair_view, with optional allocator. ```cpp template< class CharT, class Traits = std::char_traits, class Alloc = std::allocator > std::basic_string basic_string( const Alloc& alloc = Alloc()) const; ``` -------------------------------- ### Key Structure Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Details the methods and operators available for the 'key' structure. ```APIDOC ## Key Structure ### Description Represents a key, likely used for identification or mapping. Provides various methods for construction, assignment, modification, and comparison. ### Methods and Operators #### Constructors - `key() = default;` - `key& operator=( const key& p ) = default;` - `key& operator=( key&& p );` - `key& operator=( string_type&& source );` - `template< class Source > key& operator=( const Source& source );` - `key& assign( string_type&& source );` - `template< class Source > key& assign( const Source& source );` - `template< class InputIt > key& assign( InputIt first, InputIt last );` #### Modifiers - `void clear();` - `void swap( key& other ) noexcept;` #### Accessors - `const value_type* c_str() const noexcept;` - `const string_type& native() const noexcept;` - `string_view_type native_view() const noexcept;` - `const string_type & native_string() const;` - `bool empty() const; - `const value_type * data() const;` - `std::size_t size() const;` #### Conversion Operators - `operator string_type() const;` - `operator string_view_type() const;` #### Comparison Methods - `int compare( const key& p ) const noexcept;` - `int compare( const string_type& str ) const;` - `int compare( string_view_type str ) const;` - `int compare( const value_type* s ) const;` #### String Conversion - `template< class CharT, class Traits = std::char_traits, class Alloc = std::allocator > std::basic_string basic_string( const Alloc& alloc = Alloc()) const;` - `std::string string() const;` - `std::wstring wstring() const;` #### Comparison Operators - `friend bool operator==(const key & l, const key & r);` - `friend bool operator!=(const key & l, const key & r);` - `friend bool operator<=(const key & l, const key & r);` - `friend bool operator>=(const key & l, const key & r);` - `friend bool operator< (const key & l, const key & r);` - `friend bool operator> (const key & l, const key & r);` #### Stream Operators - `template< class CharT, class Traits > friend std::basic_ostream& operator<<( std::basic_ostream& os, const key& p );` - `template< class CharT, class Traits > friend std::basic_istream& operator>>( std::basic_istream& is, key& p );` ``` -------------------------------- ### Redirect Subprocess I/O with Readable Pipes Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Redirect standard input, output, and error streams of a subprocess using `process_stdio`. This example redirects stdout to a `readable_pipe` for reading the compiler version. ```cpp asio::io_context ctx; asio::readable_pipe rp{ctx}; process proc(ctx, "/usr/bin/g++", {"--version"}, process_stdio{{ /* in to default */}, rp, { /* err to default */}}); std::string output; boost::system::error_code ec; asio::read(rp, asio::dynamic_buffer(output), ec); assert(!ec || (ec == asio::error::eof)); proc.wait(); ``` -------------------------------- ### Popen Class Methods Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html This snippet details the methods available in the `basic_popen` class for interacting with child processes. ```APIDOC ## POST /api/users ### Description This endpoint allows for the creation of new user resources. ### Method POST ### Endpoint /api/users ### Parameters #### Query Parameters - **limit** (integer) - Optional - The maximum number of users to return. #### Request Body - **name** (string) - Required - The name of the user. - **email** (string) - Required - The email address of the user. ### Request Example ```json { "name": "John Doe", "email": "john.doe@example.com" } ``` ### Response #### Success Response (200) - **id** (integer) - The unique identifier for the newly created user. - **name** (string) - The name of the user. - **email** (string) - The email address of the user. #### Response Example ```json { "id": 123, "name": "John Doe", "email": "john.doe@example.com" } ``` ## GET /api/users/{id} ### Description This endpoint retrieves the details of a specific user. ### Method GET ### Endpoint /api/users/{id} ### Parameters #### Path Parameters - **id** (integer) - Required - The unique identifier of the user to retrieve. ### Response #### Success Response (200) - **id** (integer) - The unique identifier for the user. - **name** (string) - The name of the user. - **email** (string) - The email address of the user. #### Response Example ```json { "id": 123, "name": "John Doe", "email": "john.doe@example.com" } ``` ``` -------------------------------- ### key_value_pair Constructors Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides various ways to construct a key_value_pair object, including from key-value views, initializer lists, strings, raw character pointers, and pairs. ```cpp key_value_pair(key_view key, value_view value); key_value_pair(key_view key, std::initializer_list> values); key_value_pair( const string_type& source ); key_value_pair( string_type&& source ); key_value_pair( const value_type * raw ); key_value_pair( value_type * raw ); explicit key_value_pair(key_value_pair_view kv) : value_(kv.c_str()) {} template< class Source > key_value_pair( const Source& source); template< typename Key, typename Value > key_value_pair( const std::pair & kv); key_value_pair(const typename conditional::value, wchar_t, char>::type * raw); template< class InputIt , typename std::iterator_traits::iterator_category> key_value_pair( InputIt first, InputIt last ); ``` -------------------------------- ### Get Child Process IDs Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Retrieves a list of process IDs for all direct child processes of a given process ID. An overload is provided to capture errors using `boost::system::error_code`. ```cpp // return child pids of pid. std::vector child_pids(pid_type pid, boost::system::error_code & ec); std::vector child_pids(pid_type pid); ``` -------------------------------- ### Environment View Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides a view of the current process's environment variables. ```APIDOC ## Struct: current_view ### Description A view object for the current environment of this process. The view might be owning (e.g., on Windows) or not (e.g., on POSIX). If it owns, it will deallocate the environment on destruction, similar to a `unique_ptr`. **Note:** Accessing the environment in this way is not thread-safe. ### Usage Example ```cpp void dump_my_env(current_view env = current()) { for (auto & [k, v] : env) std::cout << k.string() << " = " << v.string() << std::endl; } ``` ### Members - `native_handle_type native_handle()`: Returns the native handle of the environment. - `iterator begin()`: Returns an iterator to the beginning of the environment. - `iterator end()`: Returns an iterator to the end of the environment. ### Iterator - `using value_type = key_value_pair_view;` - `operator++()`: Advances the iterator. - `key_value_pair_view operator*() const`: Dereferences the iterator to get a key-value pair view. ``` -------------------------------- ### Converting key_value_pair_view to std::string or std::wstring Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Provides methods to convert a key_value_pair_view to std::string or std::wstring. ```cpp std::string string() const; std::wstring wstring() const; ``` -------------------------------- ### Find Executable in Environment Source: https://www.boost.org/doc/libs/latest/libs/process/doc/html/index.html Searches for a specified executable file within the provided environment. The environment must be a range supporting key-value pairs. Defaults to the current process environment. ```cpp template filesystem::path find_executable(BOOST_PROCESS_V2_NAMESPACE::filesystem::path name, Environment && env = current()); ```