Esempio n. 1
0
int config_setting_lookup_string(const config_setting_t *setting,
                                 const char *name, const char **value)
{
  config_setting_t *member = config_setting_get_member(setting, name);
  if(! member)
    return(CONFIG_FALSE);

  if(config_setting_type(member) != CONFIG_TYPE_STRING)
    return(CONFIG_FALSE);

  *value = config_setting_get_string(member);
  return(CONFIG_TRUE);
}
Esempio n. 2
0
int config_setting_lookup_bool(const config_setting_t *setting,
                               const char *name, int *value)
{
  config_setting_t *member = config_setting_get_member(setting, name);
  if(! member)
    return(CONFIG_FALSE);

  if(config_setting_type(member) != CONFIG_TYPE_BOOL)
    return(CONFIG_FALSE);

  *value = config_setting_get_bool(member);
  return(CONFIG_TRUE);
}
Esempio n. 3
0
int config_lookup_string (const config_t * config, const char *path, const char **value)
{
    const config_setting_t *s = config_lookup (config, path);
    if (!s)
        return (CONFIG_FALSE);

    if (config_setting_type (s) != CONFIG_TYPE_STRING)
        return (CONFIG_FALSE);

    *value = config_setting_get_string (s);

    return (CONFIG_TRUE);
}
Esempio n. 4
0
int config_lookup_float(const config_t *config, const char *path,
                        double *value)
{
  const config_setting_t *s = config_lookup(config, path);
  if(! s)
    return(CONFIG_FALSE);

  if(config_setting_type(s) != CONFIG_TYPE_FLOAT)
    return(CONFIG_FALSE);

  *value = config_setting_get_float(s);
  return(CONFIG_TRUE);
}
Esempio n. 5
0
int config_setting_lookup_float(const config_setting_t *setting,
                                const char *name, double *value)
{
  config_setting_t *member = config_setting_get_member(setting, name);
  if(! member)
    return(CONFIG_FALSE);

  if(config_setting_type(member) != CONFIG_TYPE_FLOAT)
    return(CONFIG_FALSE);

  *value = config_setting_get_float(member);
  return(CONFIG_TRUE);
}
Esempio n. 6
0
int config_setting_lookup_int64(const config_setting_t *setting,
                                const char *name, long long *value)
{
  config_setting_t *member = config_setting_get_member(setting, name);
  if(! member)
    return(CONFIG_FALSE);

  if(config_setting_type(member) != CONFIG_TYPE_INT64)
    return(CONFIG_FALSE);

  *value = config_setting_get_int64(member);
  return(CONFIG_TRUE);
}
Esempio n. 7
0
int config_lookup_int64(const config_t *config, const char *path,
                        long long *value)
{
  const config_setting_t *s = config_lookup(config, path);
  if(! s)
    return(CONFIG_FALSE);

  if(config_setting_type(s) != CONFIG_TYPE_INT64)
    return(CONFIG_FALSE);

  *value = config_setting_get_int64(s);

  return(CONFIG_TRUE);
}
Esempio n. 8
0
/**
 * Same as config_setting_lookup_bool, but uses bool instead of int.
 *
 * @param[in]  setting The setting to read.
 * @param[in]  name    The setting name to lookup.
 * @param[out] value   The output value.
 *
 * @retval CONFIG_TRUE  in case of success.
 * @retval CONFIG_FALSE in case of failure.
 */
int config_setting_lookup_bool_real(const struct config_setting_t *setting, const char *name, bool *value)
{
	struct config_setting_t *member = config_setting_get_member(setting, name);

	if (!member)
		return CONFIG_FALSE;

	if (config_setting_type(member) != CONFIG_TYPE_BOOL)
		return CONFIG_FALSE;

	*value = config_setting_get_bool_real(member);

	return CONFIG_TRUE;
}
Esempio n. 9
0
/**
 * Looks up a configuration entry of type CONFIG_TYPE_INT and reads it as int16.
 *
 * @param[in]  setting The setting to read.
 * @param[in]  name    The setting name to lookup.
 * @param[out] value   The output value.
 *
 * @retval CONFIG_TRUE  in case of success.
 * @retval CONFIG_FALSE in case of failure.
 */
int config_setting_lookup_int16(const struct config_setting_t *setting, const char *name, int16 *value)
{
	struct config_setting_t *member = config_setting_get_member(setting, name);

	if (!member)
		return CONFIG_FALSE;

	if (config_setting_type(member) != CONFIG_TYPE_INT)
		return CONFIG_FALSE;

	*value = config_setting_get_int16(member);

	return CONFIG_TRUE;
}
Esempio n. 10
0
File: config.c Progetto: joshdk/muxd
int config_get_port(config_t *config,char **port){
	config_setting_t *cs_port=NULL;
	if((cs_port=config_lookup(config,"port"))==NULL){
		return 1;
	}
	switch(config_setting_type(cs_port)){
		case CONFIG_TYPE_INT:
			*port=calloc(16,sizeof(*port));
			sprintf(*port,"%lu",config_setting_get_int(cs_port));
			return 0;
		case CONFIG_TYPE_STRING:
			*port=strdup(config_setting_get_string(cs_port));
			return 0;
	}
	return 1;
}
Esempio n. 11
0
/**
 * Load uint16 array from config.
 * @param[in] cfg Configuration option.
 * @param[in] option Option name.
 * @param[in,out] array Resulting array.
 * @return Zero on success.
 */
