#include "uexec.h" USING_PTYPES void example1() { // the simplest usage: pass a full path to the program // to execute and call run(). input and output are connected // to stdin and stdout by default. uexec sh; sh.program = "/bin/ls"; sh.run(); } void example2() { // run grep on a file "uexec_test.cxx" with the given regular // expression and record the result into a memory stream. // this method is useful when the child produces small // amount of data. uexec sh; compref result; sh.program = "/usr/bin/grep"; aadd(sh.params, "-i"); // case insensitive search aadd(sh.params, "ABC"); // regular expression sh.uin = new infile("uexec_test.cxx"); sh.run(); pout.put(result->get_strdata()); } void example3() { // run directory listing "ls -la /" and intercept // its output using a local pipe. this method works // even if the child produces large amount of data, // since you can infinitely read incoming data in a // loop. uexec sh; sh.program = "/bin/ls"; aadd(sh.params, "-la"); aadd(sh.params, "/"); // create a pair of stream objects and open them // as a local pipe, then assign one end to the // uexec unit compref pipeout = new outfile(); compref pipein = new infile(); pipein->pipe(*pipeout); sh.uout = pipeout; // now run the unit asynchronously and read the other // end of the pipe sh.run(true); while (!pipein->get_eof()) pout.putf("child says: %s\n", pconst(pipein->line())); // wait for completion sh.waitfor(); } int main() { pout.putline("--- EXAMPLE 1"); example1(); pout.putline("--- EXAMPLE 2"); example2(); pout.putline("--- EXAMPLE 3"); example3(); return 0; }