conf-parser.c revision 2b583ce6576d4a074ce6f1570b3e60b65c64ae7d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
#include "conf-parser.h"
#include "util.h"
#include "macro.h"
#include "strv.h"
#include "log.h"
/* Run the user supplied parser for an assignment */
static int next_assignment(
const char *filename,
unsigned line,
const char *section,
const ConfigItem *t,
bool relaxed,
const char *lvalue,
const char *rvalue,
void *userdata) {
assert(t);
continue;
continue;
continue;
if (!t->parse)
return 0;
}
/* Warn about unknown non-extension fields. */
log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, strna(section));
return 0;
}
/* Parse a variable assignment line */
static int parse_line(const char *filename, unsigned line, char **section, const char* const * sections, const ConfigItem *t, bool relaxed, char *l, void *userdata) {
char *e;
l = strstrip(l);
if (!*l)
return 0;
return 0;
if (startswith(l, ".include ")) {
char *fn;
int r;
return -ENOMEM;
return r;
}
if (*l == '[') {
size_t k;
char *n;
k = strlen(l);
assert(k > 0);
if (l[k-1] != ']') {
return -EBADMSG;
}
return -ENOMEM;
*section = n;
return 0;
}
return 0;
if (!(e = strchr(l, '='))) {
return -EBADMSG;
}
*e = 0;
e++;
}
/* Go through the file and parse each line */
int config_parse(const char *filename, FILE *f, const char* const * sections, const ConfigItem *t, bool relaxed, void *userdata) {
unsigned line = 0;
int r;
bool ours = false;
char *continuation = NULL;
assert(t);
if (!f) {
r = -errno;
goto finish;
}
ours = true;
}
while (!feof(f)) {
bool escaped = false;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
r = -errno;
goto finish;
}
truncate_nl(l);
if (continuation) {
if (!(c = strappend(continuation, l))) {
r = -ENOMEM;
goto finish;
}
continuation = NULL;
p = c;
} else
p = l;
for (e = p; *e; e++) {
if (escaped)
escaped = false;
else if (*e == '\\')
escaped = true;
}
if (escaped) {
*(e-1) = ' ';
if (c)
continuation = c;
else if (!(continuation = strdup(l))) {
r = -ENOMEM;
goto finish;
}
continue;
}
free(c);
if (r < 0)
goto finish;
}
r = 0;
if (f && ours)
fclose(f);
return r;
}
int config_parse_int(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int *i = data;
int r;
return r;
}
return 0;
}
int config_parse_uint64(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int r;
if ((r = safe_atou64(rvalue, u)) < 0) {
return r;
}
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
unsigned *u = data;
int r;
return r;
}
return 0;
}
int config_parse_size(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
unsigned u;
int r;
return r;
}
return 0;
}
int config_parse_bool(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int k;
bool *b = data;
if ((k = parse_boolean(rvalue)) < 0) {
return k;
}
*b = !!k;
return 0;
}
int config_parse_string(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char **s = data;
char *n;
if (*rvalue) {
return -ENOMEM;
} else
n = NULL;
free(*s);
*s = n;
return 0;
}
int config_parse_path(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char **s = data;
char *n;
if (!path_is_absolute(rvalue)) {
return -EINVAL;
}
return -ENOMEM;
free(*s);
*s = n;
return 0;
}
int config_parse_strv(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char **n;
char *w;
unsigned k;
size_t l;
char *state;
k = strv_length(*sv);
k++;
if (!(n = new(char*, k+1)))
return -ENOMEM;
if (*sv)
for (k = 0; (*sv)[k]; k++)
n[k] = (*sv)[k];
else
k = 0;
if (!(n[k++] = cunescape_length(w, l)))
goto fail;
n[k] = NULL;
*sv = n;
return 0;
fail:
for (; k > 0; k--)
free(n[k-1]);
free(n);
return -ENOMEM;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char **n;
char *w;
unsigned k;
size_t l;
char *state;
int r;
k = strv_length(*sv);
k++;
if (!(n = new(char*, k+1)))
return -ENOMEM;
k = 0;
if (*sv)
for (; (*sv)[k]; k++)
n[k] = (*sv)[k];
if (!(n[k] = cunescape_length(w, l))) {
r = -ENOMEM;
goto fail;
}
if (!path_is_absolute(n[k])) {
r = -EINVAL;
goto fail;
}
path_kill_slashes(n[k]);
k++;
}
n[k] = NULL;
*sv = n;
return 0;
fail:
free(n[k]);
for (; k > 0; k--)
free(n[k-1]);
free(n);
return r;
}