/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Portions Copyright 1996 Warm Silence Software Ltd.
 */

/******************************************************/
/*                                                    */
/* Name: PGerr.c                                      */
/* Author: Paul Gardiner.                             */
/*                                                    */ 
/******************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "kernel.h"
#include "swis.h"
#include "PGwin.h"
#include "PGptr.h"
#include "PGerr.h"

static _kernel_swi_regs regs;

static _kernel_oserror errblk;

static int delay = 0;
static int error_pending = 0;
static int fatal;


static void report_error(void)
{
    PtrOn();
    PtrAnimate(NULL, 0, 0, 0, 0);
    regs.r[0] = (int) &errblk;
    regs.r[1] = (fatal ? 1 : 3);
    regs.r[2] = (int) WinTaskName();
    _kernel_swi(Wimp_ReportError, &regs, &regs);
    if(fatal || regs.r[1] == 2)
        exit(0);
    error_pending = 0;
}

void Error(char *fmt, ...)
{
    va_list args;

    errblk.errnum = 1;
    va_start(args, fmt);
    vsprintf(errblk.errmess, fmt, args);
    va_end(args);
    errblk.errmess[251] = '\0';
    error_pending = 1;
    fatal = 0;
    if(!delay)
        report_error();
}

void ErrorErr(_kernel_oserror *err)
{
    errblk = *err;
    error_pending = 1;
    fatal = 1;
    if(!delay)
        report_error();
}

void ErrorFatal(char *fmt, ...)
{
    va_list args;

    errblk.errnum = 1;
    va_start(args, fmt);
    vsprintf(errblk.errmess, fmt, args);
    va_end(args);
    errblk.errmess[251] = '\0';
    error_pending = 1;
    fatal = 1;
    if(!delay)
        report_error();
}

void ErrorDelay(int flag)
{
    delay = flag;
    if(!delay && error_pending)
    {
        report_error();
    }
}
