/*
 * exit.c
 *
 * Exit and error reporting stuff
 *
 * $Id :$
 * $Log:$
 *
 */

#include "defs.h"
#include "exit.h"
#include <stdarg.h>
#include <stdlib.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <intuition/intuition.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>


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

/*
 * MyExit()
 *
 * General exit routine
 *
 * INPUTS:  err_message - if we wish to report an error, this
 *          is a pointer to the text. It supports Printf() style
 *          formatting.
 *
 * NOTES: If the program is being run from the WB, the message will
 * be displayed in a standard requester; if from DOS, it gets dumped
 * to the error stream.
 *
 * If a message is supplied, the program will exit via the standard
 * exit() call with a RETURN_FAIL code; otherwise RETURN_OK.
 *
 * If you want to report an error code, set this via SetIoErr() before
 * calling this routine.
 */


VOID
MyExit( const STRPTR err_message, ... )
{

    /* was there an error */
    if( err_message )
    {
        /* better report it then */
        va_list va;

        /* init pointer to arg list */
        va_start( va, err_message );

        /* Were we started from the WB? */
        if(WBMSG)
        {
            /* Yes. Show err_message in a requester */
            struct EasyStruct err_req = { sizeof(struct EasyStruct),
                        0,                  /* Flags */
                        "Error",            /* Requester title */
                        NULL,               /* Body text. Should be: err_message. See below */
                        "Abort" };          /* Gadgets */

            /* VBCC won't do compound initilization with non constants */
            /* and it doesn't believe that err_message is a constant. :-( */
            /* So we'll fill in body text manually */
            err_req.es_TextFormat = err_message;

            /* Put up the requester. */
            /* We ignore the result since we only have one gadget and there's no */
            /* way of finding out if intuition couldn't create the requester */
            EasyRequestArgs( NULL, &err_req, NULL, va );
        }
        else
        {
            /* We were run from CLI - dump to error stream */
            BPTR StdErr;

            /* Try to get a handle on the default error stream */
            {
                /* find ourselves ;-) */
                struct Process *this_task = (struct Process *) FindTask( NULL );

                /* Do we have an error stream? */
                /* If not use the default output stream */
                StdErr = ( this_task->pr_CES ) ? this_task->pr_CES : this_task->pr_COS;
            }

            VFPrintf( StdErr, err_message, va );
            FPutC( StdErr, '\n' );
        }

        /* finished with args */
        va_end(va);
    }

    exit( err_message ? RETURN_FAIL : RETURN_OK );
}


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