Diff

Differences From Artifact [e693b0baee]:

To Artifact [09c6afb854]:


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
#endif

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif

struct lc_varhandler_st *varhandlers = NULL;

int lc_process(int argc, char **argv, const char *appname, lc_conf_type_t type, const char *extra) {
	int retval = -1;

	switch (type) {
		case LC_CONF_SECTION:
			retval = lc_process_conf_section(appname, extra);
			break;
		case LC_CONF_APACHE:
			retval = lc_process_conf_apache(appname, extra);
			break;
		case LC_CONF_COLON:
			retval = lc_process_conf_colon(appname, extra);
			break;
		case LC_CONF_EQUAL:
			retval = lc_process_conf_equal(appname, extra);
			break;
		case LC_CONF_SPACE:
			retval = lc_process_conf_space(appname, extra);
			break;
		case LC_CONF_XML:
			retval = lc_process_conf_xml(appname, extra);
			break;
		default:
			break;
	}

	return(retval);
}

static int lc_process_var_string(void *data, const char *value) {
	char **dataval;

	dataval = data;
	*dataval = strdup(value);








|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







17
18
19
20
21
22
23
24




























25
26
27
28
29
30
31
#endif

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif

struct lc_varhandler_st *varhandlers = NULL;
lc_err_t lc_errno = LC_ERR_NONE;





























static int lc_process_var_string(void *data, const char *value) {
	char **dataval;

	dataval = data;
	*dataval = strdup(value);

106
107
108
109
110
111
112

113
114
115
116
117
118
119

120
121

122
123
124
125
126
127
128
129
	if (strcasecmp(value, "enable") == 0 ||
	    strcasecmp(value, "true") == 0 ||
	    strcasecmp(value, "yes") == 0 ||
	    strcasecmp(value, "on") == 0 ||
            strcasecmp(value, "y") == 0 ||
	    strcasecmp(value, "1") == 0) {
		*dataval = 1;

	} else if (strcasecmp(value, "disable") == 0 ||
	    strcasecmp(value, "false") == 0 ||
	    strcasecmp(value, "off") == 0 ||
	    strcasecmp(value, "no") == 0 ||
            strcasecmp(value, "n") == 0 ||
	    strcasecmp(value, "0") == 0) {
		*dataval = 0;

	}


	return(0);
}

static long long lc_process_size(const char *value) {
	long long retval = -1;
	char *mult = NULL;

	retval = strtoull(value, &mult, 10);







>







>


>
|







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
	if (strcasecmp(value, "enable") == 0 ||
	    strcasecmp(value, "true") == 0 ||
	    strcasecmp(value, "yes") == 0 ||
	    strcasecmp(value, "on") == 0 ||
            strcasecmp(value, "y") == 0 ||
	    strcasecmp(value, "1") == 0) {
		*dataval = 1;
		return(0);
	} else if (strcasecmp(value, "disable") == 0 ||
	    strcasecmp(value, "false") == 0 ||
	    strcasecmp(value, "off") == 0 ||
	    strcasecmp(value, "no") == 0 ||
            strcasecmp(value, "n") == 0 ||
	    strcasecmp(value, "0") == 0) {
		*dataval = 0;
		return(0);
	}

	lc_errno = LC_ERR_BADFORMAT;
	return(-1);
}

static long long lc_process_size(const char *value) {
	long long retval = -1;
	char *mult = NULL;

	retval = strtoull(value, &mult, 10);
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

	dataval = data;
	*dataval = lc_process_size(value);

	return(0);
}

static int lc_handle(struct lc_varhandler_st *handler, const char *value, lc_flags_t flags) {
	switch (handler->mode) {
		case LC_MODE_CALLBACK:
			if (handler->callback != NULL) {
				return(handler->callback(handler->var, value, flags));
			}
			break;
		case LC_MODE_VAR:
			switch (handler->type) {
				case LC_VAR_STRING:
					return(lc_process_var_string(handler->data, value));
					break;
				case LC_VAR_LONG_LONG:
					return(lc_process_var_longlong(handler->data, value));
					break;
				case LC_VAR_LONG:
					return(lc_process_var_long(handler->data, value));
					break;
				case LC_VAR_INT:
					return(lc_process_var_int(handler->data, value));
					break;
				case LC_VAR_SHORT:
					return(lc_process_var_short(handler->data, value));
					break;
				case LC_VAR_BOOL:
					return(lc_process_var_bool(handler->data, value));
					break;
				case LC_VAR_SIZE_LONG_LONG:
					return(lc_process_var_sizelonglong(handler->data, value));
					break;
				case LC_VAR_SIZE_LONG:
					return(lc_process_var_sizelong(handler->data, value));
					break;
				case LC_VAR_SIZE_INT:
					return(lc_process_var_sizeint(handler->data, value));
					break;
				case LC_VAR_SIZE_SHORT:
					return(lc_process_var_sizeshort(handler->data, value));
					break;
				case LC_VAR_TIME:
				case LC_VAR_DATE:
				case LC_VAR_FILENAME:
				case LC_VAR_DIRECTORY:
					PRINTERR_D("Not implemented yet!");
					return(-1);
				case LC_VAR_NONE:
				case LC_VAR_UNKNOWN:

				case LC_VAR_SECTIONSTART:
				case LC_VAR_SECTIONEND:
					return(0);
					break;
			}




























		break;























































































































































































	}

	return(-1);
}

int lc_process_var(const char *var, const char *varargs, const char *value, lc_flags_t flags) {
	struct lc_varhandler_st *handler = NULL;

	for (handler = varhandlers; handler != NULL; handler = handler->_next) {
		if (handler->var != var && (handler->var == NULL || var == NULL)) {
			continue;
		}
		if (handler->var != NULL) {
			if (strcasecmp(handler->var, var) != 0) {
				continue;
			}
		}

		return(lc_handle(handler, value, flags));
	}
	return(-1);
}

int lc_register_callback(const char *var, int (*callback)(const char *, const char *, lc_flags_t)) {
	struct lc_varhandler_st *newhandler = NULL;

	newhandler = malloc(sizeof(*newhandler));

	if (newhandler == NULL) {
		return(-1);
	}

	if (var == NULL) {
		newhandler->var = NULL;
	} else {
		newhandler->var = strdup(var);
	}
	newhandler->callback = callback;
	newhandler->type = LC_VAR_UNKNOWN;

	newhandler->mode = LC_MODE_CALLBACK;
	newhandler->opt = '\0';
	newhandler->_next = varhandlers;

	varhandlers = newhandler;

	return(0);
}








|
|
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<













|
|
>
|
|







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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428


429
















430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454

	dataval = data;
	*dataval = lc_process_size(value);

	return(0);
}

