cfgfile.c 6.02 KB
Newer Older
pdw's avatar
pdw committed
1 2 3 4 5 6 7 8
/*
 * cfgfile.c:
 *
 * Copyright (c) 2003 DecisionSoft Ltd.
 *
 */

#include <stdio.h>
chris's avatar
chris committed
9
#include <string.h>
pdw's avatar
pdw committed
10 11 12 13 14 15 16 17 18 19 20 21 22
#include <errno.h>

#include "stringmap.h"
#include "iftop.h"
#include "options.h"
#include "cfgfile.h"

#define CONFIG_TYPE_STRING 0
#define CONFIG_TYPE_BOOL   1
#define CONFIG_TYPE_INT    2

#define MAX_CONFIG_LINE     2048

pdw's avatar
pdw committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
char * config_directives[] = {
	"interface", 
	"dns-resolution",
	"port-resolution",
	"filter-code",
	"show-bars", 
	"promiscuous",
	"hide-source",
	"hide-destination",
	"use-bytes", 
	"sort", 
	"line-display", 
	"show-totals", 
	"log-scale", 
	"max-bandwidth",
	"net-filter", 
39 40
	"net-filter6", 
        "link-local",
pdw's avatar
pdw committed
41 42
	"port-display", 
	NULL
pdw's avatar
pdw committed
43 44 45 46 47 48 49
};

stringmap config;

extern options_t options ;

int is_cfgdirective_valid(const char *s) {
50 51 52
    int t;
    for (t = 0; config_directives[t] != NULL; t++)
       if (strcmp(s, config_directives[t]) == 0) return 1;
pdw's avatar
pdw committed
53 54 55 56 57 58 59 60 61 62 63 64 65
    return 0;
}

int config_init() {
    config = stringmap_new();
    return config != NULL;
}

/* read_config_file:
 * Read a configuration file consisting of key: value tuples, returning a
 * stringmap of the results. Prints errors to stderr, rather than using
 * syslog, since this file is called at program startup. Returns 1 on success
 * or 0 on failure. */
66
int read_config_file(const char *f, int whinge) {
pdw's avatar
pdw committed
67 68 69 70 71 72 73 74 75
    int ret = 0;
    FILE *fp;
    char *line;
    int i = 1;

    line = xmalloc(MAX_CONFIG_LINE);

    fp = fopen(f, "rt");
    if (!fp) {
76
        if(whinge) fprintf(stderr, "%s: %s\n", f, strerror(errno)); 
pdw's avatar
pdw committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        goto fail;
    }

    while (fgets(line, MAX_CONFIG_LINE, fp)) {
        char *key, *value, *r;

        for (r = line + strlen(line) - 1; r > line && *r == '\n'; *(r--) = 0);

        /* Get continuation lines. Ugly. */
        while (*(line + strlen(line) - 1) == '\\') {
            if (!fgets(line + strlen(line) - 1, MAX_CONFIG_LINE - strlen(line), fp))
                break;
            for (r = line + strlen(line) - 1; r > line && *r == '\n'; *(r--) = 0);
        }

        /* Strip comment. */
        key = strpbrk(line, "#\n");
        if (key) *key = 0;

        /*    foo  : bar baz quux
         * key^    ^value          */
        key = line + strspn(line, " \t");
        value = strchr(line, ':');

        if (value) {
            /*    foo  : bar baz quux
             * key^  ^r ^value         */
            ++value;

            r = key + strcspn(key, " \t:");
            if (r != key) {
                item *I;
                *r = 0;

                /*    foo\0: bar baz quux
                 * key^      ^value      ^r */
                value += strspn(value, " \t");
                r = value + strlen(value) - 1;
                while (strchr(" \t", *r) && r > value) --r;
                *(r + 1) = 0;

                /* (Removed check for zero length value.) */

                /* Check that this is a valid key. */
                if (!is_cfgdirective_valid(key))
                    fprintf(stderr, "%s:%d: warning: unknown directive \"%s\"\n", f, i, key);
                else if ((I = stringmap_insert(config, key, item_ptr(xstrdup(value)))))
                    /* Don't warn of repeated directives, because they
                     * may have been specified via the command line
                     * Previous option takes precedence.
                     */
                    fprintf(stderr, "%s:%d: warning: repeated directive \"%s\"\n", f, i, key);
            }
        }

        memset(line, 0, MAX_CONFIG_LINE); /* security paranoia */

        ++i;
    }

    ret = 1;

fail:
    if (fp) fclose(fp);
    if (line) xfree(line);

    return ret;
}

int config_get_int(const char *directive, int *value) {
    stringmap S;
    char *s, *t;

    if (!value) return -1;

    S = stringmap_find(config, directive);
    if (!S) return 0;

    s = (char*)S->d.v;
    if (!*s) return -1;
    errno = 0;
    *value = strtol(s, &t, 10);
    if (*t) return -1;

    return errno == ERANGE ? -1 : 1;
}

/* config_get_float:
 * Get an integer value from a config string. Returns 1 on success, -1 on
 * failure, or 0 if no value was found. */
int config_get_float(const char *directive, float *value) {
chris's avatar
chris committed
168
    stringmap S;
pdw's avatar
pdw committed
169 170 171 172 173
    item *I;
    char *s, *t;

    if (!value) return -1;

chris's avatar
chris committed
174 175
    if (!(S = stringmap_find(config, directive)))
        return 0;
pdw's avatar
pdw committed
176

chris's avatar
chris committed
177
    s = (char*)S->d.v;
pdw's avatar
pdw committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    if (!*s) return -1;
    errno = 0;
    *value = strtod(s, &t);
    if (*t) return -1;

    return errno == ERANGE ? -1 : 1;
}

/* config_get_string;
 * Get a string value from the config file. Returns NULL if it is not
 * present. */
char *config_get_string(const char *directive) {
    stringmap S;

    S = stringmap_find(config, directive);
    if (S) return (char*)S->d.v;
    else return NULL;
}

/* config_get_bool:
 * Get a boolean value from the config file. Returns false if not present. */
int config_get_bool(const char *directive) {
    char *s;

    s = config_get_string(directive);
    if (s && (strcmp(s, "yes") == 0 || strcmp(s, "true") == 0))
        return 1;
    else
        return 0;
}

/* config_get_enum:
 * Get an enumeration value from the config file. Returns false if not 
 * present or an invalid value is found. */
int config_get_enum(const char *directive, config_enumeration_type *enumeration, int *value) {
    char *s;
    config_enumeration_type *t;
    s = config_get_string(directive);
    if(s) {
        for(t = enumeration; t->name; t++) {
            if(strcmp(s,t->name) == 0) {
                *value = t->value;
                return 1;
            }
        }
        fprintf(stderr,"Invalid enumeration value \"%s\" for directive \"%s\"\n", s, directive);
    }
    return 0;
}

/* config_set_string; Sets a value in the config, possibly overriding
 * an existing value
 */
void config_set_string(const char *directive, const char* s) {
    stringmap S;

    S = stringmap_find(config, directive);
235 236 237 238 239 240 241
    if (S) {
      xfree(S->d.v);
      S->d = item_ptr(xstrdup(s));
    }
    else {
      stringmap_insert(config, directive, item_ptr(xstrdup(s)));
    }
pdw's avatar
pdw committed
242 243
}

244
int read_config(char *file, int whinge_on_error) {
pdw's avatar
pdw committed
245 246
    void* o;

247
    return read_config_file(file, whinge_on_error);
pdw's avatar
pdw committed
248
}