/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1990, 1991, 1993, 1994, 1995 by Wayne C. Gramlich.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * for any purpose is hereby granted without fee provided that the above
 * copyright notice and this permission are retained.  The author makes
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/* An implementation of some error routines: */

#ifndef ERROR_EXPORTS_H
#include "error_exports.h"
#endif

#ifndef LIBC_EXPORTS_H
#include "libc_exports.h"
#endif

#ifndef STR_EXPORTS_H
#include "str_exports.h"
#endif

#ifndef PORT_H
#include "port.h"
#endif

#ifndef UNIX_STDIO_H
#include "unix_stdio.h"
#endif

#ifndef UNIX_STDLIB_H
#include "unix_stdlib.h"
#endif

#ifndef UNIX_UNISTD_H
#include "unix_unistd.h"
#endif

#ifndef UNIX_STDARG_H
#include "unix_stdarg.h"
#endif

/* LINTLIBRARY */

int error_count = 0;
int error_warn_count = 0;

/*
 * error(format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" to stderr using "format".
 *	The error is non-fatal and returns.
 */
void
error(
	Str		format,
	...)
{
	va_list		args;

	va_start(args, format);
	(void)fprintf(stderr, "Error: ");
	vfprintf(stderr, format, args);
	if (format[strlen(format)-1] != '\n') {
		(void)putc('\n', stderr);
	}
	(void)fflush(stderr);
	va_end(args);
	error_count++;
}

/*
 * error_abort(format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" to stderr using "format".
 *	A core dump is generated.
 */
void
error_abort(
	Str		format,
	...)
{
	va_list		args;

	va_start(args, format); /* TCOVOK - This routine calls abort(),
			 * which disables tcov output.  */
	(void)fprintf(stderr, "Internal error: ");
	vfprintf(stderr, format, args);
	if (format[strlen(format)-1] != '\n') {
		(void)putc('\n', stderr); /* TCOVOK */
	}
	(void)fflush(stderr); /* TCOVOK */
	va_end(args);
	(void)abort();
}

/*
 * error_count_get()
 *	This routine will return the number of time error() has been
 *	called.
 */
int
error_count_get(PORT_NO_ARGS)
{
	return error_count;
}

/*
 * error_fatal(format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" to stderr using "format".
 *	The error is non-recoverable and causes an immediate exit.
 */
void
error_fatal(
	Str		format,
	...)
{
	va_list		args;

	va_start(args, format);
	(void)fprintf(stderr, "Non-recoverable error: ");
	vfprintf(stderr, format, args);
	if (format[strlen(format)-1] != '\n') {
		(void)putc('\n', stderr);
	}
	(void)fflush(stderr);
	va_end(args);
	exit(1);
}

/*
 * error_warn(format, arg1, ..., argN)
 *	This routine will print "arg1" through "argN" to stderr using "format".
 *	The error is non-fatal and returns.
 */
void
error_warn(
	Str		format,
	...)
{
	va_list		args;

	va_start(args, format);
	(void)fprintf(stderr, "Warning: ");
	vfprintf(stderr, format, args);
	if (format[strlen(format)-1] != '\n') {
		(void)putc('\n', stderr);
	}
	(void)fflush(stderr);
	va_end(args);
	error_warn_count++;
}

/*
 * error_warn_count_get()
 *	This routine will return the number of time error_warn() has been
 *	called.
 */
int
error_warn_count_get(PORT_NO_ARGS)
{
	return error_warn_count;
}

