]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
Removed squid-old.h
[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 "compat/strtoll.h"
37 #include "Parsing.h"
38 #include "protos.h"
39
40 /*
41 * These functions is the same as atoi/l/f, except that they check for errors
42 */
43
44 double
45 xatof(const char *token)
46 {
47 char *end;
48 double ret = strtod(token, &end);
49
50 if (ret == 0 && end == token)
51 self_destruct();
52
53 return ret;
54 }
55
56 int
57 xatoi(const char *token)
58 {
59 return xatol(token);
60 }
61
62 long
63 xatol(const char *token)
64 {
65 char *end;
66 long ret = strtol(token, &end, 10);
67
68 if (end == token || *end)
69 self_destruct();
70
71 return ret;
72 }
73
74 unsigned short
75 xatos(const char *token)
76 {
77 long port = xatol(token);
78
79 if (port & ~0xFFFF)
80 self_destruct();
81
82 return port;
83 }
84
85 int64_t
86 GetInteger64(void)
87 {
88 char *token = strtok(NULL, w_space);
89 int i;
90
91 if (token == NULL)
92 self_destruct();
93
94 i = strtoll(token, NULL, 10);
95
96 return i;
97 }
98
99 int
100 GetInteger(void)
101 {
102 char *token = strtok(NULL, w_space);
103 int i;
104
105 if (token == NULL)
106 self_destruct();
107
108 // %i honors 0 and 0x prefixes, which are important for things like umask
109 if (sscanf(token, "%i", &i) != 1)
110 self_destruct();
111
112 return i;
113 }
114
115 unsigned short
116 GetShort(void)
117 {
118 char *token = strtok(NULL, w_space);
119
120 if (token == NULL)
121 self_destruct();
122
123 return xatos(token);
124 }
125
126 bool
127 StringToInt(const char *s, int &result, const char **p, int base)
128 {
129 if (s) {
130 char *ptr = 0;
131 const int h = (int) strtol(s, &ptr, base);
132
133 if (ptr != s && ptr) {
134 result = h;
135
136 if (p)
137 *p = ptr;
138
139 return true;
140 }
141 }
142
143 return false;
144 }
145
146 bool
147 StringToInt64(const char *s, int64_t &result, const char **p, int base)
148 {
149 if (s) {
150 char *ptr = 0;
151 const int64_t h = (int64_t) strtoll(s, &ptr, base);
152
153 if (ptr != s && ptr) {
154 result = h;
155
156 if (p)
157 *p = ptr;
158
159 return true;
160 }
161 }
162
163 return false;
164 }
165
166 bool
167 GetHostWithPort(char *token, Ip::Address *ipa)
168 {
169 char *t;
170 char *host;
171 char *tmp;
172 unsigned short port;
173
174 host = NULL;
175 port = 0;
176
177 if (*token == '[') {
178 /* [host]:port */
179 host = token + 1;
180 t = strchr(host, ']');
181 if (!t)
182 return false;
183 *t = '\0';
184 ++t;
185 if (*t != ':')
186 return false;
187 port = xatos(t + 1);
188 } else if ((t = strchr(token, ':'))) {
189 /* host:port */
190 host = token;
191 *t = '\0';
192 port = xatos(t + 1);
193
194 if (0 == port)
195 return false;
196 } else if ((port = strtol(token, &tmp, 10)), !*tmp) {
197 /* port */
198 } else {
199 host = token;
200 port = 0;
201 }
202
203 if (NULL == host)
204 ipa->SetAnyAddr();
205 else if ( ipa->GetHostByName(host) ) /* dont use ipcache. Accept either FQDN or IPA. */
206 (void) 0;
207 else
208 return false;
209
210 /* port MUST be set after the IPA lookup/conversion is performed. */
211 ipa->SetPort(port);
212
213 return true;
214 }