]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ConfigParser.cc
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
7 * Squid is the result of efforts by numerous individuals from
8 * the Internet community; see the CONTRIBUTORS file for full
9 * details. Many organizations have provided support for Squid's
10 * development; see the SPONSORS file for full details. Squid is
11 * Copyrighted (C) 2001 by the Regents of the University of
12 * California; see the COPYRIGHT file for full details. Squid
13 * incorporates software developed and/or copyrighted by other
14 * sources; see the CREDITS file for full details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
36 #include "ConfigParser.h"
41 char *ConfigParser::lastToken
= NULL
;
42 std::queue
<std::string
> ConfigParser::undo
;
45 ConfigParser::destruct()
48 fatalf("Bungled %s line %d: %s",
49 cfg_filename
, config_lineno
, config_input_line
);
53 ConfigParser::strtokFileUndo()
60 ConfigParser::strtokFilePutBack(const char *tok
)
67 ConfigParser::strtokFile(void)
69 static int fromFile
= 0;
70 static FILE *wordFile
= NULL
;
71 LOCAL_ARRAY(char, undoToken
, CONFIG_LINE_LIMIT
);
74 LOCAL_ARRAY(char, buf
, CONFIG_LINE_LIMIT
);
77 strncpy(undoToken
, undo
.front().c_str(), sizeof(undoToken
));
78 undoToken
[sizeof(undoToken
) - 1] = '\0';
87 t
= (strtok(NULL
, w_space
));
89 if (!t
|| *t
== '#') {
91 } else if (*t
== '\"' || *t
== '\'') {
92 /* quote found, start reading from file */
95 while (*t
&& *t
!= '\"' && *t
!= '\'')
100 if ((wordFile
= fopen(fn
, "r")) == NULL
) {
101 debugs(28, DBG_CRITICAL
, "strtokFile: " << fn
<< " not found");
106 setmode(fileno(wordFile
), O_TEXT
);
111 return lastToken
= t
;
116 if (fgets(buf
, CONFIG_LINE_LIMIT
, wordFile
) == NULL
) {
117 /* stop reading from file */
125 /* skip leading and trailing white space */
126 t
+= strspn(buf
, w_space
);
127 t2
= t
+ strcspn(t
, w_space
);
128 t3
= t2
+ strspn(t2
, w_space
);
130 while (*t3
&& *t3
!= '#') {
131 t2
= t3
+ strcspn(t3
, w_space
);
132 t3
= t2
+ strspn(t2
, w_space
);
139 /* skip blank lines */
140 } while ( *t
== '#' || !*t
);
142 return lastToken
= t
;
146 ConfigParser::ParseQuotedString(char **var
, bool *wasQuoted
)
149 ParseQuotedString(&sVar
, wasQuoted
);
150 *var
= xstrdup(sVar
.termedBuf());
154 ConfigParser::ParseQuotedString(String
*var
, bool *wasQuoted
)
156 // Get all of the remaining string
157 char *token
= strtok(NULL
, "");
162 token
= strtok(token
, w_space
);
167 } else if (wasQuoted
)
171 /* scan until the end of the quoted string, unescaping " and \ */
172 while (*s
&& *s
!= '"') {
174 const char * next
= s
+1; // may point to 0
175 memmove(s
, next
, strlen(next
) + 1);
181 debugs(3, DBG_CRITICAL
, "ParseQuotedString: missing '\"' at the end of quoted string" );
184 strtok(s
-1, "\""); /*Reset the strtok to point after the " */
191 ConfigParser::QuoteString(String
&var
)
193 static String quotedStr
;
194 const char *s
= var
.termedBuf();
195 bool needQuote
= false;
197 for (const char *l
= s
; !needQuote
&& *l
!= '\0'; ++l
)
198 needQuote
= !isalnum(*l
);
204 quotedStr
.append('"');
205 for (; *s
!= '\0'; ++s
) {
206 if (*s
== '"' || *s
== '\\')
207 quotedStr
.append('\\');
208 quotedStr
.append(*s
);
210 quotedStr
.append('"');
211 return quotedStr
.termedBuf();