static int lc_handle_type(lc_var_type_t type, const char *value, void *data) {
	switch (type) {







		case LC_VAR_STRING:
			return(lc_process_var_string(data, value));
			break;
		case LC_VAR_LONG_LONG:
			return(lc_process_var_longlong(data, value));
			break;
		case LC_VAR_LONG:
			return(lc_process_var_long(data, value));
			break;
		case LC_VAR_INT:
			return(lc_process_var_int(data, value));
			break;
		case LC_VAR_SHORT:
			return(lc_process_var_short(data, value));
			break;
		case LC_VAR_BOOL:
			return(lc_process_var_bool(data, value));
			break;
		case LC_VAR_SIZE_LONG_LONG:
			return(lc_process_var_sizelonglong(data, value));
			break;
		case LC_VAR_SIZE_LONG:
			return(lc_process_var_sizelong(data, value));
			break;
		case LC_VAR_SIZE_INT:
			return(lc_process_var_sizeint(data, value));
			break;
		case LC_VAR_SIZE_SHORT:
			return(lc_process_var_sizeshort(data, value));
			break;
		case LC_VAR_TIME:
		case LC_VAR_DATE:
		case LC_VAR_FILENAME:
		case LC_VAR_DIRECTORY:
			PRINTERR_D("Not implemented yet!");
			return(-1);
		case LC_VAR_NONE:
		case LC_VAR_UNKNOWN:
		case LC_VAR_SECTION:
		case LC_VAR_SECTIONSTART:
		case LC_VAR_SECTIONEND:
			return(0);
			break;
	}

	return(-1);
}

static int lc_handle(struct lc_varhandler_st *handler, const char *var, const char *varargs, const char *value, lc_flags_t flags) {
	const char *localvar = NULL;
	int retval;

	if (var != NULL) {
		localvar = strrchr(var, '.');
		if (localvar == NULL) {
			localvar = var;
		} else {
			*localvar++;
		}
	} else {
		localvar = NULL;
	}

	switch (handler->mode) {
		case LC_MODE_CALLBACK:
			if (handler->callback != NULL) {
				retval = handler->callback(localvar, var, varargs, value, flags, handler->extra);
				if (retval < 0) {
					lc_errno = LC_ERR_CALLBACK;
				}
				return(retval);
			}
			break;
		case LC_MODE_VAR:
			return(lc_handle_type(handler->type, value, handler->data));
			break;
	}

	return(-1);
}

