File: module.c

package info (click to toggle)
dotconf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 488 kB
  • sloc: ansic: 2,265; makefile: 186; sh: 39
file content (137 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* this example does not work for WIN32 */

#ifndef WIN32
#include <dlfcn.h>		/* for dlopen(), dlerror(), dlclose() */
#include <unistd.h>
#endif

#include <dotconf.h>

/* define our callbacks; */
/* for definition of DOTCONF_CB see dotconf.h */

static DOTCONF_CB(cb_example);
static DOTCONF_CB(cb_moreargs);
static DOTCONF_CB(cb_addmodule);
static DOTCONF_CB(cb_multiline);	/* here document test */
static DOTCONF_CB(cb_unknown);	/* fallback for unknwon options */

/* define the Option ExampleOption that expects a string
 * and tell the parser to call the function cb_example
 */
static const configoption_t options[] = {
	{"ExampleOption", ARG_STR, cb_example, 0, 0},
	{"MoreArgs", ARG_LIST, cb_moreargs, 0, 0},
	{"AddModule", ARG_STR, cb_addmodule, 0, 0},
	{"MultiLineRaw", ARG_STR, cb_multiline, 0, 0},
	{"", ARG_NAME, cb_unknown, 0, 0},
	LAST_OPTION
};

#define NUM_MODULES 10
static void *handles[NUM_MODULES];	/* handles of dynamically loaded modules */

int main(int argc, char **argv)
{
	int i;
	configfile_t *configfile;

	memset(handles, 0, sizeof(handles));
	putenv("TESTUSER=lukas");

	/*
	 * start reading config, CASE_INSENSITIVE (specify NONE or 0 for the
	 * the default behaviour which matches case sensitive
	 */
	configfile = dotconf_create(argv[1] ? argv[1] : "sample.conf",
				    options, 0, 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);

	for (i = 0; i < NUM_MODULES && handles[i]; i++)
		dlclose(handles[i]);

	return 0;
}

/* the error-handler; a new feature of v0.7.0 to filter out messages
   issued using config_warning
FUNC_ERRORHANDLER(my_errorhandler)
{
  printf("ERROR [type=%d][%d] %s\n", type, dc_errno, msg);
}
*/

/* declare our callback function */
DOTCONF_CB(cb_example)
{
	printf("%s:%ld: ExampleOption: [arg=%s]\n", cmd->configfile->filename,
	       cmd->configfile->line, cmd->data.str);
	return NULL;
}

/*
 * we dont need the userdata, so dont mention it
 * otherwise we should've appended it as argument 3
 */
DOTCONF_CB(cb_moreargs)
{
	int i;
	for (i = 0; i < cmd->arg_count; i++)
		printf("%s:%ld: [MoreArgs] Arg #%d '%s'\n",
		       cmd->configfile->filename, cmd->configfile->line, i + 1,
		       cmd->data.list[i]);
	return 0;
}

DOTCONF_CB(cb_addmodule)
{
	int i;
	char filename[128];	/* filename of modules */

	for (i = 0; (i < NUM_MODULES) && (handles[i] != 0); i++) ;

	snprintf(filename, 128, "./%s.so", cmd->data.str);

	if (!access(filename, R_OK)) {	/* if file access is permitted */
		/* load library */
		handles[i] = dlopen(filename, RTLD_LAZY);
		if (!handles[i])
			printf("Error opening library: %s\n", dlerror());
		dotconf_register_options(cmd->configfile,
					 dlsym(handles[i], "new_options"));
	}
	printf("Module %s successfully loaded\n", cmd->data.str);
	return NULL;
}

DOTCONF_CB(cb_multiline)
{
	printf("%s:%ld: [MultiLine - START] -%s- [MultiLine - END]\n",
	       cmd->configfile->filename, cmd->configfile->line, cmd->data.str);
	return NULL;
}

DOTCONF_CB(cb_unknown)
{
	int i = 0;

	printf("%s:%ld: UNKNOWN [%s]",
	       cmd->configfile->filename, cmd->configfile->line, cmd->name);

	for (i = 0; cmd->data.list[i]; i++)
		printf(", %s", cmd->data.list[i]);
	printf("\n");
	return NULL;
}