2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
error.c
|
|
|
|
|
|
|
|
Errors management routines
|
|
|
|
|
|
|
|
(c) 2000-2007 Benoit Minisini <gambas@users.sourceforge.net>
|
|
|
|
|
|
|
|
This program 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 1, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GB_ERROR_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include "gb_buffer.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
ERROR_INFO ERROR_info;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
static const char *_message[] =
|
|
|
|
{
|
|
|
|
/* 0 E_UNKNOWN */ "Unknown error",
|
|
|
|
/* 1 E_MEMORY */ "Out of memory",
|
|
|
|
/* 2 E_OPEN */ "Cannot open file: &1",
|
|
|
|
/* 3 E_READ */ "Cannot read file: &1",
|
|
|
|
/* 4 E_SYNTAX */ "Syntax error",
|
|
|
|
/* 5 E_UNEXPECTED */ "Unexpected &1",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static ERROR_CONTEXT *_current = NULL;
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_enter(ERROR_CONTEXT *err)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
CLEAR(err);
|
|
|
|
err->prev = _current;
|
|
|
|
_current = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_leave(ERROR_CONTEXT *err)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (err->prev != ERROR_LEAVE_DONE)
|
|
|
|
{
|
|
|
|
/*ERROR_panic("ERROR_leave already done");*/
|
|
|
|
|
|
|
|
_current = err->prev;
|
|
|
|
err->prev = ERROR_LEAVE_DONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
char *ERROR_get(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
if (code > 0 && code < 256)
|
|
|
|
return strerror(code);
|
|
|
|
else
|
|
|
|
return ERROR_Message[code - 256];
|
|
|
|
*/
|
|
|
|
return strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_define(const char *pattern, const char *arg[])
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
uchar c;
|
2009-07-08 21:57:50 +02:00
|
|
|
bool subst;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void _add_char(uchar c)
|
|
|
|
{
|
|
|
|
if (n >= MAX_ERROR_MSG)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ERROR_info.msg[n++] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _add_string(const char *s)
|
|
|
|
{
|
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
_add_char(*s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
if ((intptr_t)pattern > 0 && (intptr_t)pattern < 256)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-01-17 22:39:26 +01:00
|
|
|
ERROR_info.code = (int)(intptr_t)pattern;
|
|
|
|
pattern = _message[(int)(intptr_t)pattern];
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ERROR_info.code = -1;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
if (arg)
|
|
|
|
{
|
|
|
|
subst = FALSE;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
c = *pattern++;
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (subst)
|
|
|
|
{
|
|
|
|
if (c >= '1' && c <= '4')
|
|
|
|
_add_string(arg[c - '1']);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_add_char('&');
|
|
|
|
_add_char(c);
|
|
|
|
}
|
|
|
|
subst = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c == '&')
|
|
|
|
subst = TRUE;
|
|
|
|
else
|
|
|
|
_add_char(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_add_string(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
_add_char(0);
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void PROPAGATE()
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ERROR_CONTEXT *err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (_must_free_index)
|
|
|
|
{
|
|
|
|
for (i = 0; i < _must_free_index; i++)
|
|
|
|
OBJECT_UNREF(&_must_free[i], "PROPAGATE");
|
|
|
|
|
|
|
|
_must_free_index = 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (_current == NULL)
|
|
|
|
ERROR_panic("Cannot propagate error. No error handler.");
|
|
|
|
|
|
|
|
err = _current;
|
|
|
|
ERROR_leave(_current);
|
|
|
|
longjmp(err->env, 1);
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void THROW(const char *code, ...)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int i;
|
|
|
|
const char *arg[4];
|
|
|
|
|
|
|
|
va_start(args, code);
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
arg[i] = va_arg(args, const char *);
|
|
|
|
|
|
|
|
ERROR_define(code, arg);
|
|
|
|
PROPAGATE();
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_panic(const char *error, ...)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, error);
|
|
|
|
|
|
|
|
fflush(NULL);
|
|
|
|
|
|
|
|
fprintf(stderr, "\n"
|
|
|
|
"** INTERNAL ERROR **\n"
|
|
|
|
);
|
|
|
|
vfprintf(stderr, error, args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
if (ERROR_info.code)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
ERROR_print();
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
fprintf(stderr, "** Program aborting... Sorry... :-(\n\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_print_at(FILE *where)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
fprintf(where, "%s\n", ERROR_info.msg);
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void ERROR_print(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ERROR_print_at(stderr);
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRACE_where(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
}
|