/*
Copyright (c) 2008, Mathias Kende

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "cexn.h"
#include <error.h>
#include <string.h>

struct s_exn_stack
{
	jmp_buf env;
	struct s_exn_stack* next;
};

struct s_handler_list
{
	int handled;
	f_handler handler;
	struct s_handler_list* next;
};

static __thread struct s_exn_stack* exn_stack = NULL;
static __thread struct s_handler_list* handler_list = NULL;
static __thread int exn_n = 0;
static __thread void* exn_exn = NULL;
static __thread const char* exn_type = NULL;
static __thread const char* exn_file = NULL;
static __thread const char* exn_fct = NULL;
static __thread int exn_line = 0;
static __thread int handler_list_size = 0;
static int return_code = 1;

static void register_first() __attribute ((constructor));
static void unregister_all() __attribute ((destructor));


static void register_first()
{
	jmp_buf* b = __get_buf();
	if (setjmp(*b))
		error(return_code,0,"uncaugh exception in function %s of file %s at line %d.",exn_fct,exn_file,exn_line);
		/* This is reasonably safe as there is no living local variable here. */
}

static void unregister_all()
{
	__clear_exn();
	while (exn_stack != NULL)
		__delete_buf();
	while (handler_list != NULL)
		__unregister_handler(handler_list->handled);
}

jmp_buf* __get_buf()
{
	struct s_exn_stack* t = exn_stack;
	exn_stack = (struct s_exn_stack*) malloc(sizeof(struct s_exn_stack));
	exn_stack->next = t;
	return &exn_stack->env;
}

void __delete_buf()
{
	struct s_exn_stack* t = exn_stack;
	exn_stack = exn_stack->next;
	free(t);
}

void __throw_exn(int n, void* exn, const char* type, const char* file, const char* fct, int line)
{
	__clear_exn();
	exn_n = n;
	exn_exn = exn;
	exn_file = file;
	exn_type = type;
	exn_fct = fct;
	exn_line = line;
	__rethrow();
}

void __rethrow()
{
	struct s_exn_stack* t = exn_stack;
	jmp_buf buf;
        memcpy(&buf,&exn_stack->env,sizeof(jmp_buf));
	exn_stack = exn_stack->next;
	free(t);
	longjmp(buf,exn_n);
}

void* __get_exn()
{
	return exn_exn;
}

void __clear_exn()
{
	if (exn_exn != NULL)
	{
		free(exn_exn);
		exn_exn = NULL;
	}
}

void __catchall(const char* file, const char* fct, int line)
{
	struct s_handler_list* cur = handler_list;
	while (cur != NULL && cur->handled < exn_n)
		cur = cur->next;
	if (cur != NULL && cur->handled == exn_n)
		(cur->handler)(exn_type,exn_exn,exn_file,exn_fct,exn_line,file,fct,line);
	else
		error(0,0,"exception of type <%s> thrown at %s:%d, catched at %s:%d.",exn_type,exn_file,exn_line,file,line);
	__clear_exn();

}

int __register_handler(int handled, f_handler handler)
{
	struct s_handler_list* last = handler_list;
	struct s_handler_list* cur = handler_list;
	while (cur != NULL && cur->handled < handled)
	{
		last = cur;
		cur = cur->next;
	}
	if (cur == NULL)
	{
		cur = (struct s_handler_list*)malloc(sizeof(struct s_handler_list));
		cur->handled = handled;
		cur->handler = handler;
		cur->next = NULL;
		if (handler_list == NULL)
			handler_list = cur;
		else
			last->next = cur;
		++handler_list_size;
		return 0;
	}
	else if (cur->handled == handled)
	{
		cur->handler = handler;
		return 1;
	}
	else
	{
		struct s_handler_list* new = (struct s_handler_list*)malloc(sizeof(struct s_handler_list));
		new->handled = handled;
		new->handler = handler;
		new->next = cur;
		if (cur == handler_list)
			handler_list = new;
		else
			last->next = new;
		++handler_list_size;
		return 0;
	}
}

int __unregister_handler(int handled)
{
	struct s_handler_list* last = handler_list;
	struct s_handler_list* cur = handler_list;
	while (cur != NULL && cur->handled < handled)
	{
		last = cur;
		cur = cur->next;
	}

	if (cur == NULL || cur->handled != handled)
		return 1; /* throwing an exception in an exception handler is a bad idea. */
	else
	{
		if (cur == handler_list)
			handler_list = cur->next;
		else
			last->next = cur->next;
		free(cur);
		--handler_list_size;
		return 0;
	}
}

int __check_handler_list()
{
	struct s_handler_list* cur = handler_list;
	int size = 0;
	while (cur != NULL)
	{
		if (cur->next != NULL)
			if (cur->handled >= cur->next->handled)
				return 1;
		cur = cur->next;
		++size;
	}
	return !(size == handler_list_size);
}

int __set_return_code(int code)
{
	int t = return_code;
	return_code = code;
	return t;
}
