System & concurrency

CCommand

Start a process, stream its input and output, wait for completion, and control its lifetime.

C++23 mc/CCommand.h
#include <mc/CCommand.h>

The string constructor invokes a shell. fromArgs() executes an argument vector directly and searches PATH without shell expansion.

Combine Input, Output, and Error to pipe streams. OutputWithError combines stderr with stdout. Persistent lets the process outlive the command object.

Jump to a declaration · 29

CCommand

class CCommand : public CResource

Types, constants & data

using Strings = CVector<cstr>;
static constexpr int Input = 0x01;
static constexpr int Output = 0x02;
static constexpr int Error = 0x04;
static constexpr int OutputWithError = 0x08;
static constexpr int Persistent = 0x10;
static constexpr int Zsh = 0x20;
static constexpr int NoStatus = CMax<int>;
static constexpr int ErrorStatus = CMax<int> - 1;

Methods

CCommand

CCommand(const cstr& command, int mode = 0);
CCommand(const CCommand&) = delete;
CCommand(CCommand&&) = delete;

Immediately launches the command through a shell, using /bin/sh unless Zsh mode is selected. Use fromArgs() for an argument vector without shell expansion.

fromArgs

static CCommand fromArgs(const Strings& arguments, int mode = 0);

Immediately launches an executable from an argument vector, searching PATH as needed. Arguments are passed literally, without shell interpolation or redirection.

~CCommand

~CCommand() override;

Closes the command and waits for termination unless Persistent mode is set. A persistent child may continue running after its wrapper and captured streams are released.

closeResource

void closeResource() override;

Performs closing cleanup for a nonpersistent command. Persistent mode leaves the child running.

readOutput

bool readOutput(cstr& out, double timeout);
void readOutput(cstr& out);

Appends output. The untimed overload reads to EOF; the timed form waits for available bytes or EOF and returns false on timeout.

matchOutput

bool matchOutput(const CRegex& regex, Strings& m, double timeout);

Waits until the currently captured standard output matches the entire regex or the timeout in seconds expires. On a match, appends captures to m and consumes that buffered output; otherwise the buffered text remains available.

readError

bool readError(cstr& err, double timeout);
void readError(cstr& err);

Like readOutput(), for captured stderr.

matchError

bool matchError(const CRegex& regex, Strings& m, double timeout);

Matches and consumes captured standard error using the same whole-buffer and timeout behavior as matchOutput().

write

void write(const cstr& in);

Writes all supplied stdin bytes or throws. Captured output continues draining during the write.

await

int await();

Closes stdin, drains both captured streams, and returns the exit status. Captured bytes remain readable.

processId

int processId();

Returns the child’s process ID while it is tracked as running, or -1 after it has been reaped.

status

int status();

Returns −1 while running. Normal completion returns the exit code; signal termination returns ErrorStatus - signalNumber. NoStatus means no status is available.

close

void close(bool await);

Signals the owned process group, or the immediate child when inheriting terminal input. Escalates after one second; false selects asynchronous cleanup.

closeInput

void closeInput();

Sends EOF on the child’s piped stdin.

setCloseSignal

void setCloseSignal(size_t signalNum);

Sets the signal used by close() to request termination. Closing may escalate if the child does not exit within the grace period.

signal

void signal(size_t signalNum);

Signals the immediate child only. A finished command is a no-op.

isPersistent

bool isPersistent() const;

Reports whether the mode allows the child to outlive this wrapper.

command

const cstr& command() const;

Borrows the original shell command text, or the executable name supplied as the first argument to fromArgs().