/* _main.c - corrected Lattice Main program */
#include "stdio.h"
#include "ctype.h"
#include "ios1.h"

#define CLIBRARY "c.library"	/* comment out if not using C library */
#ifdef CLIBRARY
extern long *ClibBase, OpenLibrary();
#endif

#define MAXARG 32              /* maximum command line arguments */

#ifndef TINY
extern int _stack,_fmode,_iomode;
extern struct UFB _ufbs[];
#endif
extern int LoadAddress;

int argc;         /* arg count */
char *argv[MAXARG];      /* arg pointers */
static char *line, argument_text[129];


/**
*
* name         _main - process command line, open files, and call "main"
*
* synopsis     _main(argline);
*              char *argline;   ptr to command line that caused execution
*
* description   This function performs the standard pre-processing for
*      the main module of a C program.  It accepts a command
*      line of the form
*
*         pgmname arg1 arg2 ...
*
*      and builds a list of pointers to each argument.  The first
*      pointer is to the program name.  For some environments, the
*      standard I/O files are also opened, using file names that
*      were set up by the OS interface module XCMAIN.
*
*  02/86, TIM Holloway [73026,2026] - added support for quoted arguments
*
*  06/86, TIH Holloway, Added support for C libary, including _CCleanup
*	and _gripe modules
*
* simple version: does not check for unmatched quotes, etc.
**/

/* handle quoted arguments */

static void
quote_it(delimiter)
register char delimiter;
{
   argv[argc++] = ++line;  /* point to string, but skip delimiter */
   while ((*line != delimiter) && (*line != '\0')) line++;
}

_main(argline)
char *argline;
{
   register char c;
   register int x;

/*
*
* Build argument pointer list
*
*/
   line = argument_text;
   while (*argline) *line++ = *argline++;       /* save for a rainy day */
   line = argument_text;
   for(argc = 0; argc < MAXARG; )
   {
      while(isspace(*line)) line++; /* skip spaces */
      if(*line == '\0') break;      /* end of line */
      if ((*line == '"') || (*line == '\'')) quote_it(*line);
      else
      {
         argv[argc++] = line;
         while((*line != '\0') && (isspace(*line) == 0)) line++;
      }
      c = *line;
      *line++ = '\0';
      if(c == '\0') break;
   }
/*
** open the C library
*/

#ifdef CLIBRARY
	ClibBase = OpenLibrary (CLIBRARY, 0);
	if (ClibBase == NULL)
	{
		_NoClibrary ("c.library could not be opened");
		_exit(20);
	}
#endif

/*
*
* Open standard files
*
*/
#ifndef TINY

stdin->_file = 0;
stdin->_flag = _IOREAD;
stdout->_file = 1;
stdout->_flag = _IOWRT;
stderr->_file = 2;
stderr->_flag = _IOWRT;
#endif

x = ((_fmode ^ _iomode) & 0x8000) ? UFB_NT : 0;
_ufbs[0].ufbfh = Input();
_ufbs[0].ufbflg = UFB_OP | UFB_RA | UFB_NC | x;
_ufbs[1].ufbfh = Output();
_ufbs[1].ufbflg = UFB_OP | UFB_WA | UFB_NC | x;
_ufbs[2].ufbfh = Output();
_ufbs[2].ufbflg = UFB_OP | UFB_WA | UFB_NC | x;



/*
*
* Call user's main program
*
*/
#ifdef DEBUG
printf("load address = %lx\n",LoadAddress);
	Debug(0);
#endif

	main(argc,argv);              /* call main function */
#ifndef TINY
	exit(0);
#else
	_exit(0);
#endif
}

/*
** _CCleanup - called by XCEXIT in cl.asm.  Primary purpose is to close
** the C library, but you can add other stuff as required.
*/

_CCleanup()
{
#ifdef CLIBRARY
	if (ClibBase != NULL) CloseLibrary (ClibBase);
#endif
}
