4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 static const struct pa_conf default_conf
= {
43 .disallow_module_loading
= 0,
45 .module_idle_time
= 20,
46 .scache_idle_time
= 20,
48 .script_commands
= NULL
,
49 .dl_search_path
= NULL
,
50 .default_script_file
= NULL
,
51 .log_target
= PA_LOG_SYSLOG
,
54 #define ENV_SCRIPT_FILE "POLYP_SCRIPT"
55 #define ENV_CONFIG_FILE "POLYP_CONFIG"
57 #ifndef DEFAULT_SCRIPT_FILE
58 #define DEFAULT_SCRIPT_FILE "/etc/polypaudio/default.pa"
61 #ifndef DEFAULT_CONFIG_FILE
62 #define DEFAULT_CONFIG_FILE "/etc/polypaudio/config"
65 #define DEFAULT_SCRIPT_FILE_LOCAL ".polypaudio.pa"
66 #define DEFAULT_CONFIG_FILE_LOCAL ".polypaudio.conf"
68 char* default_file(const char *envvar
, const char *global
, const char *local
) {
71 assert(envvar
&& global
&& local
);
73 if ((p
= getenv(envvar
)))
76 if ((h
= getenv("HOME"))) {
78 p
= pa_sprintf_malloc("%s/%s", h
, local
);
79 if (stat(p
, &st
) >= 0)
85 return pa_xstrdup(global
);
89 struct pa_conf
* pa_conf_new(void) {
90 struct pa_conf
*c
= pa_xmemdup(&default_conf
, sizeof(default_conf
));
91 c
->default_script_file
= default_file(ENV_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE
, DEFAULT_SCRIPT_FILE_LOCAL
);
95 void pa_conf_free(struct pa_conf
*c
) {
97 pa_xfree(c
->script_commands
);
98 pa_xfree(c
->dl_search_path
);
99 pa_xfree(c
->default_script_file
);
103 #define WHITESPACE " \t\n"
104 #define COMMENTS "#;\n"
106 #define PARSE_BOOLEAN(t, v) \
108 if (!strcmp(lvalue, t)) { \
110 if ((b = pa_parse_boolean(rvalue)) < 0) \
117 #define PARSE_STRING(t, v) \
119 if (!strcmp(lvalue, t)) { \
121 c->v = *rvalue ? pa_xstrdup(rvalue) : NULL; \
126 #define PARSE_INTEGER(t, v) \
128 if (!strcmp(lvalue, t)) { \
130 int i = strtol(rvalue, &x, 0); \
138 static int next_assignment(struct pa_conf
*c
, char *lvalue
, char *rvalue
, unsigned n
) {
139 PARSE_BOOLEAN("daemonize", daemonize
);
140 PARSE_BOOLEAN("fail", fail
);
141 PARSE_BOOLEAN("verbose", verbose
);
142 PARSE_BOOLEAN("high-priority", high_priority
);
143 PARSE_BOOLEAN("disallow-module-loading", disallow_module_loading
);
145 PARSE_INTEGER("exit-idle-time", exit_idle_time
);
146 PARSE_INTEGER("module-idle-time", module_idle_time
);
147 PARSE_INTEGER("scache-idle-time", scache_idle_time
);
149 PARSE_STRING("dl-search-path", dl_search_path
);
150 PARSE_STRING("default-script-file", default_script_file
);
152 if (!strcmp(lvalue
, "log-target")) {
153 if (!strcmp(rvalue
, "auto"))
154 c
->auto_log_target
= 1;
155 else if (!strcmp(rvalue
, "syslog")) {
156 c
->auto_log_target
= 0;
157 c
->log_target
= PA_LOG_SYSLOG
;
158 } else if (!strcmp(rvalue
, "stderr")) {
159 c
->auto_log_target
= 0;
160 c
->log_target
= PA_LOG_STDERR
;
168 pa_log(__FILE__
": line %u: parse error.\n", n
);
175 static int in_string(char c
, const char *s
) {
183 static char *strip(char *s
) {
184 char *b
= s
+strspn(s
, WHITESPACE
);
188 if (!in_string(*e
, WHITESPACE
))
197 static int parse_line(struct pa_conf
*conf
, char *l
, unsigned n
) {
198 char *e
, *c
, *b
= l
+strspn(l
, WHITESPACE
);
200 if ((c
= strpbrk(b
, COMMENTS
)))
206 if (!(e
= strchr(b
, '='))) {
207 pa_log(__FILE__
": line %u: missing '='.\n", n
);
214 return next_assignment(conf
, strip(b
), strip(e
), n
);
218 int pa_conf_load(struct pa_conf
*c
, const char *filename
) {
226 filename
= def
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_LOCAL
);
228 if (!(f
= fopen(filename
, "r"))) {
230 pa_log(__FILE__
": WARNING: failed to open configuration file '%s': %s\n", filename
, strerror(errno
));
237 if (!fgets(l
, sizeof(l
), f
)) {
239 pa_log(__FILE__
": WARNING: failed to read configuration file '%s': %s\n", filename
, strerror(errno
));
244 if (parse_line(c
, l
, ++n
) < 0)
258 char *pa_conf_dump(struct pa_conf
*c
) {
259 struct pa_strbuf
*s
= pa_strbuf_new();
262 d
= default_file(ENV_CONFIG_FILE
, DEFAULT_CONFIG_FILE
, DEFAULT_CONFIG_FILE_LOCAL
);
263 pa_strbuf_printf(s
, "### Default configuration file: %s ###\n", d
);
265 pa_strbuf_printf(s
, "verbose = %i\n", !!c
->verbose
);
266 pa_strbuf_printf(s
, "daemonize = %i\n", !!c
->daemonize
);
267 pa_strbuf_printf(s
, "fail = %i\n", !!c
->fail
);
268 pa_strbuf_printf(s
, "high-priority = %i\n", !!c
->high_priority
);
269 pa_strbuf_printf(s
, "disallow-module-loading = %i\n", !!c
->disallow_module_loading
);
270 pa_strbuf_printf(s
, "exit-idle-time = %i\n", c
->exit_idle_time
);
271 pa_strbuf_printf(s
, "module-idle-time = %i\n", c
->module_idle_time
);
272 pa_strbuf_printf(s
, "scache-idle-time = %i\n", c
->scache_idle_time
);
273 pa_strbuf_printf(s
, "dl-search-path = %s\n", c
->dl_search_path
? c
->dl_search_path
: "");
274 pa_strbuf_printf(s
, "default-script-file = %s\n", c
->default_script_file
);
275 pa_strbuf_printf(s
, "log-target = %s\n", c
->auto_log_target
? "auto" : (c
->log_target
== PA_LOG_SYSLOG
? "syslog" : "stderr"));
279 return pa_strbuf_tostring_free(s
);