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