Exemple #1
0
void configure_cgi_handlers(spade_server* server, config_t* configuration) {
    config_setting_t* handler_settings = config_lookup(configuration,
            "cgi.handlers");
    int cgi_handler_count = config_setting_length(handler_settings);

    for (int n = 0; n < cgi_handler_count; n++) {
        config_setting_t* handler_setting = config_setting_get_elem(
                handler_settings, n);
        const char* handler = NULL;
        config_setting_lookup_string(handler_setting, "handler", &handler);

        const char* url = NULL;
        config_setting_lookup_string(handler_setting, "url", &url);

        if(!register_cgi_handler(server, url, handler)) {
            log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                    "Registered CGI handler '%s' for URL prefix '%s'",
                    handler, url);
        }
    }
    if (server->cgi_handler_count == 0) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "No CGI handlers registered");
    } else {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "Registered a total of %d CGI handlers",
                server->cgi_handler_count);
    }
}
Exemple #2
0
/**
 * Checks if player group can use @/#command
 * @param group_id ID of the group
 * @param command Command name without @/# and params
 * @param type enum AtCommanndType { COMMAND_ATCOMMAND = 1, COMMAND_CHARCOMMAND = 2 }
 */
bool pc_group_can_use_command(int group_id, const char *command, AtCommandType type)
{
	int result = 0;
	config_setting_t *commands = NULL;
	GroupSettings *group = NULL;

	if (pc_group_has_permission(group_id, PC_PERM_USE_ALL_COMMANDS))
		return true;

	if ((group = id2group(group_id)) == NULL)
		return false;

	commands = group->commands;
	if (commands != NULL) {
		config_setting_t *cmd = NULL;
		
		// <commandname> : <bool> (only atcommand)
		if (type == COMMAND_ATCOMMAND && config_setting_lookup_bool(commands, command, &result))
			return (bool)result;

		// <commandname> : [ <bool>, <bool> ] ([ atcommand, charcommand ])
		if ((cmd = config_setting_get_member(commands, command)) != NULL &&
		    config_setting_is_aggregate(cmd) && config_setting_length(cmd) == 2)
			return (bool)config_setting_get_bool_elem(cmd, AtCommandType2idx(type));
	}
	return false;
}
Exemple #3
0
/**
 * Load client rules list.
 * @param[in] option Configuration option.
 * @param[in,out] rules Resulting rules.
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_client_rules(const config_setting_t *option, zclient_rules_t *rules)
{
    zclient_rules_init(rules);

    if (!option) {
        return 1;
    }

    if (CONFIG_TYPE_LIST != option->type) {
        return -1;
    }

    int count = config_setting_length(option);

    zclient_rule_parser_t *rule_parser = zclient_rule_parser_new();

    for (int i = 0; i < count; i++) {
        const char *str = config_setting_get_string_elem(option, i);
        if (!zclient_rule_parse(rule_parser, rules, str)) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid client rule: %s", option->parent->name, option->name, str);
            zclient_rule_parser_free(rule_parser);
            return -1;
        }
    }

    zclient_rule_parser_free(rule_parser);
    return 0;
}
Exemple #4
0
/**
 * Load uint16 array.
 * @param[in] option Configuration option.
 * @param[in,out] array Resulting array.
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_uint16_array(const config_setting_t *option, UT_array *array)
{
    utarray_init(array, &ut_uint16_icd);

    if (!option) {
        return 1;
    }

    if (CONFIG_TYPE_ARRAY != option->type) {
        return -1;
    }

    int count = config_setting_length(option);

    for (int i = 0; i < count; i++) {
        int item = config_setting_get_int_elem(option, i);

        if ((item < 0) || (item > UINT16_MAX)) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid port: %d", option->parent->name, option->name, item);
            utarray_done(array);
            return -1;
        }

        uint16_t port = (uint16_t) item;
        utarray_push_back(array, &port);
    }

    if (utarray_len(array)) {
        utarray_sort(array, uint16_cmp);
    }

    return 0;
}
static int processRequest(char *request) {
    int count = 0;
    int responseTotal = 0;
    
    config_setting_t *responseConfig = NULL;
    config_setting_t *responseCurrent = NULL;
    const char *responseValue = NULL;
    const char *requestName = NULL;
    const char *requestValue = NULL;
    long volume;
    
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
    pthread_mutex_lock(&lockConfig);
    
    responseConfig = config_lookup(&config, "response");
    responseTotal = config_setting_length(responseConfig);
    
    for(count = 0; count < responseTotal; count++) {
        responseCurrent = config_setting_get_elem(responseConfig, count);
        if((responseValue = config_setting_get_string_elem(responseCurrent, 1)) != NULL &&
        strcmp(responseValue, request) == 0) {
            responseValue = config_setting_get_string_elem(responseCurrent, 2);
            if(config_setting_get_bool_elem(responseCurrent, 0) == 1) { // formulating default response
            
                pthread_mutex_unlock(&lockConfig);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
                common_data.interface->reply(responseValue, strlen(responseValue));
            }
            else { // attempt to formulate custom response
                requestName = config_setting_name(responseCurrent);
                
                pthread_mutex_unlock(&lockConfig);
                pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
                
                if(strcmp(requestName, "volume") == 0) {
                    if(getMixer(&volume) == EXIT_FAILURE) {
                        return EXIT_FAILURE;
                    }
                    replyVolumeCommand(&volume);
                }
                else {
                    statusInfo.retrieve(requestName, &requestValue);
                    if(requestValue != NULL)
                        replyDeviceCommand((char *)requestName, (char *)requestValue);
                    else // custom response not possible, reverting to default value
                        replyDeviceCommand((char *)requestName, (char *)responseValue);
                }
            }
            syslog(LOG_DEBUG, "Successfully processed request: %s", request);
            return EXIT_SUCCESS; // command is matched, returning
        }
        else {
            pthread_mutex_unlock(&lockConfig);
            pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
        }
    }
    syslog(LOG_DEBUG, "Could not identify request: %s", request);
    return EXIT_SUCCESS;
}
Exemple #6
0
// TODO: audit exit routes to "unset"/"unalloc" values.
int upd8_config_parse(upd8_config_t *config, const char *config_file) {
  // TODO: check that config is not null.
  config_t *cfg = malloc(sizeof(config_t));

  config_init(cfg);

  if (!config_read_file(cfg, config_file)) {
    fprintf(stderr, "%s:%d - %s\n", config_error_file(cfg),
            config_error_line(cfg), config_error_text(cfg));
    config_destroy(cfg);
    return EXIT_FAILURE;
  }

  config->cfg = cfg;

  config_setting_t *settings;
  settings = config_lookup(cfg, settings_path);

  if (settings == NULL) {
    fprintf(stderr, "Unable to find %s in %s\n", settings_path, config_file);
    config_destroy(cfg);
    return EXIT_FAILURE;
  }

  config->num_sources = config_setting_length(settings);
  config->sources = malloc(config->num_sources * sizeof(upd8_source_t));
  for (int i = 0; i < config->num_sources; ++i) {
    config_setting_t *individual_config = config_setting_get_elem(settings, i);
    upd8_source_parse(&(config->sources[i]), individual_config);
  }
  return 0;
}
Exemple #7
0
/*********************
return RET_NOK on error
*********************/
static ret_code_t __write_list_index(const char * table, const char * file, const char * data, int index, va_list ap)
{
	config_setting_t * setting = NULL;
	const config_t * config;
	int list_size;


	SDL_LockMutex(entry_mutex);
	config = get_config(table,file);
	if(config==NULL) {
		SDL_UnlockMutex(entry_mutex);
		return RET_NOK;
	}

	setting = create_tree(config,NULL,NULL,NULL,CONFIG_TYPE_ARRAY,ap);

	// Create empty entry before index
	list_size = config_setting_length (setting);
	while( list_size <= index ) {
		if(config_setting_set_string_elem(setting,-1,"")==NULL) {
			SDL_UnlockMutex(entry_mutex);
			return RET_NOK;
		}
		list_size++;
	}

	if(config_setting_set_string_elem(setting,index,data)==NULL) {
		SDL_UnlockMutex(entry_mutex);
		return RET_NOK;
	}

	write_config(config,table,file);
	SDL_UnlockMutex(entry_mutex);
	return RET_OK;
}
Exemple #8
0
static config_setting_t *interface_group_for(const char *ifname) {
	assert(config_setting_is_list(app_config));

	config_setting_t *default_if_conf = NULL;
	for (int i = 0; i < config_setting_length(app_config); i++) {
		config_setting_t *cur = config_setting_get_elem(app_config, i);

		config_setting_t *cur_ifname = config_setting_get_member(cur, "interface");
		if (cur_ifname != NULL) {
			const char *cur_ifname_str = config_setting_get_string(cur_ifname);
			assert(cur_ifname_str != NULL);
			if (strcmp(cur_ifname_str, ifname) == 0) return cur;
		}

		config_setting_t *cur_is_default = config_setting_get_member(cur, "default");
		if (
			default_if_conf == NULL &&
			cur_is_default != NULL &&
			config_setting_get_bool(cur_is_default)
		) {
			default_if_conf = cur;
		}
	}
	return default_if_conf;
}
void configureTuner(config_t *propConfig) {
    config_setting_t *blockSetting = config_lookup(propConfig, "tuner.block");
    config_setting_t *gridSetting = config_lookup(propConfig, "tuner.grid");

    if (blockSetting != NULL && gridSetting != NULL) {
        int length = config_setting_length(blockSetting); // Only need the length of one metric
        performanceMetricCount = length;
        performanceMetric.blockDimList = malloc(sizeof(CudaDim) * length);
        performanceMetric.gridDimList = malloc(sizeof(CudaDim) * length);
        int i;
        for (i = 0; i < length; ++i) {
            int x, y, z;
            config_setting_t *block = config_setting_get_elem(blockSetting, i);
            config_setting_t *grid = config_setting_get_elem(gridSetting, i);

            config_setting_lookup_int(block, "x", &x);
            config_setting_lookup_int(block, "y", &y);
            config_setting_lookup_int(block, "z", &z);
            performanceMetric.blockDimList[i].x = (uint32_t) x;
            performanceMetric.blockDimList[i].y = (uint32_t) y;
            performanceMetric.blockDimList[i].z = (uint32_t) z;

            config_setting_lookup_int(grid, "x", &x);
            config_setting_lookup_int(grid, "y", &y);
            config_setting_lookup_int(grid, "z", &z);
            performanceMetric.gridDimList[i].x = (uint32_t) x;
            performanceMetric.gridDimList[i].y = (uint32_t) y;
            performanceMetric.gridDimList[i].z = (uint32_t) z;
        }
    }
}
Exemple #10
0
/*
 * Check if the software can run on the hardware
 */
