/* NAME:
 *	logfacs - convert syslog facility name to value
 *
 * DESCRIPTION:
 *	This routine uses a simple hash function to convert a string
 *	representation of a syslog facility to a value.
 *
 * AUTHOR:
 *	Simon J. Gerraty <sjg@quick.com.au>
 */
/*
 *      @(#)Copyright (c) 1994, Simon J. Gerraty.
 *      
 *      This is free software.  It comes with NO WARRANTY.
 *      Permission to use, modify and distribute this source code 
 *      is granted subject to the following conditions.
 *      1/ that the above copyright notice and this notice 
 *      are preserved in all copies and that due credit be given 
 *      to the author.  
 *      2/ that any changes to this code are clearly commented 
 *      as such so that the author does not get blamed for bugs 
 *      other than his own.
 *      
 *      Please send copies of changes and bug-fixes to:
 *      sjg@quick.com.au
 */

#ifndef lint
static char RCSid[] = "$Id: logfacs.c,v 1.2 1997/02/08 07:11:00 sjg Exp $";
#endif

#include <string.h>
#include <sys/cdefs.h>
#include <syslog.h>

#include "logfacs.h"

extern long Hash __P((const char *));

int
logfacs(spec)
	const char *spec;
{
	char buf[128];
	char *cp;
	int log_fac = 0;
	
	(void) strncpy(buf, spec, sizeof(buf) - 1);
	buf[sizeof(buf) - 1] = '\0';

	if (cp = strchr(buf, '.'))
		*cp++ = '\0';

	/* Most of this code was generated by sed */
	switch (Hash(buf)) {
	case H_KERN: log_fac |= LOG_KERN; break;
	case H_USER: log_fac |= LOG_USER; break;
	case H_MAIL: log_fac |= LOG_MAIL; break;
	case H_DAEMON: log_fac |= LOG_DAEMON; break;
	case H_AUTH: log_fac |= LOG_AUTH; break;
	case H_SYSLOG: log_fac |= LOG_SYSLOG; break;
#ifdef LOG_LPR
	case H_LPR: log_fac |= LOG_LPR; break;
#endif
#ifdef LOG_NEWS
	case H_NEWS: log_fac |= LOG_NEWS; break;
#endif
#ifdef LOG_UUCP
	case H_UUCP: log_fac |= LOG_UUCP; break;
#endif
#ifdef LOG_CRON
	case H_CRON: log_fac |= LOG_CRON; break;
#endif
#ifdef LOG_FTP
	case H_FTP: log_fac |= LOG_FTP; break;
#endif
	case H_LOCAL0: log_fac |= LOG_LOCAL0; break;
	case H_LOCAL1: log_fac |= LOG_LOCAL1; break;
	case H_LOCAL2: log_fac |= LOG_LOCAL2; break;
	case H_LOCAL3: log_fac |= LOG_LOCAL3; break;
	case H_LOCAL4: log_fac |= LOG_LOCAL4; break;
	case H_LOCAL5: log_fac |= LOG_LOCAL5; break;
	case H_LOCAL6: log_fac |= LOG_LOCAL6; break;
	case H_LOCAL7: log_fac |= LOG_LOCAL7; break;
	default:
		log_fac |= LOG_DAEMON;
		break;
	}
	if (cp) {
		switch (Hash(cp)) {
		case H_EMERG: log_fac |= LOG_EMERG; break;
		case H_ALERT: log_fac |= LOG_ALERT; break;
		case H_CRIT: log_fac |= LOG_CRIT; break;
		case H_ERR: log_fac |= LOG_ERR; break;
		case H_WARNING: log_fac |= LOG_WARNING; break;
		case H_NOTICE: log_fac |= LOG_NOTICE; break;
		case H_INFO: log_fac |= LOG_INFO; break;
		case H_DEBUG: log_fac |= LOG_DEBUG; break;
		}
	}
	return log_fac;
}

int
pencode(spec)
	const char *spec;
{
	return logfacs(spec);
}