static int load_uint16_list(const config_setting_t *cfg, const char *option, UT_array *array)
{
    config_setting_t *cfg_list = config_setting_get_member(cfg, option);

    if (!cfg_list) {
        ZERO_LOG(LOG_ERR, "config: missing %s entry", option);
        return 0;
    }

    if (config_setting_type(cfg_list) != CONFIG_TYPE_LIST) {
        ZERO_LOG(LOG_ERR, "config: invalid %s entry", option);
        return -1;
    }

    int count = config_setting_length(cfg_list);

    if (0 >= count) {
        return 0;
    }

    utarray_init(array, &ut_uint16_icd);

    for (int i = 0; i < count; i++) {
        int entry = config_setting_get_int_elem(cfg_list, i);

        if (!entry) {
            ZERO_LOG(LOG_ERR, "config: failed to get next %s record", option);
            continue;
        }

        if (entry < UINT16_MAX) {
            uint16_t port = entry;
            utarray_push_back(array, &port);
            continue;
        }

        // if we here, then entry is invalid
        ZERO_LOG(LOG_ERR, "config: invalid %s item: %d", option, entry);
        utarray_done(array);
        return -1;
    }

    utarray_sort(array, uint16_cmp);

    return 0;
}
Esempio n. 12
0
int jalu_config_lookup_string(const config_setting_t *setting,
	const char *name, char **field, int required) {
	if (!setting || !name || !field || *field) {
		//library error, should never happen
		fprintf(stderr, "Error: misuse of jalls_config_lookup_string\n");
		goto err_out;
	}
	config_setting_t *member = config_setting_get_member(setting, name);
	if (!member && required) {
		fprintf(stderr, "Config Error: line %d: missing required field \"%s\"\n",
			config_setting_source_line(setting), name);
		goto err_out;
	}
	if (!member) {
		goto out;
	}
	if(config_setting_type(member) != CONFIG_TYPE_STRING) {
		fprintf(stderr, "Config Error: line %d: field \"%s\" should be a string\n",
			config_setting_source_line(member), name);
		goto err_out;
	}
	const char *tmp = config_setting_get_string(member);
	if (!tmp && required) {
		fprintf(stderr, "Config Error: line %d: empty required field \"%s\"\n",
			config_setting_source_line(setting), name);
		goto err_out;
	}
	if (!tmp) {
		fprintf(stderr, "Config Warning: line %d: empty value for field \"%s\"\n",
			config_setting_source_line(setting), name);
		goto out;
	}
	*field = strdup(tmp);
	if (*field == NULL) {
		fprintf(stderr, "strdup failed: insufficient memory");
		goto err_out;
	}
out:
	return 0;

err_out:

	return -1;
}
Esempio n. 13
0
void config_setting_copy_aggregate (config_setting_t *parent, const config_setting_t *src)
{
	config_setting_t *newAgg;
	int i, n;
	newAgg = config_setting_add (parent, config_setting_name (src), config_setting_type (src));

	if (newAgg == NULL)
		return;

	n = config_setting_length (src);

	for (i = 0; i < n; i++) {
		if (config_setting_is_group (src)) {
			config_setting_copy_simple (newAgg, config_setting_get_elem (src, i));
		} else {
			config_setting_copy_elem (newAgg, config_setting_get_elem (src, i));
		}
	}
}
Esempio n. 14
0
static void get_value_libconfig(const config_setting_t *e, void *dest)
{
	int type = config_setting_type(e);
	switch (type) {
	case CONFIG_TYPE_INT:
		*(int *)dest = config_setting_get_int(e);
	case CONFIG_TYPE_INT64:
		*(long long *)dest = config_setting_get_int64(e);
		break;
	case CONFIG_TYPE_STRING:
		dest = (void *)config_setting_get_string(e);
		break;
	case CONFIG_TYPE_BOOL:
		*(int *)dest = config_setting_get_bool(e);
		break;
	case CONFIG_TYPE_FLOAT:
		*(double *)dest = config_setting_get_float(e);
		/* Do nothing, add if needed */
	}
}
Esempio n. 15
0
static void read_app_config(void) {
	char *app_name = getenv("MACSPOOF_APPLICATION");
	if (app_name == NULL) app_name = DEFAULT_APPLICATION_NAME;

	config_setting_t *root = config_root_setting(config);
	assert(root != NULL);
	app_config = config_setting_get_member(root, app_name);
	if (app_config == NULL) die("Application \"%s\" not found.", app_name);
	switch(config_setting_type(app_config)) {
		case CONFIG_TYPE_ARRAY:
			typecheck_config_array(app_config);
			break;
		case CONFIG_TYPE_LIST:
			typecheck_config_ifgrouplist(app_config);
			break;
		default:
			die("Config for application \"%s\" must be an array or group.", app_name);
	}

}
Esempio n. 16
0
static void get_field_string_libconfig(config_setting_t *e, const char *path, void *dest, size_t n)
{
	config_setting_t *elem;
	const char *str;

	if (path)
		elem = config_setting_lookup(e, path);
	else
		elem = e;

	if (!elem || config_setting_type(elem) != CONFIG_TYPE_STRING)
		return;

	if (path) {
		if (config_setting_lookup_string(e, path, &str))
			strncpy(dest, str, n);
	} else {
		if ((str = config_setting_get_string(e)) != NULL)
			strncpy(dest, str, n);
	}
}
Esempio n. 17
0
/**
 * Print a configuration setting. 
 * @param f File stream to print to
 * @param cs Configuration setting
 * @param d Current depth.
 */
