/*
** ICE BREAKER (CLI VERSION) V1.0
** ------------------------------
** Author:    Paul Manias
** Date:      25 April 1997.
** Copyright: DreamWorld Productions 1997.
** Compile:   sc IceBreaker.c nolink
**            PhxAss /IceAssembler.asm
**            slink FROM lib:c.o,IceBreaker.o,/IceAssembler.o TO IceBreaker LIBRARY LIB:sc.lib,LIB:Amiga.lib
**
** Doc:       This is the entry code for IceBreaker, it sets things up
**            so that the assembler code is ready to intercept debug
**            messages.  This particular version outputs to the CLI
**            or whatever stdout has been redirected to.
*/

#include <proto/games.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <dos/dos.h>

extern struct ExecBase *SysBase;
extern struct GFXBase *GFXBase;

#define LVODEBUGMESSAGE -378
#define LVOERRORMESSAGE -384
#define LVOSTEPBACK     -390

extern ULONG (libDebugMessage)();
extern ULONG (libErrorMessage)();
extern ULONG (libStepBack)();

struct GMSBase *GMSBase;
struct Task *maintask;
struct Task *edittask = NULL;

BPTR conhandle=0;      /* Handle to output debug information */

/***************************** INITIALISATION CODE *********************************/

void mainroutine(void);
long getlen(char string[]);
void wtext(char string[]);

void main(void)
{
 void *oldDebugMessage;
 void *oldErrorMessage;
 void *oldStepBack;

 maintask = FindTask(0);

 if (GMSBase = (struct GMSBase *) OpenLibrary("GMS:libs/dpkernal.library",0)) {
  if (GFXBase = (struct GFXBase *) OpenLibrary("graphics.library",0)) {
   if (DebugActive() == ERR_OK) {
    if (conhandle = Output()) {

       Forbid();
       oldDebugMessage = SetFunction((struct Library *)GMSBase,LVODEBUGMESSAGE,&libDebugMessage);
       oldErrorMessage = SetFunction((struct Library *)GMSBase,LVOERRORMESSAGE,&libErrorMessage);
       oldStepBack     = SetFunction((struct Library *)GMSBase,LVOSTEPBACK,&libStepBack);
       SumLibrary((struct Library *)GMSBase);
       Permit();

       mainroutine();

       Forbid();
       SetFunction((struct Library *)GMSBase,LVODEBUGMESSAGE,oldDebugMessage);
       SetFunction((struct Library *)GMSBase,LVOERRORMESSAGE,oldErrorMessage);
       SetFunction((struct Library *)GMSBase,LVOSTEPBACK,oldStepBack);
       SumLibrary((struct Library *)GMSBase);
       Permit();

    }
   DebugInactive();
   }
  CloseLibrary((struct Library *)GFXBase);
  }
 CloseLibrary((struct Library *)GMSBase);
 }
}

/*********************************** MAIN LOOP *************************************/

void mainroutine(void)
{
  LONG result;

  wtext("\nWelcome to IceBreaker V1.0, the debugger for GMS programs.\n");
  wtext("This program will wait until a program using GMS is executed.  Any\n");
  wtext("debug messages that are sent while it is active will be intercepted\n");
  wtext("and printed out to this window.\n\n");
  wtext("Press CTRL-C at any time to exit.\n\n");
  wtext("Type                 | Data\n");
  wtext("---------------------+---------------------------------------------\n");

  do {
     result = Wait(SIGBREAKF_CTRL_C);
  } while (!(result & SIGBREAKF_CTRL_C));

}

/***********************************************************************************/

void wtext(char string[]) {
  Write(conhandle,(ULONG)string,getlen(string));
}

/***********************************************************************************/

long getlen(char string[])
{
  int length=0;

  while (string[length] != 0) {
    length++;
    if (length > 256) break;
  }
  return(length);
}

