conf-parser.c revision d7832d2c6e0ef5f2839a2296c1cc2fc85c7d9632
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/***
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering This file is part of systemd.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Copyright 2010 Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is free software; you can redistribute it and/or modify it
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering under the terms of the GNU General Public License as published by
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering the Free Software Foundation; either version 2 of the License, or
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering (at your option) any later version.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering systemd is distributed in the hope that it will be useful, but
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering General Public License for more details.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering You should have received a copy of the GNU General Public License
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering***/
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <string.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdio.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <errno.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <assert.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdlib.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "conf-parser.h"
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek#include "util.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "macro.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "strv.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "log.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "utf8.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_item_table_lookup(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *table,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigParserCallback *func,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int *ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void **data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigTableItem *t;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(table);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(func);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(ltype);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (t = table; t->lvalue; t++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!streq(lvalue, t->lvalue))
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering continue;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering if (!streq_ptr(section, t->section))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering continue;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *func = t->parse;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *ltype = t->ltype;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *data = t->data;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering return 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_item_perf_lookup(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *table,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigParserCallback *func,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int *ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void **data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const ConfigPerfItem *p;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(table);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(func);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(ltype);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!section)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering p = lookup(lvalue, strlen(lvalue));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering else {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek char *key;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering key = join(section, ".", lvalue, NULL);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!key)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ENOMEM;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering p = lookup(key, strlen(key));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(key);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!p)
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *func = p->parse;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *ltype = p->ltype;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *data = (uint8_t*) userdata + p->offset;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/* Run the user supplied parser for an assignment */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic int next_assignment(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigItemLookup lookup,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *table,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering bool relaxed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ConfigParserCallback func = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(line > 0);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(lookup);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(lvalue);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(rvalue);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek if (r > 0) {
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek if (func)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return func(filename, line, section, lvalue, ltype, rvalue, data, userdata);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* Warn about unknown non-extension fields. */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!relaxed && !startswith(lvalue, "X-"))
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek log_info("[%s:%u] Unknown lvalue '%s' in section '%s'. Ignoring.", filename, line, lvalue, section);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return 0;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek}
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek/* Parse a variable assignment line */
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmekstatic int parse_line(
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek const char *filename,
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmek unsigned line,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek const char *sections,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek ConfigItemLookup lookup,
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmek void *table,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek bool relaxed,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek char **section,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek char *l,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek void *userdata) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek char *e;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(filename);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(line > 0);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(lookup);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering l = strstrip(l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!*l)
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (strchr(COMMENTS, *l))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (startswith(l, ".include ")) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char *fn;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering fn = file_in_same_dir(filename, strstrip(l+9));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!fn)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ENOMEM;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = config_parse(fn, NULL, sections, lookup, table, relaxed, userdata);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek free(fn);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return r;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (*l == '[') {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering size_t k;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char *n;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering k = strlen(l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(k > 0);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (l[k-1] != ']') {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Invalid section header.", filename, line);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -EBADMSG;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering n = strndup(l+1, k-2);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!n)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ENOMEM;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (sections && !nulstr_contains(sections, n)) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!relaxed)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("[%s:%u] Unknown section '%s'. Ignoring.", filename, line, n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *section = NULL;
d3b6d0c21ea5a0d15ec6dbd8b8d179138b7463bcZbigniew Jędrzejewski-Szmek } else {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(*section);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *section = n;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek if (sections && !*section) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!relaxed)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("[%s:%u] Assignment outside of section. Ignoring.", filename, line);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek e = strchr(l, '=');
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!e) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Missing '='.", filename, line);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -EBADMSG;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *e = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering e++;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return next_assignment(
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering lookup,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering table,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering strstrip(l),
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering strstrip(e),
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering relaxed,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering userdata);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek/* Go through the file and parse each line */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering FILE *f,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *sections,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek ConfigItemLookup lookup,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek void *table,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek bool relaxed,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek void *userdata) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek unsigned line = 0;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek char *section = NULL;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek int r;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek bool ours = false;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek char *continuation = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lookup);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!f) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering f = fopen(filename, "re");
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek if (!f) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = -errno;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to open configuration file '%s': %s", filename, strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering goto finish;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ours = true;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering while (!feof(f)) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char l[LINE_MAX], *p, *c = NULL, *e;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering bool escaped = false;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!fgets(l, sizeof(l), f)) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (feof(f))
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering break;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = -errno;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to read configuration file '%s': %s", filename, strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering goto finish;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering truncate_nl(l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (continuation) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering c = strappend(continuation, l);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!c) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = -ENOMEM;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering goto finish;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(continuation);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering continuation = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering p = c;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering p = l;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering for (e = p; *e; e++) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (escaped)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering escaped = false;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering else if (*e == '\\')
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering escaped = true;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (escaped) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek *(e-1) = ' ';
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (c)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek continuation = c;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer else {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek continuation = strdup(l);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (!continuation) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = -ENOMEM;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek goto finish;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek continue;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = parse_line(filename,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek ++line,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek sections,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek lookup,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek table,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek relaxed,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek &section,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek p,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek userdata);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek free(c);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (r < 0)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek goto finish;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekfinish:
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek free(section);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek free(continuation);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (f && ours)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek fclose(f);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint config_parse_int(
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *filename,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek unsigned line,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *section,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *lvalue,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int ltype,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *rvalue,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek void *data,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek void *userdata) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int *i = data;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(filename);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(lvalue);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(rvalue);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(data);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if ((r = safe_atoi(rvalue, i)) < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("[%s:%u] Failed to parse numeric value, ingoring: %s", filename, line, rvalue);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint config_parse_long(
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *filename,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek unsigned line,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *section,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering long *i = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if ((r = safe_atoli(rvalue, i)) < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_uint64(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t *u = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if ((r = safe_atou64(rvalue, u)) < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmekint config_parse_unsigned(
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
7ff7394d9e4e9189c30fd018235e6b1728c6f2d0Zbigniew Jędrzejewski-Szmek const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek void *userdata) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek unsigned *u = data;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if ((r = safe_atou(rvalue, u)) < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("[%s:%u] Failed to parse numeric value: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint config_parse_bytes_size(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek size_t *sz = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering off_t o;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (parse_bytes(rvalue, &o) < 0 || (off_t) (size_t) o != o) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse byte value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *sz = (size_t) o;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_bytes_off(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering const char *lvalue,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering void *data,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering off_t *bytes = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_cc(sizeof(off_t) == sizeof(uint64_t));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (parse_bytes(rvalue, bytes) < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse bytes value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_bool(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int k;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering bool *b = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if ((k = parse_boolean(rvalue)) < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse boolean value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *b = !!k;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_tristate(
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering const char *filename,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering unsigned line,
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int k;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int *b = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* Tristates are like booleans, but can also take the 'default' value, i.e. "-1" */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering k = parse_boolean(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (k < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Failed to parse boolean value, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *b = !!k;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_string(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char **s = data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char *n;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering n = cunescape(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!n)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ENOMEM;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!utf8_is_valid(n)) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(*s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (*n)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *s = n;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering else {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *s = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek return 0;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek}
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmekint config_parse_path(
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek const char *filename,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek unsigned line,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek const char *section,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek const char *lvalue,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek int ltype,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek const char *rvalue,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek void *data,
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek void *userdata) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek char **s = data;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek char *n;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(filename);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(lvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(data);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!utf8_is_valid(rvalue)) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("[%s:%u] Path is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (!path_is_absolute(rvalue)) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("[%s:%u] Not an absolute path, ignoring: %s", filename, line, rvalue);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering n = strdup(rvalue);
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering if (!n)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -ENOMEM;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering path_kill_slashes(n);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(*s);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering *s = n;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringint config_parse_strv(
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *filename,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering unsigned line,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *section,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *lvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int ltype,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering const char *rvalue,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering void *userdata) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek char*** sv = data;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek char **n;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek char *w;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek unsigned k;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek size_t l;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek char *state;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek int r;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek assert(filename);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek assert(lvalue);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek assert(rvalue);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek assert(data);
2e8fb7026d3c560194cfe9f83935ce0b16263da0Lukas Nykryn
464264ac5a35b655065c5d95b8d8ffbbc7ff3bcfLukas Nykryn k = strv_length(*sv);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, l, rvalue, state)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek k++;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek n = new(char*, k+1);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (!n)
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek return -ENOMEM;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek if (*sv)
2e8fb7026d3c560194cfe9f83935ce0b16263da0Lukas Nykryn for (k = 0; (*sv)[k]; k++)
464264ac5a35b655065c5d95b8d8ffbbc7ff3bcfLukas Nykryn n[k] = (*sv)[k];
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek else
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek k = 0;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek FOREACH_WORD_QUOTED(w, l, rvalue, state) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek n[k] = cunescape_length(w, l);
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek if (!n[k]) {
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek r = -ENOMEM;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek goto fail;
54b7254c1fa629937f92fd6fa34bdf127b696a00Zbigniew Jędrzejewski-Szmek }
if (!utf8_is_valid(n[k])) {
log_error("[%s:%u] String is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
free(n[k]);
continue;
}
k++;
}
n[k] = NULL;
free(*sv);
*sv = n;
return 0;
fail:
for (; k > 0; k--)
free(n[k-1]);
free(n);
return r;
}
int config_parse_path_strv(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
char*** sv = data;
char **n;
char *w;
unsigned k;
size_t l;
char *state;
int r;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
k = strv_length(*sv);
FOREACH_WORD_QUOTED(w, l, rvalue, state)
k++;
n = new(char*, k+1);
if (!n)
return -ENOMEM;
k = 0;
if (*sv)
for (; (*sv)[k]; k++)
n[k] = (*sv)[k];
FOREACH_WORD_QUOTED(w, l, rvalue, state) {
n[k] = strndup(w, l);
if (!n[k]) {
r = -ENOMEM;
goto fail;
}
if (!utf8_is_valid(n[k])) {
log_error("[%s:%u] Path is not UTF-8 clean, ignoring assignment: %s", filename, line, rvalue);
free(n[k]);
continue;
}
if (!path_is_absolute(n[k])) {
log_error("[%s:%u] Not an absolute path, ignoring: %s", filename, line, rvalue);
free(n[k]);
continue;
}
path_kill_slashes(n[k]);
k++;
}
n[k] = NULL;
free(*sv);
*sv = n;
return 0;
fail:
for (; k > 0; k--)
free(n[k-1]);
free(n);
return r;
}
int config_parse_usec(
const char *filename,
unsigned line,
const char *section,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
usec_t *usec = data;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
if (parse_usec(rvalue, usec) < 0) {
log_error("[%s:%u] Failed to parse time value, ignoring: %s", filename, line, rvalue);
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) {
mode_t *m = data;
long l;
char *x = NULL;
assert(filename);
assert(lvalue);
assert(rvalue);
assert(data);
errno = 0;
l = strtol(rvalue, &x, 8);
if (!x || *x || errno) {
log_error("[%s:%u] Failed to parse mode value, ignoring: %s", filename, line, rvalue);
return 0;
}
if (l < 0000 || l > 07777) {
log_error("[%s:%u] mode value out of range, ignoring: %s", filename, line, rvalue);
return 0;
}
*m = (mode_t) l;
return 0;
}