Provided by: libbobcat-dev_6.04.00-1ubuntu3_amd64 

NAME
FBB::Process - Runs external programs
SYNOPSIS
#include <bobcat/process>
Linking option: -lbobcat
DESCRIPTION
The FBB::Process class offers an extensive interface to calling external programs and/or scripts from a
C++ program (so-called child-processes). The class offers an easy to use, stream-based interface to the
standard input, standard output and standard error streams of child processes.
Objects of the class Process use standard process-executing functions, like members of the execl(2)
family or sh(1) to execute child processes. Thus, child processes can be executable programs or
shell-scripts.
The standard input, output and error streams of child processes may be accessed through their Process
parent objects. Input expected by child processes may be inserted by Process objects, and output
generated by child processes may be extracted from Process objects.
When using (output) redirection with the USE_SHELL path specification (see below for the path and IOMode
specifications), the IGNORE_COUT IOMode (and possibly IGNORE_CERR) should normally be specified.
Process objects may repeatedly be used to execute the same or different child processes. Before the next
child process is started, the Process object first terminates its currently active child process.
Alternatively, a currently active child process is automatically ended if the Process object goes out of
scope, if its stop or eoi (end-of-information) member is called, or if the eoi manipulator is inserted
into the Process object.
Programs called as child processes may be specified when constructing a Process object or by using
Process’s setCommand member. Process constructors (or Process set-members) never start child processes.
Child processes are started through start members or the assignment operator.
Child processes may receive information at their standard input streams through information inserted into
Process objects. In these cases the Process objects must inform their child processes that they have
received all input. For this the close or eoi member or the eoi manipulator can be used. After calling
the close member, the waitForChild member should be called as well. This is not necessary if either the
eoi member or the eoi manipulator is used.
If waitForChild is not called (but information sent to the child which could not be fully processed by
the child process in case the child process terminated as a result of the Process object going out of
scope), then the operating system issues a Broken pipe message, indicating that information in a pipe was
lost.
Arguments passed to child processes may be surrounded by double or single quotes. Arguments surrounded by
double quotes have their double quotes removed, while interpreting any escape-sequences that may have
been used within. Arguments surrounded by single quotes have their single quotes removed, while accepting
their content as-is. In addition unquoted escape-sequences may be specified: those escape sequences are
evaluated and replaced by their intended characters (e.g., \100 is converted to @).
A full command specification may be surrounded by backtics (`-characters). These backtick characters are
removed by the Process object when the command is started.
Child processes may be allowed a limited amount of time (in seconds) to complete. By default no time
limit is imposed upon child processes.
By default the standard input, output and error streams of child processes are accessed through their
Process parent processes: information inserted into the Process object is forwarded to the child
process’s standard input stream, information sent by the child process to its standard output stream can
be extracted from its parent Process object, and information sent by the child process to its standard
error stream may be obtained through Process’s childErrStream member.
If the parent and child processes have agreed on some communication process, then information may
alternatingly be sent to and received from the child process through the Process’s ostream and istream
facilities. Alternatively, unspecified amounts of information written by child processes may be processed
by separate threads (cf. this manual page’s EXAMPLES section).
Process objects use Pipe objects (cf. pipe(3bobcat)) for communication with its child processes. To
ensure that these pipes are properly closed the members waitForChild, stop or the eoi manipulator should
be used. Once a Process object ceases to exist pipes to its child process are also closed.
NAMESPACE
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the
namespace FBB.
INHERITS FROM
FBB::Fork(3bobcat) (private),
FBB:IOStream(3bobcat), and by implication: FBB::Eoi,
FBB:ProcessEnums
The struct ProcessEnums defines enumerations and support functions which are used by several classes. Its
enumerations are documented below; there is no separate ProcessEnums man-page.
ENUMERATIONS
enum ProcessType:
The enum ProcessType defines how a child process is started or located. Its values are specified at
constructor-time or through the setProcessType member. This enumeration defines the following symbolic
constants:
o NO_PATH:
The program specified as child process is started as specified, without searching the elements of
the PATH environment variable.
o USE_PATH:
The elements of the PATH environment variable are used when locating the program specified as
child process.
o USE_SHELL:
The program specified as child process is called using /bin/sh -c. When (output) redirection is
used with the specified command the IGNORE_COUT IOMode (and possibly also the IGNORE_CERR IOMode)
should be specified.
enum IOMode:
Values of the enum IOMode are used to define which of the child process’s standard streams can be
accessed through the Process object. Its symbolic constants may be combined using the bit_or operator. By
default CIN | COUT | CERR is used (see below).
The following symbolic constants are available:
o ALL:
Shortcut for CIN | COUT | CERR.
o CIN:
Information inserted into the Process object is forwarded to its child process. If this is not
required then CIN should not be specified.
o CERR:
Information written by the child process to its standard error stream is accessible through
Process’s childErrStream member. If this is not required then CERR should not be specified.
o COUT:
Information written by the child process to its standard output stream may be directly be
extracted from the Process object, or from its childOutStream member. If this is not required
then COUT should not be specified.
o DIRECT:
When starting a child process (see below at the member start) the current process (i.e., the
program defining the Process object) is replaced by the child process, inheriting the current
process’s standard input and output streams. If this mode is specified in combination with any
other IOMode (except for NONE, see below) an std::invalid_argument exception is thrown.
o IGNORE_CERR:
Information written by the child process to its standard error stream is sent to /dev/null. An
std::invalid_argument exception is thrown if this mode is specified in combination with DIRECT,
CERR and/or MERGE_COUT_CERR.
o IGNORE_COUT:
Information written by the child process to its standard output stream is sent to /dev/null. An
std::invalid_argument exception is thrown if this mode is specified in combination with COUT,
DIRECT and/or MERGE_COUT_CERR.
o IGNORE_COUT_CERR:
Shortcut for IGNORE_CERR | IGNORE_COUT.
o MERGE_COUT_CERR:
Information extracted from the Process object is written by the child process to its standard
output and standard error streams. An std::invalid_argument exception is thrown if this mode is
specified in combination with COUT, CERR, DIRECT, IGNORE_COUT or IGNORE_CERR.
o NONE:
The Process object does not extract information from or insert information into the standard
streams of its child process. The child process reads the same standard input stream and writes
the same standard output streams as its parent Process object. When this mode is specified in
combination with other IOMode values it is silently ignored.
enum ChildOutput:
The ChildOutput enumeration defines values returned by the available member (see below) indicating to
which standard stream the child process has written information. This enumeration defines the following
values:
o NOTHING_AVAILABLE:
The child process did not (yet) write any information to its standard streams;
o CHILD_COUT:
The child process wrote information to its standard output stream which is waiting for extraction.
o CHILD_CERR:
The child process wrote information to its standard error stream which is waiting for extraction.
The latter two values may be combined using the bit_or operator. The bit_and operator,returning a
bool value can be used to test whether information on a specific output stream is available.
PROCESS PARAMETERS
Four process parameters may be specified: the sizes of the stream buffers which are used when
communicating with child processes; to specify which of the standard streams of child processes can be
accessed from the Process object combinations of IOMode values are used; to specify how child programs
are found a ProcessType value is used; to specify the maximum time (in seconds) the child program is
allowed to run a size_t values is used.
By default, the stream buffers hold 200 bytes; all the child’s standard streams (standard input, output
and error) are accessible from the Parent process; the PATH environment variable is not used to locate
the child program; and the child processes will be allowed an unlimited amount of time to run.
After constructing a Process object all default parameters may be modified. These parameters may either
be altered for a single process or a Process object’s general defaults may be modified. The set* members
(see below) may be used to change the default process parameters. When parameters are specified
otherwise, they will only be active for the next process.
CONSTRUCTORS
The command provided to the following constructors may be the (initial part of the) specification of an
external program to run. When the program is eventually started it may start and end with a back-tick
(`). The back-ticks will be removed just before the specified program is executed.
Child processes are not started automatically following Process object constructions. A start member or
the assignment operator (see below) is used to start the specified child process.
Constructors expecting an IOMode argument may be provided with multiple IOMode values by combining them
using the bit-or operator.
After constructing a Process object its parameters can be changed using set-member functions, function
call operators or start members.
o Process(std::string const &cmd = ""):
This constructor is used to specify the (initial part of a) command to execute from a Process
object. Default values are used for the process parameters (see section PROCESS PARAMETERS).
o Process(IOMode mode, std::string const &cmd = ""):
This constructor requires the specification of the object’s IOMode, and it can be used to specify
the (initial part of a) command to execute from a Process object. Default values are used for the
remaining process parameters (see section PROCESS PARAMETERS).
o Process(IOMode mode, ProcessType type, std::string const &cmd = ""):
This constructor requires the specification of the object’s IOMode and ProcessType, and it can be
used to specify the (initial part of a) command to execute from a Process object. Default values
are used for the remaining process parameters (see section PROCESS PARAMETERS).
o Process(IOMode mode, ProcessType type, size_t timeLimit, std::string const &cmd = ""):
This constructor requires the specification of the object’s IOMode, ProcessType, and child process
time limit. The (initial part of a) command to execute from a Process object may optionally be
specified. The default process parameter is used for the sizes of the internally used stream
buffers (see section PROCESS PARAMETERS).
o Process(IOMode mode, ProcessType type, size_t timeLimit, size_t bufferSize, std::string const &cmd
= ""):
This constructor requires the specification of the object’s IOMode, ProcessType, child process
time limit, and size of the internally used stream buffers. The (initial part of a) command to
execute from a Process object may optionally be specified. Note that this constructor’s mode
parameter does not accept a size_t argument.
Copy and move constructors (and assignment operators) are not available.
OVERLOADED OPERATORS
o Process &operator<<(Type value):
This operator inserts value into the child’s standard input stream. I.e., the child process reads
value from its standard input. A value of any type that can be inserted into an ostream can be
inserted into a Process object. Nothing happens if the member is used when the child process has
terminated. Manipulators like std::endl are also supported. The behavior of this operator is
undefined unless IOMode CIN was specified.
o Process &operator>>(Type value):
This operator extracts value from the child’s standard output stream and optionally (if IOMode
MERGE_COUT_CERR was specified) from the child’s error stream. I.e., value may be extracted from
Process objects. A value of any type that can be extracted from an istream can be extracted from a
Process object. Nothing happens if the member is used when the child process has terminated. The
behavior of this operator is undefined unless IOMode COUT or MERGE_COUT_CERR was specified.
o Process &operator+=(std::string const &):
This operator adds the provided std::string object to the currenly defined command specification
of a Process object. The member operator+= does not add a separating blank space between the
currently stored command specification and the text to append. It merely adds its right-hand side
string to the command stored so far. It does not affect a currently running child process.
o int operator=(std::string const &cmd):
The operator= member defines cmd as the stored command in a Process object.
Before starting the child process a possibly active child process is first stopped by calling
stop. It returns stop’s return value. Immediately after calling stop the new command (cmd) is
started. If stopping and restarting another command should be separate actions then use stop,
followed by setCommand, followed by calling an appropriate overloaded version of the member start
(start() uses the object’s current IOMode, ProcessType, and time limit).
o Process &operator()(IOMode mode):
This operator changes the the Process object’s IOMode parameter. A reference to the Process object
is returned, allowing constructions like
process(Process::COUT) = "/bin/cat";
to start a new child process with the specified IOMode.
o Process &operator()(IOMode mode, ProcessType type):
This operator changes the Process object’s IOMode and ProcessType process parameters.
o Process &operator()(IOMode mode, ProcessType type, size_t timeLimit):
This operator changes the Process object’s mentioned process parameters. The currently specified
default size of the stream buffers is kept as-is.
o Process &operator()(IOMode mode, ProcessType type, size_t timeLimit, size_t bufferSize):
This operator changes all of the Process object’s process parameters.
o Process &operator|(Process &lhs, Process &rhs):
This operator implements process piping: information sent b lhs to its standard output becomes the
rhs’s standard input. The operator returns rhs.
This operator mimics the piping-operator supported by most command-shell programs and should not
be confused with the binary-or operator. The operator starts the lhs’s child process, but the
rhs’s child process (and thus pipe processing) must explicitly be started.
Since operator| is left-associative and rhs is returned piping can be chained, allowing
constructions like p1 | p2 | p3, where p1, p2 and p3 are Process objects.
The following idiom can be used to start the execution of a chain of processes: (p1 | p2 |
p3).start(). Alternatively, the following two-step procedure can be used:
p1 | p2 | p3;
p3.start();
If p1 specifies Process::CIN then this IOMode is forwared to the final process of the chain of
processes. It is not necessary to specify Process::CIN for p3. In fact, most IOMode flags of
processes passed to operator| are ignored or modified. Acceptable IOModes are Process::IGNORE_CERR
and Process::CERR (accepted for all processes), Process::CIN (accepted for the first process of
the chain), and Process::COUT (for the last process of the chain).
Note: when connecting a series of processes using operator| all input and output (except for the
standard error streams) is handled through the last process: if Process::CIN is specified for the
first process then this mode is transferred to the last process, so information inserted into the
last process enters the pipe through the first process’s standard input.
The next example illustrates how input can be inserted into the first process from a main process
and sent to the standard output stream by the final process:
using namespace std;
using namespace FBB;
Process p1(Process::CIN, "/bin/cat");
Process p2("/bin/cat");
Process p3(Process::NONE, "/bin/cat");
p1 | p2 | p3;
p3.start();
p3 << cin.rdbuf() << eoi;
When joining multiple commands using the piping operator (’|’), the process type USE_SHELL is not
required, even though process-piping is commonly used as a shell-feature. Process’s operator|
handles I/O piping itself, and thus can avoid the additional shell process.
MEMBERS
o bool active():
This member returns true if the child process is currently running and false if not.
o size_t available():
This member returns immediately. Its return value indicates whether any information can be
obtained from the child process as value(s) from the enum ChildOutput. NOTHING_AVAILABLE is
returned if no information is ready for extraction. CHILD_COUT is returned if information from the
child process’s standard output stream is available; CHILD_CERR is returned if information from
the child process’s standard error stream is available; CHILD_COUT | CHILD_CERR is returned if
information from both the standard output and standard error streams is available. The bit_and
operator, returning a bool value, can be used to determine which stream has any pending
information. E.g.,
if (process.available() & Process::CHILD_COUT)
cout << "Process has child standard output available";
o size_t bufSize() const:
This member returns the default size of the stream buffers that are used for communication with
child processes. If called while a child process is actually running, then the value returned by
this member may differ from the value that was actually used when starting the child process, as
the default value may be altered by a function call operator just before starting the child
process.
o std::istream &childErrStream():
If Process::CERR was specified then this member interfaces to the child’s standard error stream.
By extracting the information from childErrStream the parent process retrieves the information
sent by its child process to its standard error stream.
o std::istream &childOutStream():
If Process::COUT or Process::MERGE_COUT_CERR was specified then this member interfaces to the
child’s standard output stream. By extracting the information from childOutStream the parent
process retrieves the information sent by its child process to its standard output stream.
Alternatively, this information may directly be extracted from the Process object itself, but this
member does not require the use of a static_cast to disambiguate the intended stream buffer in
statements like
cout << process.childOutStream().rdbbuf()
o void close():
This member closes the child’s input stream. In situations where the child continuously reads
information from its standard input stream this member can be used to inform the child process
that input has terminated. This member should only be used when IOMode CIN was specified for the
currently running child process; otherwise its behavior is undefined. Alternatively, the eoi
member or manipulator may be used.
o int eoi():
This member closes the child’s input stream, and then calls waitForChild to wait for the child
process to end. In situations where the child continuously reads information from its standard
input stream this member can be used to inform the child process that input has terminated. This
member should only be used when IOMode CIN was specified for the currently running child process;
otherwise its behavior is undefined. Alternatively, the close member or eoi manipulator may be
used. The exit-status of the called child process is returned.
o int exitStatus() const:
After calling eoi or inserting the eoi manipulator into the Process object this member returns the
child process’s exit status. In other cases the value returned by exitStatus is not defined, and
waitForChild should be used.
o IOMode ioMode() const:
This member returns the default IOMode. If called while a child process is actually running, then
the value returned by this member may differ from the value that was actually used when starting
the child process, as the default value may be altered by a function call operator just before
starting the child process.
o ProcessType processType() const:
This member returns the default ProcessType of child proceses. If called while a child process is
actually running, then the value returned by this member may differ from the value that was
actually used when starting the child process, as the default value may be altered by a function
call operator just before starting the child process.
o size_t timeLimit() const:
This member returns the default time limit (in seconds) of child processes. A return value of zero
indicates that no time limit is enforced. If called while a child process is actually running,
then the value returned by this member may differ from the value that was actually used when
starting the child process, as the default value may be altered by a function call operator just
before starting the child process.
o void setBufSize(size_t bufSize):
This member changes the default stream buffer size that is used for communication with child
processes. A zero byte buffer size is silently changed into one. The new default value will be
used when starting the next child process.
o void setCommand(std::string const &cmd):
The setCommand member (re)defines the (initial part of a) child process command specification.
This member does not actually start the child process, and operator+= may be used to append
additional text to the command specification. Also, this member may be used when a child process
is currently active: its use does not affect a currently running child process.
o void setIOMode(iomode mode):
This member changes the default IOMode. The new default value will be used when starting the next
child process.
o void setProcessType(ProcessType type):
This member changes the default ProcessType. The new default value will be used when starting the
next child process.
o void setTimeLimit(size_t timeLimit):
This member changes the default execution time limit (in seconds). No time limit will be imposed
upon child processes if timeLimit 0 is specified. The new default value will be used when starting
the next child process.
o void start():
The currently specified command is started using the Process object’s process parameters.
Having specified a command to start, the first white-space delimited element of the specified
command is used as the name of the program to start. If the program should be called through
sh(1), the USE_SHELL ProcessType or a system member should be used.
If a child process does not terminate by itself, then it is terminated when it has run for its
alloted time; when the Process object’s start or stop members are called; when the object’s
assignment operator is used; or when the object goes out of scope.
Alternatively, the member waitForChild (see below) may have to be called to end a running process.
o void start(IOMode mode):
The currently specified command is started using the specified IOMode, but otherwise using the
currently configured Process object’s process parameters. The specified IOMode is only used for
the child process that is started by this member.
o void start(IOMode mode, ProcessType type):
The currently specified command is started using the specified IOMode and ProcessType, but
otherwise using the currently configured Process object’s process parameters. The specified
process parameter values are only used for the child process that is started by this member.
o void start(size_t mode, Program program, size_t timeLimit):
The currently specified command is started using the specified IOMode, ProcessType, and time limit
(silently converting the size_t mode to an IOMode value), and using the currently configured
Process object’s stream buffer size parameter. The specified process parameter values are only
used for the child process that is started by this member.
o void start(IOMode mode, Program program, size_t timeLimit, size_t bufferSize):
The currently specified command is started using the specified process parameters. The specified
parameter values are only used for the child process that is started by this member.
o void showMode(char const *lab) const:
This member displays the label lab, followed by the current process ID, followed by the child
process’s process ID, followed by a textual representation of the currently active IOMode.
o std::string const &str() const:
This member returns the content of the current child process command specification. It shows the
command as it will be (or has been) executed by start, system or the assignment operator.
o void system():
This member executes the currently stored command as a command to sh(1). When using system
redirections can be included in the command itself (this renders the redirected streams implied by
the current IOMode) useless. The currently set process parameters are used when sh(1) is executed.
o void system(IOMode mode):
This member executes the currently stored command as a command to sh(1) (cf. system above) using
the specified IOMode rather than the current default IOMode setting.
o void system(IOMode mode, size_t timeLimit):
This member executes the currently stored command as a command to sh(1) (cf. system above) using
the specified IOMode and time limit.
o void system(IOMode mode, size_t timeLimit, size_t bufSize):
This member executes the currently stored command as a command to sh(1) (cf. system above) using
the specified IOMode, time limit, and stream buffer size values.
o int stop():
This member terminates a currently active child process. The child process is twice sent a
SIG_TERM signal, followed by a SIG_KILL signal. This member returns the exit-value of the child
process that was stopped. Its operation and return value are undefined if called without a running
child process.
Following stop a new command may be called using start, system or the assignment operator (see
earlier). Those members first calls stop. When the intention is to start another child process,
then there’s no need to call stop explicitly. Also, stop is called when the Process object goes
out of scope.
o int waitForChild():
This member calls the identically named member from the class FBB::Fork, waiting for a child
process to end. It is called to prevent premature termination of a child process before calling
stop. It is not always necessary to call waitForChild. E.g., when a process writes to its standard
output stream and all output has been read then the child process can be stopped without calling
waitForChild.
MANIPULATOR
o FBB::eoi:
This manipulator may be inserted into a Process object for which IOMode CIN was specified. It
closes the child’s input stream, and then calls waitForChild to wait for the child process to end.
In situations where the child continuously reads information from its standard input stream this
member can be used to inform the child process that input has terminated. Alternatively, Process
object’s close or eoi members may be used.
EXAMPLES
The first example shows how a program only producing output can be called. Its child process simply is
/bin/ls:
int main()
{
Process process(Process::COUT, "/bin/ls -Fla");
process.start();
cout << process.childOutStream().rdbuf();
}
The next example shows how a child program can be given a limited amount of execution time: lines entered
at the keyboard are echoed to the standard output stream for at most 5 seconds:
int main()
{
Process process(Process::CIN | Process::COUT, "/bin/cat");
process.setTimeLimit(5);
process.start();
while (true)
{
cout << "? ";
string line;
if (not getline(cin, line))
return 0;
process << line << endl; // to /bin/cat
line.clear();
if (not getline(process, line)) // from /bin/cat
break;
cout << "Received: " << line << endl;
}
cout << "/bin/cat time limit of 5 seconds reached: child process ended\n";
}
The final example shows how multi threading can be used to access the child program’s standard output and
standard error streams through the Process object:
void collect(ostream *outStream, streambuf *rdbuf)
{
*outStream << rdbuf << flush;
}
int main()
{
string cmd(getcwd(0, 0));
cmd += "/cincoutcerr";
Process all(Process::ALL, cmd);
all.start();
thread outThread(collect, &cout, all.childOutStream().rdbuf());
thread errThread(collect, &cerr, all.childErrStream().rdbuf());
all << cin.rdbuf() << eoi;
outThread.join();
errThread.join();
}
Additional examples are found in the distribution’s bobcat/process/driver directory.
FILES
bobcat/process - defines the class interface
SEE ALSO
bobcat(7), execle(3), exec(3bobcat), coutextractor(3bobcat), cerrextractor(3bobcat), fork(3bobcat),
cininserter(3bobcat), proc(3bobcat), sh(1), stdextractor(3bobcat).
BUGS
None reported
BOBCAT PROJECT FILES
o https://fbb-git.gitlab.io/bobcat/: gitlab project page;
o bobcat_6.04.00-x.dsc: detached signature;
o bobcat_6.04.00-x.tar.gz: source archive;
o bobcat_6.04.00-x_i386.changes: change log;
o libbobcat1_6.04.00-x_*.deb: debian package containing the libraries;
o libbobcat1-dev_6.04.00-x_*.deb: debian package containing the libraries, headers and manual pages;
BOBCAT
Bobcat is an acronym of `Brokken’s Own Base Classes And Templates’.
COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL).
AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl).
libbobcat-dev_6.04.00 2005-2023 FBB::Process(3bobcat)