#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>

char *param[21];

/* Splits the input given by user into words in array param[] */
void createparam(char *input){
	int i;
	/* Clear the array every time we get a new input */
	for(i = 0; i < 21; i++){
		param[i] = '\0';
	}
	char *line = input;
	char del[] = " ";
	char *token;
	int spaces = 0;
	//printf("2\n");
	/* Count spaces in line */
	for(i = 0; line[i] != '\0'; i++){
		if(line[i] == ' '){
			spaces++;
		}
	}
	//printf("3\n");
	/* Find first word in line and place in array if present */
	token = strtok(line, del);
	if (token != NULL) {
		param[0] = malloc(30);
		strcpy(param[0], token);
	}
	
	i = 0;
	/* Runs once for each word, using spaces as reference
	Tried to simply use while(token != NULL), but wouldnt work */
	while(i < spaces){
		i++;
		token = strtok(NULL, del);
		param[i] = malloc(30);
		strcpy(param[i], token);
	}
	param[spaces+1] = "0";
	
	/* If user didnt leave any input param[0] would be NULL,
	and the program would crash when while() in main() runs next time */
	if (param[0] == NULL){
		param[0] = " ";
	}
	
	/* Test method to output parameter array 
	int j;
	for(j = 0; j < 5; j++){
		printf("%s\n", param[j]);
	}*/
}

int main(void){
	int cmdcount = 0;
	char input[120];
	param[0] = " ";
	
	while((strcmp(param[0],"exit")) != 0 || (strcmp(param[0],"quit")) != 0) {
		//printf("%s\n", param[0]);
		printf("ifish %d >", cmdcount);
		gets(input);
		createparam(input);
		cmdcount++;
		int j;
		for(j = 0; j < 5; j++){
			printf("%s\n", param[j]);
		}
	}
}
