OpenCOBOL 1.1pre-rel
Defines | Enumerations | Functions | Variables
config.c File Reference
#include "config.h"
#include "defaults.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "cobc.h"
#include "config.def"
Include dependency graph for config.c:

Go to the source code of this file.

Defines

#define CB_CONFIG_ANY(type, var, name)   type var;
#define CB_CONFIG_INT(var, name)   int var;
#define CB_CONFIG_STRING(var, name)   const char *var;
#define CB_CONFIG_BOOLEAN(var, name)   int var;
#define CB_CONFIG_SUPPORT(var, name)   enum cb_support var;
#define CB_CONFIG_ANY(type, var, name)   {ANY, name, &var, NULL},
#define CB_CONFIG_INT(var, name)   {INT, name, &var, NULL},
#define CB_CONFIG_STRING(var, name)   {STRING, name, &var, NULL},
#define CB_CONFIG_BOOLEAN(var, name)   {BOOLEAN, name, &var, NULL},
#define CB_CONFIG_SUPPORT(var, name)   {SUPPORT, name, &var, NULL},

Enumerations

enum  cb_config_type {
  ANY, INT, STRING, BOOLEAN,
  SUPPORT
}

Functions

static char * read_string (const char *text)
static void invalid_value (const char *fname, const int line, const char *name)
static void unsupported_value (const char *fname, const int line, const char *val)
int cb_load_std (const char *name)
int cb_load_conf (const char *fname, const int check_nodef, const int prefix_dir)

Variables

struct noreservenorestab = NULL
struct {
   enum cb_config_type   type
   const char *   name
   void *   var
   char *   val
config_table []

Define Documentation

#define CB_CONFIG_ANY (   type,
  var,
  name 
)    type var;

Definition at line 36 of file config.c.

#define CB_CONFIG_ANY (   type,
  var,
  name 
)    {ANY, name, &var, NULL},

Definition at line 36 of file config.c.

#define CB_CONFIG_BOOLEAN (   var,
  name 
)    int var;

Definition at line 39 of file config.c.

#define CB_CONFIG_BOOLEAN (   var,
  name 
)    {BOOLEAN, name, &var, NULL},

Definition at line 39 of file config.c.

#define CB_CONFIG_INT (   var,
  name 
)    int var;

Definition at line 37 of file config.c.

#define CB_CONFIG_INT (   var,
  name 
)    {INT, name, &var, NULL},

Definition at line 37 of file config.c.

#define CB_CONFIG_STRING (   var,
  name 
)    {STRING, name, &var, NULL},

Definition at line 38 of file config.c.

#define CB_CONFIG_STRING (   var,
  name 
)    const char *var;

Definition at line 38 of file config.c.

#define CB_CONFIG_SUPPORT (   var,
  name 
)    {SUPPORT, name, &var, NULL},

Definition at line 40 of file config.c.

#define CB_CONFIG_SUPPORT (   var,
  name 
)    enum cb_support var;

Definition at line 40 of file config.c.


Enumeration Type Documentation

Enumerator:
ANY 
INT 
STRING 
BOOLEAN 
SUPPORT 

Definition at line 43 of file config.c.

                    {
        ANY,
        INT,                    /* integer */
        STRING,                 /* "..." */
        BOOLEAN,                /* 'yes', 'no' */
        SUPPORT                 /* 'ok', 'archaic', 'obsolete',
                                   'skip', 'ignore', 'unconformable' */
};

Function Documentation

int cb_load_conf ( const char *  fname,
const int  check_nodef,
const int  prefix_dir 
)

Definition at line 112 of file config.c.

