/*
* This program shows what the LineBreaker object can do. It reads a line,
* breaks it into words, prints the contents, then tries to run it as a
* command using exec.
*/
// C++ language includes.
#include <iostream>
#include <string>
// C language include for the strerror function and errno variable. These
// are part of the C standard, though they're pretty deeply tied to whatever
// OS is underneath.
#include <string.h>
#include <errno.h>
// Unix standard include for execv
#include <unistd.h>
#include "linebreaker.h"
using std::string;
int main()
{
// Enter a line.
std::cout << "Enter line> ";
string line;
std::getline(std::cin, line);
// Create the object and see what words we have.
LineBreaker lb(line);
if(lb.size() == 0) {
std::cout << "Empty line." << std::endl;
} else {
// Print the list.
std::cout << lb.size() << " items: " << std::endl;
for(int i = 0; i < lb.size(); ++i) {
std::cout << " " << lb[i] << std::endl;
}
std::cout << "Attempting exec()" << std::endl;
// Now, we'll try to run it as a command. If the exec
// succeeds, it will replace this program with the one
// you specified.
execvp(lb[0],lb);
// If you see anything here, the command did not run.
std::cout << "Exec failed. " << strerror(errno) << std::endl;
}
}