static int parse_hw_compatibility(config_t *cfg, struct swupdate_cfg *swcfg)
{
	const config_setting_t *setting, *hw;
	int count, i;
	const char *s;
	struct hw_type *hwrev;

	setting = get_setting(cfg, "hardware-compatibility", swcfg);
	if (setting == NULL) {
		ERROR("HW compatibility not found\n");
		return -1;
	}

	count = config_setting_length(setting);

	for(i = 0; i < count; ++i) {
		hw = config_setting_get_elem(setting, i);

		s = config_setting_get_string(hw);
		if (!s)
			continue;

		hwrev = (struct hw_type *)calloc(1, sizeof(struct hw_type));
		if (!hwrev) {
			ERROR("No memory: malloc failed\n");
			return -1;
		}

		strncpy(hwrev->revision, s, sizeof(hwrev->revision));
		LIST_INSERT_HEAD(&swcfg->hardware, hwrev, next);
		TRACE("Accepted Hw Revision : %s", hwrev->revision);
	}

	return 0;
}
Exemple #11
0
void print_layouts(int num_frame)
{
	
	config_setting_t *category_list, *category, *layout_list, *layout;
	config_t layout_config;
	int layout_length, i;
	const char* ascii_image;

	config_init(&layout_config);
	config_read_file(&layout_config, "./layout.cfg");
	
	category_list = config_lookup(&layout_config, "application.layout_group");
	category = config_setting_get_elem(category_list, num_frame - MIN_NUM_FRAME);
	
	layout_list = config_setting_get_member(category, "layout");
	layout_length = config_setting_length(layout_list);
	for(i = 0; i < layout_length; i++)
	{
		layout = config_setting_get_elem(layout_list, i);
		config_setting_lookup_string(layout, "image", &ascii_image);
		printf(" %c)\n", 'a' + i);
		printf("%s\n", ascii_image);
	}
	
	config_destroy(&layout_config);
}
Exemple #12
0
void configure_clay_handlers(spade_server* server, config_t* configuration) {
    config_setting_t* handler_settings = config_lookup(configuration,
            "clay.handlers");
    if (!handler_settings) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "No Clay handlers registered");
        return;
    }
    int clay_handler_count = config_setting_length(handler_settings);

    for (int n = 0; n < clay_handler_count; n++) {
        config_setting_t* handler_setting = config_setting_get_elem(
                handler_settings, n);
        const char* endpoint = NULL;
        config_setting_lookup_string(handler_setting, "endpoint", &endpoint);

        const char* url = NULL;
        config_setting_lookup_string(handler_setting, "url", &url);

        if(!register_clay_handler(server, url, endpoint)) {
            log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                    "Registered Clay handler for URL prefix '%s' at endpoint %s",
                    url, endpoint);
        }
    }
    if (server->clay_handler_count == 0) {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "No Clay handlers registered");
    } else {
        log4c_category_log(log4c_category_get("spade"), LOG4C_PRIORITY_INFO,
                "Registered a total of %d Clay handlers",
                server->clay_handler_count);
    }
}
static int
load_mods(config_t *config, config_setting_t *setting)
{
    int err;
    unsigned int mod_count;
    int i;
    const char* mod_name;
    const char* mod_so;
    const char* mod_ident;
    config_setting_t *mod_setting;
    err = 0;

    fprintf(stderr, "load mods from config\n");
    setting = config_lookup(config, "mods");
    if (setting != NULL) {
        mod_count = config_setting_length(setting);
        for (i = 0; i < mod_count; ++i) {
            mod_setting = config_setting_get_elem(setting, i);
            if (mod_setting) {
                if (!config_setting_lookup_string(mod_setting,
                                                  "name",
                                                  &mod_name) ||
                        !config_setting_lookup_string(mod_setting,
                                                      "so",
                                                      &mod_so)) {
                    continue;
                }
                if (!config_setting_lookup_string(mod_setting,
                                                  "ident",
                                                  &mod_ident)) {
                    mod_ident = NULL;
                }
                fprintf(stderr, "load module %s - %s - [%s]\n",
                        mod_name, mod_so, mod_ident);
                module_t *mod = module_open(mod_name,
                                            mod_ident,
                                            mod_so,
                                            RTLD_NOW);
                if (!mod) {
                    err = 1;
                    break;
                }
                if (module_map_insert(&g_config.module_root, mod) == NULL) {
                    err = 1;
                    module_close(mod);
                    break;
                }
                if (module_call_init_func(mod, &g_config)) {
                    fprintf(stderr, "ERROR %s returned not 0\n", mod->name);
                    err = 1;
                    module_close(mod);
                    break;
                }
            }
        }
    }

    return err;
}
Exemple #14
0
static void typecheck_config_ifgrouplist(config_setting_t *list) {
	assert(config_setting_is_list(list));
	for (int i = 0; i < config_setting_length(list); i++) {
		config_setting_t *ifgroup = config_setting_get_elem(list, i);
		if (!config_setting_is_group(ifgroup))
			die("Interface group lists must only contain groups.");
		typecheck_config_ifgroup(ifgroup);
	}
}
Exemple #15
0
/**
 * Attempt to create a global channel from the channel config
 * @param chan: Channel list
 * @param tmp_chan: Temporary channel data
 * @param i: Index
 * @return True on success or false on failure
 */
