src/config.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Sat, 03 Jan 2009 21:41:51 +0100
changeset 33 56571d34d754
parent 18 4435146391ae
child 36 6889aacd038e
permissions -rw-r--r--
safe buffers, a memory leak and cleanups sizeof(buffer) is always the right choice for a static buffer... The error handler must appear before the printf Useful make targets like deb and debclean Trivial changes like removing trailing white spaces 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
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
const
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
char *CONFIG_OPTIONS[] = {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
    "NICK", "PASSWORD", "SERVER", "PORT",
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    18
    "CHANNEL", "TOPIC", NULL
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
};
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
int config (UC * uc, char *fname)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
{
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
    FILE *f;
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    24
    char buffer[DEFAULT_BUF_SIZE];
10
311ea5fa60dd Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 6
diff changeset
    25
    char **line;
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    char *token;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
    char *value;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    int i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
    if ((f = fopen (fname, "r")) == NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
        return -1;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
    while (CONFIG_OPTIONS[i] != NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
        i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    36
    line = calloc ((size_t) (i + 1), sizeof (char *));
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
    /*
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
     * We can easily provide default values ...
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
     */
6
7eb12be31bb5 Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 0
diff changeset
    41
    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
    42
    uc->port = 6667;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    43
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    44
    while (!feof (f)) {
33
56571d34d754 safe buffers, a memory leak and cleanups
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 18
diff changeset
    45
        (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
    46
        token = buffer;
0
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 (*token == '\t')  /* Eat trailing tabs */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
            token++;
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    50
        while (*token == ' ')   /* Eat trailing whitespaces */
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
            token++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
        token = strtok (token, ":");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
        if (token != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
            value = strtok (NULL, "\n");
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
            i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
            while (CONFIG_OPTIONS[i] != NULL) {
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    59
                if (!strcmp (CONFIG_OPTIONS[i], token)) {
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    60
                    if (value)
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    61
                        line[i] = compat_strdup (value);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
                }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
                i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
        }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
    if (fclose (f) != 0)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
        return -1;
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
    i = 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    while (CONFIG_OPTIONS[i] != NULL) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
        if (line[i] != NULL)
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
            switch (i) {
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
            case 0:            /* NICK */
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    76
                uc->nick = compat_strdup (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    77
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
            case 1:            /* PASSWORD */
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    80
                uc->pass = compat_strdup (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    81
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
            case 2:            /* SERVER */
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    84
                uc->server = compat_strdup (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    85
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    87
            case 3:            /* PORT */
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    88
                uc->port = atoi (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    89
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
            case 4:            /* CHANNEL */
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    92
                uc->channel = compat_strdup (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    93
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    94
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    95
            case 5:            /* TOPIC */
13
d3554afaa768 mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 11
diff changeset
    96
                uc->topic = compat_strdup (line[i]);
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
    97
                free (line[i]);
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    98
                break;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    99
            }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   100
        i++;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   101
    }
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   102
11
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   103
    if (line != NULL)
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   104
        free (line);
a769385a59c6 Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 10
diff changeset
   105
18
4435146391ae LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 13
diff changeset
   106
    if (!(uc->nick && uc->server && uc->channel))
0
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   107
        return -2;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   108
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   109
    return 0;
586472add385 Initial Layout
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
   110
}