{
        char                    *s;
        char                    *e;
        const char              *name;
        const char              *val;
        void                    *var;
        FILE                    *fp;
        char                    *nores;
        struct noreserve        *noresptr;
        int                     i;
        int                     j;
        int                     ret;
        int                     saveret;
        int                     line;
        char                    buff[COB_SMALL_BUFF];

        /* initialize the config table */
        if (check_nodef) {
                for (i = 0; config_table[i].name; i++) {
                        config_table[i].val = NULL;
                }
        }

        if (prefix_dir) {
                snprintf (buff, COB_SMALL_MAX, "%s/%s", cob_config_dir, fname);
                name = buff;
        } else {
                name = fname;
        }
        /* open the config file */
        fp = fopen (name, "r");
        if (fp == NULL) {
                perror (name);
                return -1;
        }

        /* read the config file */
        ret = 0;
        line = 0;
        while (fgets (buff, COB_SMALL_BUFF, fp)) {
                line++;

                /* skip comments */
                if (buff[0] == '#') {
                        continue;
                }

                /* skip blank lines */
                for (s = buff; *s; s++) {
                        if (isgraph (*s)) {
                                break;
                        }
                }
                if (!*s) {
                        continue;
                }

                /* get the tag */
                s = strpbrk (buff, " \t:=");
                if (!s) {
                        fprintf (stderr, "%s:%d: invalid line\n", fname, line);
                        ret = -1;
                        continue;
                }
                *s = 0;

                /* find the entry */
                for (i = 0; config_table[i].name; i++) {
                        if (strcmp (buff, config_table[i].name) == 0) {
                                break;
                        }
                }
                if (!config_table[i].name) {
                        fprintf (stderr, "%s:%d: unknown tag '%s'\n", fname, line, buff);
                        ret = -1;
                        continue;
                }

                /* get the value */
                for (s++; *s && strchr (" \t:=", *s); s++) {
                        ;
                }
                e = s + strlen (s) - 1;
                for (; e >= s && strchr (" \t\r\n", *e); e--) {
                        ;
                }
                e[1] = 0;
                config_table[i].val = s;

                /* set the value */
                name = config_table[i].name;
                var = config_table[i].var;
                val = config_table[i].val;
                switch (config_table[i].type) {
                case ANY:
                        if (strcmp (name, "assign-clause") == 0) {
                                if (strcmp (val, "cobol2002") == 0) {
                                        unsupported_value (fname, line, val);
                                        ret = -1;
                                } else if (strcmp (val, "mf") == 0) {
                                        cb_assign_clause = CB_ASSIGN_MF;
                                } else if (strcmp (val, "ibm") == 0) {
                                        cb_assign_clause = CB_ASSIGN_IBM;
                                } else {
                                        invalid_value (fname, line, name);
                                        ret = -1;
                                }
                        } else if (strcmp (name, "binary-size") == 0) {
                                if (strcmp (val, "2-4-8") == 0) {
                                        cb_binary_size = CB_BINARY_SIZE_2_4_8;
                                } else if (strcmp (val, "1-2-4-8") == 0) {
                                        cb_binary_size = CB_BINARY_SIZE_1_2_4_8;
                                } else if (strcmp (val, "1--8") == 0) {
                                        cb_binary_size = CB_BINARY_SIZE_1__8;
                                } else {
                                        invalid_value (fname, line, name);
                                        ret = -1;
                                }
                        } else if (strcmp (name, "binary-byteorder") == 0) {
                                if (strcmp (val, "native") == 0) {
                                        cb_binary_byteorder = CB_BYTEORDER_NATIVE;
                                } else if (strcmp (val, "big-endian") == 0) {
                                        cb_binary_byteorder = CB_BYTEORDER_BIG_ENDIAN;
                                } else {
                                        invalid_value (fname, line, name);
                                        ret = -1;
                                }
                        }
                        break;
                case INT:
                        for (j = 0; val[j]; j++) {
                                if (!isdigit (val[j])) {
                                        invalid_value (fname, line, name);
                                        ret = -1;
                                        break;
                                }
                        }
                        *((int *)var) = atoi (val);
                        break;
                case STRING:
                        val = read_string (val);

                        if (strcmp (name, "include") == 0) {
                                /* include another conf file */
                                saveret = ret;
                                if (cb_load_conf (val, 0, 1) != 0) {
                                        return -1;
                                }
                                ret = saveret;
                        } else if (strcmp (name, "not-reserved") == 0) {
                                nores = read_string (val);
                                noresptr = cobc_malloc (sizeof (struct noreserve));
                                noresptr->noresword = cobc_malloc (strlen (nores) + 1);
                                strcpy (noresptr->noresword, nores);
                                noresptr->next = norestab;
                                norestab = noresptr;
                        } else {
                                *((const char **)var) = val;
                        }
                        break;
                case BOOLEAN:
                        if (strcmp (val, "yes") == 0) {
                                *((int *)var) = 1;
                        } else if (strcmp (val, "no") == 0) {
                                *((int *)var) = 0;
                        } else {
                                invalid_value (fname, line, name);
                                ret = -1;
                        }
                        break;
                case SUPPORT:
                        if (strcmp (val, "ok") == 0) {
                                *((enum cb_support *)var) = CB_OK;
                        } else if (strcmp (val, "warning") == 0) {
                                *((enum cb_support *)var) = CB_WARNING;
                        } else if (strcmp (val, "archaic") == 0) {
                                *((enum cb_support *)var) = CB_ARCHAIC;
                        } else if (strcmp (val, "obsolete") == 0) {
                                *((enum cb_support *)var) = CB_OBSOLETE;
                        } else if (strcmp (val, "skip") == 0) {
                                *((enum cb_support *)var) = CB_SKIP;
                        } else if (strcmp (val, "ignore") == 0) {
                                *((enum cb_support *)var) = CB_IGNORE;
                        } else if (strcmp (val, "error") == 0) {
                                *((enum cb_support *)var) = CB_ERROR;
                        } else if (strcmp (val, "unconformable") == 0) {
                                *((enum cb_support *)var) = CB_UNCONFORMABLE;
                        } else {
                                invalid_value (fname, line, name);
                                ret = -1;
                        }
                        break;
                default:
                        fprintf (stderr, _("%s:%d: invalid type for '%s'\n"),
                                 fname, line, name);
                        ret = -1;
                        break;
                }
        }
        fclose (fp);

        /* checks for no definition */
        if (check_nodef) {
                for (i = 2; config_table[i].name; i++) {
                        if (config_table[i].val == NULL) {
                                fprintf (stderr, "%s: no definition of '%s'\n",
                                         fname, config_table[i].name);
                                ret = -1;
                        }
                }
        }

        return ret;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int cb_load_std ( const char *  name)

Definition at line 106 of file config.c.

{
        return cb_load_conf (name, 1, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void invalid_value ( const char *  fname,
const int  line,
const char *  name 
) [static]

Definition at line 94 of file config.c.

{
        fprintf (stderr, _("%s:%d: invalid value for '%s'\n"), fname, line, name);
}

Here is the caller graph for this function:

static char* read_string ( const char *  text) [static]

Definition at line 77 of file config.c.

{
        char    *p;
        char    *s = strdup (text);

        if (*s == '\"') {
                s++;
        }
        for (p = s; *p; p++) {
                if (*p == '\"') {
                        *p = '\0';
                }
        }
        return s;
}

Here is the caller graph for this function:

static void unsupported_value ( const char *  fname,
const int  line,
const char *  val 
) [static]

Definition at line 100 of file config.c.

{
        fprintf (stderr, _("%s:%d: '%s' not supported\n"), fname, line, val);
}

Here is the caller graph for this function:


Variable Documentation

struct { ... } config_table[] [static]
const char* name

Definition at line 56 of file config.c.

struct noreserve* norestab = NULL

Definition at line 52 of file config.c.

Definition at line 55 of file config.c.

char* val

Definition at line 58 of file config.c.

void* var

Definition at line 57 of file config.c.

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines