File: duplicate.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 (315 lines) | stat: -rw-r--r-- 8,468 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

/* You need at least dot.conf 1.0.9 to compile and run this example !! */

#include <stdio.h>		/* fprintf(), stderr       */
#include <stdlib.h>		/* realloc()               */
#include <string.h>		/* strcmp()                */
/* this example does not work for WIN32 */

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

#include <libpool.h>
#include <dotconf.h>

struct config_context {
	long current_context;
	char current_end_token[1024];

	pool_t *pool;
};

struct ptr_list {
	int n_entries;
	void **entries;
};

static DOTCONF_CB(section_open);
static DOTCONF_CB(section_close);
static DOTCONF_CB(common_option);
static DOTCONF_CB(addmodule_option);

/*
// the last field is used to specify the context needed for
// each option. These will be checked at runtime by our contextchecker
//
*/
static const configoption_t options[] = {
	{"AddModule", ARG_LIST, addmodule_option, NULL, 0},
	{"ToggleOption", ARG_TOGGLE, common_option, NULL, 0},
	LAST_OPTION
};

/*
// The pointer list of dynamic options.
// This is necessary to be able to free it up later.
//
*/
struct ptr_list memory_junk;

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

const char *context_checker(command_t * cmd, unsigned long option_context)
{
	struct config_context *context = cmd->context;

	if (context->current_context != option_context) {
		return pool_strcat(context->pool, "Option '", cmd->name,
				   "' not allowed in <",
				   (strlen(context->current_end_token) >
				    2) ? context->current_end_token +
				   2 : "global>", " context", NULL);
	}

	return NULL;
}

FUNC_ERRORHANDLER(error_handler)
{
	fprintf(stderr, "%s:%ld:[error] %s\n",
		configfile->filename, configfile->line, msg);

	/* continue reading the configfile ; return 1 stop after first error found */
	return 0;
}

int main(int argc, char *argv[])
{
	configfile_t *configfile;
	struct config_context context;

	if (argc < 2) {
		fprintf(stderr, "Usage : %s <configfile>\n", argv[0]);
		return 1;
	}
	context.current_end_token[0] = '\0';
	context.current_context = 0;
	context.pool = pool_new(NULL);

	memory_junk.n_entries = 0;
	memory_junk.entries = 0;

	memset(handles, 0, sizeof(handles));

	configfile = dotconf_create(argv[1], options, (void *)&context,
				    CASE_INSENSITIVE | DUPLICATE_OPTION_NAMES);
	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");
		return 1;
	}

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

	/* clean up the possible memjunk which needed to stay in memory */
	if (memory_junk.n_entries) {
		int idx;
		for (idx = 0; idx < memory_junk.n_entries; idx++) {
			free(memory_junk.entries[idx]);
		}
	}
	free(memory_junk.entries);

	return 0;
}

DOTCONF_CB(section_open)
{
	struct config_context *context = (struct config_context *)ctx;
	const char *old_end_token = context->current_end_token;
	int prev_context = context->current_context;
	const char *err = 0;

	context->current_context = (long)cmd->option->info;
	sprintf(context->current_end_token, "</%s", cmd->name + 1);

	while (!cmd->configfile->eof) {
		err = dotconf_command_loop_until_error(cmd->configfile);
		if (!err) {
			err =
			    pool_strcat(context->pool, "</", cmd->name + 1,
					" is missing", NULL);
			break;
		}

		if (err == context->current_end_token) {
			break;
		}
		dotconf_warning(cmd->configfile, DCLOG_ERR, 0, err);
	}

	sprintf(context->current_end_token, "%s", old_end_token);
	context->current_context = prev_context;

	if (err != context->current_end_token) {
		return err;
	}

	return NULL;
}

DOTCONF_CB(section_close)
{
	struct config_context *context = (struct config_context *)ctx;

	if (!context->current_end_token) {
		return pool_strcat(context->pool, cmd->name,
				   " without matching <", cmd->name + 2,
				   " section", NULL);
	}

	if (strcmp(context->current_end_token, cmd->name) != 0) {
		return pool_strcat(context->pool, "Expected '",
				   context->current_end_token, "' but saw ",
				   cmd->name, NULL);
	}

	return context->current_end_token;
}

DOTCONF_CB(common_option)
{
	fprintf(stderr,
		"common_option : Option %s called  Not doing anything with it...\n",
		cmd->name);
	return NULL;
}