bool channel_read_sub(config_setting_t *chan, struct Channel *tmp_chan, uint8 i) {
	config_setting_t  *group_list = NULL;
	int delay = 1000, autojoin = 0, leave = 1, chat = 1, color_override = 0,
		self_notif = 1, join_notif = 0, leave_notif = 0;
	int type = CHAN_TYPE_PUBLIC, group_count = 0;
	const char *name = NULL, *password = NULL, *alias = NULL, *color_str = "Default", *type_str = NULL;

	if (tmp_chan == NULL)
		return false;

	if (!config_setting_lookup_string(chan, "name", &name)) {
		ShowError("Please input channel 'name' at '%s' line '%d'! Skipping...\n", chan->file, chan->line);
		return false;
	}
	if (config_setting_lookup_string(chan, "type", &type_str) && !script_get_constant(type_str, &type)) {
		ShowError("Invalid channel type %s at '%s' line '%d'! Skipping...\n", type_str, chan->file, chan->line);
		return false;
	}
	config_setting_lookup_string(chan, "password", &password);
	config_setting_lookup_string(chan, "alias", &alias);
	config_setting_lookup_string(chan, "color", &color_str);
	config_setting_lookup_int(chan, "delay", &delay);
	config_setting_lookup_bool(chan, "autojoin", &autojoin);
	config_setting_lookup_bool(chan, "leave", &leave);
	config_setting_lookup_bool(chan, "chat", &chat);
	config_setting_lookup_bool(chan, "color_override", &color_override);
	config_setting_lookup_bool(chan, "self_notif", &self_notif);
	config_setting_lookup_bool(chan, "join_notif", &join_notif);
	config_setting_lookup_bool(chan, "leave_notif", &leave_notif);

	safestrncpy(tmp_chan->name,name+1,sizeof(tmp_chan->name));
	if (password)
		safestrncpy(tmp_chan->pass,password,sizeof(tmp_chan->pass));
	else
		tmp_chan->pass[0] = '\0';
	safestrncpy(tmp_chan->alias,alias?alias:name,sizeof(tmp_chan->alias));
	tmp_chan->msg_delay = delay;
	tmp_chan->type = (enum Channel_Type)type;
	tmp_chan->color = channel_getColor(color_str);

	tmp_chan->opt = (autojoin ? CHAN_OPT_AUTOJOIN : 0) |
		(leave ? CHAN_OPT_CAN_LEAVE : 0) |
		(chat ? CHAN_OPT_CAN_CHAT : 0) |
		(color_override ? CHAN_OPT_COLOR_OVERRIDE : 0) |
		(self_notif ? CHAN_OPT_ANNOUNCE_SELF : 0) |
		(join_notif ? CHAN_OPT_ANNOUNCE_JOIN : 0) |
		(leave_notif ? CHAN_OPT_ANNOUNCE_LEAVE : 0);

	if ((group_list = config_setting_get_member(chan, "groupid")) && (group_count = config_setting_length(group_list)) > 0) {
		int j;
		CREATE(tmp_chan->groups, unsigned short, group_count);
		tmp_chan->group_count = group_count;
		for (j = 0; j < group_count; j++) {
			int groupid = config_setting_get_int_elem(group_list, j);
			tmp_chan->groups[j] = groupid;
		}
	}
Exemple #16
0
/**
 * @param[in] root Root section of config.
 * @param[in] zconf Config handle.
 * @return True on success.
 */
bool zconfig_load_sections(const config_setting_t *root, zconfig_t *zconf)
{
    // global section
    config_setting_t *section = config_setting_get_member(root, ZCFG_SECTION_GLOBAL);
    if (!section) {
        ZLOG(LOG_ERR, "config: %s section not found", ZCFG_SECTION_GLOBAL);
        return false;
    }
    if (0 != zconfig_global_load(section, zconf)) {
        ZLOG(LOG_ERR, "config: failed to load %s section", ZCFG_SECTION_GLOBAL);
        return false;
    }

    // all other sections parse as scopes
    u_int sections_count = (u_int) config_setting_length(root);

    // global section + minimum one scope section
    if (sections_count < 2) {
        ZLOG(LOG_ERR, "config: no scopes found");
        return false;
    }

    utarray_init(&zconf->scopes, &ut_ptr_icd);
    for (u_int i = 0; i < sections_count; i++) {
        section = config_setting_get_elem(root, i);

        if (!config_setting_is_group(section)) {
            continue;
        }
        if (0 == strcmp(section->name, ZCFG_SECTION_GLOBAL)) {
            continue;
        }

        for (size_t j = 0; j < utarray_len(&zconf->scopes); j++) {
            zconfig_scope_t *sc = *(zconfig_scope_t **) utarray_eltptr(&zconf->scopes, j);
            if (0 == strcasecmp(sc->name, section->name)) {
                ZLOG(LOG_ERR, "config: duplicate scope %s", section->name);
                return false;
            }
        }

        zconfig_scope_t *scope = malloc(sizeof(*scope));
        if (0 == zconfig_scope_load(section, scope)) {
            utarray_push_back(&zconf->scopes, &scope);
            ZLOG(LOG_DEBUG, "config: loaded scope %s", scope->name);
        } else {
            zconfig_scope_destroy(scope);
            free(scope);
            ZLOG(LOG_ERR, "config: failed to load scope %s", section->name);
            return false;
        }
    }

    return true;
}
Exemple #17
0
int get_array_length(parsertype p, void *root)
{
	switch (p) {
	case LIBCFG_PARSER:
		return config_setting_length(root);
	case JSON_PARSER:
		return json_object_array_length(root);
	}

	return 0;
}
Exemple #18
0
void initSerialPort() {
  struct termios options;
	const config_setting_t *device;
	int count;
	device = config_lookup(cf, "device");
  count = config_setting_length(device);
    
  DEBUG(LOG_INFO, "There are %d device in your config file",count);
  int n;
  for (n = 0; n < count; n++) {
    fd = open(config_setting_get_string_elem(device, n), O_RDWR | O_NOCTTY);
    if (fd != -1) break;
  }
	
	if (fd == -1) {
		DEBUG(LOG_ERR, "Open_port: Unable to open serial ports");
		printf("Exiting: no serial port available");
		exit(EXIT_FAILURE);/*
    * Could not open the port.*/
	}
	DEBUG(LOG_INFO,"Opening %s",config_setting_get_string_elem(device, n));
	
  fcntl(fd, F_SETFL, 0);
			
  /*
  * Get the current options for the port...
	*/ 
   tcgetattr(fd, &options);
	/*
 	* Set the baud rates to 19200...
  */
	cfsetispeed(&options, B9600);
	cfsetospeed(&options, B9600);
	
	/*
  * Enable the receiver and set local mode...
	*/ 
  options.c_cflag |= (CLOCAL | CREAD | CS8 );
	options.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
//	options.c_cflag &= ~CRTSCTS ;
    	
	options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

  options.c_iflag &= ~(ICRNL | IXON | IXOFF | IXANY);
    
  options.c_oflag &= ~OPOST;
    
  /*
	* Set the new options for the port...
	*/
	tcsetattr(fd, TCSAFLUSH, &options);
	fcntl(fd, F_SETFL, FNDELAY);
}
Exemple #19
0
static void typecheck_config_array(config_setting_t *array) {
	assert(config_setting_is_array(array));
	int length = config_setting_length(array);
	if (length > 6) die("Arrays must be fewer than seven elements.");
	for (int i = 0; i < length; i++) {
		config_setting_t *elem = config_setting_get_elem(array, i);
		int val = config_setting_get_int(elem);
		if (config_setting_type(elem) != CONFIG_TYPE_INT || val > 0xFF || val < -1) {
			die("Arrays must only contain numbers in the range -1 to 0xFF.");
		}
	}
}
Exemple #20
0
/**
 * Load interfaces section.
 * @param[in] option Option name.
 * @param[in,out] array Resulting array.
 * @return <0 - error. 0 - success. >0 - not found.
 */
int zcfg_load_interfaces(const config_setting_t *option, UT_array *array)
{
    utarray_init(array, &ut_zif_pair_icd);

    if (!option) {
        return 1;
    }

    if (CONFIG_TYPE_LIST != option->type) {
        return -1;
    }

    u_int count = (u_int) config_setting_length(option);

    for (u_int i = 0; i < count; i++) {
        zifpair_t if_pair;
        const char *str;
        config_setting_t *entry = config_setting_get_elem(option, i);

        if (!config_setting_lookup_string(entry, ZCFG_LAN, &str)) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid or missing 'lan' property", option->parent->name, option->name);
            goto fail;
        }
        strncpy(if_pair.lan, str, sizeof(if_pair.lan));

        if (!config_setting_lookup_string(entry, ZCFG_WAN, &str)) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid or missing 'wan' property", option->parent->name, option->name);
            goto fail;
        }
        strncpy(if_pair.wan, str, sizeof(if_pair.wan));

        int affinity = 0;
        if (!config_setting_lookup_int(entry, ZCFG_AFFINITY, &affinity)) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid or missing 'affinity' property", option->parent->name, option->name);
            goto fail;
        }
        if ((affinity < 0) || affinity >= UINT16_MAX) {
            ZLOG(LOG_ERR, "config:%s:%s: invalid 'affinity' value", option->parent->name, option->name);
            goto fail;
        }
        if_pair.affinity = (uint16_t) affinity;

        utarray_push_back(array, &if_pair);
    }

    return 0;

    fail:
    utarray_done(array);
    return -1;
}
Exemple #21
0
int get_server_config(s_config *c, char *config_path)
{
	config_t cfg;
	config_setting_t *modules_setting, *module_setting;
	unsigned int count, i;
	const char *module_name;

	config_init(&cfg);

	if (!config_read_file(&cfg, config_path)) {
		config_destroy(&cfg);
		return CONFIG_FILE_READ_ERROR;
	}

	if (config_lookup_int(&cfg, "port", &(c->port))
		&& config_lookup_string(&cfg, "host", &(c->host))
		&& config_lookup_string(&cfg, "web_root", &(c->web_root))
		&& config_lookup_string(&cfg, "web_prefix", &(c->web_prefix))
		&& config_lookup_string(&cfg, "api_prefix", &(c->api_prefix))
		&& config_lookup_string(&cfg, "index_file", &(c->index_file))
		&& config_lookup_string(&cfg, "api_modules_path", &(c->api_modules_path))
		&& config_lookup_int(&cfg, "buffer_size", &(c->buffer_size))
		&& config_lookup_int(&cfg, "api_modules_number", &(c->api_modules_number))
	) {
		c->buffer = (char*) calloc((size_t) c->buffer_size, sizeof(char));

		/* Output a list of all movies in the inventory. */
		modules_setting = config_lookup(&cfg, "api_modules");
		if (modules_setting != NULL) {
			count = (unsigned int) config_setting_length(modules_setting);
			if (count != (unsigned int) c->api_modules_number)
				return CONFIG_INCONSISTENT_DATA;
			else if (count == 0)
				return CONFIG_FILE_READ_OK;

			c->api_modules_names = malloc(count * sizeof(char*));
			for (i = 0; i < count; ++i) {
				module_setting = config_setting_get_elem(modules_setting, i);
				module_name = config_setting_get_string(module_setting);

				c->api_modules_names[i] = module_name;
			}

			map_init(&c->api_modules, c->api_modules_number);
		}

		return CONFIG_FILE_READ_OK;
	}
	else
		return CONFIG_MISSING_KEY;
}
Exemple #22
0
/*******************************************************************************
 函数名称  : webauth_rdx_read_setting
 功能描述  : 读取服务器配置
 输入参数  : 无
 输出参数  : 服务器配置列表
 返 回 值  :
--------------------------------------------------------------------------------
 最近一次修改记录 :
 修改作者   :      王群
 修改目的   :      新添加函数
 修改日期   :      2010-08-16
*******************************************************************************/
webauth_rdx_server_conf_t *webauth_rdx_read_setting (void)
{
    config_t cfg;
    webauth_rdx_server_conf_t *server_cfg, *head;
    config_setting_t *server_set;
    config_setting_t *root_set;
    int server_cnt, i;
    int max_index;

    config_init (&cfg);

    head = NULL;
    if (0 == config_read_file (&cfg, RADIUS_CFG_FILE))
    {
        config_destroy (&cfg);
        return head;
    }

    root_set = config_root_setting (&cfg);
    server_cnt = config_setting_length (root_set);
    max_index = 0;
    for (i = 0; i < server_cnt; i++)
    {
        /* 遍历所有的RADIUS服务器配置,找到指定的服务器 */
        server_set = config_setting_get_elem (root_set, (unsigned int) i);
        if (NULL != server_set)
        {
            server_cfg = webauth_rdx_new_setting (config_setting_name (server_set));
            if (NULL != server_cfg)
            {
                webauth_rdx_read_server_setting (server_set, server_cfg);
                server_cfg->next = head;
                head = server_cfg;
                if (max_index < server_cfg->index)
                {
                    max_index = server_cfg->index;
                }
            }
        }
    }
    /* 如果配置的server没有指定index, 则指定之 */
    for (server_cfg = head; NULL != server_cfg; server_cfg = server_cfg->next)
    {
        if (-1 == server_cfg->index)
        {
            server_cfg->index = ++max_index;
        }
    }
    config_destroy (&cfg);
    return head;
}
Exemple #23
0
void configure_audio()
{
	const char *setting_devices_audio_output = NULL;

	if (config_lookup_string(&app_config, CFG_DEVICES_AUDIO_OUTPUT, &setting_devices_audio_output) != CONFIG_TRUE)
	{
		LOG_ERROR("Missing audio output device in config");
		exit(EXIT_FAILURE);
	}

	if (alsa_initialise(setting_devices_audio_output, 128) < 0)
	{
		exit(EXIT_FAILURE);
	}

	config_setting_t *setting_auto_duck = config_lookup(&app_config, CFG_DEVICES_AUDIO_AUTO_DUCK);

	if (setting_auto_duck != NULL)
	{
		int duck_setting_count = config_setting_length(setting_auto_duck);

		if (duck_setting_count != synth_model.voice_count)
		{
			LOG_ERROR("Invalid number of auto duck levels %d - should be %d", duck_setting_count, synth_model.voice_count);
			exit(EXIT_FAILURE);
		}

		for (int i = 0; i < duck_setting_count; i++)
		{
			float duck_level_factor = config_setting_get_float_elem(setting_auto_duck, i);
			if (duck_level_factor < 0.0f || duck_level_factor > 1.0f)
			{
				LOG_ERROR("Invalid auto duck level %d of %f - should be between 0 and 1", i + 1, duck_level_factor);
				exit(EXIT_FAILURE);
			}

			int32_t duck_level = LEVEL_MAX * duck_level_factor;

			if (duck_level > duck_level_by_voice_count[i])
			{
				LOG_ERROR("Invalid auto duck level %d of %f - values should be decreasing", i + 1, duck_level_factor);
				exit(EXIT_FAILURE);
			}

			duck_level_by_voice_count[i + 1] = duck_level;
		}
	}

	synth_model_set_ducking_levels(&synth_model, duck_level_by_voice_count);
}
Exemple #24
0
/**
 * Load subnet array.
 * @param[in] cfg Config section.
 * @param[in] option Option name.
 * @param[in,out] array Resulting array.
 * @return <0 - error. 0 - success. >0 - not found.
 */
