Mercurial > sm-archive
annotate src/context.cpp @ 19:b24369330483 stable-1-0-7
Fedora 9 compile and const correctness.
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 12 Jun 2008 18:17:33 -0700 |
parents | 8ebecad6530f |
children | 09564d4acd9e |
rev | line source |
---|---|
0 | 1 /* |
2 | |
13 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
0 | 6 |
7 */ | |
8 | |
9 #include "includes.h" | |
10 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
11 const char *token_envfrom; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
12 const char *token_include; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
13 const char *token_lbrace; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
14 const char *token_rbrace; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
15 const char *token_rcptto; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
16 const char *token_remove; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
17 const char *token_semi; |
0 | 18 |
19 string_set all_strings; // owns all the strings, only modified by the config loader thread | |
20 const int maxlen = 1000; // used for snprintf buffers | |
21 | |
22 CONFIG::CONFIG() { | |
23 reference_count = 0; | |
24 generation = 0; | |
25 load_time = 0; | |
26 } | |
27 | |
28 | |
29 CONFIG::~CONFIG() { | |
30 } | |
31 | |
32 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
33 bool CONFIG::find(const char *needle, string_set &haystack) { |
13 | 34 string_set::iterator i = haystack.find(needle); |
35 if (i != haystack.end()) return true; // found user@domain.tld key | |
0 | 36 char *x = strchr(needle, '@'); |
37 if (x) { | |
38 x++; | |
39 i = haystack.find(x); | |
13 | 40 if (i != haystack.end()) return true; // found domain.tld key |
0 | 41 char y = *x; |
42 *x = '\0'; | |
43 i = haystack.find(needle); | |
44 *x = y; | |
13 | 45 if (i != haystack.end()) return true; // found user@ key |
46 } | |
47 return false; | |
48 } | |
49 | |
50 | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
51 const char *CONFIG::find(const char *needle, string_map &haystack) { |
13 | 52 string_map::iterator i = haystack.find(needle); |
53 if (i != haystack.end()) return (*i).second; // found user@domain.tld key | |
54 char *x = strchr(needle, '@'); | |
55 if (x) { | |
56 x++; | |
57 i = haystack.find(x); | |
58 if (i != haystack.end()) return (*i).second; // found domain.tld key | |
59 char y = *x; | |
60 *x = '\0'; | |
61 i = haystack.find(needle); | |
62 *x = y; | |
63 if (i != haystack.end()) return (*i).second; // found user@ key | |
0 | 64 } |
65 return NULL; | |
66 } | |
67 | |
68 | |
69 void CONFIG::dump() { | |
70 printf("rcpt_to {\n"); | |
71 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
72 const char *to = (*i).first; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
73 const char *target = (*i).second; |
13 | 74 if (!target) target = "\"\""; |
75 bool rem = find_remove(to); | |
76 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : ""); | |
0 | 77 } |
78 printf("};\n"); | |
79 printf("env_from {\n"); | |
80 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
81 const char *from = (*i).first; |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
82 const char *target = (*i).second; |
13 | 83 if (!target) target = "\"\""; |
84 printf(" %s \t %s;\n", from, target); | |
0 | 85 } |
86 printf("};\n"); | |
87 } | |
88 | |
89 | |
90 //////////////////////////////////////////////// | |
91 // helper to discard the strings held by a string_set | |
92 // | |
93 void discard(string_set &s) { | |
94 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
95 free((void*)*i); |
0 | 96 } |
97 s.clear(); | |
98 } | |
99 | |
100 | |
101 //////////////////////////////////////////////// | |
102 // helper to register a string in a string set | |
103 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
104 const char* register_string(string_set &s, const char *name) { |
0 | 105 string_set::iterator i = s.find(name); |
106 if (i != s.end()) return *i; | |
107 char *x = strdup(name); | |
108 s.insert(x); | |
109 return x; | |
110 } | |
111 | |
112 | |
113 //////////////////////////////////////////////// | |
114 // register a global string | |
115 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
116 const char* register_string(const char *name) { |
0 | 117 return register_string(all_strings, name); |
118 } | |
119 | |
120 | |
121 //////////////////////////////////////////////// | |
13 | 122 // clear all global strings, helper for valgrind checking |
123 // | |
124 void clear_strings() { | |
125 discard(all_strings); | |
126 } | |
127 | |
128 | |
129 //////////////////////////////////////////////// | |
0 | 130 // |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
131 bool tsa(TOKEN &tok, const char *token); |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
132 bool tsa(TOKEN &tok, const char *token) { |
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
133 const char *have = tok.next(); |
0 | 134 if (have == token) return true; |
135 tok.token_error(token, have); | |
136 return false; | |
137 } | |
138 | |
139 | |
140 //////////////////////////////////////////////// | |
141 // | |
142 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc); | |
143 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc) { | |
144 if (!tsa(tok, token_lbrace)) return false; | |
145 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
146 const char *have = tok.next(); |
0 | 147 if (!have) break; |
148 if (have == token_rbrace) break; | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
149 const char *target = tok.next(); |
13 | 150 dc.add_to(have, target); |
151 target = tok.next(); | |
152 if (target == token_remove) { | |
153 dc.add_remove(have); | |
154 target = tok.next(); | |
0 | 155 } |
13 | 156 if (target != token_semi) { |
157 tok.token_error(token_semi, target); | |
158 break; | |
0 | 159 } |
160 } | |
161 return tsa(tok, token_semi); | |
162 } | |
163 | |
164 | |
165 //////////////////////////////////////////////// | |
166 // | |
167 bool parse_env_from(TOKEN &tok, CONFIG &dc); | |
168 bool parse_env_from(TOKEN &tok, CONFIG &dc) { | |
169 if (!tsa(tok, token_lbrace)) return false; | |
170 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
171 const char *have = tok.next(); |
0 | 172 if (!have) break; |
173 if (have == token_rbrace) break; | |
174 if (have == token_semi) { | |
175 // optional separators | |
176 } | |
177 else { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
178 const char *target = tok.next(); |
0 | 179 dc.add_from(have, target); |
180 } | |
181 } | |
182 return tsa(tok, token_semi); | |
183 } | |
184 | |
185 | |
186 //////////////////////////////////////////////// | |
187 // parse a config file | |
188 // | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
189 bool load_conf(CONFIG &dc, const char *fn) { |
0 | 190 TOKEN tok(fn, &dc.config_files); |
191 while (true) { | |
19
b24369330483
Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
17
diff
changeset
|
192 const char *have = tok.next(); |
0 | 193 if (!have) break; |
194 if (have == token_envfrom) { | |
195 if (!parse_env_from(tok, dc)) { | |
196 tok.token_error("load_conf() failed to parse env_from"); | |
197 return false; | |
198 } | |
199 } | |
200 else if (have == token_rcptto) { | |
201 if (!parse_rcpt_to(tok, dc)) { | |
202 tok.token_error("load_conf() failed to parse rcpt_to"); | |
203 return false; | |
204 } | |
205 } | |
206 else { | |
3 | 207 tok.token_error("env_from/rcpt_to", have); |
0 | 208 return false; |
209 } | |
210 } | |
211 return true; | |
212 } | |
213 | |
214 | |
215 //////////////////////////////////////////////// | |
216 // init the tokens | |
217 // | |
218 void token_init() { | |
219 token_envfrom = register_string("env_from"); | |
3 | 220 token_include = register_string("include"); |
0 | 221 token_lbrace = register_string("{"); |
222 token_rbrace = register_string("}"); | |
223 token_rcptto = register_string("rcpt_to"); | |
13 | 224 token_remove = register_string("remove"); |
0 | 225 token_semi = register_string(";"); |
226 } |