]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
2 * DEBUG: section 03 Configuration File Parsing
3 * AUTHOR: Harvest Derived
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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.
35 #include "compat/strtoll.h"
41 * These functions is the same as atoi/l/f, except that they check for errors
45 xatof(const char *token
)
48 double ret
= strtod(token
, &end
);
50 if (ret
== 0 && end
== token
) {
51 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
56 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
64 xatoi(const char *token
)
66 int64_t input
= xatoll(token
, 10);
67 int ret
= (int) input
;
69 if (input
!= static_cast<int64_t>(ret
)) {
70 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
78 xatoui(const char *token
)
80 int64_t input
= xatoll(token
, 10);
82 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The input value '" << token
<< "' cannot be less than 0.");
86 unsigned int ret
= (unsigned int) input
;
87 if (input
!= static_cast<int64_t>(ret
)) {
88 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'unsigned int'.");
96 xatol(const char *token
)
98 int64_t input
= xatoll(token
, 10);
99 long ret
= (long) input
;
101 if (input
!= static_cast<int64_t>(ret
)) {
102 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'long'.");
110 xatoll(const char *token
, int base
)
113 int64_t ret
= strtoll(token
, &end
, base
);
116 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
121 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
129 xatos(const char *token
)
131 long port
= xatol(token
);
134 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' cannot be less than 0.");
138 if (port
& ~0xFFFF) {
139 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'short'.");
149 char *token
= strtok(NULL
, w_space
);
154 return xatoll(token
, 10);
158 * This function is different from others (e.g., GetInteger64, GetShort)
159 * because it supports octal and hexadecimal numbers
164 char *token
= strtok(NULL
, w_space
);
170 // The conversion must honor 0 and 0x prefixes, which are important for things like umask
171 int64_t ret
= xatoll(token
, 0);
174 if (ret
!= static_cast<int64_t>(i
)) {
175 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
183 * This function is similar as GetInteger() but the token might contain
184 * the percentage symbol (%) and we check whether the value is in the range
186 * So, we accept two types of input: 1. XX% or 2. XX , 0<=XX<=100
192 char *token
= strtok(NULL
, w_space
);
195 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: A percentage value is missing.");
199 //if there is a % in the end of the digits, we remove it and go on.
200 char* end
= &token
[strlen(token
)-1];
207 if (p
< 0 || p
> 100) {
208 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is out of range. A percentage should be within [0, 100].");
218 char *token
= strtok(NULL
, w_space
);
227 StringToInt(const char *s
, int &result
, const char **p
, int base
)
231 const int h
= (int) strtol(s
, &ptr
, base
);
233 if (ptr
!= s
&& ptr
) {
247 StringToInt64(const char *s
, int64_t &result
, const char **p
, int base
)
251 const int64_t h
= (int64_t) strtoll(s
, &ptr
, base
);
253 if (ptr
!= s
&& ptr
) {
267 GetHostWithPort(char *token
, Ip::Address
*ipa
)
280 t
= strchr(host
, ']');
288 } else if ((t
= strchr(token
, ':'))) {
296 } else if (strtol(token
, &tmp
, 10) && !*tmp
) {
305 else if ( ipa
->GetHostByName(host
) ) /* dont use ipcache. Accept either FQDN or IPA. */
310 /* port MUST be set after the IPA lookup/conversion is performed. */