#include <stdio.h>

struct momba {
union {
	 void   (*doit)(void);
   	 void 	(*sayhellow)(int ,char *);
    	 void 	(*sobye)(char *);
 	 char   **(*split)(char *);
  
 } life;
  int flag;
  char *msg;
  char **argv;
  int argc;

};

#define hello life.sayhellow
#define byebye life.sobye
#define split  life.split

#define clean(x) memset(x,0,sizeof x)
char ** s_splitcmd(char *string);

char ** s_splitcmd(char *string)
{
char **dst;
int len,x,y;
char tmp[518];




	if ( !(dst = malloc(sizeof(char **))))
		return NULL;


	len = strlen(string);
	x = y = 0;
	clean(tmp);

	do
	{

	  if ( *string  == 0x20 )
	  {
		 
		  tmp [x] = '\0';
	
		  dst[y] = strdup(tmp);
		  
		  clean(tmp);

		  x=0;
		  string ++;
		  y++;
		  
	  }


		tmp[x] = *string;
	 
	   x++;
	   string ++;

	} while ( ((*string != 0) && ( x < len  ) ));
	
	if ( strlen (tmp) > 0 ) 
	dst[y++]  = strdup ( tmp );
	dst[y++] = NULL;
	

	return dst;
}



void yo(int flag, char *me)
{
printf("Say hello\n");
printf("flag = %d\nme=%s\n",flag,me);

}

void ok (char *msg)
{
printf("So bye\n");
printf("msg = %s\n",msg);
}

int action ( struct momba *mon )
{
void (*funct)();
char **(*ff)();

funct = mon->life.doit;
ff = mon->split;

if ( mon ->flag == 2)
   {
   ((char **)mon ->argv) = ff(mon ->msg);
   return 0;
}

funct (mon ->flag, mon ->msg);


}


int main(void)
{
struct momba mom;



mom .flag = 1;
mom .msg = strdup("Starting up the proccess");
mom.hello  = yo;


/* start the process */
action(&mom);


mom.msg = strdup("Bringing it");


/* Splitting the arguments */
mom .flag = 2;
mom .split = s_splitcmd;
action(&mom);
printf("argv[0] = %s\nargv[1]= %s\n",mom.argv[0],mom.argv[1]);

/* Take it down */
mom .msg = strdup("My job here is done, Terminating..\n");
mom .byebye = ok;
action(&mom);

}