static void config_setting_fprint(FILE *f, config_setting_t * cs, int d)
{
    assert(cs && d >= 0);

    int i;
    for (i = 0; i < d; i++)
        fprintf(f, "  ");

    char *n = config_setting_name(cs);

    switch (config_setting_type(cs)) {
    case CONFIG_TYPE_GROUP:
        if (d > 0)
            fprintf(f, "%s = {\n", n);

        for (i = 0; i < config_setting_length(cs); i++)
            config_setting_fprint(f, config_setting_get_elem(cs, i), d + 1);

        if (d > 0) {
            for (i = 0; i < d; i++)
                fprintf(f, "  ");
            fprintf(f, "};\n");
        }
        break;
    case CONFIG_TYPE_STRING:
        fprintf(f, "%s\t= \"%s\";\n", n, config_setting_get_string(cs));
        break;
    case CONFIG_TYPE_FLOAT:
        fprintf(f, "%s\t= %7.5f;\n", n, config_setting_get_float(cs));
        break;
    case CONFIG_TYPE_INT:
        fprintf(f, "%s\t= %d;\n", n, config_setting_get_int(cs));
        break;
    default:
        error("Unsupported type for configuration setting '%s'", n);
    }
}
Esempio n. 18
0
/*! \brief return tcp port form config setting
 *  \return port, in case of failure returns ZFS_PORT (default value)
 */
uint16_t read_tcp_port_setting(config_setting_t * setting)
{
	config_setting_t * setting_node_port = config_setting_get_member(setting, "port");
	if (setting_node_port == NULL)
	{
		return ZFS_PORT;
	}

	if (config_setting_type(setting_node_port) != CONFIG_TYPE_INT)
	{
		message(LOG_WARNING, FACILITY_CONFIG, "TCP port has wrong type, it should be int, using default one.\n");
		return ZFS_PORT;
	}

	int tcp_port = config_setting_get_int(setting_node_port);
	// check port range
	if (tcp_port < 0 || tcp_port >= (1<<16))
	{
		message(LOG_ERROR, FACILITY_CONFIG, "TCP  port is out of range, should be in 1..65536, using default one.\n");
		return ZFS_PORT;
	}

	return (uint16_t) tcp_port & 0xffff;
}
Esempio n. 19
0
static const char *test_libconfig_setting_types(void)
{
	struct config_t config;
	struct config_setting_t *t;
	const char *input = "/* Test File */\n"
		"Setting_Int: 1;\n"
		"Setting_Int64: 1L;\n"
		"Setting_Float: 1.0;\n"
		"Setting_Bool: true;\n"
		"Setting_String: \"1\";\n"
		"Setting_Array: [ ];\n"
		"Setting_Group: { };\n"
		"Setting_List: ( );\n"
		"/* End test file */\n";

	if (libconfig->read_string(&config, input) == CONFIG_FALSE) {
		libconfig->destroy(&config);
		return "Unable to parse configuration.";
	}

	if (config_setting_type(config.root) != CONFIG_TYPE_GROUP) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_GROUP failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Int")) == NULL || config_setting_type(t) != CONFIG_TYPE_INT
			|| config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| config_setting_is_aggregate(t) || !config_setting_is_scalar(t) || !config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_INT failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Int64")) == NULL || config_setting_type(t) != CONFIG_TYPE_INT64
			|| config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| config_setting_is_aggregate(t) || !config_setting_is_scalar(t) || !config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_INT64 failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Float")) == NULL || config_setting_type(t) != CONFIG_TYPE_FLOAT
			|| config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| config_setting_is_aggregate(t) || !config_setting_is_scalar(t) || !config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_FLOAT failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Bool")) == NULL || config_setting_type(t) != CONFIG_TYPE_BOOL
			|| config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| config_setting_is_aggregate(t) || !config_setting_is_scalar(t) || config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_BOOL failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_String")) == NULL || config_setting_type(t) != CONFIG_TYPE_STRING
			|| config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| config_setting_is_aggregate(t) || !config_setting_is_scalar(t) || config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_STRING failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Array")) == NULL || config_setting_type(t) != CONFIG_TYPE_ARRAY
			|| config_setting_is_group(t) || !config_setting_is_array(t) || config_setting_is_list(t)
			|| !config_setting_is_aggregate(t) || config_setting_is_scalar(t) || config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_ARRAY failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_Group")) == NULL || config_setting_type(t) != CONFIG_TYPE_GROUP
			|| !config_setting_is_group(t) || config_setting_is_array(t) || config_setting_is_list(t)
			|| !config_setting_is_aggregate(t) || config_setting_is_scalar(t) || config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_GROUP failed.";
	}

	if ((t = libconfig->lookup(&config, "Setting_List")) == NULL || config_setting_type(t) != CONFIG_TYPE_LIST
			|| config_setting_is_group(t) || config_setting_is_array(t) || !config_setting_is_list(t)
			|| !config_setting_is_aggregate(t) || config_setting_is_scalar(t) || config_setting_is_number(t)
	) {
		libconfig->destroy(&config);
		return "CONFIG_TYPE_LIST failed.";
	}

	libconfig->destroy(&config);

	return NULL;
}
Esempio n. 20
0
/**
   Load interfaces section.
 * @param[in] cfg Config section.
 * @param[in] option Option name.
 * @param[in,out] array Resulting array.
 * @return Zero on success.
 */
