Exemplo n.º 1
0
struct etherconf *config_read(char *config_filename){
	configfile_t *configfile;

	configfile = dotconf_create(config_filename ? config_filename : DEFAULT_CONFIGFILE,
				    options, NULL, CASE_INSENSITIVE);
	if (!configfile) {
		return NULL;
	}
	configs=malloc(sizeof(struct etherconf));
	configs[sections_idx]=defconfig;
	config=configs;
	config->name=DEFAULT_SECTION;

        configfile->errorhandler = (dotconf_errorhandler_t) errorhandler;

	if (dotconf_command_loop(configfile) == 0){
		logit(LOG_ERR, "Error reading config file\n");
		dotconf_cleanup(configfile);
		return NULL;
	}
	if(config->name == NULL){ /* no name for last section */
		logit(LOG_ERR,"Section unnamed");
		return NULL;
	}

	if(configfile)dotconf_cleanup(configfile);
	return configs;
}
Exemplo n.º 2
0
int main(void)
{
	configfile_t *configfile;
	struct mycontext context;

	context.current_end_token = 0;
	context.permissions = 0;

	context.pool = pool_new(NULL);
	configfile =
	    dotconf_create("./context.conf", options, (void *)&context,
			   CASE_INSENSITIVE);
	if (!configfile) {
		fprintf(stderr, "Error opening configuration file\n");
		return 1;
	}
	configfile->errorhandler = (dotconf_errorhandler_t) error_handler;
	configfile->contextchecker = (dotconf_contextchecker_t) context_checker;
	if (dotconf_command_loop(configfile) == 0)
		fprintf(stderr, "Error reading configuration file\n");

	dotconf_cleanup(configfile);
	pool_free(context.pool);

	return 0;
}
Exemplo n.º 3
0
static int32_t devman_config(int8_t *path_filename)
{
	configfile_t *configfile;

	if (path_filename == NULL)
	{
		printf("error: NULL input\n");
		return 1;
	}
	configfile = dotconf_create(path_filename, options, NULL, CASE_INSENSITIVE);
	if (!configfile)
	{
		fprintf(stderr, "Error creating configfile\n");
		return 1;
	}

	if (dotconf_command_loop(configfile) == 0)
	{
		fprintf(stderr, "Error parsing config file %s\n", configfile->filename);
		return 1;
	}

	dotconf_cleanup(configfile);

	return 0;
}
Exemplo n.º 4
0
Arquivo: simple.c Projeto: CMB/dotconf
int main(int argc, char **argv)
{
	configfile_t *configfile;

	configfile = dotconf_create(argv[1] ? argv[1] : "simple.conf",
				    options, NULL, CASE_INSENSITIVE);
	if (!configfile) {
		fprintf(stderr, "Error opening config file\n");
		return 1;
	}

	if (dotconf_command_loop(configfile) == 0)
		fprintf(stderr, "Error reading config file\n");

	dotconf_cleanup(configfile);

	return 0;
}
Exemplo n.º 5
0
int 
print_ganglia_25_config( char *path )
{
   configfile_t *configfile;
   FILE *fp;
   static configoption_t gmond_options[] =
      {
         {"name", ARG_STR, cb_name, &gmond_config, 0},
         {"owner", ARG_STR, cb_owner, &gmond_config, 0},
         {"latlong", ARG_STR, cb_latlong, &gmond_config, 0},
         {"url", ARG_STR, cb_url, &gmond_config, 0},
         {"location", ARG_STR, cb_location, &gmond_config, 0},
         {"mcast_channel", ARG_STR, cb_mcast_channel, &gmond_config, 0},
         {"mcast_port", ARG_INT, cb_mcast_port, &gmond_config, 0},
         {"mcast_if", ARG_STR, cb_mcast_if, &gmond_config, 0},
         {"mcast_ttl", ARG_INT, cb_mcast_ttl, &gmond_config, 0},
         {"mcast_threads", ARG_INT, cb_mcast_threads, &gmond_config, 0},
         {"xml_port", ARG_INT, cb_xml_port, &gmond_config, 0},
         {"xml_threads", ARG_INT, cb_xml_threads, &gmond_config, 0},
         {"trusted_hosts", ARG_LIST, cb_trusted_hosts, &gmond_config, 0},
         {"num_nodes", ARG_INT, cb_num_nodes, &gmond_config, 0},
         {"num_custom_metrics", ARG_INT, cb_num_custom_metrics, &gmond_config, 0},
         {"mute", ARG_TOGGLE, cb_mute, &gmond_config, 0},
         {"deaf", ARG_TOGGLE, cb_deaf, &gmond_config, 0},
         {"allow_extra_data", ARG_TOGGLE, cb_allow_extra_data, &gmond_config, 0},
         {"debug_level", ARG_INT, cb_debug_level, &gmond_config, 0},
         {"no_setuid", ARG_TOGGLE, cb_no_setuid, &gmond_config, 0},
         {"setuid", ARG_STR, cb_setuid, &gmond_config, 0},
         {"no_gexec", ARG_TOGGLE, cb_no_gexec, &gmond_config, 0},
         {"all_trusted", ARG_TOGGLE, cb_all_trusted, &gmond_config, 0},
         {"host_dmax", ARG_INT, cb_host_dmax, &gmond_config, 0},
         LAST_OPTION
      };

   if(!path)
     {
       return 1;
     }
   fp = fopen( path, "r");
   if(!fp)
     {
       fprintf(stderr,"Unable to open ganglia 2.5 configuration '%s'. Exiting.\n", path);
       return 1; 
     }

   set_defaults(&gmond_config);

   configfile = dotconf_create(path, gmond_options, 0, CASE_INSENSITIVE);
   if(!configfile)
     {
       fprintf(stderr,"dotconf_create() error for configuration '%s'. Exiting.\n", path);
       return 1;
     }

   configfile->errorhandler = (dotconf_errorhandler_t) errorhandler;

   if (dotconf_command_loop(configfile) == 0)
      {
         dotconf_cleanup(configfile);
         return 1;
      }
   dotconf_cleanup(configfile);

   print_config(path, &gmond_config);
   return 0;
}
Exemplo n.º 6
0
configfile_t *dotconf_create(char *fname, const configoption_t * options,
			     context_t * context, unsigned long flags)
{
	char *dc_env = NULL;
	int registered = 0;
	configfile_t *new_cfg = calloc(1, sizeof(configfile_t));
	char *path = NULL;
	char *cwd = NULL;

	if (!new_cfg)
		return NULL;

	/*
	 * From here on, we can use dotconf_cleanup to free resources
	 * when errors occur.  All of our pointers are NULL, because
	 * we allocated with calloc.  When an error occurs, dotconf_cleanup
	 * will free all of the resources that were allocated prior to the
	 * error.
	 */

	new_cfg->context = context;
	new_cfg->flags = flags;
	if (new_cfg->flags & CASE_INSENSITIVE)
		new_cfg->cmp_func = strncasecmp;
	else
		new_cfg->cmp_func = strncmp;

	new_cfg->stream = fopen(fname, "r");
	if (new_cfg->stream == NULL) {
		fprintf(stderr, "Error opening configuration file '%s'\n",
			fname);
		dotconf_cleanup(new_cfg);
		return NULL;
	}

	registered = dotconf_register_options(new_cfg, dotconf_options);
	if (!registered) {
		dotconf_cleanup(new_cfg);
		return NULL;
	}

	registered = dotconf_register_options(new_cfg, options);
	if (!registered) {
		dotconf_cleanup(new_cfg);
		return NULL;
	}

	new_cfg->filename = strdup(fname);
	if (!new_cfg->filename) {
		dotconf_cleanup(new_cfg);
		return NULL;
	}

	new_cfg->includepath = malloc(CFG_MAX_FILENAME);
	if (!new_cfg->includepath) {
		dotconf_cleanup(new_cfg);
		return NULL;
	}

	new_cfg->includepath[0] = 0x00;

	/*
	 * take default includepath from environment if present
	 * Otherwise, resolve the path of the configuration file and use that
	 * as default includepath.
	 */
	dc_env = getenv(CFG_INCLUDEPATH_ENV);
	if (dc_env != NULL) {
		snprintf(new_cfg->includepath, CFG_MAX_FILENAME, "%s", dc_env);
	} else {
		path = get_path(fname);
		if (path != NULL) {
			if (path[0] == '/') {
				snprintf(new_cfg->includepath, CFG_MAX_FILENAME, "%s", path);
			} else {
				cwd = get_cwd();
				if (cwd != NULL) {
					snprintf(new_cfg->includepath, CFG_MAX_FILENAME,
						"%s/%s", cwd, path);
					free(cwd);
				}
			}
			free(path);
		}
	}
	return new_cfg;
}
Exemplo n.º 7
0
/* ------ a '*' in name and calls the Internal Include function on that filename -- */
int dotconf_handle_star(command_t * cmd, char *path, char *pre, char *ext)
{
	configfile_t *included;
	DIR *dh = 0;
	struct dirent *dirptr = 0;

	char new_pre[CFG_MAX_FILENAME];
	char new_ext[CFG_MAX_FILENAME];
	char already_matched[CFG_MAX_FILENAME];

	char wc = '\0';

	char *new_path = 0;
	char *s_ext = 0;
	char *t_ext = 0;
	char *sub = 0;
	char *wc_path = 0;
	char *wc_pre = 0;
	char *wc_ext = 0;
	char *temp = NULL;

	int pre_len;
	int new_path_len;
	int name_len = 0;
	int alloced = 0;
	int match_state = 0;
	int t_ext_count = 0;
	int sub_count = 0;

	pre_len = strlen(pre);
	memset(already_matched, 0, CFG_MAX_FILENAME);
	s_ext = ext;

	while (dotconf_is_wild_card(*s_ext)) {	/* remove trailing wild-cards proceeded by * */
		s_ext++;
	}

	t_ext = s_ext;

	while (t_ext != NULL && !(dotconf_is_wild_card(*t_ext))
	       && *t_ext != '\0') {
		t_ext++;	/* find non-wild-card string */
		t_ext_count++;
	}

	strncpy(new_ext, s_ext, t_ext_count);
	new_ext[t_ext_count] = '\0';

	if ((dh = opendir(path)) != NULL) {
		while ((dirptr = readdir(dh)) != NULL) {
			sub_count = 0;
			t_ext_count = 0;

			match_state =
			    dotconf_star_match(dirptr->d_name, pre, s_ext);

			if (match_state >= 0) {
				name_len = strlen(dirptr->d_name);
				new_path_len =
				    strlen(path) + name_len + strlen(s_ext) + 1;

				if (!alloced) {
					if ((new_path =
					     (char *)malloc(new_path_len)) ==
					    NULL) {
						return -1;
					}

					alloced = new_path_len;

				} else {

					if (new_path_len > alloced) {
						temp =
						    realloc(new_path,
							    new_path_len);
						if (temp == NULL) {
							free(new_path);
							return -1;
						}
						new_path = temp;
						alloced = new_path_len;

					}

				}

				if (match_state == 1) {

					if ((sub =
					     strstr((dirptr->d_name + pre_len),
						    new_ext)) == NULL) {
						continue;
					}

					while (sub != dirptr->d_name) {
						sub--;
						sub_count++;
					}

					if (sub_count + t_ext_count > name_len) {
						continue;
					}

					strncpy(new_pre, dirptr->d_name,
						(sub_count + t_ext_count));
					new_pre[sub_count + t_ext_count] = '\0';
					strcat(new_pre, new_ext);

					sprintf(new_path, "%s%s%s", path,
						new_pre, t_ext);

					if (strcmp(new_path, already_matched) ==
					    0) {
						continue;	/* Already searched this expression */

					} else {

						strcpy(already_matched,
						       new_path);

					}

					if (dotconf_find_wild_card
					    (new_path, &wc, &wc_path, &wc_pre,
					     &wc_ext) >= 0) {
						if (dotconf_handle_wild_card
						    (cmd, wc, wc_path, wc_pre,
						     wc_ext) < 0) {
							dotconf_warning(cmd->
									configfile,
									DCLOG_WARNING,
									ERR_INCLUDE_ERROR,
									"Error occured while processing wildcard %c\n"
									"Filename is '%s'\n",
									wc,
									new_path);

							free(new_path);
							dotconf_wild_card_cleanup
							    (wc_path, wc_pre);
							return -1;
						}

						dotconf_wild_card_cleanup
						    (wc_path, wc_pre);
						continue;
					}

				}

				sprintf(new_path, "%s%s", path, dirptr->d_name);

				if (access(new_path, R_OK)) {
					dotconf_warning(cmd->configfile,
							DCLOG_WARNING,
							ERR_INCLUDE_ERROR,
							"Cannot open %s for inclusion.\n"
							"IncludePath is '%s'\n",
							new_path,
							cmd->configfile->
							includepath);
					return -1;
				}

				included =
				    dotconf_create(new_path,
						   cmd->configfile->
						   config_options[1],
						   cmd->configfile->context,
						   cmd->configfile->flags);
				if (included) {
					included->errorhandler =
					    cmd->configfile->errorhandler;
					included->contextchecker =
					    cmd->configfile->contextchecker;
					dotconf_command_loop(included);
					dotconf_cleanup(included);
				}

			}

		}

		closedir(dh);
		free(new_path);

	}

	return 0;
}
Exemplo n.º 8
0
/* ------ a '?' in name and calls the Internal Include function on that filename -- */
int dotconf_handle_question_mark(command_t * cmd, char *path, char *pre,
				 char *ext)
{
	configfile_t *included;
	DIR *dh = 0;
	struct dirent *dirptr = 0;
	int i;

	char new_pre[CFG_MAX_FILENAME];
	char already_matched[CFG_MAX_FILENAME];

	char wc = '\0';

	char *new_path = 0;
	char *wc_path = 0;
	char *wc_pre = 0;
	char *wc_ext = 0;
	char *temp = NULL;

	int pre_len;
	int new_path_len;
	int name_len = 0;
	int alloced = 0;
	int match_state = 0;

	pre_len = strlen(pre);

	if ((dh = opendir(path)) != NULL) {
		while ((dirptr = readdir(dh)) != NULL) {
			match_state =
			    dotconf_question_mark_match(dirptr->d_name, pre,
							ext);

			if (match_state >= 0) {
				name_len = strlen(dirptr->d_name);
				new_path_len =
				    strlen(path) + name_len + strlen(ext) + 1;

				if (!alloced) {
					if ((new_path =
					     (char *)malloc(new_path_len)) ==
					    NULL) {
						return -1;
					}

					alloced = new_path_len;

				} else {

					if (new_path_len > alloced) {
						temp =
						    realloc(new_path,
							    new_path_len);
						if (temp == NULL) {
							free(new_path);
							return -1;
						}
						new_path = temp;
						alloced = new_path_len;

					}

				}

				if (match_state == 1) {

					strncpy(new_pre, dirptr->d_name,
						(name_len >
						 pre_len) ? (pre_len +
							     1) : pre_len);
					new_pre[(name_len >
						 pre_len) ? (pre_len +
							     1) : pre_len] =
					    '\0';

					sprintf(new_path, "%s%s%s", path,
						new_pre, ext);

					if (strcmp(new_path, already_matched) ==
					    0) {
						continue;	/* Already searched this expression */

					} else {

						strcpy(already_matched,
						       new_path);

					}

					if (dotconf_find_wild_card
					    (new_path, &wc, &wc_path, &wc_pre,
					     &wc_ext) >= 0) {
						if (dotconf_handle_wild_card
						    (cmd, wc, wc_path, wc_pre,
						     wc_ext) < 0) {
							dotconf_warning(cmd->
									configfile,
									DCLOG_WARNING,
									ERR_INCLUDE_ERROR,
									"Error occured while processing wildcard %c\n"
									"Filename is '%s'\n",
									wc,
									new_path);

							free(new_path);
							dotconf_wild_card_cleanup
							    (wc_path, wc_pre);
							return -1;
						}

						dotconf_wild_card_cleanup
						    (wc_path, wc_pre);
						continue;
					}

				}

				sprintf(new_path, "%s%s", path, dirptr->d_name);

				if (access(new_path, R_OK)) {
					dotconf_warning(cmd->configfile,
							DCLOG_WARNING,
							ERR_INCLUDE_ERROR,
							"Cannot open %s for inclusion.\n"
							"IncludePath is '%s'\n",
							new_path,
							cmd->configfile->
							includepath);
					return -1;
				}

				included =
				    dotconf_create(new_path,
						   cmd->configfile->
						   config_options[1],
						   cmd->configfile->context,
						   cmd->configfile->flags);
				if (included) {
					for (i = 2;
					     cmd->configfile->config_options[i];
					     i++)
						dotconf_register_options
						    (included,
						     cmd->configfile->
						     config_options[i]);
					included->errorhandler =
					    cmd->configfile->errorhandler;
					included->contextchecker =
					    cmd->configfile->contextchecker;
					dotconf_command_loop(included);
					dotconf_cleanup(included);
				}

			}

		}

		closedir(dh);
		free(new_path);

	}

	return 0;
}
Exemplo n.º 9
0
int main(int argc, char *argv[])
{
	char *cmd_buf;
	int ret;
	int ret_init;
	size_t n;
	char *configfilename;
	configfile_t *configfile;
	otts_synth_plugin_t *synth;
	char *status_info = NULL;

	g_thread_init(NULL);
	init_logging();
	open_log("stderr", 3);

	synth = synth_plugin_get();

	/* Initialize ltdl's list of preloaded audio backends. */
	LTDL_SET_PRELOADED_SYMBOLS();
	module_num_dc_options = 0;
	module_audio_id = 0;

	if (argc >= 2) {
		configfilename = g_strdup(argv[1]);
	} else {
		configfilename = NULL;
	}

	ret = synth->load();
	if (ret == -1)
		synth->close(1);

	if (configfilename != NULL) {
		/* Add the LAST option */
		module_dc_options = module_add_config_option(module_dc_options,
							     &module_num_dc_options,
							     "", 0, NULL, NULL,
							     0);

		configfile =
		    dotconf_create(configfilename, module_dc_options, 0,
				   CASE_INSENSITIVE);
		if (configfile) {
			if (dotconf_command_loop(configfile) == 0) {
				log_msg(OTTS_LOG_CRIT,
					"Error reading config file\n");
				synth->close(1);
			}
			dotconf_cleanup(configfile);
			log_msg(OTTS_LOG_NOTICE,
				"Configuration (pre) has been read from \"%s\"\n",
				configfilename);

			g_free(configfilename);
		} else {
			log_msg(OTTS_LOG_ERR,
				"Can't read specified config file!\n");
		}
	} else {
		log_msg(OTTS_LOG_WARN,
			"No config file specified, using defaults...\n");
	}

	ret_init = synth->init(&status_info);

	if (status_info == NULL) {
		status_info = g_strdup("unknown, was not set by module");
	}

	cmd_buf = NULL;
	n = 0;
	ret = otts_getline(&cmd_buf, &n, stdin);
	if (ret == -1) {
		log_msg(OTTS_LOG_CRIT,
			"Broken pipe when reading INIT, exiting... \n");
		synth->close(2);
	}

	if (!strcmp(cmd_buf, "INIT\n")) {
		if (ret_init != 0) {
			printf("399-%s\n", status_info);
			ret = printf("%s\n", "399 ERR CANT INIT MODULE");
			g_free(status_info);
			return -1;
		}

		printf("299-%s\n", status_info);
		ret = printf("%s\n", "299 OK LOADED SUCCESSFULLY");

		if (ret < 0) {
			log_msg(OTTS_LOG_CRIT, "Broken pipe, exiting...\n");
			synth->close(2);
		}
		fflush(stdout);
	} else {
		log_msg(OTTS_LOG_ERR,
			"ERROR: Wrong communication from module client: didn't call INIT\n");
		synth->close(3);
	}

	g_free(status_info);
	xfree(cmd_buf);

	while (1) {
		cmd_buf = NULL;
		n = 0;
		ret = otts_getline(&cmd_buf, &n, stdin);
		if (ret == -1) {
			log_msg(OTTS_LOG_CRIT, "Broken pipe, exiting... \n");
			synth->close(2);
		}

		log_msg(OTTS_LOG_INFO, "CMD: <%s>", cmd_buf);

		dispatch_cmd(synth, cmd_buf);

		xfree(cmd_buf);
	}
}