#include "iolib.h"
#include "ccfunc.h"
#include <libraries/dosextens.h>
#include <workbench/startup.h>

long _savsp, _stkbase;

extern short _math;	 /* this is in "math.c" */
extern long blocksize;	 /* this is in blocksize.c */

struct WBStartup *WBenchMsg;
void *_oldtrap, **_trapaddr;
void (*exit_fcn)();
void *MathIeeeDoubBasBase, *CCLibBase, *SysBase, *DOSBase, *MathBase;
FILE *stdin, *stdout, *stderr;
long errno;
char *type;

_main(alen, aptr)
long alen;
char *aptr;
{
extern void _exit();
struct Task *_FindTask();
struct Library *_OpenLibrary();
struct Message *_GetMsg();
void _WaitPort();
register task_UserData *ud;
register struct Process *ThisProcess;

/* This stuff is needed for the Aztec C compiler */
_stkbase = _savsp - *((long *)_savsp+1) + 8;
*(long *)_stkbase = 0x4d414e58L;

ThisProcess = (struct Process *)_FindTask(0L);

/* Get the Workbench Message if this program is being executed
 * from the workbench. */
if( !ThisProcess->pr_CLI )
   {
   _WaitPort(&ThisProcess->pr_MsgPort);
   WBenchMsg = (struct WBStartup *)_GetMsg(&ThisProcess->pr_MsgPort);
   }

/* Open the C library */
if( !(CCLibBase = _OpenLibrary(CCLIBNAME,0L)) )
   goto abort;

if( _math )  /* open up the Ieee math library */
   if( !(MathIeeeDoubBasBase = _OpenLibrary("mathieeedoubbas.library",0L)) )
      goto abort;

/* Do some further initialization of the task specific structures.
 * This will:
 *
 * 1) Initialize the task_UserData structure.
 * 2) Parse the command line arguments or the Workbench message.
 * 3) Open standard stream IO for the application.
 * 4) Set up pointers to errno, and blocksize.
 * 5) Give the library a pointer to the Ieee math library if opened above.
 * 6) Set the standard abort function.
 */
if( !SetupSTDIO(&stdin,&stdout,&stderr,
    &errno,&blocksize,&type,MathIeeeDoubBasBase,alen,aptr,WBenchMsg,_exit) )
   goto abort;

/* get a pointer to this task's user data structure */
ud = GetSTDIO();

/* application entry point */
main(ud->_argc, ud->_argv);

abort:
_exit(0L);
}

void _CloseLibrary();

void _exit(code)
long code;
{
long rv = code;
if( exit_fcn )
   (*exit_fcn)();
if( _trapaddr ) /* clean up signal handling */
   *_trapaddr = _oldtrap;

if( MathIeeeDoubBasBase )
   _CloseLibrary( MathIeeeDoubBasBase );

if( CCLibBase )
   {
   ClearSTDIO();
   _CloseLibrary(CCLibBase);
   }

/* This stuff is needed for the Aztec C compiler */
   {
#asm
   move.l  -4(a5),d0
   move.l  __savsp#,sp
   rts
#endasm
   }
}

void exit(code)
long code;
{
void _exit();
_exit(code);
}






