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