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