Example #1
0
int Parser::execute()
{
	// If I use my imagination hard enough
	// this will magically work
	// every odd number is a connector (besides the last one);
	// 0 and even numbers are the commands
	int status = 0;	
	unsigned int i = 0;
	Executer E;

	// if user puts "ls test ||" or "echo test &&", etc
	// then prompt the user ">" and another command
	if ((cmd.size() != 1) && ((cmd.back() == "||") || (cmd.back() == "&&")))
	{
		string cmd;
		cout << ">";
		getline(cin, cmd);
		setCommand(cmd);
		parse();
	}

	//for(unsigned int i = 0; i < cmd.size(); i++)
	//	cout << i << ": " << cmd.at(i) << endl;

	//for(unsigned int i = 0; i < test.size(); i++)
	//	cout << test[i] << endl;

	//for(unsigned int i = 0; i < cmd.size(); i++)
	//	cout << cmd[i] << endl;

	// this loop analyzes the connectors
	// and determines what gets runned
	while(i < cmd.size())
	{
		E.setCount(i);						
		this->bPtr = &E;		

		status = bPtr->execute();			
		if(status == -1)		// if status is -1 then "exit was inputted"
		{						// so we want to exit rshell program
			return status;
		}
		// should on on connector now
		// b/c connectors are on odd numbers
		i++;	
		
		if(i >= cmd.size())
		{
			return status;
		}

		if(cmd.at(i) == ";")
		{
			//cout << "Before i++" << endl;
			i++;
			//cout << "After i++" << endl;
		}
		// if next one is && move to the next one if 
		// previous worked
		else if(cmd.at(i) == "&&")
		{
			// if previous worked
			if(status == 0)
			{
				i++;	 // go to the next command
			}
			else
			{
				i+=3;   // moves on to next connector		
			}
		}
		else if(cmd.at(i) == "||")
		{
			// if previous failed
			if(status == 149)
			{
				i++;	// go to the next command
			}
			else
			{
				i+=3;
			}
		}

		//cout << "i after everything: " << i << endl;
		if(isOdd(i) || i > cmd.size())
		{
			//cout << "goes here" << endl;
			return status;
		}
	}	
	return status;
}