static int lc_process_environment(const char *appname) {
	/* XXX: write this */
	return(0);
}

static int lc_process_cmdline(int argc, char **argv) {
	struct lc_varhandler_st *handler = NULL;
	char *cmdarg = NULL, *cmdoptarg = NULL;
	char *lastcomponent_handler = NULL;
	int cmdargidx = 0;
	int retval = 0, chkretval = 0;
	int ch = 0;

	for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
		cmdarg = argv[cmdargidx];

		/* Make sure we have an argument here. */
		if (cmdarg == NULL) {
			break;
		}

		/* If the argument isn't an option, abort. */
		if (cmdarg[0] != '-') {
			continue;
		}
		*cmdarg++;

		/* Handle long options. */
		if (cmdarg[0] == '-') {
			*cmdarg++;

			/* Don't process arguments after the '--' option. */
			if (cmdarg[0] == '\0') {
				break;
			}

			/* Look for a variable name that matches */
			for (handler = varhandlers; handler != NULL; handler = handler->_next) {
				/* Skip handlers with no variable name. */
				if (handler->var == NULL) {
					continue;
				}
				/* Skip handlers which don't agree with being
				   processed on the command line. */
				if (handler->type == LC_VAR_SECTION ||
				    handler->type == LC_VAR_SECTIONSTART ||
				    handler->type == LC_VAR_SECTIONEND ||
				    handler->type == LC_VAR_UNKNOWN) {
					continue;
				}

				/* Find the last part of the variable and compare it with 
				   the option being processed. */
				lastcomponent_handler = strrchr(handler->var, '.');
				if (lastcomponent_handler == NULL) {
					lastcomponent_handler = handler->var;
				} else {
					*lastcomponent_handler++;
				}

				/* Ignore this handler if they don't match. */
				if (strcasecmp(lastcomponent_handler, cmdarg) != 0) {
					continue;
				}

				if (handler->type == LC_VAR_NONE) {
					cmdoptarg = NULL;
				} else {
					cmdargidx++;
					if (cmdargidx >= argc) {
						PRINTERR("Argument required.");
						lc_errno = LC_ERR_BADFORMAT;
						return(-1);
					}
					cmdoptarg = argv[cmdargidx];
				}

				chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
				if (chkretval < 0) {
					retval = -1;
				}

				break;
			}

			if (handler == NULL) {
				PRINTERR("Unknown option: --%s", cmdarg);
				lc_errno = LC_ERR_INVCMD;
				return(-1);
			}
		} else {
			for (; *cmdarg != '\0'; *cmdarg++) {
				ch = *cmdarg;

				for (handler = varhandlers; handler != NULL; handler = handler->_next) {
					if (handler->opt != ch || handler->opt == '\0') {
						continue;
					}
					/* Skip handlers which don't agree with being
					   processed on the command line. */
					if (handler->type == LC_VAR_SECTION ||
					    handler->type == LC_VAR_SECTIONSTART ||
					    handler->type == LC_VAR_SECTIONEND ||
					    handler->type == LC_VAR_UNKNOWN) {
						continue;
					}

					if (handler->type == LC_VAR_NONE) {
						cmdoptarg = NULL;
					} else {
						cmdargidx++;
						if (cmdargidx >= argc) {
							PRINTERR("Argument required.");
							lc_errno = LC_ERR_BADFORMAT;
							return(-1);
						}
						cmdoptarg = argv[cmdargidx];
					}

					chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
					if (chkretval < 0) {
						retval = -1;
					}

					break;
				}

				if (handler == NULL) {
					PRINTERR("Unknown option: -%c", ch);
					lc_errno = LC_ERR_INVCMD;
					return(-1);
				}
			}
		}
	}

	return(retval);
}


int lc_process_var(const char *var, const char *varargs, const char *value, lc_flags_t flags) {
	struct lc_varhandler_st *handler = NULL;
	const char *lastcomponent_handler = NULL, *lastcomponent_var = NULL;

	lastcomponent_var = strrchr(var, '.');
	if (lastcomponent_var == NULL) {
		lastcomponent_var = var;
	} else {
		*lastcomponent_var++;
	}

	for (handler = varhandlers; handler != NULL; handler = handler->_next) {
		/* If either handler->var or var is NULL, skip, unless both are NULL. */
		if (handler->var != var && (handler->var == NULL || var == NULL)) {
			continue;
		}

		/* If both are not-NULL, compare them. */
		if (handler->var != NULL) {
			/* Wild-card-ish match. */
			if (handler->var[0] == '*' && handler->var[1] == '.') {
				/* Only compare the last components */

				lastcomponent_handler = strrchr(handler->var, '.') + 1; /* strrchr() won't return NULL, because we already checked it. */

				if (strcasecmp(lastcomponent_handler, lastcomponent_var) != 0) {
					continue;
				}
			} else if (strcasecmp(handler->var, var) != 0) {
				/* Exact (case-insensitive comparison) failed. */
				continue;
			}
		}

		return(lc_handle(handler, var, varargs, value, flags));
	}

	return(-1);
}



