/* Error message utilites V1.00 27/8/04
   See error.h for docs
   Copyright 2008 Jeffrey Lee
   This file is part of WOUM.
   WOUM is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   WOUM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   You should have received a copy of the GNU General Public License
   along with WOUM.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _ERROR_C
#define _ERROR_C

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "error.h"

static volatile int running = 0;

void (*warning_func)(char *msg) = 0;

void _warning(char *file,int line,char *fmt,...)
{
	va_list a;
	char buf[1024];
	int l;
	l = sprintf(buf,"%s:%d:",file,line);
	va_start(a,fmt);
	l += vsprintf(buf+l,fmt,a);
	buf[l] = 10;
	buf[l+1] = 0;
	fputs(buf,stderr);
	if ((warning_func) && (running == 0))
	{
		running = 1;
		(warning_func)(buf);
		running = 0;
	}
}

void _error(char *file,int line,char *fmt,...)
{
	va_list a;
	char buf[1024];
	int l;
	l = sprintf(buf,"%s:%d:",file,line);
	va_start(a,fmt);
	l += vsprintf(buf+l,fmt,a);
	buf[l] = 10;
	buf[l+1] = 0;
	fputs(buf,stderr);
	exit(EXIT_FAILURE);
}

#endif
