/*****************************************************************************
 *
 * File ..................: mbsed/rdconfig.c
 * Purpose ...............: Read configuration $MBSE_ROOT/etc/mbsed.conf
 * Last modification date : 18-Apr-1999
 *
 *****************************************************************************
 * Copyright (C) 1997-1998
 *   
 * Michiel Broek		FIDO:		2:2801/16
 * Beekmansbos 10		Internet:	mbroek@ux123.pttnwb.nl
 * 1971 BV IJmuiden
 * the Netherlands
 *
 * This file is part of MBSE BBS.
 *
 * This BBS 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.
 *
 * MB BBS 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 MB BBS; see the file COPYING.  If not, write to the Free
 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *****************************************************************************/


#include "libs.h"
#include "logger.h"
#include "rdconfig.h"

#define	S(x) (x)?(x):"(null)"


/**********************************************************************
 *
 *  Global configuration variables
 */

char *clientname=(char *)"";		/* Name of legal client system	*/
char *authcode=(char *)"";		/* Authentication code		*/
char *zmhstart=(char *)"";		/* Zone Mail Hour start		*/
char *zmhend=(char *)"";		/* Zone Mail Hour end		*/
int logtrans=0;				/* Show transactions in the log	*/
int logdebug=0;				/* Debug loglevel, 0..10	*/


/* Local */

char *configname=(char *)"";
static char *k,*v;
static int linecnt=0;


/* parser prototypes */

static int getstr(char**);
static int getlong(char**);


/* keyword table */

static struct _keytab {
	char *key;
	int (*prc)(char**);
	char** dest;
} keytab[] = {
	{(char *)"client",		getstr,		&clientname},
	{(char *)"authcode",		getstr,		&authcode},
	{(char *)"logtrans",		getlong,	(char**)&logtrans},
	{(char *)"logdebug",		getlong,	(char**)&logdebug},
	{(char *)"zmhs",		getstr,		&zmhstart},
	{(char *)"zmhe",		getstr,		&zmhend},
	{NULL,				NULL,		NULL}
};


char *xmalloc(size_t);
char *xmalloc(size_t size)
{
	char *tmp;

	tmp=malloc(size);
	if (!tmp) abort();
	
	return tmp;
}



char *xstrcpy(char *src)
{
	char	*tmp;

	if (src == NULL) return(NULL);
	tmp=xmalloc(strlen(src)+1);
	strcpy(tmp,src);
	return tmp;
}



char *xstrcat(char *src, char *add)
{
	char	*tmp;
	size_t	size = 0;

	if ((add == NULL) || (strlen(add) == 0))
		return src;
	if (src)
		size = strlen(src);
	size += strlen(add);
	tmp = xmalloc(size + 1);
	*tmp = '\0';
	if (src) {
		strcpy(tmp, src);
		free(src);
	}
	strcat(tmp, add);
	return tmp;
}



int readconfig(char *base)
{
	int err=0, i;
	FILE *fp;
	char buf[512], *p;

	sprintf(buf, "%s/etc/mbsed.conf", base);
	configname = buf;

	if ((fp=fopen(configname,"r")) == NULL) {
		mbselog((char *)"?", (char *)"$rdconfig: cannot open file \"%s\" ", S(configname));
		printf("PANIC: cannot open file: %s\n", configname);
		exit(1);
	}

	while (fgets(buf,sizeof(buf)-1,fp)) {
		linecnt++;
		if (*(p=buf+strlen(buf)-1) != '\n') {
			mbselog((char *)"?", (char *)"mbsed.conf(%d): %s - line too long", linecnt, buf);
			while (fgets(buf,sizeof(buf)-1,fp) &&
				(*(p=buf+strlen(buf)-1) != '\n'));
			continue;
		}

		*p--='\0';
		while ((p >= buf) && isspace(*p)) 
			*p--='\0';
		k=buf;
		while (*k && isspace(*k)) 
			k++;
		p=k;
		while (*p && !isspace(*p)) 
			p++;
		*p++='\0';
		v=p;
		while (*v && isspace(*v)) 
			v++;

		/*
		 * Skip empty and comment lines 
		 */
		if ((*k == '\0') || (*k == '#')) 
			continue;

		for (i=0;keytab[i].key;i++)
			if (strcasecmp(k,keytab[i].key) == 0) 
				break;

		if (keytab[i].key == NULL) {
			mbselog((char *)"?", (char *)"mbsed.conf(%d): %s %s - unknown keyword", 
				linecnt, S(k), S(v));
			err++;
		} else {
			keytab[i].prc(keytab[i].dest);
		}
	}
	fclose(fp);
	if (err != 0) {
		printf("Configuration error(s), see logfile for details\n");
		exit(1);
	}
	if (logdebug >= 4) {
		mbselog((char *)"#", (char *)"Client name      : %s", clientname);
		mbselog((char *)"#", (char *)"Authisation code : %s", authcode);
		mbselog((char *)"#", (char *)"Log transactions : %d", logtrans);
		mbselog((char *)"#", (char *)"Debug level      : %d", logdebug);
		mbselog((char *)"#", (char *)"Zone Mail Hour   : %s - %s", zmhstart, zmhend);
	}
	return 0;
}




/************************************************************************
 *
 *  Parsers
 */


/* Read a string from the config file until eol. */

static int getstr(char **);
static int getstr(char **dest)
{
	*dest=xstrcpy(v);
	return 0;
}



static int getlong(char **);
static int getlong(char **dest)
{
	if (strspn(v,"0123456789") != strlen(v)) 
		mbselog((char *)"?", (char *)"mbsed.conf(%d): %s %s - bad numeric", linecnt, S(k), S(v));
	else 
		*((long*)dest)=atol(v);
	return 0;
}


