Mercurial > sm-archive
comparison 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 |
comparison
equal
deleted
inserted
replaced
18:e1a028daceb9 | 19:b24369330483 |
---|---|
6 | 6 |
7 */ | 7 */ |
8 | 8 |
9 #include "includes.h" | 9 #include "includes.h" |
10 | 10 |
11 char *token_envfrom; | 11 const char *token_envfrom; |
12 char *token_include; | 12 const char *token_include; |
13 char *token_lbrace; | 13 const char *token_lbrace; |
14 char *token_rbrace; | 14 const char *token_rbrace; |
15 char *token_rcptto; | 15 const char *token_rcptto; |
16 char *token_remove; | 16 const char *token_remove; |
17 char *token_semi; | 17 const char *token_semi; |
18 | 18 |
19 string_set all_strings; // owns all the strings, only modified by the config loader thread | 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 | 20 const int maxlen = 1000; // used for snprintf buffers |
21 | 21 |
22 CONFIG::CONFIG() { | 22 CONFIG::CONFIG() { |
28 | 28 |
29 CONFIG::~CONFIG() { | 29 CONFIG::~CONFIG() { |
30 } | 30 } |
31 | 31 |
32 | 32 |
33 bool CONFIG::find(char *needle, string_set &haystack) { | 33 bool CONFIG::find(const char *needle, string_set &haystack) { |
34 string_set::iterator i = haystack.find(needle); | 34 string_set::iterator i = haystack.find(needle); |
35 if (i != haystack.end()) return true; // found user@domain.tld key | 35 if (i != haystack.end()) return true; // found user@domain.tld key |
36 char *x = strchr(needle, '@'); | 36 char *x = strchr(needle, '@'); |
37 if (x) { | 37 if (x) { |
38 x++; | 38 x++; |
46 } | 46 } |
47 return false; | 47 return false; |
48 } | 48 } |
49 | 49 |
50 | 50 |
51 char *CONFIG::find(char *needle, string_map &haystack) { | 51 const char *CONFIG::find(const char *needle, string_map &haystack) { |
52 string_map::iterator i = haystack.find(needle); | 52 string_map::iterator i = haystack.find(needle); |
53 if (i != haystack.end()) return (*i).second; // found user@domain.tld key | 53 if (i != haystack.end()) return (*i).second; // found user@domain.tld key |
54 char *x = strchr(needle, '@'); | 54 char *x = strchr(needle, '@'); |
55 if (x) { | 55 if (x) { |
56 x++; | 56 x++; |
67 | 67 |
68 | 68 |
69 void CONFIG::dump() { | 69 void CONFIG::dump() { |
70 printf("rcpt_to {\n"); | 70 printf("rcpt_to {\n"); |
71 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) { | 71 for (string_map::iterator i=rcpt_to.begin(); i!=rcpt_to.end(); i++) { |
72 char *to = (*i).first; | 72 const char *to = (*i).first; |
73 char *target = (*i).second; | 73 const char *target = (*i).second; |
74 if (!target) target = "\"\""; | 74 if (!target) target = "\"\""; |
75 bool rem = find_remove(to); | 75 bool rem = find_remove(to); |
76 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : ""); | 76 printf(" %s \t %s%s;\n", to, target, (rem) ? " remove" : ""); |
77 } | 77 } |
78 printf("};\n"); | 78 printf("};\n"); |
79 printf("env_from {\n"); | 79 printf("env_from {\n"); |
80 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | 80 for (string_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { |
81 char *from = (*i).first; | 81 const char *from = (*i).first; |
82 char *target = (*i).second; | 82 const char *target = (*i).second; |
83 if (!target) target = "\"\""; | 83 if (!target) target = "\"\""; |
84 printf(" %s \t %s;\n", from, target); | 84 printf(" %s \t %s;\n", from, target); |
85 } | 85 } |
86 printf("};\n"); | 86 printf("};\n"); |
87 } | 87 } |
90 //////////////////////////////////////////////// | 90 //////////////////////////////////////////////// |
91 // helper to discard the strings held by a string_set | 91 // helper to discard the strings held by a string_set |
92 // | 92 // |
93 void discard(string_set &s) { | 93 void discard(string_set &s) { |
94 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { | 94 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { |
95 free(*i); | 95 free((void*)*i); |
96 } | 96 } |
97 s.clear(); | 97 s.clear(); |
98 } | 98 } |
99 | 99 |
100 | 100 |
101 //////////////////////////////////////////////// | 101 //////////////////////////////////////////////// |
102 // helper to register a string in a string set | 102 // helper to register a string in a string set |
103 // | 103 // |
104 char* register_string(string_set &s, char *name) { | 104 const char* register_string(string_set &s, const char *name) { |
105 string_set::iterator i = s.find(name); | 105 string_set::iterator i = s.find(name); |
106 if (i != s.end()) return *i; | 106 if (i != s.end()) return *i; |
107 char *x = strdup(name); | 107 char *x = strdup(name); |
108 s.insert(x); | 108 s.insert(x); |
109 return x; | 109 return x; |
111 | 111 |
112 | 112 |
113 //////////////////////////////////////////////// | 113 //////////////////////////////////////////////// |
114 // register a global string | 114 // register a global string |
115 // | 115 // |
116 char* register_string(char *name) { | 116 const char* register_string(const char *name) { |
117 return register_string(all_strings, name); | 117 return register_string(all_strings, name); |
118 } | 118 } |
119 | 119 |
120 | 120 |
121 //////////////////////////////////////////////// | 121 //////////////////////////////////////////////// |
126 } | 126 } |
127 | 127 |
128 | 128 |
129 //////////////////////////////////////////////// | 129 //////////////////////////////////////////////// |
130 // | 130 // |
131 bool tsa(TOKEN &tok, char *token); | 131 bool tsa(TOKEN &tok, const char *token); |
132 bool tsa(TOKEN &tok, char *token) { | 132 bool tsa(TOKEN &tok, const char *token) { |
133 char *have = tok.next(); | 133 const char *have = tok.next(); |
134 if (have == token) return true; | 134 if (have == token) return true; |
135 tok.token_error(token, have); | 135 tok.token_error(token, have); |
136 return false; | 136 return false; |
137 } | 137 } |
138 | 138 |
141 // | 141 // |
142 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc); | 142 bool parse_rcpt_to(TOKEN &tok, CONFIG &dc); |
143 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; | 144 if (!tsa(tok, token_lbrace)) return false; |
145 while (true) { | 145 while (true) { |
146 char *have = tok.next(); | 146 const char *have = tok.next(); |
147 if (!have) break; | 147 if (!have) break; |
148 if (have == token_rbrace) break; | 148 if (have == token_rbrace) break; |
149 char *target = tok.next(); | 149 const char *target = tok.next(); |
150 dc.add_to(have, target); | 150 dc.add_to(have, target); |
151 target = tok.next(); | 151 target = tok.next(); |
152 if (target == token_remove) { | 152 if (target == token_remove) { |
153 dc.add_remove(have); | 153 dc.add_remove(have); |
154 target = tok.next(); | 154 target = tok.next(); |
166 // | 166 // |
167 bool parse_env_from(TOKEN &tok, CONFIG &dc); | 167 bool parse_env_from(TOKEN &tok, CONFIG &dc); |
168 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; | 169 if (!tsa(tok, token_lbrace)) return false; |
170 while (true) { | 170 while (true) { |
171 char *have = tok.next(); | 171 const char *have = tok.next(); |
172 if (!have) break; | 172 if (!have) break; |
173 if (have == token_rbrace) break; | 173 if (have == token_rbrace) break; |
174 if (have == token_semi) { | 174 if (have == token_semi) { |
175 // optional separators | 175 // optional separators |
176 } | 176 } |
177 else { | 177 else { |
178 char *target = tok.next(); | 178 const char *target = tok.next(); |
179 dc.add_from(have, target); | 179 dc.add_from(have, target); |
180 } | 180 } |
181 } | 181 } |
182 return tsa(tok, token_semi); | 182 return tsa(tok, token_semi); |
183 } | 183 } |
184 | 184 |
185 | 185 |
186 //////////////////////////////////////////////// | 186 //////////////////////////////////////////////// |
187 // parse a config file | 187 // parse a config file |
188 // | 188 // |
189 bool load_conf(CONFIG &dc, char *fn) { | 189 bool load_conf(CONFIG &dc, const char *fn) { |
190 TOKEN tok(fn, &dc.config_files); | 190 TOKEN tok(fn, &dc.config_files); |
191 while (true) { | 191 while (true) { |
192 char *have = tok.next(); | 192 const char *have = tok.next(); |
193 if (!have) break; | 193 if (!have) break; |
194 if (have == token_envfrom) { | 194 if (have == token_envfrom) { |
195 if (!parse_env_from(tok, dc)) { | 195 if (!parse_env_from(tok, dc)) { |
196 tok.token_error("load_conf() failed to parse env_from"); | 196 tok.token_error("load_conf() failed to parse env_from"); |
197 return false; | 197 return false; |