/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by Mike Parker.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

#include <stdio.h>
#include <signal.h>

#ifndef VXWORKS_CFG
#include <sys/types.h>
#include <sys/time.h>
#else
#include <taskLib.h>
#include <ioLib.h>
#define main test_
#endif

#include "ctree.h"

char *tree;
char input[256];

handler()
{
	printf ("\nhi there\n");
}

#ifdef VXWORKS_CFG
int parentTaskId;
killtask()
{
	taskDelay(100);
	printf ("\nsignaling\n");
	kill (parentTaskId,SIGHUP);
}
#endif

char **argvec;

main(argc, argv)
int argc;
char **argv;
{
	int noBlocking = 0;
	int status;
	int tfd = 0;

	argvec = argv;
	argc--; argv++;
	while (argc > 0)
	 { if (strcmp (*argv, "-nb") == 0)
	    { noBlocking = 1;
	    }
	   else
	    { goto usage;
	    }
	   argc--; argv++;
	   continue;
usage:
	   fprintf(stderr,"Usage: %s [-nb]\n", argvec[0]);
	   exit(-1);
	 }

	tree = tree_match_parse("\
	(quit								\
	  debug (on off)						\
	  forget (memoryAlloc						\
		  memoryFoobar						\
	 	 )							\
	importMemoryAlloc						\
	importMemory							\
)",0);
	if (! tree)
	 { fprintf(stderr,"Parse failed: %.30s\n",tree_match_err_at());
	   exit(1);
	 }
	input[0] = '\0';
#if 0 /* VXWORKS_CFG */
	parentTaskId = taskIdSelf();
	signal(SIGHUP, handler);
	taskSpawn ("", 1, (VX_SUPERVISOR_MODE|VX_DEALLOC_STACK|VX_STDIO),
		       0x1000, killtask);
#endif
	if (noBlocking)
	 { tree_match_setup();
	   while (strcmp(input,"quit"))
	    { 
	      status = tree_match_nb (tree,">> ",input,&tfd);
	      if (status == 1)
	       { 
		 printf("%s\n",input);
		 if (!strcmp(input,"debug on"))
		  { *tree_match_flags() |= CTREE_F_DEBUG;
		  }
		 else if (!strcmp(input,"debug off"))
		  { *tree_match_flags() &= ~CTREE_F_DEBUG;
		  }
		 else if (!strcmp(input,"importMemory"))
		  { FILE *inf;
		    int i;
		    inf = tree_match_input_stream();
		    if (fscanf(inf,"%d",&i) != 1)
		     { printf("fscanf error");
		     }
		    else
		     { printf("number is %d\n",i);
		     }
		  }
		 continue;
	       }
#ifdef VXWORKS_CFG
	       { int n = 0;
		 do
		  { ioctl(tfd, FIONREAD, &n);
		  }
		 while (n <= 0);
	       }
#else
	       { fd_set rset;
		 FD_ZERO(&rset);
		 FD_SET(tfd,&rset);
		 if (select (FD_SETSIZE, &rset, (fd_set*)0, (fd_set*)0,
						 (struct timeval*)0) <= 0)
		  { fprintf(stderr, "error -- select returned with nothing\n");
		    tree_match_reset();
		    exit (-1);
		  }
	       }
#endif
	    }
	   tree_match_reset();
	 }
	else
	 { while (strcmp(input,"quit"))
	    { tree_match(tree,">> ",input);
	      printf("%s\n",input);
	      if (!strcmp(input,"debug on"))
	       { *tree_match_flags() |= CTREE_F_DEBUG;
	       }
	      else if (!strcmp(input,"debug off"))
	       { *tree_match_flags() &= ~CTREE_F_DEBUG;
	       }
	      else if (!strcmp(input,"importMemory"))
	       { FILE *inf;
		 int i;
		 inf = tree_match_input_stream();
		 if (fscanf(inf,"%d",&i) != 1)
		  { printf("fscanf error");
		  }
		 else
		  { printf("number is %d\n",i);
		  }
	       }
	    }
	 }
}
