2 * Trivial smb.conf parsing code
4 * Copyright Volker Lendecke <vl@samba.org> 2014
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, and the entire permission notice in its entirety,
11 * including the disclaimer of warranties.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 * products derived from this software without specific prior
19 * ALTERNATIVELY, this product may be distributed under the terms of
20 * the GNU Public License Version 3 or later, in which case the
21 * provisions of the GPL are required INSTEAD OF the above restrictions.
22 * (This clause is necessary due to a potential bad interaction between the
23 * GPL and the restrictions contained in a BSD-style copyright.)
25 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35 * OF THE POSSIBILITY OF SUCH DAMAGE.
46 static int next_content(FILE *f)
50 for (c = fgetc(f); c != EOF; c = fgetc(f)) {
62 static int next_end_of_line(FILE *f)
66 for (c = fgetc(f); c != EOF; c = fgetc(f)) {
74 static bool make_space(char **buf, size_t *buflen, size_t position)
78 if (position < *buflen) {
81 tmp = realloc(*buf, (*buflen) * 2);
91 * Get a conf line into *pbuf (which must be a malloc'ed buffer already).
93 * Ignores leading spaces
94 * Ignores comment lines
96 * Takes care of continuation lines
97 * Zaps multiple spaces into one
100 static int get_line(FILE *f, char **pbuf, size_t *pbuflen)
117 if ((c == '#') || (c == ';')) {
119 * Line starting with a comment, skip
121 c = next_end_of_line(f);
135 for ( ; c != EOF ; c = fgetc(f)) {
139 if ((pos > 0) && (buf[pos-1] == '\\')) {
141 * Line ends in "\". Continuation.
147 if ((pos > 1) && (buf[pos-2] == '\\') &&
148 isspace(buf[pos-1])) {
150 * Line ends in "\ ". Mind that we zap
151 * multiple spaces into one. Continuation.
158 * No continuation, done with the line
163 if ((pos > 0) && isspace(buf[pos-1]) && isspace(c)) {
165 * Zap multiple spaces to one
170 if (!make_space(&buf, &buflen, pos)) {
176 if (!make_space(&buf, &buflen, pos)) {
185 static bool parse_section(
186 char *buf, bool (*sfunc)(const char *section, void *private_data),
191 p = buf+1; /* skip [ */
199 return sfunc(p, private_data);
202 static char *trim_one_space(char *buf)
206 if (isspace(buf[0])) {
213 if (isspace(buf[len-1])) {
220 static bool parse_param(char *buf,
221 bool (*pfunc)(const char *name, const char *value,
229 equals = strchr(buf, '=');
230 if (equals == NULL) {
235 name = trim_one_space(buf);
241 value = trim_one_space(equals+1);
243 return pfunc(name, value, private_data);
246 bool tini_parse(FILE *f,
247 bool (*sfunc)(const char *section, void *private_data),
248 bool (*pfunc)(const char *name, const char *value,
257 buf = malloc(buflen);
266 ret = get_line(f, &buf, &buflen);
269 /* No lines anymore */
284 ok = parse_section(buf, sfunc, private_data);
287 ok = parse_param(buf, pfunc, private_data);