2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
37 #include "conf-parser.h"
39 #define WHITESPACE " \t\n"
40 #define COMMENTS "#;\n"
42 /* Run the user supplied parser for an assignment */
43 static int next_assignment(
47 const pa_config_item
*t
,
57 for (; t
->parse
; t
++) {
59 if (t
->lvalue
&& !pa_streq(lvalue
, t
->lvalue
))
62 if (t
->section
&& !section
)
65 if (t
->section
&& !pa_streq(section
, t
->section
))
68 return t
->parse(filename
, line
, section
, lvalue
, rvalue
, t
->data
, userdata
);
71 pa_log("[%s:%u] Unknown lvalue '%s' in section '%s'.", filename
, line
, lvalue
, pa_strna(section
));
76 /* Returns non-zero when c is contained in s */
77 static int in_string(char c
, const char *s
) {
87 /* Remove all whitepsapce from the beginning and the end of *s. *s may
89 static char *strip(char *s
) {
90 char *b
= s
+strspn(s
, WHITESPACE
);
94 if (!in_string(*e
, WHITESPACE
))
103 /* Parse a variable assignment line */
104 static int parse_line(const char *filename
, unsigned line
, char **section
, const pa_config_item
*t
, char *l
, void *userdata
) {
107 b
= l
+strspn(l
, WHITESPACE
);
109 if ((c
= strpbrk(b
, COMMENTS
)))
115 if (pa_startswith(b
, ".include ")) {
116 char *path
= NULL
, *fn
;
120 if (!pa_is_path_absolute(fn
)) {
122 if ((k
= strrchr(filename
, '/'))) {
123 char *dir
= pa_xstrndup(filename
, k
-filename
);
124 fn
= path
= pa_sprintf_malloc("%s" PA_PATH_SEP
"%s", dir
, fn
);
129 r
= pa_config_parse(fn
, NULL
, t
, userdata
);
141 pa_log("[%s:%u] Invalid section header.", filename
, line
);
146 *section
= pa_xstrndup(b
+1, k
-2);
150 if (!(e
= strchr(b
, '='))) {
151 pa_log("[%s:%u] Missing '='.", filename
, line
);
158 return next_assignment(filename
, line
, *section
, t
, strip(b
), strip(e
), userdata
);
161 /* Go through the file and parse each line */
162 int pa_config_parse(const char *filename
, FILE *f
, const pa_config_item
*t
, void *userdata
) {
165 pa_bool_t do_close
= !f
;
166 char *section
= NULL
;
171 if (!f
&& !(f
= pa_fopen_cloexec(filename
, "r"))) {
172 if (errno
== ENOENT
) {
173 pa_log_debug("Failed to open configuration file '%s': %s", filename
, pa_cstrerror(errno
));
178 pa_log_warn("Failed to open configuration file '%s': %s", filename
, pa_cstrerror(errno
));
185 if (!fgets(l
, sizeof(l
), f
)) {
189 pa_log_warn("Failed to read configuration file '%s': %s", filename
, pa_cstrerror(errno
));
193 if (parse_line(filename
, ++line
, §ion
, t
, l
, userdata
) < 0)
208 int pa_config_parse_int(const char *filename
, unsigned line
, const char *section
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
217 if (pa_atoi(rvalue
, &k
) < 0) {
218 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
226 int pa_config_parse_unsigned(const char *filename
, unsigned line
, const char *section
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
235 if (pa_atou(rvalue
, &k
) < 0) {
236 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
244 int pa_config_parse_size(const char *filename
, unsigned line
, const char *section
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
253 if (pa_atou(rvalue
, &k
) < 0) {
254 pa_log("[%s:%u] Failed to parse numeric value: %s", filename
, line
, rvalue
);
262 int pa_config_parse_bool(const char *filename
, unsigned line
, const char *section
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
271 if ((k
= pa_parse_boolean(rvalue
)) < 0) {
272 pa_log("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
281 int pa_config_parse_not_bool(
282 const char *filename
, unsigned line
,
284 const char *lvalue
, const char *rvalue
,
285 void *data
, void *userdata
) {
295 if ((k
= pa_parse_boolean(rvalue
)) < 0) {
296 pa_log("[%s:%u] Failed to parse boolean value: %s", filename
, line
, rvalue
);
305 int pa_config_parse_string(const char *filename
, unsigned line
, const char *section
, const char *lvalue
, const char *rvalue
, void *data
, void *userdata
) {
314 *s
= *rvalue
? pa_xstrdup(rvalue
) : NULL
;