#include "parse.h"

char **split(char *line) {
   char **newargs = (char **)calloc(num_elem(line)+1,sizeof(char *)); 
   int itemIndex = 0; 
   int i = 0; 
   int len = strlen(line);
   if (line[len-1]=='\n') line[len-1]='\0';

//   printf("num of elem is %d \n",num_elem(line));
   while (*line != '\0') { 
      while (*line == ' ') 
         line++; 
      if (*line == '\0') break; 
      newargs[itemIndex] = line; 

      while ((*line != ' ') && (*line != '\0')) 
         line++; 
      itemIndex++; 
      if (*line == '\0') break; 
      *line = '\0'; 
      line++; 
   }
   newargs[itemIndex] = NULL; 

/*   printf("before exiting frop split : \n"); 
   for (i=0; i<itemIndex; i++) 
      printf("par %d is %s \n",i,newargs[i]); */ 
   return newargs;
}

int num_elem(char *line) {
   int itemIndex = 0; 
   while (*line != '\0') { 
      while (*line == ' ') 
         line++; 
      if (*line == '\0') break; 

      while ((*line != ' ') && (*line != '\0')) 
         line++; 
      itemIndex++; 
      if (*line == '\0') break; 
      line++; 
   }
   return itemIndex;
}


