conf-parser.c revision d3b6d0c21ea5a0d15ec6dbd8b8d179138b7463bc
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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"
#include "utf8.h"
#include "path-util.h"
#include "set.h"
#include "exit-status.h"
void *table,
const char *section,
const char *lvalue,
int *ltype,
void **data,
void *userdata) {
ConfigTableItem *t;
continue;
continue;
return 1;
}
return 0;
}
void *table,
const char *section,
const char *lvalue,
int *ltype,
void **data,
void *userdata) {
const ConfigPerfItem *p;
if (!section)
else {
char *key;
if (!key)
return -ENOMEM;
}
if (!p)
return 0;
return 1;
}
/* Run the user supplied parser for an assignment */
static int next_assignment(
const char *filename,
unsigned line,
void *table,
const char *section,
const char *lvalue,
const char *rvalue,
bool relaxed,
void *userdata) {
int ltype = 0;
int r;
if (r < 0)
return r;
if (r > 0) {
if (func)
return 0;
}
/* Warn about unknown non-extension fields. */
log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, section);
return 0;
}
/* Parse a variable assignment line */
static int parse_line(
const char *filename,
unsigned line,
const char *sections,
void *table,
bool relaxed,
char **section,
char *l,
void *userdata) {
char *e;
assert(l);
l = strstrip(l);
if (!*l)
return 0;
return 0;
if (startswith(l, ".include ")) {
char *fn;
int r;
if (!fn)
return -ENOMEM;
return r;
}
if (*l == '[') {
size_t k;
char *n;
k = strlen(l);
assert(k > 0);
if (l[k-1] != ']') {
return -EBADMSG;
}
if (!n)
return -ENOMEM;
if (!relaxed)
free(n);
} else {
*section = n;
}
return 0;
}
if (!relaxed)
return 0;
}
e = strchr(l, '=');
if (!e) {
return -EBADMSG;
}
*e = 0;
e++;
return next_assignment(
line,
*section,
strstrip(l),
strstrip(e),
userdata);
}
/* Go through the file and parse each line */
int config_parse(
const char *filename,
FILE *f,
const char *sections,
void *table,
bool relaxed,
void *userdata) {
unsigned line = 0;
int r;
if (!f) {
if (!f) {
return -errno;
}
}
while (!feof(f)) {
bool escaped = false;
if (!fgets(l, sizeof(l), f)) {
if (feof(f))
break;
return -errno;
}
truncate_nl(l);
if (continuation) {
c = strappend(continuation, l);
if (!c)
return -ENOMEM;
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 {
continuation = strdup(l);
if (!continuation)
return -ENOMEM;
}
continue;
}
r = parse_line(filename,
++line,
§ion,
p,
userdata);
free(c);
if (r < 0)
return r;
}
return 0;
}
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;
if (r < 0) {
return 0;
}
return 0;
}
int config_parse_long(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
long *i = data;
int r;
r = safe_atoli(rvalue, i);
if (r < 0) {
return 0;
}
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;
r = safe_atou64(rvalue, u);
if (r < 0) {
return 0;
}
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;
if (r < 0) {
return r;
}
return 0;
}
int config_parse_double(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
double *d = data;
int r;
if (r < 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) {
off_t o;
return 0;
}
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
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 0;
}
*b = !!k;
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int k;
int *b = data;
/* Tristates are like booleans, but can also take the 'default' value, i.e. "-1" */
k = parse_boolean(rvalue);
if (k < 0) {
return 0;
}
*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 (!n)
return log_oom();
if (!utf8_is_valid(n)) {
free(n);
return 0;
}
free(*s);
if (*n)
*s = n;
else {
free(n);
*s = NULL;
}
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 (!utf8_is_valid(rvalue)) {
return 0;
}
if (!path_is_absolute(rvalue)) {
return 0;
}
if (!n)
return log_oom();
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) {
size_t l;
int r;
char **empty;
/* Empty assignment resets the list. As a special rule
* we actually fill in a real empty array here rather
* than NULL, since some code wants to know if
* something was set at all... */
if (!empty)
return log_oom();
return 0;
}
_cleanup_free_ char *n;
n = cunescape_length(w, l);
if (!n)
return log_oom();
if (!utf8_is_valid(n)) {
continue;
}
r = strv_extend(sv, n);
if (r < 0)
return log_oom();
}
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
size_t l;
int r;
/* Empty assignment resets the list */
return 0;
}
_cleanup_free_ char *n;
n = strndup(w, l);
if (!n)
return log_oom();
if (!utf8_is_valid(n)) {
continue;
}
if (!path_is_absolute(n)) {
continue;
}
r = strv_extend(sv, n);
if (r < 0)
return log_oom();
}
return 0;
}
int config_parse_sec(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
return 0;
}
int config_parse_nsec(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
return 0;
}
return 0;
}
int config_parse_mode(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
long l;
char *x = NULL;
errno = 0;
return 0;
}
if (l < 0000 || l > 07777) {
return 0;
}
*m = (mode_t) l;
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int *o = data, x;
if (x < 0) {
return 0;
}
*o = (x << 3) | LOG_PRI(*o);
return 0;
}
int config_parse_level(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int *o = data, x;
x = log_level_from_string(rvalue);
if (x < 0) {
return 0;
}
*o = (*o & LOG_FACMASK) | x;
return 0;
}
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char *w;
size_t l;
char *state;
int r;
/* Empty assignment resets the list */
return 0;
}
int val;
char *temp;
if (!temp)
return log_oom();
if (r < 0) {
if (val > 0) {
if (r < 0)
return log_oom();
if (r < 0) {
return r;
}
} else {
return 0;
}
} else {
else {
if (r < 0)
return log_oom();
if (r < 0) {
return r;
}
}
}
}
return 0;
}