/* * Copyright (c) 1995, 1996 Gunther Schadow. All rights reserved. * * 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 2 of the License, 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. */ #include "pg_config.h" #pragma implementation #include "exception.h" #include "logfile.h" #include #include #include #include #include void _abort_handler(int typ, const char *msg) { abort(); } void _exit_handler(int typ, const char *msg) { exit(FAIL); } void _nop_handler(int typ, const char *msg) { return; } static exception_handler_t exception_handlers[E_MAX + 1] = { _abort_handler, _exit_handler, _exit_handler, _exit_handler, _exit_handler, _nop_handler, }; static exception_handler_t default_handlers[E_MAX + 1] = { _abort_handler, _exit_handler, _exit_handler, _exit_handler, _exit_handler, _nop_handler, }; #define E_MSGBUFSIZE 1024 void exception(int typ, const char *org, const char *msg, ...) { char buf[E_MSGBUFSIZE] = ""; va_list pvar; va_start(pvar, msg); if (msg != NULL) { int llevel; switch(typ) { case E_FATAL: llevel = L_FATAL; break; case E_ERROR: llevel = L_ERROR; break; case E_ERROS: llevel = L_ERROS; break; case E_PARSE: llevel = L_ERROR; break; case E_PROTO: llevel = L_ERROR; break; case E_EOF: llevel = L_WARN; break; default: llevel = L_MESG; break; } if(org != NULL) strncpy(buf, org, E_MSGBUFSIZE); strncat(buf, ": ", E_MSGBUFSIZE); strncat(buf, msg, E_MSGBUFSIZE); vlprintf(llevel, buf, pvar); /* FIXME: must be vsnprintf! (but HP-UX doesn't have it :-( */ vsprintf(buf, msg, pvar); va_end(pvar); } else lprintf(L_MESG, "exception %d raised by `%s'", typ, org); (*(exception_handlers[typ]))(typ, buf); FATAL("exception handler did return"); } void exception_handler(int typ, exception_handler_t hdl) { if (0 < typ && typ <= E_MAX) if (hdl != NULL) exception_handlers[typ] = hdl; else exception_handlers[typ] = default_handlers[typ]; }