#include "token.h"

int tokenise (char *inputLine, char *argv[])
{
	int numTokens=0;
	int i;
	char *lastfound=inputLine;
	// printf("%s\n", inputLine);
	argv[0] = NULL;
	int length = strlen(inputLine);
	
	// loop through every cahracter
	for (i = 0; i < (length + 1) && numTokens != -1; i++)
	{
		// if we find a character to split on
		if (inputLine[i] == ' ' || inputLine[i] == ';' || inputLine[i] == '\t' || inputLine[i] == '\n' || inputLine[i] == '\0')
		{
			numTokens++;
			// if we reach the limit, drop out of the loop
			if (numTokens == MAX_NUM_TOKENS)
			{
				numTokens = -1;
			}
			else
			{
				// set the current value to null
				inputLine[i] = '\0';
				argv[numTokens - 1] = lastfound;
				lastfound = &inputLine[i + 1];
			}
		}
	}
	
	// initialise the rest of the array
	for (i = numTokens; i < MAX_NUM_TOKENS; i++)
		argv[i] = NULL;
	
	return numTokens;
}

void ResetArgs(char *args[])
{
	int i;
	for (i = 0; i < MAX_NUM_TOKENS; i++)
		args[i] = NULL;
}
