]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ConfigParser.cc
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
37 #include "ConfigParser.h"
43 ConfigParser::destruct()
46 fatalf("Bungled %s line %d: %s",
47 cfg_filename
, config_lineno
, config_input_line
);
51 ConfigParser::strtokFile(void)
53 static int fromFile
= 0;
54 static FILE *wordFile
= NULL
;
57 LOCAL_ARRAY(char, buf
, CONFIG_LINE_LIMIT
);
62 t
= (strtok(NULL
, w_space
));
64 if (!t
|| *t
== '#') {
66 } else if (*t
== '\"' || *t
== '\'') {
67 /* quote found, start reading from file */
70 while (*t
&& *t
!= '\"' && *t
!= '\'')
75 if ((wordFile
= fopen(fn
, "r")) == NULL
) {
76 debugs(28, DBG_CRITICAL
, "strtokFile: " << fn
<< " not found");
81 setmode(fileno(wordFile
), O_TEXT
);
91 if (fgets(buf
, CONFIG_LINE_LIMIT
, wordFile
) == NULL
) {
92 /* stop reading from file */
100 /* skip leading and trailing white space */
101 t
+= strspn(buf
, w_space
);
102 t2
= t
+ strcspn(t
, w_space
);
103 t3
= t2
+ strspn(t2
, w_space
);
105 while (*t3
&& *t3
!= '#') {
106 t2
= t3
+ strcspn(t3
, w_space
);
107 t3
= t2
+ strspn(t2
, w_space
);
114 /* skip blank lines */
115 } while ( *t
== '#' || !*t
);
121 ConfigParser::ParseQuotedString(char **var
, bool *wasQuoted
)
124 ParseQuotedString(&sVar
, wasQuoted
);
125 *var
= xstrdup(sVar
.termedBuf());
129 ConfigParser::ParseQuotedString(String
*var
, bool *wasQuoted
)
131 // Get all of the remaining string
132 char *token
= strtok(NULL
, "");
137 token
= strtok(token
, w_space
);
142 } else if (wasQuoted
)
146 /* scan until the end of the quoted string, unescaping " and \ */
147 while (*s
&& *s
!= '"') {
149 const char * next
= s
+1; // may point to 0
150 memmove(s
, next
, strlen(next
) + 1);
156 debugs(3, DBG_CRITICAL
, "ParseQuotedString: missing '\"' at the end of quoted string" );
159 strtok(s
-1, "\""); /*Reset the strtok to point after the " */
166 ConfigParser::QuoteString(String
&var
)
168 static String quotedStr
;
169 const char *s
= var
.termedBuf();
170 bool needQuote
= false;
172 for (const char *l
= s
; !needQuote
&& *l
!= '\0'; ++l
)
173 needQuote
= !isalnum(*l
);
179 quotedStr
.append('"');
180 for (; *s
!= '\0'; ++s
) {
181 if (*s
== '"' || *s
== '\\')
182 quotedStr
.append('\\');
183 quotedStr
.append(*s
);
185 quotedStr
.append('"');
186 return quotedStr
.termedBuf();