mpv/core/parser-cfg.c

236 lines
7.1 KiB
C
Raw Normal View History

/*
* 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.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include "osdep/io.h"
#include "parser-cfg.h"
#include "core/mp_msg.h"
#include "core/m_option.h"
#include "m_config.h"
/// Maximal include depth.
2011-07-27 18:11:10 +00:00
#define MAX_RECURSION_DEPTH 8
/// Current include depth.
static int recursion_depth = 0;
/// Setup the \ref Config from a config file.
/** \param config The config object.
* \param conffile Path to the config file.
* \return 1 on sucess, -1 on error, 0 if file not accessible.
*/
2011-07-27 18:11:10 +00:00
int m_config_parse_config_file(m_config_t *config, const char *conffile)
{
#define PRINT_LINENUM mp_msg(MSGT_CFGPARSER, MSGL_ERR, "%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
Fix potential bugs and issues, general cleanups Most of these are reimar fixing issues found by Coverity static analyzer, and possibly some more cleanup commits independent from this. Since these commits are rather noisy, squash them all together. Try to make code a bit clearer. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35294 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: audio/out/ao_alsa.c Check the correct variable for NULL. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35323 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless unreachable code (the loop condition already checks the 0xff case). git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35325 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix typo that might have caused reading beyond the string end. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35326 b3059339-0415-0410-9bf9-f77b7e298cf2 Do not needlessly use "long" types. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35331 b3059339-0415-0410-9bf9-f77b7e298cf2 Use AV_RB32 to avoid sign extension issues and validate offset before using it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35332 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove nonsense casts. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35343 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix crash in case sh_audio allocation failed. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35348 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix potential NULL dereference. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35351 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: libmpcodecs/ad_ffmpeg.c Note: Slightly modified. Fix malloc failure check to check the correct variable. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35353 b3059339-0415-0410-9bf9-f77b7e298cf2 Avoid code duplication and pointless casts. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35363 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/tv.c Error out if an invalid channel list name was specified instead of continuing and reading outside array bounds all over the place. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35364 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/tv.c Make array "static const". git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35365 b3059339-0415-0410-9bf9-f77b7e298cf2 Properly free resources even when encountering many parse errors. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35367 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: parser-cfg.c Avoid leaks in error handling. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35380 b3059339-0415-0410-9bf9-f77b7e298cf2 Do not do sign comparisons on "char" type which can be both signed or unsigned. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35381 b3059339-0415-0410-9bf9-f77b7e298cf2 Free cookies file data after parsing it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35382 b3059339-0415-0410-9bf9-f77b7e298cf2 http_set_field only makes a copy of the string, so we still need to free it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35383 b3059339-0415-0410-9bf9-f77b7e298cf2 check4proxies does not modify input URL, so mark it const. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35390 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove proxy "support" from stream_rtp and stream_upd, trying to use a http proxy for UDP connections makes no sense. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35394 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/stream_rtp.c stream/stream_udp.c Add url_new_with_proxy function to reduce code duplication and memleaks. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35395 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/pnm.c stream/stream_live555.c stream/stream_nemesi.c stream/stream_rtsp.c Fix off-by-one errors in file descriptor validity checks. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35402 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless cast. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35403 b3059339-0415-0410-9bf9-f77b7e298cf2 Abort when opening the file failed instead of calling "write" with an invalid descriptor. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35404 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless local variable. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35411 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/http.c
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;
int prev_mode = config->mode;
m_profile_t *profile = NULL;
mp_msg(MSGT_CFGPARSER, MSGL_V, "Reading config file %s", conffile);
if (recursion_depth > MAX_RECURSION_DEPTH) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
": too deep 'include'. check your configfiles\n");
ret = -1;
goto out;
} else
config->mode = M_CONFIG_FILE;
if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
mp_msg(MSGT_CFGPARSER, MSGL_FATAL,
"\ncan't get memory for 'line': %s", strerror(errno));
ret = -1;
goto out;
} else
mp_msg(MSGT_CFGPARSER, MSGL_V, "\n");
if ((fp = fopen(conffile, "r")) == NULL) {
mp_msg(MSGT_CFGPARSER, MSGL_V, ": %s\n", strerror(errno));
ret = 0;
goto out;
}
while (fgets(line, MAX_LINE_LEN, fp)) {
if (errors >= 16) {
mp_msg(MSGT_CFGPARSER, MSGL_FATAL, "too many errors\n");
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;
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too long option\n");
2011-07-27 18:11:10 +00:00
errors++;
ret = -1;
goto nextline;
}
}
if (opt_pos == 0) {
PRINT_LINENUM;
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "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;
}
2011-07-27 18:11:10 +00:00
/* skip whitespaces */
while (isspace(line[line_pos]))
++line_pos;
param_pos = 0;
2011-07-27 18:11:10 +00:00
/* check '=' */
if (line[line_pos] == '=') {
line_pos++;
/* 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;
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"option %s has a too long parameter\n", opt);
ret = -1;
errors++;
goto nextline;
}
2011-07-27 18:11:10 +00:00
}
line_pos++; /* skip the closing " or ' */
} else {
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;
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too long parameter\n");
ret = -1;
errors++;
goto nextline;
}
2011-07-27 18:11:10 +00:00
}
}
while (isspace(line[line_pos]))
++line_pos;
2011-07-27 18:11:10 +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;
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"extra characters: %s\n", line + line_pos);
2011-07-27 18:11:10 +00:00
ret = -1;
}
if (profile) {
if (!strcmp(opt, "profile-desc"))
m_profile_set_desc(profile, param), tmp = 1;
else
tmp = m_config_set_profile_option(config, profile,
opt, param);
} else
options: get rid of ambiguous option parsing Options parsing used to be ambiguous, as in the splitting into option and values pairs was ambiguous. Example: -option -something It wasn't clear whether -option actually takes an argument or not. The string "-something" could either be a separate option, or an argument to "-option". The code had to call the option specific parser function to resolve this. This made everything complicated and didn't even have a real use. There was only one case where this was actually used: string lists (m_option_type_string_list) and options based on it. That is because this option type actually turns a single option into a proxy for several real arguments, e.g. "vf*" can handle "-vf-add" and "-vf-clr". Options suffixed with "-clr" are the only options of this group which take no arguments. This is ambiguous only with the "old syntax" (as shown above). The "new" option syntax always puts option name and value into same argument. (E.g. "--option=--something" or "--option" "--something".) Simplify the code by making it statically known whether an option takes a parameter or not with the flag M_OPT_TYPE_OLD_SYNTAX_NO_PARAM. If it's set, the option parser assumes the option takes no argument. The only real ambiguity left, string list options that end on "-clr", are special cased in the parser. Remove some duplication of the logic in the command line parser by moving all argument splitting logic into split_opt(). (It's arguable whether that can be considered code duplication, but now the code is a bit simpler anyway. This might be subjective.) Remove the "ambiguous" parameter from all option parsing related code. Make m_config unaware of the pre-parsing concept. Make most CONF_NOCFG options also CONF_GLOBAL (except those explicitly usable as per-file options.)
2012-08-05 21:34:28 +00:00
tmp = m_config_set_option0(config, opt, param);
2011-07-27 18:11:10 +00:00
if (tmp < 0) {
PRINT_LINENUM;
if (tmp == M_OPT_UNKNOWN) {
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"unknown option '%s'\n", opt);
2011-07-27 18:11:10 +00:00
continue;
}
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
"setting option %s='%s' failed\n", opt, param);
2011-07-27 18:11:10 +00:00
continue;
/* break */
}
nextline:
2011-07-27 18:11:10 +00:00
;
}
out:
Fix potential bugs and issues, general cleanups Most of these are reimar fixing issues found by Coverity static analyzer, and possibly some more cleanup commits independent from this. Since these commits are rather noisy, squash them all together. Try to make code a bit clearer. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35294 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: audio/out/ao_alsa.c Check the correct variable for NULL. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35323 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless unreachable code (the loop condition already checks the 0xff case). git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35325 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix typo that might have caused reading beyond the string end. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35326 b3059339-0415-0410-9bf9-f77b7e298cf2 Do not needlessly use "long" types. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35331 b3059339-0415-0410-9bf9-f77b7e298cf2 Use AV_RB32 to avoid sign extension issues and validate offset before using it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35332 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove nonsense casts. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35343 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix crash in case sh_audio allocation failed. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35348 b3059339-0415-0410-9bf9-f77b7e298cf2 Fix potential NULL dereference. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35351 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: libmpcodecs/ad_ffmpeg.c Note: Slightly modified. Fix malloc failure check to check the correct variable. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35353 b3059339-0415-0410-9bf9-f77b7e298cf2 Avoid code duplication and pointless casts. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35363 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/tv.c Error out if an invalid channel list name was specified instead of continuing and reading outside array bounds all over the place. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35364 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/tv.c Make array "static const". git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35365 b3059339-0415-0410-9bf9-f77b7e298cf2 Properly free resources even when encountering many parse errors. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35367 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: parser-cfg.c Avoid leaks in error handling. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35380 b3059339-0415-0410-9bf9-f77b7e298cf2 Do not do sign comparisons on "char" type which can be both signed or unsigned. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35381 b3059339-0415-0410-9bf9-f77b7e298cf2 Free cookies file data after parsing it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35382 b3059339-0415-0410-9bf9-f77b7e298cf2 http_set_field only makes a copy of the string, so we still need to free it. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35383 b3059339-0415-0410-9bf9-f77b7e298cf2 check4proxies does not modify input URL, so mark it const. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35390 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove proxy "support" from stream_rtp and stream_upd, trying to use a http proxy for UDP connections makes no sense. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35394 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/stream_rtp.c stream/stream_udp.c Add url_new_with_proxy function to reduce code duplication and memleaks. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35395 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/pnm.c stream/stream_live555.c stream/stream_nemesi.c stream/stream_rtsp.c Fix off-by-one errors in file descriptor validity checks. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35402 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless cast. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35403 b3059339-0415-0410-9bf9-f77b7e298cf2 Abort when opening the file failed instead of calling "write" with an invalid descriptor. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35404 b3059339-0415-0410-9bf9-f77b7e298cf2 Remove pointless local variable. git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@35411 b3059339-0415-0410-9bf9-f77b7e298cf2 Conflicts: stream/http.c
2012-10-30 17:28:34 +00:00
free(line);
if (fp)
fclose(fp);
2011-07-27 18:11:10 +00:00
config->mode = prev_mode;
--recursion_depth;
if (ret < 0) {
mp_msg(MSGT_CFGPARSER, MSGL_FATAL, "Error loading config file %s.\n",
conffile);
}
2011-07-27 18:11:10 +00:00
return ret;
}