]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
Merged from trunk
[thirdparty/squid.git] / src / Parsing.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 03 Configuration File Parsing
5 * AUTHOR: Harvest Derived
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "cache_cf.h"
37 #include "compat/strtoll.h"
38 #include "Parsing.h"
39 #include "protos.h"
40
41 /*
42 * These functions is the same as atoi/l/f, except that they check for errors
43 */
44
45 double
46 xatof(const char *token)
47 {
48 char *end;
49 double ret = strtod(token, &end);
50
51 if (ret == 0 && end == token)
52 self_destruct();
53
54 return ret;
55 }
56
57 int
58 xatoi(const char *token)
59 {
60 return xatol(token);
61 }
62
63 long
64 xatol(const char *token)
65 {
66 char *end;
67 long ret = strtol(token, &end, 10);
68
69 if (end == token || *end)
70 self_destruct();
71
72 return ret;
73 }
74
75 unsigned short
76 xatos(const char *token)
77 {
78 long port = xatol(token);
79
80 if (port & ~0xFFFF)
81 self_destruct();
82
83 return port;
84 }
85
86 int64_t
87 GetInteger64(void)
88 {
89 char *token = strtok(NULL, w_space);
90 int i;
91
92 if (token == NULL)
93 self_destruct();
94
95 i = strtoll(token, NULL, 10);
96
97 return i;
98 }
99
100 int
101 GetInteger(void)
102 {
103 char *token = strtok(NULL, w_space);
104 int i;
105
106 if (token == NULL)
107 self_destruct();
108
109 // %i honors 0 and 0x prefixes, which are important for things like umask
110 if (sscanf(token, "%i", &i) != 1)
111 self_destruct();
112
113 return i;
114 }
115
116 unsigned short
117 GetShort(void)
118 {
119 char *token = strtok(NULL, w_space);
120
121 if (token == NULL)
122 self_destruct();
123
124 return xatos(token);
125 }
126
127 bool
128 StringToInt(const char *s, int &result, const char **p, int base)
129 {
130 if (s) {
131 char *ptr = 0;
132 const int h = (int) strtol(s, &ptr, base);
133
134 if (ptr != s && ptr) {
135 result = h;
136
137 if (p)
138 *p = ptr;
139
140 return true;
141 }
142 }
143
144 return false;
145 }
146
147 bool
148 StringToInt64(const char *s, int64_t &result, const char **p, int base)
149 {
150 if (s) {
151 char *ptr = 0;
152 const int64_t h = (int64_t) strtoll(s, &ptr, base);
153
154 if (ptr != s && ptr) {
155 result = h;
156
157 if (p)
158 *p = ptr;
159
160 return true;
161 }
162 }
163
164 return false;
165 }
166
167 bool
168 GetHostWithPort(char *token, Ip::Address *ipa)
169 {
170 char *t;
171 char *host;
172 char *tmp;
173 unsigned short port;
174
175 host = NULL;
176 port = 0;
177
178 if (*token == '[') {
179 /* [host]:port */
180 host = token + 1;
181 t = strchr(host, ']');
182 if (!t)
183 return false;
184 *t = '\0';
185 ++t;
186 if (*t != ':')
187 return false;
188 port = xatos(t + 1);
189 } else if ((t = strchr(token, ':'))) {
190 /* host:port */
191 host = token;
192 *t = '\0';
193 port = xatos(t + 1);
194
195 if (0 == port)
196 return false;
197 } else if ((port = strtol(token, &tmp, 10)), !*tmp) {
198 /* port */
199 } else {
200 host = token;
201 port = 0;
202 }
203
204 if (NULL == host)
205 ipa->SetAnyAddr();
206 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
207 (void) 0;
208 else
209 return false;
210
211 /* port MUST be set after the IPA lookup/conversion is performed. */
212 ipa->SetPort(port);
213
214 return true;
215 }