src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 28 May 2009 14:08:06 +0200
changeset 36 6889aacd038e
parent 33 56571d34d754
child 47 a689b6a8e6ed
permissions -rw-r--r--
enumerated types for config.c The config section gets an enumerated type for constant values. This makes the code more readable and less error prone. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 *  $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 *
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
 */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdio.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <stdlib.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <string.h>
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    11
#include <compat.h>
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
#include <config.h>
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    13
#include "common.h"
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    15
enum config_map {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    16
    NICK, PASSWORD, SERVER, PORT, CHANNEL, TOPIC,
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    17
};
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    18
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
const
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
char *CONFIG_OPTIONS[] = {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
    "NICK", "PASSWORD", "SERVER", "PORT",
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    22
    "CHANNEL", "TOPIC", NULL
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
};
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
int config (UC * uc, char *fname)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    FILE *f;
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    28
    char buffer[DEFAULT_BUF_SIZE];
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
    29
    char **line;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    char *token;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    char *value;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    32
    enum config_map map = 0;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
    if ((f = fopen (fname, "r")) == NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    37
    while (CONFIG_OPTIONS[map] != NULL)
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    38
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    40
    line = calloc ((size_t) (map + 1), sizeof (char *));
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
     * We can easily provide default values ...
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
     */
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    45
    uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
    uc->port = 6667;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    48
    while (!feof (f)) {
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    49
        (void)fgets (buffer, sizeof (buffer), f);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    50
        token = buffer;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    52
        while (*token == '\t')  /* Eat trailing tabs */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
            token++;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    54
        while (*token == ' ')   /* Eat trailing whitespaces */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
            token++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
        token = strtok (token, ":");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        if (token != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
            value = strtok (NULL, "\n");
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    61
            map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    62
            while (CONFIG_OPTIONS[map] != NULL) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    63
                if (!strcmp (CONFIG_OPTIONS[map], token)) {
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    64
                    if (value)
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    65
                        line[map] = compat_strdup (value);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
                }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    67
                map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    if (fclose (f) != 0)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    75
    map = 0;
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    76
    while (CONFIG_OPTIONS[map] != NULL) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    77
        if (line[map] != NULL)
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    78
            switch (map) {
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    79
            case NICK:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    80
                uc->nick = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    81
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    83
            case PASSWORD:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    84
                uc->pass = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    85
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    87
            case SERVER:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    88
                uc->server = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    89
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    91
            case PORT:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    92
                uc->port = atoi (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    93
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    95
            case CHANNEL:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    96
                uc->channel = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    97
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
                break;
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
    99
            case TOPIC:
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   100
                uc->topic = compat_strdup (line[map]);
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   101
                free (line[map]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   103
            }
36
6889aacd038e enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 33
diff changeset
   104
        map++;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   105
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   106
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   107
    if (line != NULL)
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   108
        free (line);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   109
18
4435146391ae LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 13
diff changeset
   110
    if (!(uc->nick && uc->server && uc->channel))
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   111
        return -2;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   112
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   113
    return 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   114
}