int load_interfaces(const config_setting_t *cfg, const char *option, UT_array *array)
{
    config_setting_t *cfg_list = config_setting_get_member(cfg, option);

    if (!cfg_list) {
        ZERO_LOG(LOG_ERR, "config: missing %s entry", option);
        return -1;
    }

    if (config_setting_type(cfg_list) != CONFIG_TYPE_LIST) {
        ZERO_LOG(LOG_ERR, "config: invalid %s entry", option);
        return -1;
    }

    int count = config_setting_length(cfg_list);

    if (0 >= count) {
        ZERO_LOG(LOG_ERR, "config: empty %s entry", option);
        return -1;
    }

    utarray_init(array, &ut_zif_pair_icd);

    for (int i = 0; i < count; i++) {
        struct zif_pair if_pair;
        const char *str;
        config_setting_t *entry = config_setting_get_elem(cfg_list, i);

        if (NULL == entry) {
            ZERO_LOG(LOG_ERR, "config: failed to read %u-th group of %s entry", i, option);
            goto fail;
        }

        if (!config_setting_lookup_string(entry, ZCFG_LAN, &str)) {
            ZERO_LOG(LOG_ERR, "config: failed to read '%s' property of %u-th group of %s entry", ZCFG_LAN, i, option);
            goto fail;
        }
        strncpy(if_pair.lan, str, sizeof(if_pair.lan));

        if (!config_setting_lookup_string(entry, ZCFG_WAN, &str)) {
            ZERO_LOG(LOG_ERR, "config: failed to read '%s' property of %u-th group of %s entry", ZCFG_WAN, i, option);
            goto fail;
        }
        strncpy(if_pair.wan, str, sizeof(if_pair.wan));

        int affinity = 0;
        if (!config_setting_lookup_int(entry, ZCFG_AFFINITY, &affinity)) {
            ZERO_LOG(LOG_ERR, "config: failed to read '%s' property of %u-th group of %s entry", ZCFG_AFFINITY, i, option);
            goto fail;
        }
        if ((affinity < 0) || affinity >= UINT16_MAX) {
            ZERO_LOG(LOG_ERR, "config: invalid value in '%s' property of %u-th group of %s entry", ZCFG_AFFINITY, i, option);
            goto fail;
        }
        if_pair.affinity = (uint16_t)affinity;

        utarray_push_back(array, &if_pair);
    }

    return 0;

fail:
    utarray_done(array);
    return -1;
}
Esempio n. 21
0
/**
 * Load ip-mask array.
 * @param[in] cfg Config section.
 * @param[in] option Option name.
 * @param[in,out] array Resulting array.
 * @return Zero on success.
 */
