2010-01-30 23:24:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
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 <ctype.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"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.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"
|
|
|
|
|
2006-04-25 01:26:10 +00:00
|
|
|
/// Maximal include depth.
|
2011-07-27 18:11:10 +00:00
|
|
|
#define MAX_RECURSION_DEPTH 8
|
2002-11-12 01:56:42 +00:00
|
|
|
|
2006-04-25 01:26:10 +00:00
|
|
|
/// Current include depth.
|
2002-11-12 01:56:42 +00:00
|
|
|
static int recursion_depth = 0;
|
|
|
|
|
2006-04-25 01:26:10 +00:00
|
|
|
/// Setup the \ref Config from a config file.
|
|
|
|
/** \param config The config object.
|
|
|
|
* \param conffile Path to the config file.
|
2013-08-02 15:59:43 +00:00
|
|
|
* \param flags M_SETOPT_* bits
|
2012-08-04 01:46:11 +00:00
|
|
|
* \return 1 on sucess, -1 on error, 0 if file not accessible.
|
2006-04-25 01:26:10 +00:00
|
|
|
*/
|
2013-08-02 15:59:43 +00:00
|
|
|
int m_config_parse_config_file(m_config_t *config, const char *conffile,
|
|
|
|
int flags)
|
2002-11-12 01:56:42 +00:00
|
|
|
{
|
2013-12-21 19:08:34 +00:00
|
|
|
#define PRINT_LINENUM MP_ERR(config, "%s:%d: ", conffile, line_num)
|
2011-07-27 18:11:10 +00:00
|
|
|
#define MAX_LINE_LEN 10000
|
|
|
|
#define MAX_OPT_LEN 1000
|
|
|
|
#define MAX_PARAM_LEN 1500
|
2012-10-30 17:28:34 +00:00
|
|
|
FILE *fp = NULL;
|
|
|
|
char *line = NULL;
|
2011-07-27 18:11:10 +00:00
|
|
|
char opt[MAX_OPT_LEN + 1];
|
|
|
|
char param[MAX_PARAM_LEN + 1];
|
|
|
|
char c; /* for the "" and '' check */
|
|
|
|
int tmp;
|
|
|
|
int line_num = 0;
|
|
|
|
int line_pos; /* line pos */
|
|
|
|
int opt_pos; /* opt pos */
|
|
|
|
int param_pos; /* param pos */
|
|
|
|
int ret = 1;
|
|
|
|
int errors = 0;
|
|
|
|
m_profile_t *profile = NULL;
|
|
|
|
|
2013-08-02 15:59:43 +00:00
|
|
|
flags = flags | M_SETOPT_FROM_CONFIG_FILE;
|
|
|
|
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_VERBOSE(config, "Reading config file %s", conffile);
|
2011-07-27 18:11:10 +00:00
|
|
|
|
|
|
|
if (recursion_depth > MAX_RECURSION_DEPTH) {
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, ": too deep 'include'. check your configfiles\n");
|
2011-07-27 18:11:10 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2012-09-21 07:22:25 +00:00
|
|
|
}
|
2011-07-27 18:11:10 +00:00
|
|
|
|
|
|
|
if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_FATAL(config, "\ncan't get memory for 'line': %s", strerror(errno));
|
2011-07-27 18:11:10 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
} else
|
|
|
|
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_VERBOSE(config, "\n");
|
2011-07-27 18:11:10 +00:00
|
|
|
|
|
|
|
if ((fp = fopen(conffile, "r")) == NULL) {
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_VERBOSE(config, ": %s\n", strerror(errno));
|
2011-07-27 18:11:10 +00:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, MAX_LINE_LEN, fp)) {
|
|
|
|
if (errors >= 16) {
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_FATAL(config, "too many errors\n");
|
2011-07-27 18:11:10 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
line_num++;
|
|
|
|
line_pos = 0;
|
|
|
|
|
|
|
|
/* skip whitespaces */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* EOL / comment */
|
|
|
|
if (line[line_pos] == '\0' || line[line_pos] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* read option. */
|
|
|
|
for (opt_pos = 0; isprint(line[line_pos]) &&
|
|
|
|
line[line_pos] != ' ' &&
|
|
|
|
line[line_pos] != '#' &&
|
|
|
|
line[line_pos] != '='; /* NOTHING */) {
|
|
|
|
opt[opt_pos++] = line[line_pos++];
|
|
|
|
if (opt_pos >= MAX_OPT_LEN) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "too long option\n");
|
2011-07-27 18:11:10 +00:00
|
|
|
errors++;
|
|
|
|
ret = -1;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt_pos == 0) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "parse error\n");
|
2011-07-27 18:11:10 +00:00
|
|
|
ret = -1;
|
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
opt[opt_pos] = '\0';
|
|
|
|
|
|
|
|
/* Profile declaration */
|
|
|
|
if (opt_pos > 2 && opt[0] == '[' && opt[opt_pos - 1] == ']') {
|
|
|
|
opt[opt_pos - 1] = '\0';
|
|
|
|
if (strcmp(opt + 1, "default"))
|
|
|
|
profile = m_config_add_profile(config, opt + 1);
|
|
|
|
else
|
|
|
|
profile = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
|
2011-07-27 18:11:10 +00:00
|
|
|
/* skip whitespaces */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
2012-09-20 01:33:57 +00:00
|
|
|
param_pos = 0;
|
2012-09-21 07:22:25 +00:00
|
|
|
bool param_set = false;
|
2011-07-27 18:11:10 +00:00
|
|
|
|
2012-09-20 01:33:57 +00:00
|
|
|
/* check '=' */
|
|
|
|
if (line[line_pos] == '=') {
|
|
|
|
line_pos++;
|
2012-09-21 07:22:25 +00:00
|
|
|
param_set = true;
|
|
|
|
|
2012-09-20 01:33:57 +00:00
|
|
|
/* whitespaces... */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* read the parameter */
|
|
|
|
if (line[line_pos] == '"' || line[line_pos] == '\'') {
|
|
|
|
c = line[line_pos];
|
|
|
|
++line_pos;
|
|
|
|
for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
|
|
|
|
param[param_pos++] = line[line_pos++];
|
|
|
|
if (param_pos >= MAX_PARAM_LEN) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "option %s has a too long parameter\n", opt);
|
2012-09-20 01:33:57 +00:00
|
|
|
ret = -1;
|
|
|
|
errors++;
|
|
|
|
goto nextline;
|
|
|
|
}
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2012-09-20 01:33:57 +00:00
|
|
|
line_pos++; /* skip the closing " or ' */
|
2013-10-14 19:00:00 +00:00
|
|
|
goto param_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line[line_pos] == '%') {
|
|
|
|
char *start = &line[line_pos + 1];
|
|
|
|
char *end = start;
|
|
|
|
unsigned long len = strtoul(start, &end, 10);
|
|
|
|
if (start != end && end[0] == '%') {
|
|
|
|
if (len >= MAX_PARAM_LEN - 1 ||
|
|
|
|
strlen(end + 1) < len)
|
|
|
|
{
|
2012-09-20 01:33:57 +00:00
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "bogus %% length\n");
|
2012-09-20 01:33:57 +00:00
|
|
|
ret = -1;
|
|
|
|
errors++;
|
|
|
|
goto nextline;
|
|
|
|
}
|
2013-10-14 19:00:00 +00:00
|
|
|
param_pos = snprintf(param, sizeof(param), "%.*s",
|
|
|
|
(int)len, end + 1);
|
|
|
|
line_pos += 1 + (end - start) + 1 + len;
|
|
|
|
goto param_done;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-14 19:00:00 +00:00
|
|
|
for (param_pos = 0; isprint(line[line_pos])
|
|
|
|
&& !isspace(line[line_pos])
|
|
|
|
&& line[line_pos] != '#'; /* NOTHING */) {
|
|
|
|
param[param_pos++] = line[line_pos++];
|
|
|
|
if (param_pos >= MAX_PARAM_LEN) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "too long parameter\n");
|
2013-10-14 19:00:00 +00:00
|
|
|
ret = -1;
|
|
|
|
errors++;
|
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
param_done:
|
|
|
|
|
2012-09-20 01:33:57 +00:00
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
2011-07-27 18:11:10 +00:00
|
|
|
}
|
2012-09-20 01:33:57 +00:00
|
|
|
param[param_pos] = '\0';
|
2011-07-27 18:11:10 +00:00
|
|
|
|
|
|
|
/* EOL / comment */
|
|
|
|
if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "extra characters: %s\n", line + line_pos);
|
2011-07-27 18:11:10 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2012-09-21 07:22:25 +00:00
|
|
|
bstr bopt = bstr0(opt);
|
|
|
|
bstr bparam = bstr0(param);
|
|
|
|
|
2013-10-14 21:41:26 +00:00
|
|
|
if (bopt.len >= 3)
|
|
|
|
bstr_eatstart0(&bopt, "--");
|
|
|
|
|
2013-02-18 16:01:25 +00:00
|
|
|
if (profile && bstr_equals0(bopt, "profile-desc")) {
|
2013-10-14 21:37:58 +00:00
|
|
|
m_profile_set_desc(profile, bparam);
|
2013-02-18 16:01:25 +00:00
|
|
|
goto nextline;
|
|
|
|
}
|
|
|
|
|
options: change handling of "no-" options yet again
Commit 4a40eed "options: change handling of "no-" options" generally
improved the handling of automatically added negation options
(recognizing "--no-opt", even though only "--opt" is declared in the
option list).
Unfortunately, one corner case was missed, which broke the option
"--input=no-default-bindings" (other suboptions, e.g. VO suboptions,
were not affected, and this is the only option where this mattered).
Instead of increasing the complexity further, use a completely different
approach: add the "--no-" options at runtime, and make them behave like
real options. This approach could be considered slightly less elegant,
because the code now has to worry about some option implementation
details rather than leaving it to the parser, but all in all the new
code is simpler and there are less weird corner cases to worry about.
2013-02-16 18:57:57 +00:00
|
|
|
tmp = m_config_option_requires_param(config, bopt);
|
2012-09-21 07:22:25 +00:00
|
|
|
if (tmp > 0 && !param_set)
|
|
|
|
tmp = M_OPT_MISSING_PARAM;
|
|
|
|
if (tmp < 0) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "error parsing option %.*s=%.*s: %s\n",
|
2013-10-14 21:37:58 +00:00
|
|
|
BSTR_P(bopt), BSTR_P(bparam), m_option_strerror(tmp));
|
2012-09-21 07:22:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-07-27 18:11:10 +00:00
|
|
|
if (profile) {
|
2013-02-18 16:01:25 +00:00
|
|
|
tmp = m_config_set_profile_option(config, profile, bopt, bparam);
|
2012-09-21 07:22:25 +00:00
|
|
|
} else {
|
2013-08-02 15:59:43 +00:00
|
|
|
tmp = m_config_set_option_ext(config, bopt, bparam, flags);
|
2012-09-21 07:22:25 +00:00
|
|
|
}
|
2011-07-27 18:11:10 +00:00
|
|
|
if (tmp < 0) {
|
|
|
|
PRINT_LINENUM;
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_ERR(config, "setting option %.*s='%.*s' failed.\n",
|
2013-10-14 21:37:58 +00:00
|
|
|
BSTR_P(bopt), BSTR_P(bparam));
|
2011-07-27 18:11:10 +00:00
|
|
|
continue;
|
|
|
|
/* break */
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
nextline:
|
2011-07-27 18:11:10 +00:00
|
|
|
;
|
|
|
|
}
|
2002-11-12 01:56:42 +00:00
|
|
|
|
|
|
|
out:
|
2012-10-30 17:28:34 +00:00
|
|
|
free(line);
|
|
|
|
if (fp)
|
|
|
|
fclose(fp);
|
2011-07-27 18:11:10 +00:00
|
|
|
--recursion_depth;
|
2012-10-31 01:03:18 +00:00
|
|
|
if (ret < 0) {
|
2013-12-21 19:08:34 +00:00
|
|
|
MP_FATAL(config, "Error loading config file %s.\n",
|
2012-10-31 01:03:18 +00:00
|
|
|
conffile);
|
|
|
|
}
|
2011-07-27 18:11:10 +00:00
|
|
|
return ret;
|
2002-11-12 01:56:42 +00:00
|
|
|
}
|