]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ConfigParser.cc
Merged from trunk
[thirdparty/squid.git] / src / ConfigParser.cc
1
2 /*
3 *
4 * SQUID Web Proxy Cache http://www.squid-cache.org/
5 * ----------------------------------------------------------
6 *
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.
15 *
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.
20 *
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.
25 *
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.
29 *
30 *
31 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
32 */
33
34 #include "squid.h"
35 #include "cache_cf.h"
36 #include "ConfigParser.h"
37 #include "Debug.h"
38 #include "fatal.h"
39 #include "globals.h"
40
41 void
42 ConfigParser::destruct()
43 {
44 shutting_down = 1;
45 fatalf("Bungled %s line %d: %s",
46 cfg_filename, config_lineno, config_input_line);
47 }
48
49 char *
50 ConfigParser::strtokFile(void)
51 {
52 static int fromFile = 0;
53 static FILE *wordFile = NULL;
54
55 char *t, *fn;
56 LOCAL_ARRAY(char, buf, CONFIG_LINE_LIMIT);
57
58 do {
59
60 if (!fromFile) {
61 t = (strtok(NULL, w_space));
62
63 if (!t || *t == '#') {
64 return NULL;
65 } else if (*t == '\"' || *t == '\'') {
66 /* quote found, start reading from file */
67 fn = ++t;
68
69 while (*t && *t != '\"' && *t != '\'')
70 ++t;
71
72 *t = '\0';
73
74 if ((wordFile = fopen(fn, "r")) == NULL) {
75 debugs(28, DBG_CRITICAL, "strtokFile: " << fn << " not found");
76 return (NULL);
77 }
78
79 #if _SQUID_WINDOWS_
80 setmode(fileno(wordFile), O_TEXT);
81 #endif
82
83 fromFile = 1;
84 } else {
85 return t;
86 }
87 }
88
89 /* fromFile */
90 if (fgets(buf, CONFIG_LINE_LIMIT, wordFile) == NULL) {
91 /* stop reading from file */
92 fclose(wordFile);
93 wordFile = NULL;
94 fromFile = 0;
95 return NULL;
96 } else {
97 char *t2, *t3;
98 t = buf;
99 /* skip leading and trailing white space */
100 t += strspn(buf, w_space);
101 t2 = t + strcspn(t, w_space);
102 t3 = t2 + strspn(t2, w_space);
103
104 while (*t3 && *t3 != '#') {
105 t2 = t3 + strcspn(t3, w_space);
106 t3 = t2 + strspn(t2, w_space);
107 }
108
109 *t2 = '\0';
110 }
111
112 /* skip comments */
113 /* skip blank lines */
114 } while ( *t == '#' || !*t );
115
116 return t;
117 }
118
119 void
120 ConfigParser::ParseQuotedString(char **var, bool *wasQuoted)
121 {
122 String sVar;
123 ParseQuotedString(&sVar, wasQuoted);
124 *var = xstrdup(sVar.termedBuf());
125 }
126
127 void
128 ConfigParser::ParseQuotedString(String *var, bool *wasQuoted)
129 {
130 // Get all of the remaining string
131 char *token = strtok(NULL, "");
132 if (token == NULL)
133 self_destruct();
134
135 if (*token != '"') {
136 token = strtok(token, w_space);
137 var->reset(token);
138 if (wasQuoted)
139 *wasQuoted = false;
140 return;
141 } else if (wasQuoted)
142 *wasQuoted = true;
143
144 char *s = token + 1;
145 /* scan until the end of the quoted string, unescaping " and \ */
146 while (*s && *s != '"') {
147 if (*s == '\\') {
148 const char * next = s+1; // may point to 0
149 memmove(s, next, strlen(next) + 1);
150 }
151 ++s;
152 }
153
154 if (*s != '"') {
155 debugs(3, DBG_CRITICAL, "ParseQuotedString: missing '\"' at the end of quoted string" );
156 self_destruct();
157 }
158 strtok(s-1, "\""); /*Reset the strtok to point after the " */
159 *s = '\0';
160
161 var->reset(token+1);
162 }
163
164 const char *
165 ConfigParser::QuoteString(String &var)
166 {
167 static String quotedStr;
168 const char *s = var.termedBuf();
169 bool needQuote = false;
170
171 for (const char *l = s; !needQuote && *l != '\0'; ++l )
172 needQuote = !isalnum(*l);
173
174 if (!needQuote)
175 return s;
176
177 quotedStr.clean();
178 quotedStr.append('"');
179 for (; *s != '\0'; ++s) {
180 if (*s == '"' || *s == '\\')
181 quotedStr.append('\\');
182 quotedStr.append(*s);
183 }
184 quotedStr.append('"');
185 return quotedStr.termedBuf();
186 }