author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 18 Nov 2010 23:32:58 +0100 | |
changeset 60 | 67a32e9d95f2 |
parent 58 | 500a5ea7fcb8 |
child 62 | 2d6419d6d4ed |
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 |
|
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
40 |
if ((line = calloc ((size_t) (map + 1), sizeof (char *))) == NULL) { |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
41 |
fclose (f); |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
42 |
return -1; |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
43 |
} |
0 | 44 |
|
45 |
/* |
|
46 |
* We can easily provide default values ... |
|
47 |
*/ |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
48 |
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL; |
58
500a5ea7fcb8
IPv6 Support for mcbot
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
51
diff
changeset
|
49 |
uc->port = "6667"; |
0 | 50 |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
51 |
while (!feof (f)) { |
47
a689b6a8e6ed
fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
36
diff
changeset
|
52 |
if (fgets (buffer, sizeof (buffer), f) == NULL) |
a689b6a8e6ed
fgets may return a NULL pointer...
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
36
diff
changeset
|
53 |
break; |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
54 |
token = buffer; |
0 | 55 |
|
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
56 |
while (*token == '\t' || *token == ' ') /* Eat trailing tabs and whitespaces */ |
0 | 57 |
token++; |
58 |
||
59 |
token = strtok (token, ":"); |
|
60 |
||
61 |
if (token != NULL) { |
|
62 |
value = strtok (NULL, "\n"); |
|
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
63 |
map = 0; |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
64 |
while (CONFIG_OPTIONS[map] != NULL) { |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
65 |
if (!strcmp (CONFIG_OPTIONS[map], token)) { |
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
66 |
if (value) { |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
67 |
if (line[map] != '\0') |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
68 |
free (line[map]); |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
69 |
line[map] = compat_strdup (value); |
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
70 |
} |
0 | 71 |
} |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
72 |
map++; |
0 | 73 |
} |
74 |
} |
|
75 |
} |
|
76 |
||
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
77 |
map = 0; |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
78 |
while (CONFIG_OPTIONS[map] != NULL) { |
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
79 |
if (line[map] != '\0') |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
80 |
switch (map) { |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
81 |
case NICK: |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
82 |
uc->nick = compat_strdup (line[map]); |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
83 |
free (line[map]); |
0 | 84 |
break; |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
85 |
case PASSWORD: |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
86 |
uc->pass = compat_strdup (line[map]); |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
87 |
free (line[map]); |
0 | 88 |
break; |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
89 |
case SERVER: |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
90 |
uc->server = compat_strdup (line[map]); |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
91 |
free (line[map]); |
0 | 92 |
break; |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
93 |
case PORT: |
58
500a5ea7fcb8
IPv6 Support for mcbot
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
51
diff
changeset
|
94 |
uc->port = compat_strdup (line[map]); |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
95 |
free (line[map]); |
0 | 96 |
break; |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
97 |
case CHANNEL: |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
98 |
uc->channel = compat_strdup (line[map]); |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
99 |
free (line[map]); |
0 | 100 |
break; |
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
101 |
case TOPIC: |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
102 |
uc->topic = compat_strdup (line[map]); |
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
103 |
free (line[map]); |
0 | 104 |
break; |
105 |
} |
|
36
6889aacd038e
enumerated types for config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
33
diff
changeset
|
106 |
map++; |
0 | 107 |
} |
108 |
||
51
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
109 |
free (line); |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
110 |
|
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
111 |
if (fclose (f) != 0) |
5d9aed3948a6
code review, close and free all resources in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
47
diff
changeset
|
112 |
return -1; |
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
113 |
|
18
4435146391ae
LOGIN without Password added
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
13
diff
changeset
|
114 |
if (!(uc->nick && uc->server && uc->channel)) |
0 | 115 |
return -2; |
116 |
||
117 |
return 0; |
|
118 |
} |