pdirt/data/
pdirt/data/HELP/
pdirt/data/HELP/0/
pdirt/data/HELP/F/
pdirt/data/HELP/G/
pdirt/data/HELP/H/
pdirt/data/HELP/J/
pdirt/data/HELP/K/
pdirt/data/HELP/O/
pdirt/data/HELP/Q/
pdirt/data/HELP/R/
pdirt/data/HELP/U/
pdirt/data/HELP/V/
pdirt/data/HELP/Y/
pdirt/data/HELP/Z/
pdirt/data/MESSAGES/
pdirt/data/POWERINFO/
pdirt/data/WIZ_ZONES/
pdirt/drv/
pdirt/drv/bin/
pdirt/drv/compiler/converter/
pdirt/drv/compiler/libs/
pdirt/drv/compiler/scripts/
pdirt/drv/include/AberChat/
pdirt/drv/include/InterMud/
pdirt/drv/include/machine/
pdirt/drv/src/InterMud/
pdirt/drv/src/Players/
pdirt/drv/utils/UAFPort/
pdirt/drv/utils/dnsresolv/
pdirt/drv/utils/gdbm/
/*****************************************************************************
 ** Module     : config.c
 ** Author     : Peter Eussen
 ** Description: This module reads in the configuration file and gives the 
 **              parameters on request to other modules through the 
 **              request_xxxx_value functions.
 **              The configuration can identify string, boolean and integer
 **              values for an configuration option, so you can directly 
 **              request the integer value of an option if you know it is 
 **              supposed to be an integer valued config option.
 ***************************************************************************/
#define CONFIG_C
#include <strings.h>
#include <stdlib.h>
#include "gen.h"
#include "lexer.h"
#include "config.h"


static ConfigOpt *options = NULL;
static char      *true_or_false[] = { "False", "True" };

int table_lookup(char *id, char **table);

ConfigOpt *find_option(char *id)
{  ConfigOpt *p = options;

   while (p!= NULL)
   {   if (strcasecmp(p->id,id) == 0)
          return p;
       p = p->next;
   }
   return NULL;
}

void config_error(token_record *the_token)
{   extern theline;

    printf("Error in configuration file:\n"
           "  Could not understand line %d after word %s\n",theline,
	   the_token->the_string != NULL ? the_token->the_string : "<null>");
}

char *request_char_value(char *id)
{   ConfigOpt *o;

    if ((o = find_option(id)) != NULL)
    {   return o->str_val;
    }
    return NULL;
}

Boolean request_bool_value(char *id)
{   ConfigOpt *o;

    if ((o = find_option(id)) != NULL)
       return o->bool_val;

    return False;
}

void show_config(void)
{  ConfigOpt *o = options;

   while (o != NULL)
   {   printf("%s - %s\n",o->id, o->str_val);
       o = o->next;
   }
}

int request_int_value(char *id)
{  ConfigOpt *o;

   if ((o = find_option(id)) != NULL)
       return o->int_val;

    return -32000;
}

int read_configuration(char *filename)
{   FILE *fp;
    token_record *the_token;
    ConfigOpt *o;

    fp = xfopen(filename);
    if (fp == NULL)
    {   printf("Can't open configuration file %s\n",filename);
        return -1;
    }

    printf("Reading Configuration from file %s\n",filename);
    the_token = get_token(fp,'\n',False);
    while (the_token->token_type != -1)
    {   if (the_token->token_type < 0)
        {   fclose(fp);
            config_error(the_token);
            return the_token->token_type;
        }

        if (the_token->token_type == T_POUND)
        {   (void)next_line(fp);
            the_token = get_token(fp,'\n',False);
            continue;
        }
   
        o = memAlloc(ConfigOpt,1,REC_CHAR);
        o->id = memAlloc(char,strlen(the_token->the_string)+1,REC_CONFIG);
        strcpy(o->id,the_token->the_string);

        the_token = get_token(fp,'\n',False);
        if (the_token->token_type < 0)
        {  config_error(the_token);
           return the_token->token_type;
        }

        if (the_token->token_type == T_EQUALS)
        {   the_token = get_token(fp,'\n',False);

            if (the_token->token_type < 0)
            {   config_error(the_token);
                return the_token->token_type;
            }

            if (the_token->token_type == T_QUOTE || the_token->token_type == T_OTHER)
            {   token_record *t = the_token;

                the_token = get_token(fp,t->the_string[0],False);
            }

            switch (the_token->token_type) {
            case T_BOOLEAN:
			o->bool_val = table_lookup(the_token->the_string,true_or_false);
			break;
            case T_NUMERICAL:
			o->int_val = atoi(the_token->the_string);
			break;
            default:
			break;
            }
            o->str_val = memAlloc(char,strlen(the_token->the_string) +1,REC_CHAR);
            strcpy(o->str_val,the_token->the_string);

            the_token = get_token(fp,'\n',False);
        }
        else
        {  o->bool_val = True;
           o->str_val = memAlloc(char,strlen(true_or_false[1]) + 1,REC_CHAR);
           strcpy(o->str_val,true_or_false[1]);
           o->int_val = 1;
        }

        o->next = options;
        options = o;
    }
    fclose(fp);
    return 0;
}