#include	"../H/ugens.h"
#include        <ctype.h>
#include        <stdio.h>
#include 	<math.h>
#include        "defs.h"     /* parser */
extern        mixerr;
extern  int   NBYTES;
extern  int   sfd;
extern  double dispatch();

/* delete this dummy function when actual performing cmix computation */


/* parse: points to tree to be executed once yacc gets done */
Tree     program;            

                 /*  ug_list MUST be initialized with something...	*/
struct	ug_item	ug_head =
	{ 0, 0, "" };
struct	ug_item *ug_list = &ug_head;

#define CMAX 16     /* allow up to 16 command line args to be passed to subs */
int aargc;
char *aargv[CMAX];  /* this are declared in ugens.h */
char name[NAMESIZE];/* to store file name found by card scanner, accessed by
                       m_open in sound.c */
int interactive;     /* for m to set interactive mode, set from command line
			arg later on */

main(argc,argv)
char *argv[];
{
	char	buf[256];
	float	p[256];
	short	n_args,j;
	char    *malloc(),*bufp,*cp;

	setlinebuf(stderr); /* just to make fprintf faster.  Note that
				this may not be portable to non BSD 
				systems */
	/* copy command-line args, if any*/

	aargc = (argc > CMAX) ? CMAX : argc;
	for(j=0; j<aargc; j++) aargv[j]=argv[j];

	/* Now, command-line args will be available to any subroutine
	   in aargc; and char *aargv[], (both declared extern in ugens.h)
	   parsing is exactly the same then as standard C routine.
	   Maximum of CMAX arguments allowed. */

	interactive = 1; /* default is interactive mode */
	SR = 0; /* sampling rate set by sf header or commandline */
	fprintf(stderr," ---------> Cmix <----------\n");
	/*
	 * "Introduce" functions and stuff to program
	 */
	ug_intro();	/* introduce standard routines */
	profile();	/* introduce user-written routines etc. */

	while((*++argv)[0] == '-') {
		argc -= 2; /* Take away two args */
		for(cp = argv[0]+1; *cp; cp++) {
			switch(*cp) { /* Grap options */
			case 'b': 
				interactive = 0; /* set up batch mode */
				fprintf(stderr,"Using batch mode\n");
				break;
			case 'r':
				SR = atof(*++argv);
				fprintf(stderr,"Sampling rate set to %f\n",SR);
				break;
			default:  
				printf("Don't know about option: %c\n",*cp);
				exit(-1);
			}
		}
	}


	setbuf(stdout, NULL);	/*  Want to see stdout errors	*/

	/*
	 *    The program is read from stdin, but this can be changed
	 */

	yyparse();    /* create intermediate representation */
	
	if(!interactive) exct(program);
	closesf();
	fprintf(stderr,"--------->Goodbye<---------");
}


/*
*   This is the function called be the M-language parser.
*   It in turn calls the standard cmix dispatch function.
*/





double parse_dispatch(str,args,n)
char *str;
int n;
double args[];
{
	int i1;
	printf ("============================\n");
	printf ("%s:  ",str);
	for (i1=0;i1<n;i1++) {
		printf ("%g ",args[i1]);
	}
	printf ("\n");
	return (dispatch(str,args,n));
};

yywrap () {return 1;}