/*
// We expect  option     name  filename
//      e.g.  AddModule first ./plugins/decision-test.so
//
// So in the list :
//  0 -> name
//  1 -> so_filename
*/
DOTCONF_CB(addmodule_option)
{
	struct config_context *context = (struct config_context *)ctx;
	configoption_t *module_options;
	const char *error = 0;
	int handle_idx = -1;
	char filename[FILENAME_MAX] = "";
	void *shared_object = 0;

	fprintf(stderr, "addmodule_option : Option %s called\n", cmd->name);
	if (cmd->arg_count < 2) {
		return pool_strcat(context->pool,
				   "Not enough parameters to option ",
				   cmd->name, " expected 2", NULL);
	}
	// load the damn thing
	for (handle_idx = 0; handle_idx < MAX_MODULES; handle_idx++) {
		if (handles[handle_idx] == 0) {
			snprintf(filename, 128, "./%s.so", cmd->data.list[1]);
			if (access(filename, R_OK) == 0) {	/* if file access is permitted */
				/* load library */
				handles[handle_idx] =
				    dlopen(filename, RTLD_LAZY);
				if (!handles[handle_idx]) {
					fprintf(stderr,
						"Error opening library: %s\n",
						dlerror());
					return "Error opening library";
				}
				shared_object = handles[handle_idx];
				break;
			} else {
				return pool_strcat(context->pool,
						   "Can't open file ", filename,
						   NULL);
			}
		}
	}
	if (handle_idx == MAX_MODULES) {
		return "Out of handle space. Not loading module\n";
	}
	// get the options
	module_options = dlsym(shared_object, "options");
	error = dlerror();
	if (error) {
		fprintf(stderr,
			"addmodule_option() : Error finding the options variable in %s p=%p (%s)\n",
			cmd->data.list[1], shared_object, error);
		dlclose(shared_object);
		handles[handle_idx] = 0;
		return NULL;
	}

	/*
	   // Scope the options of this module to a <NAME></NAME> block where NAME is
	   // the name that was specified in the configfile.
	   //
	   // The context field holds a unique identifier so we can verify in our
	   // contextchecker that this option belongs to the right scope.
	   //
	 */
	{
		char *begin_context_tag =
		    (char *)malloc(strlen(cmd->data.list[1]) + 2 + 1);
		char *end_context_tag =
		    (char *)malloc(strlen(cmd->data.list[1]) + 3 + 1);
		configoption_t *scope_options = 0;
		int opt_idx = -1;

		scope_options =
		    (configoption_t *) malloc(3 * sizeof(configoption_t));
		if (!scope_options || !begin_context_tag || !end_context_tag) {
			return "Error allocating memory";
		}
		sprintf(begin_context_tag, "<%s>", cmd->data.list[0]);
		sprintf(end_context_tag, "</%s>", cmd->data.list[0]);

		// create our two extra options (scope begin/end) and a NULL option to close
		// the list
		scope_options[0].name = begin_context_tag;
		scope_options[0].type = ARG_NONE;
		scope_options[0].callback = section_open;
		scope_options[0].info = shared_object;
		scope_options[0].context = CTX_ALL;

		scope_options[1].name = end_context_tag;
		scope_options[1].type = ARG_NONE;
		scope_options[1].callback = section_close;
		scope_options[1].info = NULL;
		scope_options[1].context = (long)shared_object;

		scope_options[2].name = "";
		scope_options[2].type = 0;
		scope_options[2].callback = NULL;
		scope_options[2].info = NULL;
		scope_options[2].context = 0;

		/* Set the context field of all options from the module to the identifier */
		for (opt_idx = 0; strlen(module_options[opt_idx].name);
		     opt_idx++) {
			module_options[opt_idx].context = (long)shared_object;
		}

		memory_junk.entries = realloc(memory_junk.entries,
					      (memory_junk.n_entries +
					       3) * sizeof(void *));
		memory_junk.entries[memory_junk.n_entries++] =
		    begin_context_tag;
		memory_junk.entries[memory_junk.n_entries++] = end_context_tag;
		memory_junk.entries[memory_junk.n_entries++] = scope_options;

		dotconf_register_options(cmd->configfile, scope_options);
		dotconf_register_options(cmd->configfile, module_options);
	}

	fprintf(stderr, "Successfully loaded module %s (%s)\n",
		cmd->data.list[1], cmd->data.list[1]);

	return NULL;
}