]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/Parsing.cc
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 03 Configuration File Parsing */
13 #include "compat/strtoll.h"
14 #include "ConfigParser.h"
20 * These functions is the same as atoi/l/f, except that they check for errors
24 xatof(const char *token
)
27 double ret
= strtod(token
, &end
);
29 if (ret
== 0 && end
== token
) {
30 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
35 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
43 xatoi(const char *token
)
45 int64_t input
= xatoll(token
, 10);
46 int ret
= (int) input
;
48 if (input
!= static_cast<int64_t>(ret
)) {
49 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
57 xatoui(const char *token
, char eov
)
59 int64_t input
= xatoll(token
, 10, eov
);
61 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The input value '" << token
<< "' cannot be less than 0.");
65 unsigned int ret
= (unsigned int) input
;
66 if (input
!= static_cast<int64_t>(ret
)) {
67 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'unsigned int'.");
75 xatol(const char *token
)
77 int64_t input
= xatoll(token
, 10);
78 long ret
= (long) input
;
80 if (input
!= static_cast<int64_t>(ret
)) {
81 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'long'.");
89 xatoll(const char *token
, int base
, char eov
)
92 int64_t ret
= strtoll(token
, &end
, base
);
95 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: No digits were found in the input value '" << token
<< "'.");
100 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: Invalid value: '" << token
<< "' is supposed to be a number.");
108 xatos(const char *token
)
110 long port
= xatol(token
);
113 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' cannot be less than 0.");
117 if (port
& ~0xFFFF) {
118 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'short'.");
128 char *token
= ConfigParser::NextToken();
131 return -1; // not reachable
134 return xatoll(token
, 10);
138 * This function is different from others (e.g., GetInteger64, GetShort)
139 * because it supports octal and hexadecimal numbers
144 char *token
= ConfigParser::NextToken();
149 return -1; // not reachable
152 // The conversion must honor 0 and 0x prefixes, which are important for things like umask
153 int64_t ret
= xatoll(token
, 0);
156 if (ret
!= static_cast<int64_t>(i
)) {
157 debugs(0, DBG_PARSE_NOTE(DBG_IMPORTANT
), "ERROR: The value '" << token
<< "' is larger than the type 'int'.");
165 * This function is similar as GetInteger() but the token might contain
166 * the percentage symbol (%) and we check whether the value is in the range
168 * So, we accept two types of input: 1. XX% or 2. XX , 0<=XX<=100
169 * unless the limit parameter is set to false.
172 GetPercentage(bool limit
)
174 char *token
= ConfigParser::NextToken();
177 debugs(3, DBG_CRITICAL
, "FATAL: A percentage value is missing.");
179 return 0.0; // not reachable
182 //if there is a % in the end of the digits, we remove it and go on.
183 char* end
= &token
[strlen(token
)-1];
188 int p
= xatoi(token
);
190 if (p
< 0 || (limit
&& p
> 100)) {
191 debugs(3, DBG_CRITICAL
, "FATAL: The value '" << token
<< "' is out of range. A percentage should be within [0, 100].");
195 return static_cast<double>(p
) / 100.0;
201 char *token
= ConfigParser::NextToken();
204 return 0; // not reachable
211 StringToInt(const char *s
, int &result
, const char **p
, int base
)
215 const int h
= (int) strtol(s
, &ptr
, base
);
217 if (ptr
!= s
&& ptr
) {
231 StringToInt64(const char *s
, int64_t &result
, const char **p
, int base
)
235 const int64_t h
= (int64_t) strtoll(s
, &ptr
, base
);
237 if (ptr
!= s
&& ptr
) {
251 GetHostWithPort(char *token
, Ip::Address
*ipa
)
264 t
= strchr(host
, ']');
272 } else if ((t
= strchr(token
, ':'))) {
280 } else if (strtol(token
, &tmp
, 10) && !*tmp
) {
289 else if ( ipa
->GetHostByName(host
) ) /* dont use ipcache. Accept either FQDN or IPA. */
294 /* port MUST be set after the IPA lookup/conversion is performed. */