/* 
   NSException.m

   Copyright (C) 1995, 1996 Ovidiu Predescu and Mircea Oancea.
   All rights reserved.

   Author: Ovidiu Predescu <ovidiu@bx.logicnet.ro>

   This file is part of libFoundation.

   Permission to use, copy, modify, and distribute this software and its
   documentation for any purpose and without fee is hereby granted, provided
   that the above copyright notice appear in all copies and that both that
   copyright notice and this permission notice appear in supporting
   documentation.

   We disclaim all warranties with regard to this software, including all
   implied warranties of merchantability and fitness, in no event shall
   we be liable for any special, indirect or consequential damages or any
   damages whatsoever resulting from loss of use, data or profits, whether in
   an action of contract, negligence or other tortious action, arising out of
   or in connection with the use or performance of this software.
*/

#include <config.h>

#ifdef HAVE_LIBC_H
# include <libc.h>
#else /* GNU CC comes with unistd.h */
# include <unistd.h>
#endif

#if HAVE_STRING_H
# include <string.h>
#endif
#if HAVE_MEMORY_H
# include <memory.h>
#endif

#if HAVE_STDLIB_H
#include <stdlib.h>
#else
extern char* getenv(char*);
#endif

#include <Foundation/common.h>
#include <Foundation/NSException.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/exceptions/GeneralExceptions.h>

static void default_handler(NSException* exception)
{
    NSLog ([exception errorString]);
    if(getenv("CRASH_WITH_ABORT"))
	abort();
    else exit(1);
}

#ifdef BROKEN_COMPILER
static NSUncaughtExceptionHandler* uncaughtHandler = default_handler;
#endif

static NSHandler firstExceptionHandler;
static NSHandler* lastHandler = NULL;

void _NSAddHandler(NSHandler *exHandler)
{
    if(!lastHandler) {
	/* We were called for the first time. */
	lastHandler = &firstExceptionHandler;
	firstExceptionHandler.previousHandler = NULL;
	memset(&(firstExceptionHandler.jmpState), 0, sizeof(jmp_buf));
#ifndef BROKEN_COMPILER
	firstExceptionHandler.handler = default_handler;
#endif
    }
    exHandler->previousHandler = lastHandler;
    lastHandler = exHandler;
}

void _NSRemoveHandler(NSHandler *handler)
{
    lastHandler = lastHandler->previousHandler;
}

@implementation NSException

+ (void)initialize
{
    static BOOL initialized = NO;

    if(!initialized) {
	initialized = YES;
	[NSAssertionHandler initialize];
    }
}

+ (void)taskNowMultiThreaded:notification
{
    // THREAD
}

+ (NSException*)exceptionWithName:(NSString*)_name
    reason:(NSString*)_reason
    userInfo:(NSDictionary*)_userInfo
{
    return [[self alloc] initWithName:_name reason:_reason userInfo:_userInfo];
}

+ (void)raise:(NSString*)_name
    format:(NSString*)_format,...
{
    va_list ap;

    va_start(ap, _format);
    [self raise:_name format:_format arguments:ap];
    va_end(ap);
}

+ (void)raise:(NSString*)_name
    format:(NSString*)_format
    arguments:(va_list)argList
{
    NSException* exception = [self exceptionWithName:_name
				    reason:Avsprintf(_format, argList)
				    userInfo:nil];
    [exception raise];
}

- (id)initWithName:(NSString*)_name
    reason:(NSString*)_reason
    userInfo:(NSDictionary*)_userInfo
{
    name = [_name retain];
    reason = [_reason retain];
    userInfo = [_userInfo retain];

    return self;
}

- (void)raise
{
    NSHandler* ex = lastHandler;

    if(!ex) {
	fprintf (stderr, "Uncaught exception %s\n",
		 [[self errorString] cString]);
	abort();
    }
#ifdef BROKEN_COMPILER
    if(!ex->previousHandler)
	uncaughtHandler(self);
    else {
	ex->exception = self;
	longjmp(ex->jmpState, 1);
    }
#else
    (lastHandler->handler)(self);
#endif
}

- (void)dealloc
{
    [name release];
    [reason release];
    [userInfo release];
}

- (NSString*)name
{
    return name ? name : NSStringFromClass(isa);
}

- (NSString*)reason		{ return reason; }
- (NSDictionary*)userInfo	{ return userInfo; }

