/****************************************************************
 * This software is Copyright (C) 1986-1998 by                  *
 *                                                              *
 * F6FBB - Jean-Paul ROUBELAT,  jpr@f6fbb.org                   *
 * 6, rue George Sand                                           *
 * 31120 - Roquettes - France                                   *
 *                                                              *
 * License to copy and use this software is granted for         *
 * non-commercial use provided that it is identified as         *
 *                                                              *
 * "FBB packet-radio BBS software by Jean-Paul ROUBELAT, F6FBB" *
 *                                                              *
 * in all material mentioning or referencing this software      *
 * or this function.                                            *
 *                                                              *
 * These notices must be retained in any copies of any part of  *
 * this documentation and/or software.                          *
 *                                                              *
 * Parts of code have been taken from many other softwares.     *
 * Thanks for the help.                                         *
 ****************************************************************/

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

#define FBBCONF "/etc/ax25/fbb.conf"

/* a keyword has a maximum of 4 characters */

typedef struct conf_list {
	char key[5];
	char *value;
	struct conf_list *next;
} ConfList;

static ConfList *conf_head = NULL;

/* Creates a list of keyword = value */

int read_fbb_conf(char *pathname)
{
	int i;
	int len;
	FILE *fp;
	char *ptr;
	char *scan;
	char line[256];
	ConfList *conf;
	
	if (pathname == NULL)
		pathname = FBBCONF;
		
	if ((fp = fopen (pathname, "r")) == NULL)
		return 1;
		
	while (fgets (line, sizeof(line), fp))
	{
		scan = line;

		/* Eats spaces */
		while (isspace(*scan))
			++scan;

		if (*scan == '#' || *scan == '\0')
			continue;

		/* remove the leading \n */
		len = strlen(scan);
		if (len && (scan[len-1] == '\n'))
			scan[len-1] = '\0';
				
		/* allocate a new structure */
		conf = malloc(sizeof(ConfList));
		if (conf == NULL)
			return 2;	/* Not enough memory */
			
		conf->value = NULL;
		conf->next  = conf_head;
		conf_head   = conf;
		
		/* extract the keyword */
		i = 0;	
		ptr = conf->key;
		while (isgraph(*scan))
		{
			if (i++ < 4)
			{
				*ptr++ = *scan;
			}
			++scan;
			if (*scan == '=')
				break;
		}
		*ptr = '\0';

		/* finds the '=' */
		scan = strchr(scan, '=');
		
		/* Copy the value */
		if (scan)
		{
			++scan;

			while (isspace(*scan))
				++scan;

			/* if the first character is '"' then string ends with '"' */
			if (*scan == '"')
			{
				++scan;
				ptr = strrchr(scan, '"');
				if (ptr)
					*ptr = '\0';
			}
			conf->value = strdup(scan);
		}
		else
		{
			conf->value = strdup("");
		}
	
	}
	
	fclose(fp);
	return 0;
}

/* returns the value of a keyword. If unknown return NULL */

char *find_fbb_conf(char *key, int next)
{
	static	ConfList *conf_next = NULL;
	ConfList *conf = conf_head;
	
	if (next)
		conf = conf_next;
	
	while (conf)
	{
		if (strncasecmp(key, conf->key, 4) == 0)
		{
			conf_next = conf->next;
			return (conf->value);
		}
		conf = conf->next;
	}
	
	return NULL;
}

/* free the list of keywords */

void free_fbb_conf(void)
{
	ConfList *conf;
	
	while (conf_head)
	{
		conf = conf_head;
		conf_head = conf_head->next;
		
		free(conf->value);
		free(conf);
	}
}
