FBB::Proc(3bobcat)

Running Child Processes
(libbobcat-dev_6.03.01)

2005-2023

NAME

FBB::Proc - Runs external programs

SYNOPSIS

#include <bobcat/proc>
Linking option: -lbobcat

DESCRIPTION

The FBB::Proc offers an alternative to the FBB::Process class. FBB::Proc 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 communicate with the standard input, output and error streams of child processes.

Objects of the class Proc 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 Proc parent objects. Input expected by child processes can be inserted into Proc objects. standard output and standard error generated by child processes are inserted into and further handled by std::ostream objects, by default std::cout and std::cerr.

When using (output) redirection when executing child processes using 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 (cf. section ENUMERATIONS for a description of the IOMode enumeration).

Proc objects may repeatedly be used to execute the same or different child processes. Before the next child process is started, the Proc object ensures that its currently active child process ends. Alternatively, a currently active child process ends if the Proc object goes out of scope, if the allotted execution time of the child process has passed, if a new process is started using Proc's assignment operator, or if its stop member is called. Otherwise child processes continue until completion when calling finish or, if the child process reads its standard input stream, when the eoi manipulator is inserted into the Proc object.

Programs called as child processes may be specified when constructing a Proc object or by using Proc's setCommand member. Proc constructors (or Proc's setCommand-member) don't start child processes. To start child processes the start members or the assignment operator must be used.

Child processes may receive information at their standard input streams through information inserted into Proc objects. In those cases Proc objects must inform their child processes that they have received all input. For this the eoi manipulator can be inserted into Proc objects, or Proc's finish member can be called.

If information sent to the child is not fully be processedd by the child process then the operating system issues a Broken pipe message, indicating that information in a pipe was lost, ending the program. The Proc class's member pipeSignal can be used to suppress the Broken pipe message.

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 also be surrounded by backtics (`-characters). These backtick characters are removed by the Proc object when the command starts.

Child processes may be allotted a limited amount of time (in seconds) to complete. By default no time limit is imposed upon child processes. If a time limit is specified then the child process, if still running after the specified number of seconds, is ended by sending it a SIGTERM signal, followed by a SIGKILL signal (cf. signal(7)).

By default the standard input, output and error streams of child processes are handled by their Proc parent processes: information inserted into the Proc object is forwarded to the child process's standard input stream, information sent by the child process to its standard output and error streams are either forwarded to the corresponding streams of the parent process, or they can be processed by streams, configured by the Proc object.

Multiple Proc processes can be combined using the pipe operator (i.e., the | operator). When used, the standard output stream of the left-hand side (lhs) Proc object is used as the standard input stream of the right-hand side (rhs) Proc object. Since the Proc objects start their own child processes, this effectively boils down to the output of the lhs's child process being used as the input of the rhs's child process. The leftmost Proc object may also read its input (using the pipe operator) from an istream object or from a file whose name (path) is specified as the most lhs argument of a pipe expression. Likewise, the rightmost Proc object may pipe its standard output to an existing ostream object or to a file whose name (path) is specified as the most rhs argument of a pipe expression.

Proc objects use Pipe objects (cf. pipe(3bobcat)) for communication with their child processes. To ensure that these pipes are properly closed the members waitForChild, stop or the eoi manipulator should be used. Once a Proc 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),
std::ostream

ENUMERATIONS

The following enumerations are defined by the Proc class:

enum ProcType:

The enum ProcType defines how a child proc is started or located. Its values are specified at constructor-time or through the setProcType member. This enumeration defines the following symbolic constants:

enum IOMode:

Values of the enum IOMode are used to define which of the child proc's standard streams can be accessed through the Proc object. Its symbolic constants may be combined using the bit_or operator. By default CIN | COUT | CERR is used (see below).

IOMode arguments may be combined using bit-or operators.

The following symbolic constants are available:

CONSTRUCTOR

Note that child processes are not started automatically following the construction of t(Proc) objects. A start member or the assignment operator (see below) is used to start child processes.

Once a Proc object has been constructed its parameters can be changed using set-member functions or start members.

Copy and move constructors (and assignment operators) are not available.

OVERLOADED OPERATORS

The pipe (|) operator mimics the pipe-operator supported by most command-shell programs and should not be confused with the bit-or operator. The pipe operator allows constructions like


    p1 | p2 | p3            // piping 3 Proc objects
    cin | p1 | p2 | cout    // p1 reads cin, p2 writes cout
    inName | p1 | outName   // inName: file name of the file
                            // read by p1, outName: file name
                            // of the file written by p1
        
When using the pipe operator Proc objects reading input automatically specify their CIN modes, while Proc objects writing their standard output automatically specify their COUT modes. Following the pipe-expression the IOMode specifications which were specified before the pipe-expression are restored.

MEMBERS

EXAMPLES

All examples should start with:


    #include <iostream>
    #include <bobcat/proc>
    
    using namespace std;
    using namespace FBB;
        

The first example illustrates how a program only producing output can be called. Its child proc simply is /bin/ls:

int main()
{
    Proc proc("/bin/ls -Fla", Proc::COUT);

    proc.start();
}

The next example illustrates a child program that's 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()
{
    Proc proc("/bin/cat", Proc::CIN | Proc::COUT);
    proc.setTimeLimit(5);

    proc.start();

    while (true)
    {
        cout << "? ";
        string line;
        if (not getline(cin, line))
            return 0;

        proc << line << endl;           // to /bin/cat

        if (not proc.good())
        {
            cout << "child time limit exceeded\n";
            break;
        }
    }

    cout << "/bin/cat time limit of 5 seconds reached: child proc ended\n";
}

Piping is illustrated next: information at the program's standard input is piped to a second Proc object, writing its standard output to the program's standard output stream:

int main()
{
    Proc proc1{ "/bin/cat" };
    Proc proc2{ "/bin/cat" };

    cin | proc1 | proc2;            // By default piping to cout. To do that
                                    // explicitly use '... | proc2 | cout'
}

FILES

bobcat/proc - defines the class interface

SEE ALSO

bobcat(7), execle(3), fork(3bobcat), process(3bobcat), ostream(3fork), sh(1)

BUGS

BOBCAT PROJECT FILES

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).