;/* Execute build for ArpC.c  (c.o replacement)
lc -j73 -cst -v -ms -O ArpC
quit
/*********************************************************************\
**                               ________________________________    **
**    A n t h o n y             |________    __    __    ________|   **
**                                       |  |o_|  |o_|  |            **
**            T h y s s e n            __|   __    __   |__          **
**                                  __|   __|  |  |  |__   |__       **
**   `` Dragon Computing ! ''    __|   __|     |  |     |__   |__    **
**                              |_____|        |__|        |_____|   **
**                                                                   **
\*********************************************************************/
/*    This is a C startup routine for use in my own programs.
**  The routine `_Startup_' must be the first code encountered and
**  this module the first module linked using `blink' replacing
**  the lattice supplied "c.o" startup code.
**    The code will open the Arp library (setting its global base
**  variables); parse any CLI arguments using the user supplied
**  Templates and the Arp `GADS' library routine; and if WB startup
**  read and return the WBStartup Message.
**    See "ArpC.h" for the global variables defined here and
**  which global variables the user MUST himself define.
*/
#include "ArpC.h"
#include <libraries/ArpBase.h>
#include <proto/Exec.h>


void __saveds __asm
_Startup_ ( register __d0 int ArgLen, register __a0 char *ArgLine )
/*   This routine MUST be the first memory used by the compiler.
** IE the first module linked and no global varibales defined
** before it (declaring externs is however ok).
**    Note the `__saveds' is used to get the complier to set the A4
**  Global Variable Register and `__asm' to read the CLI argument
**  line handed to the program in registers D0 and A0.
*/
{
  if( ArpBase = (struct ArpBase *)OpenLibrary("arp.library", 39) ) {
    IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;
    GfxBase       = (struct GfxBase *)       ArpBase->GfxBase;
  }
  WBMsg = (struct WBStartup *)NULL;
  Process = (struct Process *)FindTask(NULL);
  if( Process->pr_CLI ) {
    int argc;
    if( !ArpBase )   Exit(100);
    argc = GADS( ArgLine, ArgLen, CLI_Help, (char **)CLI_Args, CLI_Template );
    if( argc < 0 )  exit(20);
    main( argc, CLI_Args );
  }
  else {  /* Workbench startup */
    WaitPort(&Process->pr_MsgPort);
    WBMsg = (struct WBstart *)GetMsg(&Process->pr_MsgPort);
    if( !ArpBase )   exit(100);
    main( 0, (char **)WBMsg );
  }
  exit( 0 );
}


void
exit( int retcode )
{
  if (ArpBase)                  /* this will close all arp stuff too */
    CloseLibrary((struct Library *)ArpBase);
  if( WBMsg ) {
    Forbid();
    ReplyMsg((struct Message *)WBMsg);
  }
  Exit(retcode);
}


/* HERE provide the global variables defined in this module */
struct ArpBase       *ArpBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase       *GfxBase;
struct Process       *Process;  /* this process */
struct WBStartup     *WBMsg;    /* WBStartup message (NULL if CLI start) */

