/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*----------------------------------------------------------------------*/
/*    Filename    : c.error	                                       	*/
/*    Author	  : R.W.Buckley						*/
/*    Date        : Mon 20th February 1995				*/
/*    Description :      						*/
/*    Modification history:    	    		       			*/
/*    		   							*/
/*----------------------------------------------------------------------*/

/* This array is accessed by the error i_numbers in convert.h, minus 1 */

#include <string.h>
#include <stdlib.h>

#include "kernel.h"
#include "error.h"
#include "swis.h"

#define TRUE  1
#define FALSE 0
#define taskname "PrintTool"

int my_report_error( _kernel_oserror *err);

	// Error function
int Error(_kernel_oserror *err)
{
  if (err != NULL){
      my_report_error(err);
      return TRUE;
  }
  return FALSE;
}

void BadError(_kernel_oserror *err)
{
  if (err != NULL){
      my_report_error(err);
      exit(ABORT);
  }
}

/*----------------------------------------------------------------------
    Name : error_construct

    Description :

    Dependencies :

    Parameters :
----------------------------------------------------------------------*/
_kernel_oserror *error_construct (char *s_token)
{
  _kernel_swi_regs reg; /* Global set of registers to use */
  static _kernel_oserror err_block;
  int    i_msg_desc[4];      /* Message file descriptor */
  char   *m_block = NULL;

  reg.r[1] = (int)FILENAME_MESSAGES;                    /* Messages file */
  err = _kernel_swi(MessageTrans_FileInfo, &reg, &reg);
  ERROR_RETURN(err);

  m_block = (char*)malloc(reg.r[2]);  /* Allocate memory for messages */
  if (m_block == NULL) return NULL;

  reg.r[0] = (int)i_msg_desc;         /* Load the messages file */
  reg.r[1] = (int)FILENAME_MESSAGES;
  reg.r[2] = (int)m_block;
  err = _kernel_swi(MessageTrans_OpenFile, &reg, &reg);
  ERROR_RETURN(err);

  reg.r[0] = (int)i_msg_desc;         /* Close the messages file */
  err = _kernel_swi(MessageTrans_CloseFile, &reg, &reg);
  ERROR_RETURN(err);

  reg.r[0] = (int)i_msg_desc;
  reg.r[1] = (int)s_token;
  reg.r[2] = (int)&(err_block.errmess);
  reg.r[3] = 256;
  reg.r[4] = reg.r[5] = reg.r[6] = reg.r[7] = 0;
  err = _kernel_swi(MessageTrans_GSLookup, &reg, &reg);
  ERROR_RETURN(err);

  free(m_block);

  err_block.errnum = 0;

  return &err_block;
}


int my_report_error( _kernel_oserror *err)
{
_kernel_swi_regs reg; /* Global set of registers to use */

	reg.r[0] = (int)err;
	reg.r[1] = 0;
	reg.r[2] = (int)taskname;

  	if (_kernel_swi(Wimp_ReportError, &reg, &reg) != NULL);

	return reg.r[1];
}
