1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#ifdef BOBCAT
#include <bobcat/process>
#else
#include "process"
#endif
#include <string>
#include <iostream>
#include <climits>
using namespace std;
using namespace FBB;
void prompt(char const *task)
{
cout << "Press Enter to start " << task << endl;
cin.ignore(INT_MAX, '\n');
}
int main(int argc, char **argv)
try
{
cout << "Size of Process: " << sizeof(Process) << endl;
string line;
// Nota bene: without IOMode you get CIN, COUT and CERR
Process p1(Process::CIN, "/bin/cat");
Process p2(Process::STD, "/bin/cat");
Process p3(Process::STD, "/bin/cat");
prompt("sending lines (until empty) to cat | cat | cat");
(p1 | p2 | p3).start();
while (getline(cin, line) && not line.empty())
{
cout << "Entering " << line << endl;
p3 << line << endl;
}
p3.close();
p3.waitForChild();
Process process(Process::CIN | Process::COUT,
"/usr/bin/sha1sum");
prompt("sha1sum");
process.start();
process << "Hello world\n"; // input to sha1sum
process.close();
process >> line; // retrieve the value
cout << line << endl;
process.stop();
prompt("cat, ignoring its output");
process.setCommand("/bin/cat");
process.setIOMode(Process::CIN | Process::IGNORE_COUT);
process.start();
process << "Hello world\n"; // input to sha1sum
process.close();
line.clear();
if (process >> line) // retrieve the value
cout << ">>>" << line << "<<<" << endl;
process.stop();
// if (argc > 1) // sending an e-mail
// {
// cout << "Sending mail to " << argv[1] << endl;
// prompt("/usr/bin/mail");
// process.setCommand("/usr/bin/mail -s 'from Process' ");
// process += argv[1];
// process.start(Process::CIN);
// process << "This mail was sent by the process drive\n";
// process << "It consists of multiple lines of text\n";
// process.close();
// process.waitForChild();
// }
prompt("5 seconds IO to /bin/cat");
process.setIOMode(Process::CIN | Process::COUT);
process.setTimeLimit(5); // change time limit
process = "/bin/cat";
while (process.active())
{
cout << "? ";
getline(cin, line);
process << line << endl; // to /bin/cat
line.clear();
if (!getline(process, line)) // from /bin/cat
break;
cout << "Received: " << line << endl;
}
cout << "/bin/cat forcefully terminated\n";
process.setTimeLimit(0);
cout << "3 times running /bin/ls\n";
for (size_t trial = 0; trial < 3; ++trial)
{
prompt("ls");
process(Process::COUT) = "/bin/ls";
cerr << process.str() << endl;
size_t count = 0;
while (getline(process, line))
cout << ++count << ": " << line << endl;
}
}
catch (Errno const &err)
{
cerr << "EXCEPTION CAUGHT: " << err.why() << endl;
return 1;
}
catch (bool)
{
return 0;
}
catch (...)
{
cerr << "Unrecognized exception in main()\n";
return 0;
}
|