
/**************************************************************************
 *                                                                        *
 * Copyright (c) 1996 Michael Richmond and Richard Treffers               *
 *                                                                        *
 *                    This software may be copied and distributed for     *
 *                    educational, research and not for profit services   *
 *                    provided that this copyright and statement are      *
 *                    included in all such copies.                        *
 *                                                                        *
 **************************************************************************/


/*	
	writes error message on screen and exits if level >0

	added ability of a program to "quiet" non-fatal error messages
	MWR 5/23/2000
*/




#include <stdio.h>
#include <string.h>
#include "pcvista.h"
#include <unistd.h>


	/*
	 * this static variable controls whether non-fatal error messages
	 * are printed to stderr, or not.  
	 * It is initially set to 1, meaning  "print to stderr"
	 * but can be set to      0, meaning  "don't print messages"
	 *
	 * The "disable_warnings" and "enable_warnings" functions
	 * can set the flag to 0 and 1, respectively.
	 */

static int 
print_warnings = 1;




	/*******************************************************************
	 * Given an error level and a message, 
	 * 
	 *    if level == 0  AND  print_warnings == 1
	 *                                 print message to stderr
	 *                                 and continue program execution
	 *
	 *    if level >  0          print message to stderr
	 *                                 and terminate program execution,
	 *                                 returning the 'level' as retcode
	 */

void 
error(level, msg)
int level;
char *msg;
{
	if ((level > 0) || ((level == 0) && (print_warnings == 1))) {
		fputs(msg, stderr);
		if (msg[strlen(msg)-1] != '\n')
			fputs("\n", stderr);
	}
	if (level > 0) {
		exit(level);
	}
}


	/***********************************************************************
	 * set the static "print_warnings" variable to 0, 
	 *   which disables printing warning messages when the 
	 *   "error" function is called with level 0
	 */

void
disable_warnings
	(
	void
	)
{
	print_warnings = 0;
}
	

	/***********************************************************************
	 * set the static "print_warnings" variable to 1, 
	 *   which enables printing warning messages when the 
	 *   "error" function is called with level 0
	 */

void
enable_warnings
	(
	void
	)
{
	print_warnings = 1;
}
	

