33#ifndef DISABLE_QCONFIG
42#include "utilities/qfile.h"
43#include "utilities/qstring.h"
44#include "utilities/qsystem.h"
45#include "extensions/qconfig.h"
47#define _INCLUDE_DIRECTIVE "@INCLUDE "
57static char *_parsestr(qlisttbl_t *tbl,
const char *str);
134 while ((strp = strstr(strp, _INCLUDE_DIRECTIVE)) != NULL) {
135 if (strp == str || strp[-1] ==
'\n') {
140 for (tmpp = strp + CONST_STRLEN(_INCLUDE_DIRECTIVE);
141 *tmpp !=
'\n' && *tmpp !=
'\0'; tmpp++)
143 int len = tmpp - (strp + CONST_STRLEN(_INCLUDE_DIRECTIVE));
144 if (len >=
sizeof(buf)) {
145 DEBUG(
"Can't process %s directive.", _INCLUDE_DIRECTIVE);
150 strncpy(buf, strp + CONST_STRLEN(_INCLUDE_DIRECTIVE), len);
155 if (!(buf[0] ==
'/' || buf[0] ==
'\\')) {
158 if (strlen(dir) + 1 + strlen(buf) >=
sizeof(buf)) {
159 DEBUG(
"Can't process %s directive.", _INCLUDE_DIRECTIVE);
164 snprintf(tmp,
sizeof(tmp),
"%s/%s", dir, buf);
172 if (strlen(buf) == 0 || (incdata =
qfile_load(buf, NULL)) == NULL) {
173 DEBUG(
"Can't process '%s%s' directive.", _INCLUDE_DIRECTIVE,
180 strncpy(buf, strp, CONST_STRLEN(_INCLUDE_DIRECTIVE) + len);
181 buf[CONST_STRLEN(_INCLUDE_DIRECTIVE) + len] =
'\0';
187 strp += CONST_STRLEN(_INCLUDE_DIRECTIVE);
224 char *section = NULL;
225 char *org, *buf, *offset;
226 for (org = buf = offset = strdup(str); *offset !=
'\0';) {
228 for (buf = offset; *offset !=
'\n' && *offset !=
'\0'; offset++)
230 if (*offset !=
'\0') {
237 if ((buf[0] ==
'#') || (buf[0] ==
'\0'))
241 if ((buf[0] ==
'[') && (buf[strlen(buf) - 1] ==
']')) {
245 section = strdup(buf + 1);
246 section[strlen(section) - 1] =
'\0';
250 if (section[0] ==
'\0') {
257 sprintf(buf,
"%c%s", sepchar, section);
261 char *value = strdup(buf);
262 char *name = _q_makeword(value, sepchar);
267 if (section != NULL) {
268 char *newname =
qstrdupf(
"%s.%s", section, name);
274 char *newvalue = _parsestr(tbl, value);
275 if (newvalue != NULL) {
276 tbl->putstr(tbl, name, newvalue);
324static char *_parsestr(qlisttbl_t *tbl,
const char *str) {
331 char *value = strdup(str);
338 for (s = value; *s !=
'\0'; s++) {
339 if (!(*s == _VAR && *(s + 1) == _VAR_OPEN))
344 for (e = s + 2; *e !=
'\0'; e++) {
345 if (*e == _VAR && *(e + 1) == _VAR_OPEN) {
349 }
else if (*e == _VAR_OPEN)
351 else if (*e == _VAR_CLOSE)
356 if (openedbrakets == 0)
361 if (openedbrakets > 0)
365 int varlen = e - s - 2;
366 char *varstr = (
char *) malloc(varlen + 3 + 1);
369 strncpy(varstr, s + 2, varlen);
370 varstr[varlen] =
'\0';
376 if (varlen - 1 == 0) {
386 if (varlen - 1 == 0) {
390 newstr = strdup(
qgetenv(varstr + 1,
""));
398 if ((newstr = tbl->getstr(tbl, varstr,
true)) == NULL) {
407 strncpy(varstr, s, varlen + 3);
408 varstr[varlen + 3] =
'\0';
419 }
while (loop ==
true);
qlisttbl_t * qconfig_parse_file(qlisttbl_t *tbl, const char *filepath, char sepchar)
Load and parse a configuration file.
qlisttbl_t * qconfig_parse_str(qlisttbl_t *tbl, const char *str, char sepchar)
Parse a configuration string.
void * qfile_load(const char *filepath, size_t *nbytes)
Load a file into memory.
char * qfile_get_dir(const char *filepath)
Get the directory part of a path.
qlisttbl_t * qlisttbl(int options)
Create a new Q_LIST linked-list container.
char * qstrtrim(char *str)
Remove whitespace, including CR and LF, from both ends of a string.
char * qstrdupf(const char *format,...)
Duplicate a formatted string.
char * qstrreplace(const char *mode, char *srcstr, const char *tokstr, const char *word)
Replace tokens or strings in a source string using the given mode.
const char * qgetenv(const char *envname, const char *defstr)
Get a system environment variable.
char * qsyscmd(const char *cmd)
Run an external command and return its output.