1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
//
// Configuration.cc
//
// Configuration: This class provides an object lookup table. Each object
// in the Configuration is indexed with a string. The objects
// can be returned by mentioning their string index. Values may
// include files with `/path/to/file` or other configuration
// variables with ${variable}
//
// Part of the ht://Dig package <https://htdig.sourceforge.net/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: Configuration.cc,v 1.20 2004/05/28 13:15:20 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include "Configuration.h"
#include "htString.h"
#include "ParsedString.h"
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
//*********************************************************************
// Configuration::Configuration()
//
Configuration::Configuration() : separators("=:"), allow_multiple(0)
{
}
//*********************************************************************
// void Configuration::NameValueSeparators(char *s)
//
void Configuration::NameValueSeparators(const String& s)
{
separators = s;
}
//*********************************************************************
// Add an entry to the configuration table.
//
void Configuration::Add(const String& str_arg)
{
const char* str = str_arg;
String name, value;
while (str && *str)
{
while (isspace(*str))
str++;
name = 0;
if (!isalpha(*str))
break;
// Some isalnum() implementations don't allow all the letters that
// isalpha() does, e.g. accented ones. They're not POSIX.2 compliant
// but we won't punish them with an infinite loop...
if (!isalnum(*str))
break;
while (isalnum(*str) || *str == '-' || *str == '_')
name << *str++;
name.lowercase();
//
// We have the name. Let's see if we will get a value
//
while (isspace(*str))
str++;
if (!*str)
{
//
// End of string. We need to store the name as a boolean TRUE
//
Add(name, "true");
return;
}
if (!strchr((char*)separators, *str))
{
//
// We are now at a new name. The previous one needs to be set
// to boolean TRUE
//
Add(name, "true");
continue;
}
//
// We now need to deal with the value
//
str++; // Skip the separator
while (isspace(*str))
str++;
if (!*str)
{
//
// End of string reached. The value must be blank
//
Add(name, "");
break;
}
value = 0;
if (*str == '"')
{
//
// Ah! A quoted value. This should be easy to deal with...
// (Just kidding!)
//
str++;
while (*str && *str != '"')
{
value << *str++;
}
Add(name, value);
if (*str == '"')
str++;
continue;
}
else if (*str == '\'')
{
// A single quoted value.
str++;
while (*str && *str != '\'')
{
value << *str++;
}
Add(name, value);
if (*str == '\'')
str++;
continue;
}
else
{
//
// A non-quoted string. This string will terminate at the
// next blank
//
while (*str && !isspace(*str))
{
value << *str++;
}
Add(name, value);
continue;
}
}
}
//*********************************************************************
// Add an entry to the configuration table, without allowing variable
// or file expansion of the value.
//
void Configuration::Add(const String& name, const String& value)
{
String escaped;
const char *s = value.get();
while (*s)
{
if (strchr("$`\\", *s))
escaped << '\\';
escaped << *s++;
}
ParsedString *ps = new ParsedString(escaped);
dcGlobalVars.Add(name, ps);
}
//*********************************************************************
// Add an entry to the configuration table, allowing parsing for variable
// or file expansion of the value.
//
void Configuration::AddParsed(const String& name, const String& value)
{
ParsedString *ps = new ParsedString(value);
if (mystrcasecmp(name, "locale") == 0)
{
String str(setlocale(LC_ALL, ps->get(dcGlobalVars)));
ps->set(str);
//
// Set time format to standard to avoid sending If-Modified-Since
// http headers in native format which http servers can't
// understand
//
setlocale(LC_TIME, "C");
}
dcGlobalVars.Add(name, ps);
}
//*********************************************************************
// Remove an entry from both the hash table and from the list of keys.
//
int Configuration::Remove(const String& name)
{
return dcGlobalVars.Remove(name);
}
//*********************************************************************
// char *Configuration::Find(const char *name) const
// Retrieve a variable from the configuration database. This variable
// will be parsed and a new String object will be returned.
//
const String Configuration::Find(const String& name) const
{
ParsedString *ps = (ParsedString *) dcGlobalVars[name];
if (ps)
{
return ps->get(dcGlobalVars);
}
else
{
#ifdef DEBUG
fprintf (stderr, "Could not find configuration option %s\n", (const char*)name);
#endif
return 0;
}
}
//-
// Return 1 if the value of configuration attribute <b>name</b> has
// been set, 0 otherwise
int Configuration::Exists(const String& name) const
{
return dcGlobalVars.Exists(name);
}
//*********************************************************************
Object *Configuration::Get_Object(char *name) {
return dcGlobalVars[name];
}
//*********************************************************************
//
int Configuration::Value(const String& name, int default_value) const
{
return Find(name).as_integer(default_value);
}
//*********************************************************************
//
double Configuration::Double(const String& name, double default_value) const
{
return Find(name).as_double(default_value);
}
//*********************************************************************
// int Configuration::Boolean(char *name, int default_value)
//
int Configuration::Boolean(const String& name, int default_value) const
{
int value = default_value;
const String s = Find(name);
if (s[0])
{
if (s.nocase_compare("true") == 0 ||
s.nocase_compare("yes") == 0 ||
s.nocase_compare("1") == 0)
value = 1;
else if (s.nocase_compare("false") == 0 ||
s.nocase_compare("no") == 0 ||
s.nocase_compare("0") == 0)
value = 0;
}
return value;
}
//*********************************************************************
//
const String Configuration::operator[](const String& name) const
{
return Find(name);
}
//*********************************************************************
//
int Configuration::Read(const String& filename)
{
FILE* in = fopen((const char*)filename, "r");
if(!in) {
fprintf(stderr, "Configuration::Read: cannot open %s for reading : ", (const char*)filename);
perror("");
return NOTOK;
}
#define CONFIG_BUFFER_SIZE (50*1024)
//
// Make the line buffer large so that we can read long lists of start
// URLs.
//
char buffer[CONFIG_BUFFER_SIZE + 1];
char *current;
String line;
String name;
char *value;
int len;
while (fgets(buffer, CONFIG_BUFFER_SIZE, in))
{
line << buffer;
line.chop("\r\n");
if (line.last() == '\\')
{
line.chop(1);
continue; // Append the next line to this one
}
current = line.get();
if (*current == '#' || *current == '\0')
{
line = 0;
continue; // Comments and blank lines are skipped
}
name = strtok(current, ": =\t");
value = strtok(0, "\r\n");
if (!value)
value = ""; // Blank value
//
// Skip any whitespace before the actual text
//
while (*value == ' ' || *value == '\t')
value++;
len = strlen(value) - 1;
//
// Skip any whitespace after the actual text
//
while (len >= 0 && (value[len] == ' ' || value[len] == '\t'))
{
value[len] = '\0';
len--;
}
if (mystrcasecmp((char*)name, "include") == 0)
{
ParsedString ps(value);
String str(ps.get(dcGlobalVars));
if (str[0] != '/') // Given file name not fully qualified
{
str = filename; // so strip dir. name from current one
len = str.lastIndexOf('/') + 1;
if (len > 0)
str.chop(str.length() - len);
else
str = ""; // No slash in current filename
str << ps.get(dcGlobalVars);
}
Read(str);
line = 0;
continue;
}
AddParsed(name, value);
line = 0;
}
fclose(in);
return OK;
}
//*********************************************************************
// void Configuration::Defaults(ConfigDefaults *array)
//
void Configuration::Defaults(const ConfigDefaults *array)
{
for (int i = 0; array[i].name; i++)
{
AddParsed(array[i].name, array[i].value);
}
}
|