2010-01-30 23:24:23 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2010-01-30 23:24:23 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
2010-01-30 23:24:23 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2010-01-30 23:24:23 +00:00
|
|
|
* 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
|
2015-04-13 07:36:54 +00:00
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-30 23:24:23 +00:00
|
|
|
*/
|
2006-04-25 01:26:10 +00:00
|
|
|
|
2002-11-12 01:56:42 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2012-02-03 07:05:11 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "parse_configfile.h"
|
Do not call strerror()
...because everything is terrible.
strerror() is not documented as having to be thread-safe by POSIX and
C11. (Which is pretty much bullshit, because both mandate threads and
some form of thread-local storage - so there's no excuse why
implementation couldn't implement this in a thread-safe way. Especially
with C11 this is ridiculous, because there is no way to use threads and
convert error numbers to strings at the same time!)
Since we heavily use threads now, we should avoid unsafe functions like
strerror().
strerror_r() is in POSIX, but GNU/glibc deliberately fucks it up and
gives the function different semantics than the POSIX one. It's a bit of
work to convince this piece of shit to expose the POSIX standard
function, and not the messed up GNU one.
strerror_l() is also in POSIX, but only since the 2008 standard, and
thus is not widespread.
The solution is using avlibc (libavutil, by its official name), which
handles the unportable details for us, mostly. We avoid some pain.
2014-11-26 20:21:56 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2014-07-01 21:10:38 +00:00
|
|
|
#include "misc/ctype.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "m_option.h"
|
2002-11-12 01:56:42 +00:00
|
|
|
#include "m_config.h"
|
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
// Skip whitespace and comments (assuming there are no line breaks)
|
|
|
|
static bool skip_ws(bstr *s)
|
|
|
|
{
|
|
|
|
*s = bstr_lstrip(*s);
|
|
|
|
if (bstr_startswith0(*s, "#"))
|
|
|
|
s->len = 0;
|
|
|
|
return s->len;
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
|
2015-04-10 18:40:50 +00:00
|
|
|
int m_config_parse(m_config_t *config, const char *location, bstr data,
|
|
|
|
char *initial_section, int flags)
|
2002-11-12 01:56:42 +00:00
|
|
|
{
|
2014-04-19 20:05:17 +00:00
|
|
|
m_profile_t *profile = m_config_add_profile(config, initial_section);
|
2015-04-03 22:02:34 +00:00
|
|
|
void *tmp = talloc_new(NULL);
|
|
|
|
int line_no = 0;
|
|
|
|
int errors = 0;
|
2011-07-27 18:11:10 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
bstr_eatstart0(&data, "\xEF\xBB\xBF"); // skip BOM
|
2011-07-27 18:11:10 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
while (data.len) {
|
|
|
|
talloc_free_children(tmp);
|
|
|
|
bool ok = false;
|
2014-07-12 19:24:55 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
line_no++;
|
|
|
|
char loc[512];
|
|
|
|
snprintf(loc, sizeof(loc), "%s:%d:", location, line_no);
|
2011-07-27 18:11:10 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
bstr line = bstr_strip_linebreaks(bstr_getline(data, &data));
|
|
|
|
if (!skip_ws(&line))
|
2011-07-27 18:11:10 +00:00
|
|
|
continue;
|
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
// Profile declaration
|
|
|
|
if (bstr_eatstart0(&line, "[")) {
|
|
|
|
bstr profilename;
|
|
|
|
if (!bstr_split_tok(line, "]", &profilename, &line)) {
|
|
|
|
MP_ERR(config, "%s missing closing ]\n", loc);
|
|
|
|
goto error;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
if (skip_ws(&line)) {
|
|
|
|
MP_ERR(config, "%s unparseable extra characters: '%.*s'\n",
|
|
|
|
loc, BSTR_P(line));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
profile = m_config_add_profile(config, bstrto0(tmp, profilename));
|
2011-07-27 18:11:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
bstr_eatstart0(&line, "--");
|
|
|
|
|
|
|
|
bstr option = line;
|
|
|
|
while (line.len && (mp_isalnum(line.start[0]) || line.start[0] == '_' ||
|
|
|
|
line.start[0] == '-'))
|
|
|
|
line = bstr_cut(line, 1);
|
|
|
|
option.len = option.len - line.len;
|
|
|
|
skip_ws(&line);
|
|
|
|
|
|
|
|
bstr value = {0};
|
|
|
|
if (bstr_eatstart0(&line, "=")) {
|
|
|
|
skip_ws(&line);
|
|
|
|
if (line.len && (line.start[0] == '"' || line.start[0] == '\'')) {
|
|
|
|
// Simple quoting, like "value"
|
|
|
|
char term[2] = {line.start[0], 0};
|
|
|
|
line = bstr_cut(line, 1);
|
|
|
|
if (!bstr_split_tok(line, term, &value, &line)) {
|
|
|
|
MP_ERR(config, "%s unterminated quote\n", loc);
|
|
|
|
goto error;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
} else if (bstr_eatstart0(&line, "%")) {
|
|
|
|
// Quoting with length, like %5%value
|
|
|
|
bstr rest;
|
|
|
|
long long len = bstrtoll(line, &rest, 10);
|
|
|
|
if (rest.len == line.len || !bstr_eatstart0(&rest, "%") ||
|
|
|
|
len > rest.len)
|
|
|
|
{
|
2015-04-17 14:06:43 +00:00
|
|
|
MP_ERR(config, "%s fixed-length quoting expected - put "
|
|
|
|
"\"quotes\" around the option value if you did not "
|
|
|
|
"intend to use this, but your option value starts "
|
|
|
|
"with '%%'\n", loc);
|
2015-04-03 22:02:34 +00:00
|
|
|
goto error;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 23:11:38 +00:00
|
|
|
value = bstr_splice(rest, 0, len);
|
|
|
|
line = bstr_cut(rest, len);
|
2015-04-03 22:02:34 +00:00
|
|
|
} else {
|
|
|
|
// No quoting; take everything until the comment or end of line
|
|
|
|
int end = bstrchr(line, '#');
|
|
|
|
value = bstr_strip(end < 0 ? line : bstr_splice(line, 0, end));
|
|
|
|
line.len = 0;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
}
|
|
|
|
if (skip_ws(&line)) {
|
|
|
|
MP_ERR(config, "%s unparseable extra characters: '%.*s'\n",
|
|
|
|
loc, BSTR_P(line));
|
|
|
|
goto error;
|
|
|
|
}
|
2011-07-27 18:11:10 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
int res;
|
2016-09-02 17:24:12 +00:00
|
|
|
if (bstr_equals0(option, "profile-desc")) {
|
|
|
|
m_profile_set_desc(profile, value);
|
|
|
|
res = 0;
|
2015-04-03 22:02:34 +00:00
|
|
|
} else {
|
2016-09-02 17:24:12 +00:00
|
|
|
res = m_config_set_profile_option(config, profile, option, value);
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
if (res < 0) {
|
|
|
|
MP_ERR(config, "%s setting option %.*s='%.*s' failed.\n",
|
|
|
|
loc, BSTR_P(option), BSTR_P(value));
|
|
|
|
goto error;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
ok = true;
|
|
|
|
error:
|
|
|
|
if (!ok)
|
|
|
|
errors++;
|
|
|
|
if (errors > 16) {
|
|
|
|
MP_ERR(config, "%s: too many errors, stopping.\n", location);
|
|
|
|
break;
|
2013-02-18 16:01:25 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
}
|
2013-02-18 16:01:25 +00:00
|
|
|
|
2017-01-13 11:02:46 +00:00
|
|
|
if (config->recursion_depth == 0)
|
|
|
|
m_config_finish_default_profile(config, flags);
|
2016-09-02 17:24:12 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
talloc_free(tmp);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-09-21 07:22:25 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
static bstr read_file(struct mp_log *log, const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(filename, "rb");
|
|
|
|
if (!f) {
|
|
|
|
mp_verbose(log, "Can't open config file: %s\n", mp_strerror(errno));
|
|
|
|
return (bstr){0};
|
|
|
|
}
|
|
|
|
char *data = talloc_array(NULL, char, 0);
|
|
|
|
size_t size = 0;
|
|
|
|
while (1) {
|
|
|
|
size_t left = talloc_get_size(data) - size;
|
|
|
|
if (!left) {
|
|
|
|
MP_TARRAY_GROW(NULL, data, size + 1);
|
2011-07-27 18:11:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
size_t s = fread(data + size, 1, left, f);
|
|
|
|
if (!s) {
|
|
|
|
if (ferror(f))
|
|
|
|
mp_err(log, "Error reading config file.\n");
|
|
|
|
fclose(f);
|
|
|
|
MP_TARRAY_APPEND(NULL, data, size, 0);
|
|
|
|
return (bstr){data, size - 1};
|
|
|
|
}
|
|
|
|
size += s;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2015-04-03 22:02:34 +00:00
|
|
|
assert(0);
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
|
2015-04-03 22:02:34 +00:00
|
|
|
// Load options and profiles from from a config file.
|
|
|
|
// conffile: path to the config file
|
|
|
|
// initial_section: default section where to add normal options
|
|
|
|
// flags: M_SETOPT_* bits
|
|
|
|
// returns: 1 on success, -1 on error, 0 if file not accessible.
|
|
|
|
int m_config_parse_config_file(m_config_t *config, const char *conffile,
|
|
|
|
char *initial_section, int flags)
|
|
|
|
{
|
|
|
|
flags = flags | M_SETOPT_FROM_CONFIG_FILE;
|
|
|
|
|
|
|
|
MP_VERBOSE(config, "Reading config file %s\n", conffile);
|
|
|
|
|
|
|
|
bstr data = read_file(config->log, conffile);
|
|
|
|
if (!data.start)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int r = m_config_parse(config, conffile, data, initial_section, flags);
|
|
|
|
talloc_free(data.start);
|
|
|
|
return r;
|
2002-11-12 01:56:42 +00:00
|
|
|
}
|