mirror of
https://github.com/mpv-player/mpv
synced 2025-04-20 14:16:36 +00:00
cosmetics: parser-cfg.c: reformat
This commit is contained in:
parent
507fa7e2c2
commit
d8374376c0
404
parser-cfg.c
404
parser-cfg.c
@ -16,14 +16,6 @@
|
|||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/// \defgroup ConfigParsers Config parsers
|
|
||||||
///
|
|
||||||
/// The \ref ConfigParsers make use of the \ref Config to setup the config variables,
|
|
||||||
/// the command line parsers also build the playlist.
|
|
||||||
///@{
|
|
||||||
|
|
||||||
/// \file
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -31,10 +23,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#ifdef MP_DEBUG
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "parser-cfg.h"
|
#include "parser-cfg.h"
|
||||||
#include "mp_msg.h"
|
#include "mp_msg.h"
|
||||||
@ -42,7 +31,7 @@
|
|||||||
#include "m_config.h"
|
#include "m_config.h"
|
||||||
|
|
||||||
/// Maximal include depth.
|
/// Maximal include depth.
|
||||||
#define MAX_RECURSION_DEPTH 8
|
#define MAX_RECURSION_DEPTH 8
|
||||||
|
|
||||||
/// Current include depth.
|
/// Current include depth.
|
||||||
static int recursion_depth = 0;
|
static int recursion_depth = 0;
|
||||||
@ -52,212 +41,223 @@ static int recursion_depth = 0;
|
|||||||
* \param conffile Path to the config file.
|
* \param conffile Path to the config file.
|
||||||
* \return 1 on sucess, -1 on error.
|
* \return 1 on sucess, -1 on error.
|
||||||
*/
|
*/
|
||||||
int m_config_parse_config_file(m_config_t* config, const char *conffile)
|
int m_config_parse_config_file(m_config_t *config, const char *conffile)
|
||||||
{
|
{
|
||||||
#define PRINT_LINENUM mp_msg(MSGT_CFGPARSER,MSGL_V,"%s(%d): ", conffile, line_num)
|
#define PRINT_LINENUM mp_msg(MSGT_CFGPARSER, MSGL_V, "%s(%d): ", conffile, line_num)
|
||||||
#define MAX_LINE_LEN 10000
|
#define MAX_LINE_LEN 10000
|
||||||
#define MAX_OPT_LEN 1000
|
#define MAX_OPT_LEN 1000
|
||||||
#define MAX_PARAM_LEN 1500
|
#define MAX_PARAM_LEN 1500
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
char *line;
|
char *line;
|
||||||
char opt[MAX_OPT_LEN + 1];
|
char opt[MAX_OPT_LEN + 1];
|
||||||
char param[MAX_PARAM_LEN + 1];
|
char param[MAX_PARAM_LEN + 1];
|
||||||
char c; /* for the "" and '' check */
|
char c; /* for the "" and '' check */
|
||||||
int tmp;
|
int tmp;
|
||||||
int line_num = 0;
|
int line_num = 0;
|
||||||
int line_pos; /* line pos */
|
int line_pos; /* line pos */
|
||||||
int opt_pos; /* opt pos */
|
int opt_pos; /* opt pos */
|
||||||
int param_pos; /* param pos */
|
int param_pos; /* param pos */
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
int errors = 0;
|
int errors = 0;
|
||||||
int prev_mode = config->mode;
|
int prev_mode = config->mode;
|
||||||
m_profile_t* profile = NULL;
|
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));
|
||||||
|
free(line);
|
||||||
|
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 at line %d\n", line_num);
|
||||||
|
errors++;
|
||||||
|
ret = -1;
|
||||||
|
goto nextline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (opt_pos == 0) {
|
||||||
|
PRINT_LINENUM;
|
||||||
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parse error at line %d\n",
|
||||||
|
line_num);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef MP_DEBUG
|
#ifdef MP_DEBUG
|
||||||
assert(config != NULL);
|
PRINT_LINENUM;
|
||||||
// assert(conf_list != NULL);
|
mp_msg(MSGT_CFGPARSER, MSGL_V, "option: %s\n", opt);
|
||||||
#endif
|
#endif
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_V,"Reading config file %s", conffile);
|
|
||||||
|
|
||||||
if (recursion_depth > MAX_RECURSION_DEPTH) {
|
/* skip whitespaces */
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,": too deep 'include'. check your configfiles\n");
|
while (isspace(line[line_pos]))
|
||||||
ret = -1;
|
++line_pos;
|
||||||
goto out;
|
|
||||||
} else
|
|
||||||
|
|
||||||
config->mode = M_CONFIG_FILE;
|
/* check '=' */
|
||||||
|
if (line[line_pos++] != '=') {
|
||||||
|
PRINT_LINENUM;
|
||||||
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
|
||||||
|
"Option %s needs a parameter at line %d\n", opt, line_num);
|
||||||
|
ret = -1;
|
||||||
|
errors++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if ((line = malloc(MAX_LINE_LEN + 1)) == NULL) {
|
/* whitespaces... */
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_FATAL,"\ncan't get memory for 'line': %s", strerror(errno));
|
while (isspace(line[line_pos]))
|
||||||
ret = -1;
|
++line_pos;
|
||||||
goto out;
|
|
||||||
} else
|
|
||||||
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_V,"\n");
|
/* 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 at line %d\n",
|
||||||
|
opt, line_num);
|
||||||
|
ret = -1;
|
||||||
|
errors++;
|
||||||
|
goto nextline;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
param[param_pos] = '\0';
|
||||||
|
|
||||||
if ((fp = fopen(conffile, "r")) == NULL) {
|
/* did we read a parameter? */
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_V,": %s\n", strerror(errno));
|
if (param_pos == 0) {
|
||||||
free(line);
|
PRINT_LINENUM;
|
||||||
ret = 0;
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
|
||||||
goto out;
|
"Option %s needs a parameter at line %d\n", opt, line_num);
|
||||||
}
|
ret = -1;
|
||||||
|
errors++;
|
||||||
while (fgets(line, MAX_LINE_LEN, fp)) {
|
continue;
|
||||||
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 at line %d\n",line_num);
|
|
||||||
errors++;
|
|
||||||
ret = -1;
|
|
||||||
goto nextline;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (opt_pos == 0) {
|
|
||||||
PRINT_LINENUM;
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"parse error at line %d\n",line_num);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef MP_DEBUG
|
#ifdef MP_DEBUG
|
||||||
PRINT_LINENUM;
|
PRINT_LINENUM;
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_V,"option: %s\n", opt);
|
mp_msg(MSGT_CFGPARSER, MSGL_V, "parameter: %s\n", param);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* skip whitespaces */
|
/* now, check if we have some more chars on the line */
|
||||||
while (isspace(line[line_pos]))
|
/* whitespace... */
|
||||||
++line_pos;
|
while (isspace(line[line_pos]))
|
||||||
|
++line_pos;
|
||||||
|
|
||||||
/* check '=' */
|
/* EOL / comment */
|
||||||
if (line[line_pos++] != '=') {
|
if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
||||||
PRINT_LINENUM;
|
PRINT_LINENUM;
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s needs a parameter at line %d\n",opt,line_num);
|
mp_msg(MSGT_CFGPARSER, MSGL_WARN,
|
||||||
ret = -1;
|
"extra characters on line %d: %s\n",
|
||||||
errors++;
|
line_num, line + line_pos);
|
||||||
continue;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* whitespaces... */
|
if (profile) {
|
||||||
while (isspace(line[line_pos]))
|
if (!strcmp(opt, "profile-desc"))
|
||||||
++line_pos;
|
m_profile_set_desc(profile, param), tmp = 1;
|
||||||
|
else
|
||||||
/* read the parameter */
|
tmp = m_config_set_profile_option(config, profile,
|
||||||
if (line[line_pos] == '"' || line[line_pos] == '\'') {
|
opt, param);
|
||||||
c = line[line_pos];
|
} else
|
||||||
++line_pos;
|
tmp = m_config_set_option(config, opt, param, false);
|
||||||
for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
|
if (tmp < 0) {
|
||||||
param[param_pos++] = line[line_pos++];
|
PRINT_LINENUM;
|
||||||
if (param_pos >= MAX_PARAM_LEN) {
|
if (tmp == M_OPT_UNKNOWN) {
|
||||||
PRINT_LINENUM;
|
mp_msg(MSGT_CFGPARSER, MSGL_WARN,
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s has a too long parameter at line %d\n",opt,line_num);
|
"Warning unknown option %s at line %d\n",
|
||||||
ret = -1;
|
opt, line_num);
|
||||||
errors++;
|
continue;
|
||||||
goto nextline;
|
}
|
||||||
}
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR,
|
||||||
}
|
"Error parsing option %s=%s at line %d\n",
|
||||||
line_pos++; /* skip the closing " or ' */
|
opt, param, line_num);
|
||||||
} else {
|
ret = -1;
|
||||||
for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos])
|
errors++;
|
||||||
&& line[line_pos] != '#'; /* NOTHING */) {
|
continue;
|
||||||
param[param_pos++] = line[line_pos++];
|
/* break */
|
||||||
if (param_pos >= MAX_PARAM_LEN) {
|
}
|
||||||
PRINT_LINENUM;
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long parameter\n");
|
|
||||||
ret = -1;
|
|
||||||
errors++;
|
|
||||||
goto nextline;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
param[param_pos] = '\0';
|
|
||||||
|
|
||||||
/* did we read a parameter? */
|
|
||||||
if (param_pos == 0) {
|
|
||||||
PRINT_LINENUM;
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s needs a parameter at line %d\n",opt,line_num);
|
|
||||||
ret = -1;
|
|
||||||
errors++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef MP_DEBUG
|
|
||||||
PRINT_LINENUM;
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_V,"parameter: %s\n", param);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* now, check if we have some more chars on the line */
|
|
||||||
/* whitespace... */
|
|
||||||
while (isspace(line[line_pos]))
|
|
||||||
++line_pos;
|
|
||||||
|
|
||||||
/* EOL / comment */
|
|
||||||
if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
|
||||||
PRINT_LINENUM;
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_WARN,"extra characters on line %d: %s\n",line_num, line+line_pos);
|
|
||||||
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
|
|
||||||
tmp = m_config_set_option(config, opt, param, false);
|
|
||||||
if (tmp < 0) {
|
|
||||||
PRINT_LINENUM;
|
|
||||||
if(tmp == M_OPT_UNKNOWN) {
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_WARN,"Warning unknown option %s at line %d\n", opt,line_num);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Error parsing option %s=%s at line %d\n",opt,param,line_num);
|
|
||||||
ret = -1;
|
|
||||||
errors++;
|
|
||||||
continue;
|
|
||||||
/* break */
|
|
||||||
}
|
|
||||||
nextline:
|
nextline:
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(line);
|
free(line);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
out:
|
out:
|
||||||
config->mode = prev_mode;
|
config->mode = prev_mode;
|
||||||
--recursion_depth;
|
--recursion_depth;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user