static int load_ip_mask_list(const config_setting_t *cfg, const char *option, UT_array *array)
{
    config_setting_t *cfg_list = config_setting_get_member(cfg, option);

    if (!cfg_list) {
        ZERO_LOG(LOG_ERR, "config: missing %s entry", option);
        return 0;
    }

    if (config_setting_type(cfg_list) != CONFIG_TYPE_LIST) {
        ZERO_LOG(LOG_ERR, "config: invalid %s entry", option);
        return -1;
    }

    int count = config_setting_length(cfg_list);

    if (0 >= count) {
        return 0;
    }

    utarray_init(array, &ut_ip_range_icd);

    for (int i = 0; i < count; i++) {
        struct ip_range range;
        const char *entry = config_setting_get_string_elem(cfg_list, i);

        if (!entry) {
            ZERO_LOG(LOG_ERR, "config: failed to get next %s record", option);
            continue;
        }

        char ip_str[INET_ADDRSTRLEN];
        const char *cidr_pos = strchr(entry, '/');

        // we search for CIDR, and make sure, that ip part is not bigger than allowed size
        if (cidr_pos && ((size_t)(cidr_pos - entry) < sizeof(ip_str))) {
            strncpy(ip_str, entry, cidr_pos - entry);
            ip_str[cidr_pos - entry] = '\0';

            struct in_addr ip_addr;
            if (0 < inet_pton(AF_INET, ip_str, &ip_addr)) {
                u_long cidr = strtoul(cidr_pos + 1, NULL, 10);
                if (cidr != ULONG_MAX && cidr <= 32) {
                    range.ip_start = ntohl(ip_addr.s_addr);
                    range.ip_end = IP_RANGE_END(range.ip_start, cidr);
                    utarray_push_back(array, &range);
                    continue;
                }
            }
        }

        // if we here, then entry is invalid
        ZERO_LOG(LOG_ERR, "config: invalid %s item: %s", option, entry);
        utarray_done(array);
        return -1;
    }

    utarray_sort(array, ip_range_cmp);

    return 0;
}
Esempio n. 22
0
static void dx_reload_cfg()
{
	int i, j;
	const char *s;

	config_setting_t *dxs = dxcfg_lookup("dx", CFG_REQUIRED);
	assert(config_setting_type(dxs) == CONFIG_TYPE_LIST);
	
	const config_setting_t *dxe;
	for (i=0; (dxe = config_setting_get_elem(dxs, i)) != NULL; i++) {
		assert(config_setting_type(dxe) == CONFIG_TYPE_GROUP);
	}
	int _dx_list_len = i-1;
	lprintf("%d dx entries\n", _dx_list_len);
	
	dx_t *_dx_list = (dx_t *) kiwi_malloc("dx_list", (_dx_list_len+1) * sizeof(dx_t));
	
	float f = 0;
	
	dx_t *dxp;
	for (i=0, dxp = _dx_list; i < _dx_list_len; i++, dxp++) {
		dxe = config_setting_get_elem(dxs, i);
		
		config_setting_t *e;
		assert((e = config_setting_get_member(dxe, "e")) != NULL);
		assert(config_setting_type(e) == CONFIG_TYPE_LIST);
		
		assert((dxp->freq = (float) config_setting_get_float_elem(e, 0)) != 0);
		if (dxp->freq < f)
			lprintf(">>>> DX: entry with freq %.2f < current freq %.2f\n", dxp->freq, f);
		else
			f = dxp->freq;

		assert((s = config_setting_get_string_elem(e, 1)) != NULL);
		dxcfg_mode(dxp, s);
		
		assert((s = config_setting_get_string_elem(e, 2)) != NULL);
		dxp->ident = str_encode((char *) s);
		
		if ((s = config_setting_get_string_elem(e, 3)) == NULL) {
			dxp->notes = NULL;
		} else {
			dxp->notes = str_encode((char *) s);
		}

		config_setting_t *flags;
		const char *flag;
		if ((flags = config_setting_get_member(dxe, "f")) != NULL) {
			if (config_setting_type(flags) == CONFIG_TYPE_ARRAY) {
				for (j=0; j < config_setting_length(flags); j++) {
					assert((flag = config_setting_get_string_elem(flags, j)) != NULL);
					dxcfg_flag(dxp, flag);
				}
			} else {
				assert((flag = config_setting_get_string(flags)) != NULL);
				dxcfg_flag(dxp, flag);
			}
		}

		config_setting_t *offset;
		if ((offset = config_setting_get_member(dxe, "o")) != NULL) {
			if (config_setting_type(offset) == CONFIG_TYPE_ARRAY) {
				assert((dxp->low_cut = (float) config_setting_get_int_elem(offset, 0)) != 0);
				assert((dxp->high_cut = (float) config_setting_get_int_elem(offset, 1)) != 0);
			} else {
				assert((dxp->offset = (float) config_setting_get_int(offset)) != 0);
			}
		}

		//printf("dxe %d f %.2f notes-%c off %.0f,%.0f\n", i, dxp->freq, dxp->notes? 'Y':'N', dxp->offset, dxp->high_cut);
	}
	
	switch_dx_list(_dx_list, _dx_list_len);

	// convert to json
	printf("### converting dx list to json\n");
	dx_save_as_json();
}
Esempio n. 23
0
void load_params(int argc, char **argv) {
  config_t cfg;
  config_setting_t *setting, *interrupt_setting;
  char const *config_socket, *inter_name, *inter_type_string, *inter_pud;
  char *config_file_name;
  int ch, inter_pin, inter_type, inter_wait, r, pud;
  int lcd_di, lcd_led, lcd_spics, read_config = 0;
  InterruptInfo *interrupt_info;

  while ((ch = getopt(argc, argv, "dhvs:a:l:c:i:")) != -1) {
    switch (ch) {
      case 'd':
        set_flag_dont_detach(1);
        break;
      case 'h':
        usage();
        exit(EXIT_SUCCESS);
        break;
     case 'v':
       set_flag_verbose(1);
       break;
     case 's':
       set_socket_filename(optarg);
       break;
     case 'a':
       if (is_valid_pin_num(atoi(optarg))) {
         set_lcd_di(atoi(optarg));
       } else {
         printf("Only valid Pinnumber between 1 and 16 allowed!\n");
         usage();
         exit(EXIT_FAILURE);
       }
       break;
     case 'l':
       if (is_valid_pin_num(atoi(optarg))) {
         set_lcd_led(atoi(optarg));
       } else {
         printf("Only valid Pinnumber between 1 and 16 allowed!\n");
         usage();
         exit(EXIT_FAILURE);
       }
       break;
     case 'c':
       if (is_valid_pin_num(atoi(optarg))) {
         set_lcd_spics(atoi(optarg));
       } else {
         printf("Only 0 or 1 allowed!\n");
         usage();
         exit(EXIT_FAILURE);
       }
       break;
     case 'i':
       read_config = 1;
       config_file_name = optarg;
       break;
     default:
       usage();
       exit(1);
    }
  }

  if (read_config) {
    config_init(&cfg);
    /* Read the config file and about on error */
    if (!config_read_file(&cfg, config_file_name)) {
      printf("\n%s:%d - %s\n", config_file_name, config_error_line(&cfg), config_error_text(&cfg));
      config_destroy(&cfg);
      exit(1);
    }

    if (config_lookup_string(&cfg, "socket", &config_socket)) {
      set_socket_filename(strndup(config_socket, strlen(config_socket)));
      printf("Socket file configured from config file as: %s\n", get_socket_filename());
    }

    setting = config_lookup(&cfg, "lcd");

    if (setting != NULL) {
      if (!config_setting_lookup_int(setting, "di_pin", &lcd_di)) {
    	  set_lcd_di(lcd_di);
    	  if (get_flag_verbose()) {
    		  printf("Set DI Pin of the LCD Display to %i with config file\n", lcd_di);
    	  }
      }
      if (!config_setting_lookup_int(setting, "led_pin", &lcd_led)) {
    	  set_lcd_led(lcd_led);
    	  if (get_flag_verbose()) {
    		  printf("Set PWM LED Pin of the LCD Display to %i with config file\n", lcd_led);
    	  }
      }
      if (!config_setting_lookup_int(setting, "spi_cs", &lcd_spics)) {
    	  set_lcd_spics(lcd_spics);
    	  if (get_flag_verbose()) {
    		  printf("Set SPI CS Pin of the LCD Display to %i with config file\n", lcd_spics);
    	  }
      }
    }

    setting = config_lookup(&cfg, "interrupt");

    if (setting != NULL)
    {
      if (config_setting_type(setting) == CONFIG_TYPE_LIST) {
        set_interrupts_count(config_setting_length(setting));
        // Max interrupts are 10 if more configured only the first 10 are used!
        if (get_interrupts_count() > 10) {
          set_interrupts_count(10);
        }
        for (r=0; r < get_interrupts_count(); r++) {
          interrupt_setting = config_setting_get_elem(setting, r);
          if (!(config_setting_lookup_int(interrupt_setting, "pin", &inter_pin)
              && config_setting_lookup_string(interrupt_setting, "type", &inter_type_string)
              && config_setting_lookup_string(interrupt_setting, "name", &inter_name)
              && config_setting_lookup_int(interrupt_setting, "wait", &inter_wait)
              && config_setting_lookup_string(interrupt_setting, "pud", &inter_pud))) {
            // TODO: Error message if configuration is not valid
            continue;
          }

          if(strncmp(inter_pud, "none", strlen("none")) == 0) {
            pud = PUD_OFF;
          } else if (strncmp(inter_pud, "up", strlen("up")) == 0) {
            pud = PUD_UP;
          } else if (strncmp(inter_pud, "down", strlen("down")) == 0) {
            pud = PUD_DOWN;
          } else {
            // TODO: Error message if configuration is not valid
            continue;
          }

          if(strncmp(inter_type_string, "falling", strlen("falling")) == 0) {
            inter_type = INT_EDGE_FALLING;
          } else if (strncmp(inter_type_string, "rising", strlen("rising")) == 0) {
            inter_type = INT_EDGE_RISING;
          } else if (strncmp(inter_type_string, "both", strlen("both")) == 0) {
            inter_type = INT_EDGE_BOTH;
          } else {
            // TODO: Error message if configuration is not valid
            continue;
          }
          interrupt_info = malloc(sizeof(InterruptInfo));

          if (r <= 10) {
            interrupt_info->pin    = inter_pin;
            interrupt_info->wait   = inter_wait;
            interrupt_info->type   = inter_type;
            interrupt_info->name   = strndup(inter_name, strlen(inter_name));
            interrupt_info->occure = 0;
            interrupt_info->pud    = pud;
            set_interrupt_info(r, *interrupt_info);
          }
        }
      }
    }

    config_destroy(&cfg);
  }
}
Esempio n. 24
0
int read_users_local_config(config_t * config)
{
	const char * default_user;

	config_setting_t * users_settings = config_lookup(config, "users");
	if (users_settings == NULL)
	{
		//TODO: set uid to nobody
		message(LOG_ERROR, FACILITY_CONFIG, "In users local config section is missing, please add is to local config.\n");
		return CONFIG_FALSE;
	}

	config_setting_t * setting_default_uid = config_setting_get_member(users_settings, "default_uid");
	config_setting_t * setting_default_user = config_setting_get_member(users_settings, "default_user");

	if (setting_default_uid != NULL && setting_default_user != NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In users local config is default_uid and default_user.\n");
		return CONFIG_FALSE;
	}

	if (setting_default_uid == NULL && setting_default_user == NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In users local config are missing default_uid and default_user.\n");
		return CONFIG_FALSE;
	}

	if (setting_default_uid != NULL)
	{
		if (config_setting_type(setting_default_uid) != CONFIG_TYPE_INT)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "In users local config default_uid is wrong type, it should be int.\n");
			return CONFIG_FALSE;
		}

		zfs_config.default_node_uid = config_setting_get_int(setting_default_uid);
		return CONFIG_TRUE;
	}

	if (config_setting_type(setting_default_user) != CONFIG_TYPE_STRING)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In users local config default_user is wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	default_user = config_setting_get_string(setting_default_user);
	if (default_user == NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In users local config default_user is wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	if (!set_default_uid(default_user))
	{
		message(LOG_ERROR, FACILITY_CONFIG,
				"In users local config is unknown (local) user.\n", default_user);
		return CONFIG_FALSE;
	}

	return CONFIG_TRUE;
}
Esempio n. 25
0
/*********************
return RET_NOK on error
*********************/
ret_code_t entry_copy_config(config_setting_t * source, config_setting_t * dest)
{
	config_setting_t * new_source;
	config_setting_t * new_dest;
	int index = -1;
	int int_value;
	long long long_value;
	double double_value;
	const char * string;

	while((new_source=config_setting_get_elem(source,index+1))!= NULL ) {
		index++;
		if(config_setting_is_group(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_GROUP)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_array(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_ARRAY)) {
				return RET_NOK;
			}
		}

		else if(config_setting_is_list(new_source)) {
			if(!entry_copy_aggregate(new_source,dest,CONFIG_TYPE_LIST)) {
				return RET_NOK;
			}
		}

		else {
			switch(config_setting_type(new_source)) {
			case CONFIG_TYPE_INT:
				int_value = config_setting_get_int(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT);
				config_setting_set_int(new_dest,int_value);
				continue;
			case CONFIG_TYPE_INT64:
				long_value = config_setting_get_int64(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_INT64);
				config_setting_set_int64(new_dest,long_value);
				continue;
			case CONFIG_TYPE_FLOAT:
				double_value = config_setting_get_float(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_FLOAT);
				config_setting_set_float(new_dest,double_value);
				continue;
			case CONFIG_TYPE_BOOL:
				int_value = config_setting_get_bool(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_BOOL);
				config_setting_set_bool(new_dest,int_value);
				continue;
			case CONFIG_TYPE_STRING:
				string = config_setting_get_string(new_source);
				new_dest = config_setting_add (dest, config_setting_name(new_source),CONFIG_TYPE_STRING);
				config_setting_set_string(new_dest,string);
				continue;
			default:
				return RET_NOK;
			}
		}
	}

	return RET_OK;
}
Esempio n. 26
0
int read_groups_local_config(config_t * config)
{
	const char * default_group;

	config_setting_t * groups_settings = config_lookup(config, "groups");
	if (groups_settings == NULL)
	{
		//TODO: set nogroup or nobody as gid
		message(LOG_ERROR, FACILITY_CONFIG, "In groups local config section is missing, please add is to local config.\n");
		return CONFIG_FALSE;
	}

	config_setting_t * setting_default_gid = config_setting_get_member(groups_settings, "default_gid");
	config_setting_t * setting_default_group = config_setting_get_member(groups_settings, "default_group");

	if (setting_default_gid != NULL && setting_default_group != NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In groups local config is default_gid and default_groups.\n");
		return CONFIG_FALSE;
	}

	if (setting_default_gid == NULL && setting_default_group == NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In groups local config are missing default_uid and default_group.\n");
		return CONFIG_FALSE;
	}

	if (setting_default_gid != NULL)
	{
		if (config_setting_type(setting_default_gid) != CONFIG_TYPE_INT)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "In groups local config default_gid is wrong type, it should be int.\n");
			return CONFIG_FALSE;
		}

		zfs_config.default_node_gid = config_setting_get_int(setting_default_gid);
		return CONFIG_TRUE;
	}

	if (config_setting_type(setting_default_group) != CONFIG_TYPE_STRING)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In groups local config default_group is wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	default_group = config_setting_get_string(setting_default_group);
	if (default_group == NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In groups local config default_group is wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	if (!set_default_gid(default_group))
	{
		message(LOG_ERROR, FACILITY_CONFIG,
				"In groups local config is unknown (local) group.\n", default_group);
		return CONFIG_FALSE;
	}

	return CONFIG_TRUE;

}
Esempio n. 27
0
int read_config_node_local_config(config_t * config)
{
	config_setting_t * config_node_setting = config_lookup(config, "config_node");
	if (config_node_setting == NULL)
	{
		message(LOG_WARNING, FACILITY_CONFIG, "Config node section is missing, using and config node this node");

		zfs_config.config_node.node_id = zfs_config.this_node.node_id;
		zfs_config.config_node.host_port = zfs_config.this_node.host_port;
		xstringdup(&zfs_config.config_node.node_name, &zfs_config.this_node.node_name);

		return CONFIG_TRUE;
	}

	config_setting_t * setting_node_id = config_setting_get_member(config_node_setting, "id");
	config_setting_t * setting_node_name = config_setting_get_member(config_node_setting, "name");
	config_setting_t * setting_node_host = config_setting_get_member(config_node_setting, "host");

	if (setting_node_id == NULL || setting_node_name == NULL || setting_node_host == NULL)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In local config node section name, node id or host name is missing, please add them to local config.\n");
		return CONFIG_FALSE;
	}

	if (config_setting_type(setting_node_id) != CONFIG_TYPE_INT)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In local node section key id has wrong type, it should be int.\n");
		return CONFIG_FALSE;
	}

	if (config_setting_type(setting_node_name) != CONFIG_TYPE_STRING)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In local node section key name has wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	if (config_setting_type(setting_node_host) != CONFIG_TYPE_STRING)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "In local node section key name has wrong type, it should be string.\n");
		return CONFIG_FALSE;
	}

	zfs_config.config_node.node_id = config_setting_get_int(setting_node_id);
	if (!is_valid_node_id(zfs_config.config_node.node_id))
	{
		message(LOG_ERROR, FACILITY_CONFIG, "Node in config node id is invalid, please fix is.\n");
		return CONFIG_FALSE;
	}

	if (zfs_config.this_node.node_id == zfs_config.config_node.node_id)
	{
		message(LOG_ERROR, FACILITY_CONFIG, "Node in config node id is same as this node id.\n");
		return CONFIG_FALSE;
	}

	const char * local_node_name = config_setting_get_string(setting_node_name);
	if (!is_valid_node_name(local_node_name))
	{
		message(LOG_ERROR, FACILITY_CONFIG, "Node name in config node is invalid.\n");
		return CONFIG_FALSE;
	}

	xmkstring(&zfs_config.config_node.node_name, local_node_name);
	if (stringeq(&zfs_config.config_node.node_name, &zfs_config.this_node.node_name))
	{
		message(LOG_ERROR, FACILITY_CONFIG, "Node name in config node is same as this node name.\n");
		return CONFIG_FALSE;
	}


	const char * local_node_host = config_setting_get_string(setting_node_host);
	if (!is_valid_host_name(local_node_host))
	{
		message(LOG_ERROR, FACILITY_CONFIG, "Host name in config node is invalid.\n");
		return CONFIG_FALSE;
	}

	xmkstring(&(zfs_config.config_node.host_name), local_node_host);

	/*read port configuration*/
	zfs_config.config_node.host_port = read_tcp_port_setting(config_node_setting);


	return CONFIG_TRUE;
}
Esempio n. 28
0
File: settings.c Progetto: bradfa/gt
int gt_parse_settings(config_t *config)
{
	config_setting_t *node, *root, *elem;
	int i, len, ret;
	struct stat st;
	const char *filename;

	if (stat(GT_USER_SETTING_PATH, &st) == 0)
		filename = GT_USER_SETTING_PATH;
	else
		filename = GT_SETTING_PATH;

	ret = config_read_file(config, filename);
	if (ret == CONFIG_FALSE)
		return -1;

	root = config_root_setting(config);

#define GET_SETTING(name, field) do { \
	node = config_setting_get_member(root, name); \
	if (node) { \
		if (config_setting_type(node) != CONFIG_TYPE_STRING) { \
			fprintf(stderr, "%s:%d: Expected string\n", \
					config_setting_source_file(node), \
					config_setting_source_line(node)); \
			return -1; \
		} \
		gt_settings.field = config_setting_get_string(node); \
	} \
} while (0)

	GET_SETTING("default-udc", default_udc);
	GET_SETTING("configfs-path", configfs_path);
	GET_SETTING("default-template-path", default_template_path);
	GET_SETTING("default-gadget", default_gadget);

	node = config_setting_get_member(root, "lookup-path");
	if (node) {
		if (config_setting_is_aggregate(node) == CONFIG_FALSE) {
			fprintf(stderr, "%s:%d: Expected list\n",
					config_setting_source_file(node),
					config_setting_source_line(node));
			return -1;
		}

		len = config_setting_length(node);
		gt_settings.lookup_path = calloc(len + 1, sizeof(*gt_settings.lookup_path));
		for (i = 0; i < len; ++i) {
			elem = config_setting_get_elem(node, i);
			if (config_setting_type(elem) != CONFIG_TYPE_STRING) {
				fprintf(stderr, "%s:%d: Expected string\n",
					config_setting_source_file(elem),
					config_setting_source_line(elem));
				goto out;
			}
			gt_settings.lookup_path[i] = config_setting_get_string(elem);
		}
	}

