#include <stdio.h>
#include <string.h>

#include "debug.h"

#define module_name "parse"

/*   Need to fix to work from the cmdline.c to handle the command-line
 * in a more systematic way.
 */

char *parse(char* buffer, int *args, char* argv[])
{
  int i,j;
  char *point;
     
  i=j=0;

  buf = strdup(buffer);

  while(buf[i] != '\0' && buf[i] != '\n')
  {
    while(buf[i] == ' ' || buf[i] == '\t')
      i++;

    if(buf[i] == '\"')
    {
      i++;
      if(buf[i] == '\0' || buf[i] == '\n')
        ERROR("Unmatched \"");
      else
      {
        argv[j++] = buf+i;

        while(buf[i] != '\"' && buf[i] != '\0' && buf[i] != '\n')
        {
          i++;
        }
      }
    }
    else
    {
	DEBUG("argument recognized");

      argv[j++] = buf+i;

      while(buf[i] != ' ' && buf[i] != '\0' && buf[i] != '\n')
        i++;

      if(buf[i] != '\0')
        buf[i++] = '\0';

      DEBUG(argv[j-1]);
    }
  }

  argv[j] = NULL;
  *args = j;
  return buf;
}

