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);
135 while ((strp = strstr(strp, _INCLUDE_DIRECTIVE)) != NULL) {
136 if (strp == str || strp[-1] ==
'\n') {
141 for (tmpp = strp + CONST_STRLEN(_INCLUDE_DIRECTIVE);
142 *tmpp !=
'\n' && *tmpp !=
'\0'; tmpp++)
144 int len = tmpp - (strp + CONST_STRLEN(_INCLUDE_DIRECTIVE));
145 if (len >=
sizeof(buf)) {
146 DEBUG(
"Can't process %s directive.", _INCLUDE_DIRECTIVE);
151 strncpy(buf, strp + CONST_STRLEN(_INCLUDE_DIRECTIVE), len);
156 if (!(buf[0] ==
'/' || buf[0] ==
'\\')) {
159 if (strlen(dir) + 1 + strlen(buf) >=
sizeof(buf)) {
160 DEBUG(
"Can't process %s directive.", _INCLUDE_DIRECTIVE);
165 snprintf(tmp,
sizeof(tmp),
"%s/%s", dir, buf);
173 if (strlen(buf) == 0 || (incdata =
qfile_load(buf, NULL)) == NULL) {
174 DEBUG(
"Can't process '%s%s' directive.", _INCLUDE_DIRECTIVE,
181 strncpy(buf, strp, CONST_STRLEN(_INCLUDE_DIRECTIVE) + len);
182 buf[CONST_STRLEN(_INCLUDE_DIRECTIVE) + len] =
'\0';
188 strp += CONST_STRLEN(_INCLUDE_DIRECTIVE);
226 char *section = NULL;
227 char *org, *buf, *offset;
228 for (org = buf = offset = strdup(str); *offset !=
'\0';) {
230 for (buf = offset; *offset !=
'\n' && *offset !=
'\0'; offset++)
232 if (*offset !=
'\0') {
239 if ((buf[0] ==
'#') || (buf[0] ==
'\0'))
243 if ((buf[0] ==
'[') && (buf[strlen(buf) - 1] ==
']')) {
247 section = strdup(buf + 1);
248 section[strlen(section) - 1] =
'\0';
252 if (section[0] ==
'\0') {
259 sprintf(buf,
"%c%s", sepchar, section);
263 char *value = strdup(buf);
264 char *name = _q_makeword(value, sepchar);
269 if (section != NULL) {
270 char *newname =
qstrdupf(
"%s.%s", section, name);
276 char *newvalue = _parsestr(tbl, value);
277 if (newvalue != NULL) {
278 tbl->putstr(tbl, name, newvalue);
326static char *_parsestr(qlisttbl_t *tbl,
const char *str) {
333 char *value = strdup(str);
340 for (s = value; *s !=
'\0'; s++) {
341 if (!(*s == _VAR && *(s + 1) == _VAR_OPEN))
346 for (e = s + 2; *e !=
'\0'; e++) {
347 if (*e == _VAR && *(e + 1) == _VAR_OPEN) {
351 }
else if (*e == _VAR_OPEN)
353 else if (*e == _VAR_CLOSE)
358 if (openedbrakets == 0)
363 if (openedbrakets > 0)
367 int varlen = e - s - 2;
368 char *varstr = (
char *) malloc(varlen + 3 + 1);
371 strncpy(varstr, s + 2, varlen);
372 varstr[varlen] =
'\0';
378 if (varlen - 1 == 0) {
388 if (varlen - 1 == 0) {
392 newstr = strdup(
qgetenv(varstr + 1,
""));
400 if ((newstr = tbl->getstr(tbl, varstr,
true)) == NULL) {
409 strncpy(varstr, s, varlen + 3);
410 varstr[varlen + 3] =
'\0';
421 }
while (loop ==
true);
qlisttbl_t * qconfig_parse_file(qlisttbl_t *tbl, const char *filepath, char sepchar)
Load & parse configuration file.
qlisttbl_t * qconfig_parse_str(qlisttbl_t *tbl, const char *str, char sepchar)
Parse string.
void * qfile_load(const char *filepath, size_t *nbytes)
Load file into memory.
char * qfile_get_dir(const char *filepath)
Get directory suffix from filepath.
qlisttbl_t * qlisttbl(int options)
Create a new Q_LIST linked-list container.
char * qstrtrim(char *str)
Remove white spaces(including CR, LF) from head and tail of the string.
char * qstrdupf(const char *format,...)
Duplicate a formatted string.
char * qstrreplace(const char *mode, char *srcstr, const char *tokstr, const char *word)
Replace string or tokens as word from source string with given mode.
const char * qgetenv(const char *envname, const char *defstr)
Get system environment variable.
char * qsyscmd(const char *cmd)
Get the result string of external command execution.