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-- |
0 | 1 |
/** |
2 |
* $Id: config.c 51 2008-01-10 00:19:39Z mbroeker $ |
|
3 |
* $URL: http://localhost/svn/c/mcbot/trunk/src/config.c $ |
|
4 |
* |
|
5 |
*/ |
|
6 |
||
7 |
#include <stdio.h> |
|
8 |
#include <stdlib.h> |
|
9 |
#include <string.h> |
|
10 |
||
13
d3554afaa768
mcbot-0.94-5 Changelog
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
11
diff
changeset
|
11 |
#include <compat.h> |
0 | 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 | 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 | 19 |
const |
20 |
char *CONFIG_OPTIONS[] = { |
|
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 | 23 |
}; |
24 |
||
25 |
int config (UC * uc, char *fname) |
|
26 |
{ |
|
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 | 30 |
char *token; |
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 | 33 |
|
34 |
if ((f = fopen (fname, "r")) == NULL) |
|
35 |
return -1; |
|
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 | 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 | 41 |
|
42 |
/* |
|
43 |
* We can easily provide default values ... |
|
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 | 46 |
uc->port = 6667; |
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 | 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 | 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 | 55 |
token++; |
56 |
||
57 |
token = strtok (token, ":"); |
|
58 |
||
59 |
if (token != NULL) { |
|
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 | 66 |
} |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
67 |
map++; |
0 | 68 |
} |
69 |
} |
|
70 |
} |
|
71 |
||
72 |
if (fclose (f) != 0) |
|
73 |
return -1; |
|
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 | 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 | 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 | 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 | 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 | 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 | 102 |
break; |
103 |
} |
|
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
104 |
map++; |
0 | 105 |
} |
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 | 111 |
return -2; |
112 |
||
113 |
return 0; |
|
114 |
} |