12 #include "wvstringtable.h"
17 void WvConf::setbool(
void *userdata,
18 WvStringParm sect, WvStringParm ent,
19 WvStringParm oldval, WvStringParm newval)
21 if (!*(
bool *)userdata)
23 WvLog log(
"Config Event", WvLog::Debug);
24 if(sect ==
"Tunnel Vision" && ent ==
"Magic Password")
25 log(
"Changed:[%s]%s\n",sect, ent);
27 log(
"Changed: [%s]%s = '%s' -> '%s'\n", sect, ent, oldval, newval);
30 *(
bool *)userdata =
true;
33 void WvConf::addname(
void *userdata,
34 WvStringParm sect, WvStringParm ent,
35 WvStringParm oldval, WvStringParm newval)
41 void WvConf::addfile(
void *userdata,
42 WvStringParm sect, WvStringParm ent,
43 WvStringParm oldval, WvStringParm newval)
46 O_WRONLY | O_CREAT | O_TRUNC, 0600);
50 tmp.print(
"%s\n", newval);
52 tmp.print(
"%s\n", ent);
56 WvConf::WvConf(WvStringParm _filename,
int _create_mode)
57 : filename(_filename), log(filename), globalsection(
"")
59 create_mode = _create_mode;
60 dirty = error = loaded_once =
false;
66 int WvConf::check_for_bool_string(
const char *s)
68 if (strcasecmp(s,
"off") == 0
69 || strcasecmp(s,
"false") == 0
70 || strncasecmp(s,
"no", 2) == 0)
73 if (strcasecmp(s,
"on") == 0
74 || strcasecmp(s,
"true") == 0
75 || strcasecmp(s,
"yes") == 0)
96 int WvConf::parse_wvconf_request(
char *request,
char *§ion,
97 char *&entry,
char *&value)
100 entry = value = NULL;
102 section = strchr(request,
'[');
108 entry = strchr(section,
']');
114 value = strchr(entry,
'=');
134 int WvConf::getint(WvStringParm section, WvStringParm entry,
int def_val)
137 return check_for_bool_string(get(section, entry, def_str));
147 return check_for_bool_string(fuzzy_get(section, entry, def_str));
153 int WvConf::fuzzy_getint(
WvStringList §ion, WvStringParm entry,
157 return check_for_bool_string(fuzzy_get(section, entry, def_str));
161 void WvConf::setint(WvStringParm section, WvStringParm entry,
int value)
164 set(section, entry, def_str);
169 void WvConf::maybesetint(WvStringParm section, WvStringParm entry,
172 if (!get(section, entry, NULL))
173 setint(section, entry, value);
177 void WvConf::load_file(WvStringParm filename)
182 bool quick_mode =
false;
184 WvFile file(filename, O_RDONLY);
192 if (file.isok() && fstat(file.getrfd(), &statbuf) == -1)
194 log(WvLog::Warning,
"Can't stat config file %s\n", filename);
198 if (file.isok() && (statbuf.st_mode & S_ISVTX))
211 if (file.geterr() != ENOENT && !loaded_once)
216 while ((from_file =
trim_string(file.getline())) != NULL)
219 if ((p = parse_section(from_file)) != NULL)
225 sect = &globalsection;
240 p = parse_value(from_file);
248 sect->quick_set(from_file, p);
250 sect->set(from_file, p);
270 const char *WvConf::get(WvStringParm section, WvStringParm entry,
276 for(s = (*
this)[section];
277 s && !cache[s->name];
278 s = (*s)[
"Inherits"] ? (*this)[(*s)[
"Inherits"]->value] : NULL)
280 const char *ret = s->get(entry);
282 cache.add(&s->name,
false);
285 return globalsection.get(entry, def_val);
294 char *section, *entry, *value;
295 parse_error = parse_wvconf_request(wvconfstr.
edit(),
296 section, entry, value);
301 return get(section, entry, value);
308 WvStringList::Iter i(sections), i2(entries);
312 for (i.rewind(); i.next(); )
314 for (i2.rewind(); i2.next();)
317 s && !cache[s->name];
318 s = (*s)[
"Inherits"] ? (*this)[(*s)[
"Inherits"]->value] : NULL)
320 const char *ret = s->get(*i2);
322 cache.add(&s->name,
false);
331 const char *WvConf::fuzzy_get(
WvStringList §ions, WvStringParm entry,
334 WvStringList::Iter i(sections);
338 for (i.rewind(); i.next(); )
341 s && !cache[s->name];
342 s = (*s)[
"Inherits"] ? (*this)[(*s)[
"Inherits"]->value] : NULL)
344 const char *ret = s->get(entry);
346 cache.add(&s->name,
false);
354 void WvConf::set(WvStringParm section, WvStringParm entry,
362 if (!value || !value[0])
369 const char *oldval = s->get(entry,
"");
370 if (!value) value =
"";
371 if (strcmp(oldval, value))
373 run_callbacks(section, entry, oldval, value);
379 s->set(entry, value);
389 void WvConf::setraw(
WvString wvconfstr,
const char *&xvalue,
int &parse_error)
391 char *section, *entry, *value;
392 parse_error = parse_wvconf_request(wvconfstr.
edit(),
393 section, entry, value);
396 set(section, entry, value);
397 xvalue =
get(section, entry, value);
405 void WvConf::maybeset(WvStringParm section, WvStringParm entry,
408 if (value && !
get(section, entry, NULL))
409 set(section, entry, value);
418 for (i.rewind(); i.next(); )
420 if (strcasecmp(i().name, section) == 0)
428 void WvConf::delete_section(WvStringParm section)
439 char *WvConf::parse_section(
char *s)
455 char *WvConf::parse_value(
char *s)
469 void WvConf::save(WvStringParm _filename)
471 if (error || !_filename)
474 WvFile fp(_filename, O_WRONLY|O_CREAT|O_TRUNC, create_mode);
478 log(WvLog::Error,
"Can't write to config file %s: %s\n",
479 _filename, strerror(errno));
480 if (fp.geterr() != ENOENT)
490 if (fstat(fp.getwfd(), &statbuf) == -1)
492 log(WvLog::Error,
"Can't stat config file %s: %s\n",
493 _filename, strerror(errno));
498 fchmod(fp.getwfd(), (statbuf.st_mode & 07777) | S_ISVTX);
501 globalsection.dump(fp);
504 for (i.rewind(); i.next();)
507 fp.print(
"\n[%s]\n", sect.name);
515 fchmod(fp.getwfd(), statbuf.st_mode & 07777);
539 void WvConf::add_callback(WvConfCallback callback,
void *userdata,
540 WvStringParm section, WvStringParm entry,
544 section, entry, cookie),
true);
548 void WvConf::del_callback(WvStringParm section, WvStringParm entry,
551 WvConfCallbackInfoList::Iter i(callbacks);
553 for (i.rewind(); i.next(); )
555 if (i->cookie == cookie && i->section == section && i->entry == entry)
564 void WvConf::run_callbacks(WvStringParm section, WvStringParm entry,
565 WvStringParm oldvalue, WvStringParm newvalue)
567 WvConfCallbackInfoList::Iter i(callbacks);
569 for (i.rewind(); i.next(); )
571 if (!i->section || !strcasecmp(i->section, section))
573 if (!i->entry || !strcasecmp(i->entry, entry))
574 i->callback(i->userdata, section, entry,
581 void WvConf::run_all_callbacks()
583 WvConfCallbackInfoList::Iter i(callbacks);
585 for (i.rewind(); i.next(); )
586 i->callback(i->userdata,
"",
"",
"",
"");