static int zcfg_load_subnet_list(const config_setting_t *option, zsubnet_group_t *array)
{
    utarray_init(array, &ut_ip_range_icd);

    if (!option) {
        return 1;
    }

    if (CONFIG_TYPE_LIST != option->type) {
        return -1;
    }

    int count = config_setting_length(option);

    for (int i = 0; i < count; i++) {
        ip_range_t range;
        char ip_str[INET_ADDRSTRLEN];
        const char *item = config_setting_get_string_elem(option, i);
        const char *cidr_pos = strchr(item, '/');

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

            struct in_addr ip_addr;
            if (0 < inet_pton(AF_INET, ip_str, &ip_addr)) {
                uint8_t cidr = 0;
                if ((0 == str_to_u8(cidr_pos + 1, &cidr)) && (cidr <= CIDR_MAX)) {
                    range.ip_start = ntohl(ip_addr.s_addr);
                    range.ip_end = IP_RANGE_END(range.ip_start, cidr);
                    utarray_push_back(array, &range);
                    continue;
                }
            }
        }

        // error handler
        ZLOG(LOG_ERR, "config:%s:%s: invalid subnet: %s", option->parent->name, option->name, item);
        utarray_done(array);
        return -1;
    }

    if (count) {
        utarray_sort(array, ip_range_cmp);
    }

    return 0;
}
Exemple #25
0
int get_layout(struct layout_t* out, int num_frame, int id_layout)
{
	config_setting_t *category_list, *category, *layout_list, *layout, *frame_list, *frame;
	config_setting_t *pos, *size;
	config_t layout_config;
	int frame_length, i;

	out->num_frames = num_frame;
	out->perc_to_pixel = 0; 
	out->frames = (struct frame_t*) malloc (num_frame * sizeof(struct frame_t));

	config_init(&layout_config);
	config_read_file(&layout_config, "./layout.cfg");
	
	category_list = config_lookup(&layout_config, "application.layout_group");
	category = config_setting_get_elem(category_list, num_frame - MIN_NUM_FRAME);
	
	layout_list = config_setting_get_member(category, "layout");
	layout = config_setting_get_elem(layout_list, id_layout);

	frame_list = config_setting_get_member(layout, "frame");
	frame_length = config_setting_length(frame_list);
	for(i = 0; i < frame_length; i++)
	{
		frame = config_setting_get_elem(frame_list, i);
		pos= config_setting_get_member(frame, "pos");
		size = config_setting_get_member(frame, "size");
		if (config_setting_lookup_float(pos, "x", &(out->frames[i].pos_x))==CONFIG_FALSE)
			printf("pos.x:\tONFIG_FALSE\n");

if ( config_setting_lookup_float(pos, "y", &(out->frames[i].pos_y))==CONFIG_FALSE)
			printf("pos.y:\tONFIG_FALSE\n");
		
if ( config_setting_lookup_float(size, "w", &(out->frames[i].width))==CONFIG_FALSE)
			printf("size.w:\tONFIG_FALSE\n");

if ( config_setting_lookup_float(size, "h", &(out->frames[i].height))==CONFIG_FALSE)
			printf("size.h:\tONFIG_FALSE\n");

		
		config_setting_lookup_float(frame, "rot", &(out->frames[i].rot));
	}

	
	config_destroy(&layout_config);
	return 0;
}
Exemple #26
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;
}
void sub_shc_popexec(int value)
{
	shc_node *item = shc_root;
    config_setting_t *root = config_lookup(&CONFIG, "shortcuts");
    char bname[1024];
    int i = 0;
    memset(bname, 0, sizeof(bname));

	for (i = 0; item != NULL && i < shc_selection; item = item->next, i++);

    if (!item || !root)
        return;

    switch (value)
    {
    case 0:
        {
            for (i = config_setting_length(root); i--;)
            {
                config_setting_t *elem = config_setting_get_elem(root, i);
                config_setting_t *member =
                    config_setting_get_member(elem, "path");

                if (member)
                {
                    if (!strcmp(config_setting_get_string(member),item->path))
                    {
                        sub_game_remove_shortcut(root, i);
                        msgbox(SDL_GetVideoSurface(),NULL, "My Shortcuts",
                               "Shortcut has been deleted.", OK);
                        cfg_save();
                        msgbox_retval();
                        return;
                    }
                }
            }
            msgbox(SDL_GetVideoSurface(),NULL,"System Error","Unable to delete Shortcut.",
                   OK);
            msgbox_retval();
        }
        break;

    default:
        break;
    }
}
Exemple #28
0
int get_num_layouts(int num_frame)
{
	config_setting_t *category_list, *category, *layout_list;
	config_t layout_config;
	int num_layouts;

	config_init(&layout_config);
	config_read_file(&layout_config, "./layout.cfg");
	
	category_list = config_lookup(&layout_config, "application.layout_group");
	category = config_setting_get_elem(category_list, num_frame - MIN_NUM_FRAME);
	
	layout_list = config_setting_get_member(category, "layout");
	num_layouts = config_setting_length(layout_list);
	
	config_destroy(&layout_config);
	return num_layouts;
}
Exemple #29
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));
		}
	}
}
Exemple #30
0
void worker_init(upstreams *upstr_ptr, mqd_t msgq_id) {
	char msgcontent[MAX_MSG_LEN_CH];
	int msgsz;
	unsigned int sender;
	struct mq_attr msgq_attr;
	struct timespec tm;
	const config_setting_t *upstr_setting;

	upstr_setting = config_lookup(&cfg, "upstreams");
	upstream_count = config_setting_length(upstr_setting);

	while (running) {
		sleep(1);
		mq_getattr(msgq_id, &msgq_attr);
		error_log(DEBUG,
				"Queue \"%s\":\n\t- stores at most %ld messages\n\t- large at most %ld bytes each\n\t- currently holds %ld messages\n",
				QUEUE, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize,
				msgq_attr.mq_curmsgs);

		/* getting a message */
		clock_gettime(CLOCK_REALTIME, &tm);
		tm.tv_sec += 1;

		msgsz = mq_timedreceive(msgq_id, msgcontent, MAX_MSG_LEN_CH, &sender,
				&tm);
		if (msgsz == -1) {
			if (errno == ETIMEDOUT) {
				continue;
			}
			error_log(ERROR, "mq_receive error: %s", strerror(errno));
			exit(EXIT_FAILURE);
		}

		/* Ok, we got file to download */
		if (initpath(msgcontent) != 0) {
			error_log(ERROR, "Initpath failed");
		} else if (download(msgcontent) != 0) {
			error_log(ERROR, "Failed to download file");
		}
	}
	error_log(ERROR, "Exiting");
}