#undef GET_SETTING

	return 0;
out:
	free(gt_settings.lookup_path);
	return -1;
}
Esempio n. 29
0
int read_versioning_config(config_t * config)
{
	config_setting_t * setting_versioning = config_lookup(config, "versioning");
	if (setting_versioning == NULL)
	{
		message(LOG_INFO, FACILITY_CONFIG, "No versioning section was found in local config.\n");
		return CONFIG_TRUE;
	}

	config_setting_t * member;

	/* versioning::enable */
	member = config_setting_get_member(setting_versioning, "enable");
	if (member != NULL)
	{
		if (config_setting_type(member) != CONFIG_TYPE_BOOL)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "Failed to read versoning::enable, enable has wrong type,\n");
			return CONFIG_FALSE;
		}
		zfs_config.versions.versioning = config_setting_get_bool(member);
	}

	/* versioning::display */
	member = config_setting_get_member(setting_versioning, "display");
	if (member != NULL)
	{
		if (config_setting_type(member) != CONFIG_TYPE_BOOL)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "Failed to read versoning::display, enable has wrong type,\n");
			return CONFIG_FALSE;
		}
		zfs_config.versions.verdisplay = config_setting_get_bool(member);
	}

	/* versioning::retention_age */
	config_setting_t * setting_age = config_setting_get_member(setting_versioning, "retention_age");
	if (setting_age != NULL)
	{
		int rv = read_interval_setting(setting_age, &(zfs_config.versions.retention_age_min),
				&(zfs_config.versions.retention_age_max));
		if (rv != CONFIG_TRUE)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "Failed to read versioning::retention age.\n");
			return CONFIG_FALSE;
		}
	}

	/* versioning::retention_num */
	config_setting_t * setting_num = config_setting_get_member(setting_versioning, "retention_num");
	if (setting_num != NULL)
	{

		int rv = read_interval_setting(setting_num, &(zfs_config.versions.retention_num_min),
				&(zfs_config.versions.retention_num_max));
		if (rv != CONFIG_TRUE)
		{
			message(LOG_ERROR, FACILITY_CONFIG, "Failed to read versioning::retention num.\n");
			return CONFIG_FALSE;
		}
	}

	return CONFIG_TRUE;
}
Esempio n. 30
0
File: config.c Progetto: joshdk/muxd
int config_get_services(config_t *config,service ***final_services){

	config_setting_t *cs_services=NULL;
	if((cs_services=config_lookup(config,"services"))==NULL){
		return 1;
	}

	int service_count=config_setting_length(cs_services);
	service **services=NULL;
	services=malloc((service_count+1)*sizeof(service *));
	int services_len=0;

	for(int n=0;n<service_count;n++){
		//printf("service: %d\n",n);
		
		config_setting_t *cs_service=NULL;
		if((cs_service=config_setting_get_elem(cs_services,n))==NULL){
			//printf("failed to retrieve service\n");
			continue;
		}

		int enabled=0;
		if(config_setting_lookup_bool(cs_service,"enabled",&enabled)){
			//printf("  enabled: %s\n",enabled?"true":"false");
			if(enabled==0){
				continue;
			}
		}

		config_setting_t *cs_host=NULL;
		if((cs_host=config_setting_get_member(cs_service,"host"))==NULL){
			continue;
		}

		char *host=NULL;
		host=strdup(config_setting_get_string(cs_host));

		if(host==NULL){
			printf("failed to retrieve host\n");
			continue;
		}

		config_setting_t *cs_port=NULL;
		if((cs_port=config_setting_get_member(cs_service,"port"))==NULL){
			free(host);
			continue;
		}

		char *port=NULL;
		switch(config_setting_type(cs_port)){
			case CONFIG_TYPE_INT:
				port=calloc(16,sizeof(*port));
				sprintf(port,"%lu",config_setting_get_int(cs_port));
				break;
			case CONFIG_TYPE_STRING:
				port=strdup(config_setting_get_string(cs_port));
				break;
		}

		if(port==NULL){
			printf("failed to retrieve port\n");
			free(host);
			continue;
		}

		config_setting_t *cs_names=NULL;
		if((cs_names=config_setting_get_member(cs_service,"names"))==NULL){
			free(host);
			free(port);
			continue;
		}

		int name_count=config_setting_length(cs_names);
		char **names=malloc((name_count+1)*sizeof(*names));
		int names_len=0;

		for(int ni=0;ni<name_count;++ni){
			const char *name=config_setting_get_string_elem(cs_names,ni);
			if(name!=NULL && strlen(name)>0){
				names[names_len]=strdup(name);
				//printf("    \"%s\"\n",names[names_len]);
				names_len++;
			}
		}
		names[names_len]=NULL;

		services[services_len]=malloc(sizeof(service));
		services[services_len]->host=host;
		services[services_len]->port=port;
		services[services_len]->names=names;
		services_len++;
	}

	services[services_len]=NULL;
	*final_services=services;
	return 0;
}