/*
 * error.h
 *
 * Error messages and handling
 *
 *  1994-1998 Straylight
 */

/*----- Licensing note ----------------------------------------------------*
 *
 * This file is part of Straylight's Dynamic Linking System (SDLS)
 *
 * SDLS 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, or (at your option)
 * any later version.
 *
 * SDLS 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 SDLS.  If not, write to the Free Software Foundation,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>

#define error__MAKETABLE
#include "error.h"

static char *error__progname;
static int error__count[5];
static int error__retcode=0;

void error_setProgName(char *name)
{
  error__progname=strrchr(name,'.');
  if (!error__progname)
    error__progname=name;
  else
    error__progname++;
}

char *error_progname(void)
{
  return (error__progname);
}

void error(int message,...)
{
  static char const *types[]={"Informational",
                              "Warning",
                              "Error",
                              "Serious error",
                              "Fatal error"};

  va_list ap;
  va_start(ap,message);
  fprintf(stderr,
          "%s: (%s) ",
          error__progname,
          types[error__table[message].sev]);
  vfprintf(stderr,error__table[message].msg,ap);
  va_end(ap);
  if (error__table[message].sev==error_FATAL)
    exit(error_FATAL);
  error__count[error__table[message].sev]++;
  if (error__table[message].sev>error__retcode)
    error__retcode=error__table[message].sev;
}

void error_show(void)
{
  if (error__retcode)
  {
    fprintf(stderr,
            "%s: completed with %i warning%s, "
            "%i error%s and %i serious error%s.\n",
            error__progname,
            error__count[error_WARN],
            error__count[error_WARN]==1 ? "" : "s",
            error__count[error_ERROR],
            error__count[error_ERROR]==1 ? "" : "s",
            error__count[error_SEVERE],
            error__count[error_SEVERE]==1 ? "" : "s");
  }
}

int error_returnCode(void)
{
  return (error__retcode);
}

extern void aof_error(void)
{
  error(0);
}