- (NSString*)descriptionWithLocale:(NSDictionary*)locale
{
    return [NSString stringWithFormat:
	@"(Exception name:%@ class:%@ reason:%@ info:%@)",
	name ? name : @"<nil>",
	NSStringFromClass(isa),
	reason ? reason : @"<nil>",
	userInfo ? [userInfo descriptionWithLocale:locale] : @"<nil>"];
}

- (NSString*)description
{
    return [self descriptionWithLocale:nil];
}

@end /* NSException */


@implementation NSException (Extensions)

- (BOOL)exceptionIsKindOfClass:(Class)class
{
    return [self isKindOfClass:class];
}

- (BOOL)exceptionIsIn:(NSArray*)exceptions
{
    int i, n = [exceptions count];

    for(i = 0; i < n; i++)
	if([self exceptionIsKindOfClass:[[exceptions objectAtIndex:i] class]])
	    return YES;
    return NO;
}

- (NSString*)errorString
{
    /* Don't use -[NSString stringWithFormat:] method because it can cause
       infinite recursion. */
    char buffer[512];

    sprintf (buffer, "exceptionClass %s\nReason: %s\n",
	     [[[self class] description] cString],
	     ([self reason] ? [[self reason] cString] : "null reason"));
    return [NSString stringWithCString:buffer];
}

- initWithFormat:(NSString*)format, ...
{
    va_list ap;

    va_start(ap, format);
    reason = [[NSString alloc] initWithFormat:format arguments:ap];
    va_end(ap);
    return self;
}

- initWithFormat:(NSString*)format arguments:(va_list)ap
{
    reason = [[NSString alloc] initWithFormat:format arguments:ap];
    return self;
}

- setName:(NSString*)_name
{
    [_name retain];
    [name release];
    name = _name;
    return self;
}

- setReason:(NSString*)_reason
{
    [_reason retain];
    [reason release];
    reason = _reason;
    return self;
}

- setUserInfo:(NSDictionary*)_userInfo
{
    [_userInfo retain];
    [userInfo release];
    userInfo = _userInfo;
    return self;
}

@end /* NSException (Extensions) */


NSString *NSInconsistentArchiveException = @"Archive is inconsistent";
NSString *NSGenericException = @"Generic exception";
NSString *NSInternalInconsistencyException = @"Internal inconsistency";
NSString *NSInvalidArgumentException = @"Invalid argument";
NSString *NSMallocException = @"Memory exhausted";
NSString *NSRangeException = @"Range exception";


NSUncaughtExceptionHandler *NSGetUncaughtExceptionHandler(void)
{
#ifdef BROKEN_COMPILER
    return uncaughtHandler;
#else
    return firstExceptionHandler.handler;
#endif
}

void NSSetUncaughtExceptionHandler(NSUncaughtExceptionHandler *handler)
{
#ifdef BROKEN_COMPILER
    uncaughtHandler = handler;
#else
    firstExceptionHandler.handler = handler;
#endif
}


/*
* Assertions.
*/

@implementation NSAssertionHandler

static id currentHandler = nil;

+ (void)initialize
{
    static BOOL initialized = NO;

    if(!initialized) {
	initialized = YES;
	currentHandler = [[self alloc] init];
	[NSException initialize];
    }
}

+ (NSAssertionHandler*)currentHandler
{
    return currentHandler;
}

- (void)handleFailureInFunction:(NSString*)functionName
    file:(NSString*)fileName
    lineNumber:(int)line
    description:(NSString*)format,...
{
    va_list ap;

    va_start(ap, format);
    NSLog(@"Assertion failed in file %@, line %d, function %@:",
			    fileName, line, functionName);
    NSLogv(format, ap);
    THROW([[AssertException alloc] initWithFormat:format arguments:ap]);
    va_end(ap);
}

- (void)handleFailureInMethod:(SEL)selector
    object:(id)object
    file:(NSString*)fileName
    lineNumber:(int)line
    description:(NSString*)format,...
{
    va_list ap;

    va_start(ap, format);
    NSLog(@"Assertion failed in file %@, line %d, method %@:",
	    fileName, line, NSStringFromSelector(selector));
    NSLogv(format, ap);
    THROW([[AssertException alloc] initWithFormat:format arguments:ap]);
    va_end(ap);
}

@end /* NSAssertionHandler */
