]>
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>
38 #include "ConfigParser.h"
44 ConfigParser::destruct()
47 fatalf("Bungled %s line %d: %s",
48 cfg_filename
, config_lineno
, config_input_line
);
52 ConfigParser::strtokFile(void)
54 static int fromFile
= 0;
55 static FILE *wordFile
= NULL
;
58 LOCAL_ARRAY(char, buf
, CONFIG_LINE_LIMIT
);
63 t
= (strtok(NULL
, w_space
));
65 if (!t
|| *t
== '#') {
67 } else if (*t
== '\"' || *t
== '\'') {
68 /* quote found, start reading from file */
71 while (*t
&& *t
!= '\"' && *t
!= '\'')
76 if ((wordFile
= fopen(fn
, "r")) == NULL
) {
77 debugs(28, DBG_CRITICAL
, "strtokFile: " << fn
<< " not found");
82 setmode(fileno(wordFile
), O_TEXT
);
92 if (fgets(buf
, CONFIG_LINE_LIMIT
, wordFile
) == NULL
) {
93 /* stop reading from file */
101 /* skip leading and trailing white space */
102 t
+= strspn(buf
, w_space
);
103 t2
= t
+ strcspn(t
, w_space
);
104 t3
= t2
+ strspn(t2
, w_space
);
106 while (*t3
&& *t3
!= '#') {
107 t2
= t3
+ strcspn(t3
, w_space
);
108 t3
= t2
+ strspn(t2
, w_space
);
115 /* skip blank lines */
116 } while ( *t
== '#' || !*t
);
122 ConfigParser::ParseQuotedString(char **var
, bool *wasQuoted
)
125 ParseQuotedString(&sVar
, wasQuoted
);
126 *var
= xstrdup(sVar
.termedBuf());
130 ConfigParser::ParseQuotedString(String
*var
, bool *wasQuoted
)
132 // Get all of the remaining string
133 char *token
= strtok(NULL
, "");
138 token
= strtok(token
, w_space
);
143 } else if (wasQuoted
)
147 /* scan until the end of the quoted string, unescaping " and \ */
148 while (*s
&& *s
!= '"') {
150 const char * next
= s
+1; // may point to 0
151 memmove(s
, next
, strlen(next
) + 1);
157 debugs(3, DBG_CRITICAL
, "ParseQuotedString: missing '\"' at the end of quoted string" );
160 strtok(s
-1, "\""); /*Reset the strtok to point after the " */
167 ConfigParser::QuoteString(String
&var
)
169 static String quotedStr
;
170 const char *s
= var
.termedBuf();
171 bool needQuote
= false;
173 for (const char *l
= s
; !needQuote
&& *l
!= '\0'; ++l
)
174 needQuote
= !isalnum(*l
);
180 quotedStr
.append('"');
181 for (; *s
!= '\0'; ++s
) {
182 if (*s
== '"' || *s
== '\\')
183 quotedStr
.append('\\');
184 quotedStr
.append(*s
);
186 quotedStr
.append('"');
187 return quotedStr
.termedBuf();