/* ---------------------------------------------------------------------- */
/*                   Copyright (C) 1991 by Natrlich!                     */
/*                      This file is copyrighted!                         */
/*                Refer to the documentation for details.                 */
/* ---------------------------------------------------------------------- */
#include "defines.h"
#include OSBIND
#include <stdio.h>
#include <setjmp.h>
#include "nasm.h"
#if OS != TOS
#include <errno.h>
#endif

static char nil[] = "***NIL***";
char  outofmem[] = "<grunt> Not enough space <wheeze> in here",
      bufoverflow[] = "An internal buffer <gasp> overflowed <gurgle>";

int         errors;
jmp_buf     panic;

extern int  maxerrors;



void nexit( no)
int   no;
{
   extern int  tossable;
 
#if OS == TOS
   if( tossable)
   {
      while( Bconstat( 2))
         Bconin( 2);
      Bconin( 2);
   }
#endif
   exit( no);
}

void pluserrors()
{
   if( ++errors == maxerrors)
   {
      fprintf( ESTREAM, "Aborting after %d errors, 'nuff'z'nuff\n",
            maxerrors);
      finalshit();
#if STATISTICS
      stats();
#endif
      nexit(1);
   }
}

void nerror( err)
char   *err;
{
   h_print();
   fprintf( ESTREAM, "Error \"%s\"\n", err);
   pluserrors();
}


void nserror( err, s)
char   *err, *s;
{
   if( ! s)
      s = nil;
   h_print();
   fprintf( ESTREAM, "Error \"%s\" ( %s )\n", err, s);
   pluserrors();
}


void nswarning( err, s)
char   *err, *s;
{
   if( ! s)
      s = nil;
   h_print();
   fprintf( ESTREAM, "Warning \"%s\" ( %s )\n", err, s);
}


void npanic( err)
char  *err;
{
   nerror(err);
   longjmp( panic, 1);
}

/* -------------------------------------------------------------- */
void nwarning( warning)
char   *warning;
{
   h_print();
   fprintf( ESTREAM, "Warning \"%s\"\n", warning);
}

void nmessage( message)
char   *message;
{
   h_print();
   fprintf( ESTREAM, "Har Har \"%s\"\n", message); /* used to be "Info ..*/
}

void finalstats( what)
{
   printf("%s with %d error%c\n",
          what ? "Finished" : "Aborted", errors, errors == 1 ? ' ' : 's');
}

/* Fatal error */
void nferror( err)
char  *err;
{
   nerror( err);
   finalstats(0);
   finalshit();
#if STATISTICS
   stats();
#endif
   nexit(1);
}

/* GEMDOS error */
void ngferror( no, s)
long  no;
char  *s;
{
#if OS == TOS   /* (MSDOS too ??) */
   switch( (int) no)
   {
      default:
#endif
         nferror( s);
#if OS == TOS
      case -12  :
      case -1   :
         nferror("General error");

      case -2   :
         nferror("Drive not ready");

      case -11  :
         nferror("Read error");

      case -37  :
         nferror("Invalid handle");
   }
#else
   h_print();
   perror("I/O error");
#endif
}

/* Katastrophaler error */
void nierror( err)
char  *err;
{
   putc('\n', ESTREAM);
   h_print();
   fprintf( ESTREAM, "Catastrophical error \"%s\"\n", err);
   nexit(0xDEAD);
}

