2001-03-18 23:32:31 +00:00
|
|
|
/*
|
|
|
|
* command line and config file parser
|
2001-04-11 00:17:08 +00:00
|
|
|
* by Szabolcs Berecz <szabi@inf.elte.hu>
|
|
|
|
* (C) 2001
|
2001-11-02 01:31:27 +00:00
|
|
|
*
|
|
|
|
* subconfig support by alex
|
2001-03-18 23:32:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2001-04-11 00:17:08 +00:00
|
|
|
#include <errno.h>
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 02:55:51 +00:00
|
|
|
#include "mp_msg.h"
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
#define COMMAND_LINE 0
|
|
|
|
#define CONFIG_FILE 1
|
|
|
|
|
2001-03-20 22:55:50 +00:00
|
|
|
#define MAX_RECURSION_DEPTH 8
|
2001-03-19 21:06:56 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cfgparser.h"
|
|
|
|
|
|
|
|
static struct config *config;
|
|
|
|
static int nr_options; /* number of options in 'conf' */
|
|
|
|
static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
|
2001-03-19 21:06:56 +00:00
|
|
|
static int recursion_depth = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
static int init_conf(struct config *conf, int mode)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(conf != NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* calculate the number of options in 'conf' */
|
|
|
|
for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
|
|
|
|
/* NOTHING */;
|
|
|
|
|
|
|
|
config = conf;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "init_conf: wrong mode!\n");
|
2001-03-18 23:32:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
parser_mode = mode;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
static int read_option(struct config *conf, int conf_optnr, char *opt, char *param)
|
2001-03-18 23:32:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
2001-03-22 18:52:45 +00:00
|
|
|
long tmp_int;
|
|
|
|
double tmp_float;
|
2001-03-19 14:35:38 +00:00
|
|
|
int ret = -1;
|
2001-03-22 18:52:45 +00:00
|
|
|
char *endptr;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "read_option: conf=%p optnr=%d opt='%s' param='%s'\n",
|
2001-11-02 02:02:52 +00:00
|
|
|
conf, conf_optnr, opt, param);
|
2001-11-02 01:31:27 +00:00
|
|
|
for (i = 0; i < conf_optnr; i++) {
|
2001-08-15 19:26:22 +00:00
|
|
|
int namelength;
|
|
|
|
/* allow 'aa*' in config.name */
|
2001-11-02 01:31:27 +00:00
|
|
|
namelength=strlen(conf[i].name);
|
|
|
|
if ( (conf[i].name[namelength-1]=='*') &&
|
|
|
|
!memcmp(opt, conf[i].name, namelength-1))
|
2001-08-15 19:26:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (!strcasecmp(opt, conf[i].name))
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-11-02 01:31:27 +00:00
|
|
|
if (i == conf_optnr) {
|
2001-10-22 20:22:25 +00:00
|
|
|
if (parser_mode == CONFIG_FILE)
|
2001-12-23 21:18:06 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid option: %s\n", opt);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "read_option: name='%s' p=%p type=%d\n",
|
2001-11-02 02:02:52 +00:00
|
|
|
conf[i].name, conf[i].p, conf[i].type);
|
2001-11-02 02:55:51 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) {
|
2001-12-23 21:18:06 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used on command line: %s\n", opt);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) {
|
2001-12-23 21:18:06 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "this option can only be used in config file: %s\n", opt);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-19 01:49:44 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
switch (conf[i].type) {
|
2001-03-18 23:32:31 +00:00
|
|
|
case CONF_TYPE_FLAG:
|
|
|
|
/* flags need a parameter in config file */
|
|
|
|
if (parser_mode == CONFIG_FILE) {
|
|
|
|
if (!strcasecmp(param, "yes") || /* any other language? */
|
|
|
|
!strcasecmp(param, "ja") ||
|
2001-03-19 15:09:47 +00:00
|
|
|
!strcasecmp(param, "si") ||
|
2001-03-18 23:32:31 +00:00
|
|
|
!strcasecmp(param, "igen") ||
|
|
|
|
!strcasecmp(param, "y") ||
|
2001-08-15 19:26:22 +00:00
|
|
|
!strcasecmp(param, "j") ||
|
2001-03-18 23:32:31 +00:00
|
|
|
!strcasecmp(param, "i") ||
|
|
|
|
!strcmp(param, "1"))
|
2001-11-02 01:31:27 +00:00
|
|
|
*((int *) conf[i].p) = conf[i].max;
|
2001-03-19 02:29:37 +00:00
|
|
|
else if (!strcasecmp(param, "no") ||
|
2001-03-18 23:32:31 +00:00
|
|
|
!strcasecmp(param, "nein") ||
|
|
|
|
!strcasecmp(param, "nicht") ||
|
|
|
|
!strcasecmp(param, "nem") ||
|
|
|
|
!strcasecmp(param, "n") ||
|
|
|
|
!strcmp(param, "0"))
|
2001-11-02 01:31:27 +00:00
|
|
|
*((int *) conf[i].p) = conf[i].min;
|
2001-03-19 14:35:38 +00:00
|
|
|
else {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "invalid parameter for flag: %s\n", param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
} else { /* parser_mode == COMMAND_LINE */
|
2001-11-02 01:31:27 +00:00
|
|
|
*((int *) conf[i].p) = conf[i].max;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONF_TYPE_INT:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-22 18:52:45 +00:00
|
|
|
|
|
|
|
tmp_int = strtol(param, &endptr, 0);
|
|
|
|
if (*endptr) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be an integer: %s\n", param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MIN)
|
|
|
|
if (tmp_int < conf[i].min) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d: %s\n", (int) conf[i].min, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MAX)
|
|
|
|
if (tmp_int > conf[i].max) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d: %s\n", (int) conf[i].max, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
*((int *) conf[i].p) = tmp_int;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
case CONF_TYPE_FLOAT:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-22 18:52:45 +00:00
|
|
|
|
|
|
|
tmp_float = strtod(param, &endptr);
|
2001-10-01 15:58:31 +00:00
|
|
|
|
|
|
|
if ((*endptr == ':') || (*endptr == '/'))
|
|
|
|
tmp_float /= strtod(endptr+1, &endptr);
|
|
|
|
|
2001-03-22 18:52:45 +00:00
|
|
|
if (*endptr) {
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be a floating point number"
|
2001-12-17 15:50:36 +00:00
|
|
|
" or a ratio (numerator[:/]denominator): %s\n", param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_MISSING_PARAM;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MIN)
|
|
|
|
if (tmp_float < conf[i].min) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %f: %s\n", conf[i].min, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MAX)
|
|
|
|
if (tmp_float > conf[i].max) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %f: %s\n", conf[i].max, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
*((float *) conf[i].p) = tmp_float;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
case CONF_TYPE_STRING:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MIN)
|
|
|
|
if (strlen(param) < conf[i].min) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be >= %d chars: %s\n",
|
|
|
|
(int) conf[i].min, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
if (conf[i].flags & CONF_MAX)
|
|
|
|
if (strlen(param) > conf[i].max) {
|
2001-12-17 15:50:36 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "parameter must be <= %d chars: %s\n",
|
|
|
|
(int) conf[i].max, param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-11-02 01:31:27 +00:00
|
|
|
*((char **) conf[i].p) = strdup(param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
2001-03-19 02:29:37 +00:00
|
|
|
case CONF_TYPE_FUNC_PARAM:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-11-02 01:31:27 +00:00
|
|
|
if ((((cfg_func_param_t) conf[i].p)(config + i, param)) < 0) {
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_FUNC_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 1;
|
2001-03-19 02:29:37 +00:00
|
|
|
break;
|
2001-08-15 19:26:22 +00:00
|
|
|
case CONF_TYPE_FUNC_FULL:
|
|
|
|
if (param!=NULL && param[0]=='-'){
|
2001-11-02 01:31:27 +00:00
|
|
|
ret=((cfg_func_arg_param_t) conf[i].p)(config + i, opt, NULL);
|
2001-08-15 19:26:22 +00:00
|
|
|
if (ret>=0) ret=0;
|
|
|
|
/* if we return >=0: param is processed again (if there is any) */
|
|
|
|
}else{
|
2001-11-02 01:31:27 +00:00
|
|
|
ret=((cfg_func_arg_param_t) conf[i].p)(config + i, opt, param);
|
2001-08-15 19:26:22 +00:00
|
|
|
/* if we return 0: need no param, precess it again */
|
|
|
|
/* if we return 1: accepted param */
|
|
|
|
}
|
|
|
|
break;
|
2001-03-19 01:49:44 +00:00
|
|
|
case CONF_TYPE_FUNC:
|
2001-11-02 01:31:27 +00:00
|
|
|
if ((((cfg_func_t) conf[i].p)(config + i)) < 0) {
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_FUNC_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 0;
|
2001-03-19 01:49:44 +00:00
|
|
|
break;
|
2001-11-02 01:31:27 +00:00
|
|
|
case CONF_TYPE_SUBCONFIG:
|
|
|
|
{
|
|
|
|
char *subparam;
|
|
|
|
char *subopt;
|
|
|
|
int subconf_optnr;
|
|
|
|
struct config *subconf;
|
|
|
|
char *token;
|
2001-12-26 13:15:44 +00:00
|
|
|
int sscanf_ret;
|
2001-11-02 01:31:27 +00:00
|
|
|
|
|
|
|
if (param == NULL)
|
|
|
|
goto err_missing_param;
|
|
|
|
|
2001-12-19 13:15:49 +00:00
|
|
|
subparam = malloc(strlen(param)+1);
|
|
|
|
subopt = malloc(strlen(param)+1);
|
2001-11-02 01:31:27 +00:00
|
|
|
|
|
|
|
subconf = conf[i].p;
|
|
|
|
for (subconf_optnr = 0; subconf[subconf_optnr].name != NULL; subconf_optnr++)
|
|
|
|
/* NOTHING */;
|
|
|
|
|
|
|
|
token = strtok(param, (char *)&(":"));
|
|
|
|
while(token)
|
|
|
|
{
|
2001-12-26 13:15:44 +00:00
|
|
|
/* clear out */
|
|
|
|
subopt[0] = subparam[0] = 0;
|
2001-11-02 01:31:27 +00:00
|
|
|
|
2001-12-26 13:15:44 +00:00
|
|
|
// sscanf_ret = sscanf(token, "%[^=]=%[^\n\0]", subopt, subparam);
|
2001-12-17 15:50:36 +00:00
|
|
|
sscanf_ret = sscanf(token, "%[^=]=%s", subopt, subparam);
|
2001-12-26 13:15:44 +00:00
|
|
|
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "token: '%s', i=%d, subopt='%s', subparam='%s' (ret: %d)\n", token, i, subopt, subparam, sscanf_ret);
|
2001-12-17 15:50:36 +00:00
|
|
|
switch(sscanf_ret)
|
2001-11-02 02:02:52 +00:00
|
|
|
{
|
|
|
|
case 1:
|
2001-12-26 13:15:44 +00:00
|
|
|
subparam[0] = 0;
|
2001-11-02 02:02:52 +00:00
|
|
|
case 2:
|
2001-12-17 15:50:36 +00:00
|
|
|
if ((ret = read_option((struct config *)subconf, subconf_optnr, subopt, subparam)) < 0)
|
2001-11-02 02:02:52 +00:00
|
|
|
{
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Subconfig parsing returned error: %d in token: %s\n",
|
2001-12-17 15:50:36 +00:00
|
|
|
ret, token);
|
|
|
|
goto out;
|
2001-11-02 02:02:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Invalid subconfig argument! ('%s')\n", token);
|
2001-12-17 15:50:36 +00:00
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
2001-11-02 02:02:52 +00:00
|
|
|
}
|
2001-11-02 01:31:27 +00:00
|
|
|
token = strtok(NULL, (char *)&(":"));
|
|
|
|
}
|
|
|
|
|
|
|
|
free(subparam);
|
|
|
|
free(subopt);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-19 02:29:37 +00:00
|
|
|
case CONF_TYPE_PRINT:
|
2001-11-02 01:31:27 +00:00
|
|
|
printf("%s", (char *) conf[i].p);
|
2001-03-19 02:29:37 +00:00
|
|
|
exit(1);
|
2001-03-18 23:32:31 +00:00
|
|
|
default:
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Unknown config type specified in conf-mplayer.h!\n");
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-03-19 14:35:38 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
err_missing_param:
|
2001-12-23 21:18:06 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "missing parameter for option: %s\n", opt);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = ERR_MISSING_PARAM;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int parse_config_file(struct config *conf, char *conffile)
|
|
|
|
{
|
|
|
|
#define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
|
|
|
|
#define MAX_LINE_LEN 1000
|
|
|
|
#define MAX_OPT_LEN 100
|
|
|
|
#define MAX_PARAM_LEN 100
|
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
2001-03-21 02:56:06 +00:00
|
|
|
char opt[MAX_OPT_LEN + 1];
|
|
|
|
char param[MAX_PARAM_LEN + 1];
|
2001-03-19 21:58:20 +00:00
|
|
|
char c; /* for the "" and '' check */
|
2001-03-18 23:32:31 +00:00
|
|
|
int tmp;
|
|
|
|
int line_num = 0;
|
|
|
|
int line_pos; /* line pos */
|
|
|
|
int opt_pos; /* opt pos */
|
|
|
|
int param_pos; /* param pos */
|
|
|
|
int ret = 1;
|
2001-06-10 23:40:20 +00:00
|
|
|
int errors = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(conffile != NULL);
|
|
|
|
#endif
|
2001-06-10 23:06:44 +00:00
|
|
|
if (++recursion_depth > 1)
|
|
|
|
printf("Reading config file: %s", conffile);
|
|
|
|
|
|
|
|
if (recursion_depth > MAX_RECURSION_DEPTH) {
|
|
|
|
printf(": too deep 'include'. check your configfiles\n");
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-19 21:06:56 +00:00
|
|
|
|
|
|
|
if (init_conf(conf, CONFIG_FILE) == -1) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-21 02:56:06 +00:00
|
|
|
if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
|
2001-11-02 02:55:51 +00:00
|
|
|
printf("\ncan't get memory for 'line': %s", strerror(errno));
|
2001-03-19 21:06:56 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp = fopen(conffile, "r")) == NULL) {
|
2001-06-10 23:06:44 +00:00
|
|
|
if (recursion_depth > 1)
|
|
|
|
printf(": %s\n", strerror(errno));
|
2001-03-18 23:32:31 +00:00
|
|
|
free(line);
|
2001-03-19 21:06:56 +00:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
2001-06-10 23:06:44 +00:00
|
|
|
if (recursion_depth > 1)
|
|
|
|
printf("\n");
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
while (fgets(line, MAX_LINE_LEN, fp)) {
|
2001-06-10 23:40:20 +00:00
|
|
|
if (errors >= 16) {
|
|
|
|
printf("too many errors\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
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;
|
|
|
|
|
2001-04-16 22:13:12 +00:00
|
|
|
/* read option. */
|
|
|
|
for (opt_pos = 0; isprint(line[line_pos]) &&
|
|
|
|
line[line_pos] != ' ' &&
|
|
|
|
line[line_pos] != '#' &&
|
|
|
|
line[line_pos] != '='; /* NOTHING */) {
|
2001-03-18 23:32:31 +00:00
|
|
|
opt[opt_pos++] = line[line_pos++];
|
|
|
|
if (opt_pos >= MAX_OPT_LEN) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("too long option\n");
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
2001-03-18 23:32:31 +00:00
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
goto nextline;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt_pos == 0) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("parse error\n");
|
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
2001-03-18 23:32:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
opt[opt_pos] = '\0';
|
2001-11-02 02:55:51 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option: %s\n", opt);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip whitespaces */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* check '=' */
|
|
|
|
if (line[line_pos++] != '=') {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option without parameter\n");
|
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
2001-03-18 23:32:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* whitespaces... */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* read the parameter */
|
2001-03-19 21:58:20 +00:00
|
|
|
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;
|
|
|
|
printf("too long parameter\n");
|
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
|
|
|
goto nextline;
|
2001-03-19 21:58:20 +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;
|
|
|
|
printf("too long parameter\n");
|
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
|
|
|
goto nextline;
|
2001-03-19 21:58:20 +00:00
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
param[param_pos] = '\0';
|
|
|
|
|
|
|
|
/* did we read a parameter? */
|
|
|
|
if (param_pos == 0) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option without parameter\n");
|
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
2001-03-18 23:32:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-11-02 02:55:51 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("parameter: %s\n", param);
|
|
|
|
#endif
|
2001-11-02 02:55:51 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
/* 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;
|
|
|
|
printf("extra characters on line: %s\n", line+line_pos);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
tmp = read_option(config, nr_options, opt, param);
|
2001-03-18 23:32:31 +00:00
|
|
|
switch (tmp) {
|
|
|
|
case ERR_NOT_AN_OPTION:
|
|
|
|
case ERR_MISSING_PARAM:
|
|
|
|
case ERR_OUT_OF_RANGE:
|
2001-03-19 01:49:44 +00:00
|
|
|
case ERR_FUNC_ERR:
|
|
|
|
PRINT_LINENUM;
|
2001-03-19 14:35:38 +00:00
|
|
|
printf("%s\n", opt);
|
2001-03-19 01:49:44 +00:00
|
|
|
ret = -1;
|
2001-06-10 23:40:20 +00:00
|
|
|
errors++;
|
2001-03-19 01:49:44 +00:00
|
|
|
continue;
|
|
|
|
/* break */
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
2001-06-11 00:12:09 +00:00
|
|
|
nextline:
|
2001-07-12 15:08:02 +00:00
|
|
|
;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
2001-03-19 21:06:56 +00:00
|
|
|
out:
|
|
|
|
--recursion_depth;
|
2001-03-18 23:32:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-08-22 19:29:47 +00:00
|
|
|
int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char ***filenames)
|
2001-03-18 23:32:31 +00:00
|
|
|
{
|
|
|
|
int i;
|
2001-08-22 19:29:47 +00:00
|
|
|
char **f = NULL;
|
|
|
|
int f_nr = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
int tmp;
|
|
|
|
char *opt;
|
2001-11-02 00:25:28 +00:00
|
|
|
int no_more_opts = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(envp != NULL);
|
|
|
|
assert(argc >= 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (init_conf(conf, COMMAND_LINE) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2001-06-10 23:06:44 +00:00
|
|
|
/* in order to work recursion detection properly in parse_config_file */
|
|
|
|
++recursion_depth;
|
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
for (i = 1; i < argc; i++) {
|
2001-11-02 00:25:28 +00:00
|
|
|
next:
|
2001-03-18 23:32:31 +00:00
|
|
|
opt = argv[i];
|
2001-11-03 19:04:58 +00:00
|
|
|
/* check for -- (no more options id.) except --help! */
|
2001-11-02 02:21:57 +00:00
|
|
|
if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) != 'h'))
|
2001-11-02 00:25:28 +00:00
|
|
|
{
|
|
|
|
no_more_opts = 1;
|
2001-11-02 02:55:51 +00:00
|
|
|
if (i+1 >= argc)
|
|
|
|
{
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "You added '--' but no filenames presented!\n");
|
|
|
|
goto err_out;
|
|
|
|
}
|
2001-11-02 00:25:28 +00:00
|
|
|
i++;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
2001-11-03 19:04:58 +00:00
|
|
|
if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
|
2001-11-02 00:25:28 +00:00
|
|
|
{
|
|
|
|
/* remove trailing '-' */
|
|
|
|
opt++;
|
2001-11-02 02:55:51 +00:00
|
|
|
|
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
|
2001-11-02 00:25:28 +00:00
|
|
|
|
2001-11-02 01:31:27 +00:00
|
|
|
tmp = read_option(config, nr_options, opt, argv[i + 1]);
|
2001-11-02 00:25:28 +00:00
|
|
|
|
|
|
|
switch (tmp) {
|
|
|
|
case ERR_NOT_AN_OPTION:
|
|
|
|
case ERR_MISSING_PARAM:
|
|
|
|
case ERR_OUT_OF_RANGE:
|
|
|
|
case ERR_FUNC_ERR:
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Error %d while parsing option: '%s'!\n",
|
2001-11-02 00:25:28 +00:00
|
|
|
tmp, opt);
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_out;
|
2001-11-02 00:25:28 +00:00
|
|
|
default:
|
2001-06-09 17:30:40 +00:00
|
|
|
i += tmp;
|
2001-11-02 00:25:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* filename */
|
|
|
|
{
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = filename: %s\n", opt);
|
|
|
|
|
2001-11-02 00:25:28 +00:00
|
|
|
/* opt is not an option -> treat it as a filename */
|
|
|
|
if (!(f = (char **) realloc(f, sizeof(*f) * (f_nr + 2))))
|
|
|
|
goto err_out_mem;
|
|
|
|
|
|
|
|
f[f_nr++] = argv[i];
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
2001-06-10 23:06:44 +00:00
|
|
|
}
|
2001-11-02 00:25:28 +00:00
|
|
|
|
2001-08-22 19:29:47 +00:00
|
|
|
if (f)
|
|
|
|
f[f_nr] = NULL;
|
|
|
|
if (filenames)
|
|
|
|
*filenames = f;
|
2001-06-10 23:06:44 +00:00
|
|
|
--recursion_depth;
|
2001-08-22 19:29:47 +00:00
|
|
|
return f_nr; //filenames_nr;
|
|
|
|
err_out_mem:
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "can't allocate memory for filenames (%s)\n", strerror(errno));
|
2001-03-19 14:35:38 +00:00
|
|
|
err_out:
|
2001-06-10 23:06:44 +00:00
|
|
|
--recursion_depth;
|
2001-11-02 02:55:51 +00:00
|
|
|
mp_msg(MSGT_CFGPARSER, MSGL_ERR, "command line: %s\n", argv[i]);
|
2001-03-19 14:35:38 +00:00
|
|
|
return -1;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|