summaryrefslogtreecommitdiffstats
path: root/polyp/conf.c
blob: b74a5ede2191aa456ae87da975b17f22a2a4d689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* $Id$ */

/***
  This file is part of polypaudio.

  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  polypaudio 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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/stat.h>

#include "conf.h"
#include "util.h"
#include "xmalloc.h"
#include "strbuf.h"

static const struct pa_conf default_conf = {
    .cmd = PA_CMD_DAEMON,
    .daemonize = 0,
    .fail = 1,
    .verbose = 0,
    .high_priority = 0,
    .disallow_module_loading = 0,
    .exit_idle_time = -1,
    .module_idle_time = 20,
    .scache_idle_time = 20,
    .auto_log_target = 1,
    .script_commands = NULL,
    .dl_search_path = NULL,
    .default_script_file = NULL,
    .log_target = PA_LOG_SYSLOG,
};

#define ENV_SCRIPT_FILE "POLYP_SCRIPT"
#define ENV_CONFIG_FILE "POLYP_CONFIG"
#define ENV_AUTOSPAWNED "POLYP_AUTOSPAWNED"

#ifndef DEFAULT_SCRIPT_FILE
#define DEFAULT_SCRIPT_FILE "/etc/polypaudio/default.pa"
#endif

#ifndef DEFAULT_CONFIG_FILE
#define DEFAULT_CONFIG_FILE "/etc/polypaudio/default.conf"
#endif

#ifndef AUTOSPAWN_CONFIG_FILE
#define AUTOSPAWN_CONFIG_FILE "/etc/polypaudio/autospawn.conf"
#endif

#define DEFAULT_SCRIPT_FILE_LOCAL ".polypaudio.pa"
#define DEFAULT_CONFIG_FILE_LOCAL ".polypaudio.conf"
#define AUTOSPAWN_CONFIG_FILE_LOCAL ".polypaudio-autospawn.conf"

char* default_file(const char *envvar, const char *global, const char *local) {
    char *p, *h;

    assert(envvar && global && local);

    if ((p = getenv(envvar)))
        return pa_xstrdup(p);

    if ((h = getenv("HOME"))) {
        struct stat st;
        p = pa_sprintf_malloc("%s/%s", h, local);
        if (stat(p, &st) >= 0)
            return p;
        
        pa_xfree(p);
    }

    return pa_xstrdup(global);
}

char *default_config_file(void) {
    char *b;
    int autospawned = 0;
    
    if ((b = getenv(ENV_AUTOSPAWNED)))
        autospawned = pa_parse_boolean(b) > 0;
    
    return default_file(ENV_CONFIG_FILE,
                        autospawned ? AUTOSPAWN_CONFIG_FILE : DEFAULT_CONFIG_FILE,
                        autospawned ? AUTOSPAWN_CONFIG_FILE_LOCAL : DEFAULT_CONFIG_FILE_LOCAL);

}

char *default_script_file(void) {
    return default_file(ENV_SCRIPT_FILE, DEFAULT_SCRIPT_FILE, DEFAULT_SCRIPT_FILE_LOCAL);
}

struct pa_conf* pa_conf_new(void) {
    struct pa_conf *c = pa_xmemdup(&default_conf, sizeof(default_conf));
    c->default_script_file = default_script_file();
    return c;
}

void pa_conf_free(struct pa_conf *c) {
    assert(c);
    pa_xfree(c->script_commands);
    pa_xfree(c->dl_search_path);
    pa_xfree(c->default_script_file);
    pa_xfree(c);
}

#define WHITESPACE " \t\n"
#define COMMENTS "#;\n"

#define PARSE_BOOLEAN(t, v) \
    do { \
        if (!strcmp(lvalue, t)) { \
            int b; \
            if ((b = pa_parse_boolean(rvalue)) < 0) \
                goto fail; \
            c->v = b; \
            return 0; \
        } \
    } while (0)

#define PARSE_STRING(t, v) \
    do { \
        if (!strcmp(lvalue, t)) { \
            pa_xfree(c->v); \
            c->v = *rvalue ? pa_xstrdup(rvalue) : NULL; \
            return 0; \
        } \
    } while (0)

#define PARSE_INTEGER(t, v) \
   do { \
       if (!strcmp(lvalue, t)) { \
           char *x = NULL; \
           int i = strtol(rvalue, &x, 0); \
           if (!x || *x) \
                 goto fail; \
           c->v = i; \
           return 0; \
       } \
   } while(0)

static int next_assignment(struct pa_conf *c, char *lvalue, char *rvalue, unsigned n) {
    PARSE_BOOLEAN("daemonize", daemonize);
    PARSE_BOOLEAN("fail", fail);
    PARSE_BOOLEAN("verbose", verbose);
    PARSE_BOOLEAN("high-priority", high_priority);
    PARSE_BOOLEAN("disallow-module-loading", disallow_module_loading);

    PARSE_INTEGER("exit-idle-time", exit_idle_time);
    PARSE_INTEGER("module-idle-time", module_idle_time);
    PARSE_INTEGER("scache-idle-time", scache_idle_time);
    
    PARSE_STRING("dl-search-path", dl_search_path);
    PARSE_STRING("default-script-file", default_script_file);

    if (!strcmp(lvalue, "log-target")) {
        if (!strcmp(rvalue, "auto"))
            c->auto_log_target = 1;
        else if (!strcmp(rvalue, "syslog")) {
            c->auto_log_target = 0;
            c->log_target = PA_LOG_SYSLOG;
        } else if (!strcmp(rvalue, "stderr")) {
            c->auto_log_target = 0;
            c->log_target = PA_LOG_STDERR;
        } else
            goto fail;

        return 0;
    }
    
fail:
    pa_log(__FILE__": line %u: parse error.\n", n);
    return -1;
}

#undef PARSE_STRING
#undef PARSE_BOOLEAN

static int in_string(char c, const char *s) {
    for (; *s; s++)
        if (*s == c)
            return 1;

    return 0;
}

static char *strip(char *s) {
    char *b = s+strspn(s, WHITESPACE);
    char *e, *l = NULL;

    for (e = b; *e; e++)
        if (!in_string(*e, WHITESPACE))
            l = e;

    if (l)
        *(l+1) = 0;

    return b;
}

static int parse_line(struct pa_conf *conf, char *l, unsigned n) {
    char *e, *c, *b = l+strspn(l, WHITESPACE);

    if ((c = strpbrk(b, COMMENTS)))
        *c = 0;
    
    if (!*b)
        return 0;

    if (!(e = strchr(b, '='))) {
        pa_log(__FILE__": line %u: missing '='.\n", n);
        return -1;
    }

    *e = 0;
    e++;

    return next_assignment(conf, strip(b), strip(e), n);
}


int pa_conf_load(struct pa_conf *c, const char *filename) {
    FILE *f;
    int r = 0;
    unsigned n = 0;
    char *def = NULL;
    assert(c);
    
    if (!filename)
        filename = def = default_config_file();

    if (!(f = fopen(filename, "r"))) {
        if (errno != ENOENT)
            pa_log(__FILE__": WARNING: failed to open configuration file '%s': %s\n", filename, strerror(errno));

        goto finish;
    }

    while (!feof(f)) {
        char l[256];
        if (!fgets(l, sizeof(l), f)) {
            if (!feof(f))
                pa_log(__FILE__": WARNING: failed to read configuration file '%s': %s\n", filename, strerror(errno));

            break;
        }

        if (parse_line(c, l, ++n) < 0)
            r = -1;
    }
    
finish:

    if (f)
        fclose(f);
    
    pa_xfree(def);
    
    return r;
}

char *pa_conf_dump(struct pa_conf *c) {
    struct pa_strbuf *s = pa_strbuf_new();
    char *d;

    d = default_config_file();
    pa_strbuf_printf(s, "### Default configuration file: %s ###\n", d);
    
    pa_strbuf_printf(s, "verbose = %i\n", !!c->verbose);
    pa_strbuf_printf(s, "daemonize = %i\n", !!c->daemonize);
    pa_strbuf_printf(s, "fail = %i\n", !!c->fail);
    pa_strbuf_printf(s, "high-priority = %i\n", !!c->high_priority);
    pa_strbuf_printf(s, "disallow-module-loading = %i\n", !!c->disallow_module_loading);
    pa_strbuf_printf(s, "exit-idle-time = %i\n", c->exit_idle_time);
    pa_strbuf_printf(s, "module-idle-time = %i\n", c->module_idle_time);
    pa_strbuf_printf(s, "scache-idle-time = %i\n", c->scache_idle_time);
    pa_strbuf_printf(s, "dl-search-path = %s\n", c->dl_search_path ? c->dl_search_path : "");
    pa_strbuf_printf(s, "default-script-file = %s\n", c->default_script_file);
    pa_strbuf_printf(s, "log-target = %s\n", c->auto_log_target ? "auto" : (c->log_target == PA_LOG_SYSLOG ? "syslog" : "stderr"));
    
    pa_xfree(d);
    
    return pa_strbuf_tostring_free(s);
}