author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 25 Dec 2008 19:55:07 +0100 | |
changeset 31 | bbcb8a3366b4 |
parent 18 | 4435146391ae |
child 33 | 56571d34d754 |
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> |
13 |
||
14 |
const |
|
15 |
char *CONFIG_OPTIONS[] = { |
|
16 |
"NICK", "PASSWORD", "SERVER", "PORT", |
|
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
17 |
"CHANNEL", "TOPIC", NULL |
0 | 18 |
}; |
19 |
||
20 |
int config (UC * uc, char *fname) |
|
21 |
{ |
|
22 |
FILE *f; |
|
23 |
char buffer[513]; |
|
10
311ea5fa60dd
Code cleanups and debian changelogs
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
6
diff
changeset
|
24 |
char **line; |
0 | 25 |
char *token; |
26 |
char *value; |
|
27 |
int i = 0; |
|
28 |
||
29 |
if ((f = fopen (fname, "r")) == NULL) |
|
30 |
return -1; |
|
31 |
||
32 |
while (CONFIG_OPTIONS[i] != NULL) |
|
33 |
i++; |
|
34 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
35 |
line = calloc ((size_t) (i + 1), sizeof (char *)); |
0 | 36 |
|
37 |
/* |
|
38 |
* We can easily provide default values ... |
|
39 |
*/ |
|
6
7eb12be31bb5
Various Bug Fixes including
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
40 |
uc->nick = uc->pass = uc->server = uc->channel = uc->topic = NULL; |
0 | 41 |
uc->port = 6667; |
42 |
||
11
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
43 |
while (!feof (f)) { |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
44 |
*buffer = '\0'; |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
45 |
(void)fgets (buffer, 512, f); |
a769385a59c6
Many Memory Leaks in config.c
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
10
diff
changeset
|
46 |
token = buffer; |
0 | 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 | 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 | 51 |
token++; |
52 |
||
53 |
token = strtok (token, ":"); |
|
54 |
||
55 |
if (token != NULL) { |
|
56 |
value = strtok (NULL, "\n"); |
|
57 |
i = 0; |
|
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 | 62 |
} |
63 |
i++; |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
if (fclose (f) != 0) |
|
69 |
return -1; |
|
70 |
||
71 |
i = 0; |
|
72 |
while (CONFIG_OPTIONS[i] != NULL) { |
|
73 |
if (line[i] != NULL) |
|
74 |
switch (i) { |
|
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 | 78 |
break; |
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 | 82 |
break; |
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 | 86 |
break; |
87 |
case 3: /* PORT */ |
|
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 | 90 |
break; |
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 | 94 |
break; |
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 | 98 |
break; |
99 |
} |
|
100 |
i++; |
|
101 |
} |
|
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 | 107 |
return -2; |
108 |
||
109 |
return 0; |
|
110 |
} |