I've built a simple Linux shell in c++. It can take in standard bash commands, redirect to a file, etc. But, what I want is for it to be able to take in input, for example, if I have a commands.txt file that contain
ls -l /
some_invalid_command
echo this displays stuff
and I pipe it into my a.out like so
$./a.out < commands.txt
I want it to run the commands inside as if I had typed them in. However, when I do that, I run into an infinite loop where the '$' prompt displays over and over until I hit ctrl+c. I'm pretty sure the cause is that I'm not checking for an end of file anywhere in my code.
So my question is, how would I go about checking for the end of file in this situation? I think the character is '^d' but I'm not sure how I'd check for that. Would something like this work?
if (inputString == "\^d") {
exit = true;
}
I'm only concerned that perhaps I want to input more information into the shell after the file has ran. setting exit to true would tell the shell to turn off.
Edit: Requested code...
void Shell::getInput() {
emptyQueue();
cout << "$ ";
getline(cin, inputString);
fillQueue();
};
void Shell::fillQueue() {
if (inputString.empty()) {
return;
};
int i = inputString.find_first_not_of(" ");
int j = inputString.find_last_not_of(" \r\n");
int k;
string stringToAdd;
while (i != -1) {
k = i;
while (inputString.at(k) != ' ') {
if (k == j) {
break;
};
k++;
};
if (k != j) {
k--;
};
stringToAdd = inputString.substr(i, k - i + 1);
i = inputString.find_first_not_of(" ", k + 1);
commandQueue.push_back(stringToAdd);
};
i = 0;
while (i < commandQueue.size()) {
pointers[i] = commandQueue[i].c_str();
i++;
};
};