int lc_register_callback(const char *var, char opt, lc_var_type_t type, int (*callback)(const char *, const char *, const char *, const char *, lc_flags_t, void *), void *extra) {
















	struct lc_varhandler_st *newhandler = NULL;

	newhandler = malloc(sizeof(*newhandler));

	if (newhandler == NULL) {
		return(-1);
	}

	if (var == NULL) {
		newhandler->var = NULL;
	} else {
		newhandler->var = strdup(var);
	}
	newhandler->mode = LC_MODE_CALLBACK;
	newhandler->type = type;
	newhandler->callback = callback;
	newhandler->opt = opt;
	newhandler->extra = extra;
	newhandler->_next = varhandlers;

	varhandlers = newhandler;

	return(0);
}

299
300
301
302
303
304
305
306
307

308
309

310
311
312
313
314
315






























































































	}

	if (var == NULL) {
		newhandler->var = NULL;
	} else {
		newhandler->var = strdup(var);
	}
	newhandler->type = type;
	newhandler->mode = LC_MODE_VAR;

	newhandler->data = data;
	newhandler->opt = opt;

	newhandler->_next = varhandlers;

	varhandlers = newhandler;

	return(0);
}





































































































<

>


>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
462
463
464
465
466
467
468

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
	}

	if (var == NULL) {
		newhandler->var = NULL;
	} else {
		newhandler->var = strdup(var);
	}

	newhandler->mode = LC_MODE_VAR;
	newhandler->type = type;
	newhandler->data = data;
	newhandler->opt = opt;
	newhandler->extra = NULL;
	newhandler->_next = varhandlers;

	varhandlers = newhandler;

	return(0);
}

int lc_process(int argc, char **argv, const char *appname, lc_conf_type_t type, const char *extra) {
	int retval = 0, chkretval = 0;

	/* XXX Handle config files.  need to handle this in a loop of config files... */
	switch (type) {
		case LC_CONF_SECTION:
			chkretval = lc_process_conf_section(appname, extra);
			break;
		case LC_CONF_APACHE:
			chkretval = lc_process_conf_apache(appname, extra);
			break;
		case LC_CONF_COLON:
			chkretval = lc_process_conf_colon(appname, extra);
			break;
		case LC_CONF_EQUAL:
			chkretval = lc_process_conf_equal(appname, extra);
			break;
		case LC_CONF_SPACE:
			chkretval = lc_process_conf_space(appname, extra);
			break;
		case LC_CONF_XML:
			chkretval = lc_process_conf_xml(appname, extra);
			break;
		default:
			chkretval = -1;
			lc_errno = LC_ERR_INVDATA;
			break;
	}

	if (chkretval < 0) {
		retval = -1;
	}

	/* Handle environment variables.*/
	chkretval = lc_process_environment(appname);
	if (chkretval < 0) {
		retval = -1;
	}

	/* Handle command line arguments */
	chkretval = lc_process_cmdline(argc, argv);
	if (chkretval < 0) {
		retval = -1;
	}

	return(retval);
}


lc_err_t lc_geterrno(void) {
	lc_err_t retval;

	retval = lc_errno;

	lc_errno = LC_ERR_NONE;

	return(retval);
}

char *lc_geterrstr(void) {
	char *retval = NULL;

	switch (lc_errno) {
		case LC_ERR_NONE:
			retval = "Success";
			break;
		case LC_ERR_INVCMD:
			retval = "Invalid command";
			break;
		case LC_ERR_INVSECTION:
			retval = "Invalid section";
			break;
		case LC_ERR_INVDATA:
			retval = "Invalid application data (internal error)";
			break;
		case LC_ERR_BADFORMAT:
			retval = "Bad data specified or incorrect format.";
			break;
		case LC_ERR_CANTOPEN:
			retval = "Can't open file.";
			break;
		case LC_ERR_CALLBACK:
			retval = "Error return from application handler.";
			break;
	}

	lc_errno = LC_ERR_NONE;

	if (retval != NULL) {
		return(retval);
	}
	return("Unknown error");
}