Diff

Differences From Artifact [9adb43042b]:

To Artifact [341b40c218]:


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







+

+



-
+





+
+



-
+
+
+
+
+



-
+



-
+



-
-
-
-
-
+








#ifdef HAVE_PWD_H
#include <pwd.h>
#endif

struct lc_varhandler_st *varhandlers = NULL;
lc_err_t lc_errno = LC_ERR_NONE;
const char *lc_errfile = NULL;
int lc_optind = 0;
int lc_errline = 0;

extern char **environ;

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

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

	*endptr = NULL;

	return(0);
}

static int lc_process_var_cidr(void *data, const char *value) {
static int lc_process_var_cidr(void *data, const char *value, const char **endptr) {
	return(-1);
}

static int lc_process_var_hostname6(void *data, const char *value, const char **endptr) {
	return(-1);
}

static int lc_process_var_hostname6(void *data, const char *value) {
static int lc_process_var_hostname4(void *data, const char *value, const char **endptr) {
	return(-1);
}

static int lc_process_var_hostname4(void *data, const char *value) {
static int lc_process_var_ip6(void *data, const char *value, const char **endptr) {
	return(-1);
}

static int lc_process_var_ip6(void *data, const char *value) {
	return(-1);
}

static int lc_process_var_ip4(void *data, const char *value) {
static int lc_process_var_ip4(void *data, const char *value, const char **endptr) {
	uint32_t *dataval, retval = 0;
	const char *dotptr = NULL;
	int tmpval = -1;
//	int dotcount

	dataval = data;

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







+
+



-
+



-
+




-
+



-
+




-
+



-
+




-
+



-
+




-
+






+
+



-
+
+
+
+

+





-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+






-
-
+
+







			break;
		}
		dotptr++;
	}

	*dataval = retval;

	*endptr = (char *) dotptr;

	return(0);
}

static int lc_process_var_longlong(void *data, const char *value) {
static int lc_process_var_longlong(void *data, const char *value, const char **endptr) {
	long long *dataval;

	dataval = data;
	*dataval = strtoll(value, NULL, 10);
	*dataval = strtoll(value, (char **) endptr, 10);

	return(0);
}

static int lc_process_var_long(void *data, const char *value) {
static int lc_process_var_long(void *data, const char *value, const char **endptr) {
	long *dataval;

	dataval = data;
	*dataval = strtoll(value, NULL, 10);
	*dataval = strtoll(value, (char **) endptr, 10);

	return(0);
}

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

	dataval = data;
	*dataval = strtoll(value, NULL, 10);
	*dataval = strtoll(value, (char **) endptr, 10);

	return(0);
}

static int lc_process_var_short(void *data, const char *value) {
static int lc_process_var_short(void *data, const char *value, const char **endptr) {
	short *dataval;

	dataval = data;
	*dataval = strtoll(value, NULL, 10);
	*dataval = strtoll(value, (char **) endptr, 10);

	return(0);
}

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

	dataval = data;

	*dataval = 1;

	*endptr = NULL;

	return(0);
}

static int lc_process_var_bool(void *data, const char *value) {
static int lc_process_var_bool(void *data, const char *value, const char **endptr) {
	char *trueval[] = {"enable", "true", "yes", "on", "y", "1"};
	char *falseval[] = {"disable", "false", "no", "off", "n", "0"};
	size_t chkvallen, vallen;
	int *dataval;
	int i;

	dataval = data;

	*dataval = -1;

	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);
	vallen = strlen(value);

	for (i = 0; i < (sizeof(trueval) / sizeof(*trueval)); i++) {
		chkvallen = strlen(trueval[i]);

		/*
		 * Skip if there's no way we could find a match here.
		 */
		if (chkvallen > vallen) {
			continue;
		}

		/*
		 * Skip if there is no partial match.
		 */
		if (strncasecmp(value, trueval[i], chkvallen) != 0) {
			continue;
		}

		if (value[chkvallen] == '\0' || value[chkvallen] == ',' || \
		    value[chkvallen] == ' ') {
			/* Declare a winner and set the next token. */
			*endptr = value + chkvallen;
			*dataval = 1;
			return(0);
		}
	}

	for (i = 0; i < (sizeof(falseval) / sizeof(*falseval)); i++) {
		chkvallen = strlen(falseval[i]);

		/*
		 * Skip if there's no way we could find a match here.
		 */
		if (chkvallen > vallen) {
			continue;
		}

		/*
		 * Skip if there is no partial match.
		 */
		if (strncasecmp(value, falseval[i], chkvallen) != 0) {
			continue;
		}

		if (value[chkvallen] == '\0' || value[chkvallen] == ',' || \
		    value[chkvallen] == ' ') {
			/* Declare a winner and set the next token. */
			*endptr = value + chkvallen;
			*dataval = 0;
			return(0);
		}
	}

	lc_errno = LC_ERR_BADFORMAT;
	return(-1);
}

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

	retval = strtoll(value, &mult, 10);
	if (mult != NULL) {
		switch (tolower(mult[0])) {
			case 'p':
				retval *= 1125899906842624LLU;
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
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







-
+



-
+




-
+



-
+




-
+



-
+




-
+



-
+




-
+



-
+






+
+
+
+
+
+
+
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+



-
+


-
+


-
+


-
+


-
+







				break;
		}
	}

	return(retval);
}

static int lc_process_var_sizelonglong(void *data, const char *value) {
static int lc_process_var_sizelonglong(void *data, const char *value, const char **endptr) {
	long long *dataval;

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

	return(0);
}

static int lc_process_var_sizelong(void *data, const char *value) {
static int lc_process_var_sizelong(void *data, const char *value, const char **endptr) {
	long *dataval;

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

	return(0);
}

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

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

	return(0);
}

static int lc_process_var_sizeshort(void *data, const char *value) {
static int lc_process_var_sizeshort(void *data, const char *value, const char **endptr) {
	short *dataval;

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

	return(0);
}

static int lc_process_var_sizesizet(void *data, const char *value) {
static int lc_process_var_sizesizet(void *data, const char *value, const char **endptr) {
	size_t *dataval;

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

	return(0);
}


static int lc_handle_type(lc_var_type_t type, const char *value, void *data) {
	const char *next;
	int is_list;

	is_list = type & LC_VAR_LIST;

	if (is_list == LC_VAR_LIST) {
	}

	switch (type) {
		case LC_VAR_STRING:
			return(lc_process_var_string(data, value));
			return(lc_process_var_string(data, value, &next));
			break;
		case LC_VAR_LONG_LONG:
			return(lc_process_var_longlong(data, value));
			return(lc_process_var_longlong(data, value, &next));
			break;
		case LC_VAR_LONG:
			return(lc_process_var_long(data, value));
			return(lc_process_var_long(data, value, &next));
			break;
		case LC_VAR_INT:
			return(lc_process_var_int(data, value));
			return(lc_process_var_int(data, value, &next));
			break;
		case LC_VAR_SHORT:
			return(lc_process_var_short(data, value));
			return(lc_process_var_short(data, value, &next));
			break;
		case LC_VAR_BOOL:
			return(lc_process_var_bool(data, value));
			return(lc_process_var_bool(data, value, &next));
			break;
		case LC_VAR_SIZE_LONG_LONG:
			return(lc_process_var_sizelonglong(data, value));
			return(lc_process_var_sizelonglong(data, value, &next));
			break;
		case LC_VAR_SIZE_LONG:
			return(lc_process_var_sizelong(data, value));
			return(lc_process_var_sizelong(data, value, &next));
			break;
		case LC_VAR_SIZE_INT:
			return(lc_process_var_sizeint(data, value));
			return(lc_process_var_sizeint(data, value, &next));
			break;
		case LC_VAR_SIZE_SHORT:
			return(lc_process_var_sizeshort(data, value));
			return(lc_process_var_sizeshort(data, value, &next));
			break;
		case LC_VAR_BOOL_BY_EXISTANCE:
			return(lc_process_var_bool_byexistance(data, value));
			return(lc_process_var_bool_byexistance(data, value, &next));
			break;
		case LC_VAR_SIZE_SIZE_T:
			return(lc_process_var_sizesizet(data, value));
			return(lc_process_var_sizesizet(data, value, &next));
			break;
		case LC_VAR_IP:
		case LC_VAR_IP4:
			return(lc_process_var_ip4(data, value));
			return(lc_process_var_ip4(data, value, &next));
			break;
		case LC_VAR_IP6:
			return(lc_process_var_ip6(data, value));
			return(lc_process_var_ip6(data, value, &next));
			break;
		case LC_VAR_HOSTNAME4:
			return(lc_process_var_hostname4(data, value));
			return(lc_process_var_hostname4(data, value, &next));
			break;
		case LC_VAR_HOSTNAME6:
			return(lc_process_var_hostname6(data, value));
			return(lc_process_var_hostname6(data, value, &next));
			break;
		case LC_VAR_CIDR:
			return(lc_process_var_cidr(data, value));
			return(lc_process_var_cidr(data, value, &next));
			break;
		case LC_VAR_TIME:
		case LC_VAR_DATE:
		case LC_VAR_FILENAME:
		case LC_VAR_DIRECTORY:
#ifdef DEBUG
			fprintf(stderr, "Not implemented yet!\n");
365
366
367
368
369
370
371


372
373
374
375
376
377



378
379
380
381


382
383
384
385
386
387
388
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







+
+






+
+
+




+
+







	size_t appnamelen = 0;
	char varnamebuf[128] = {0};
	char **currvar;
	char *sep = NULL, *value = NULL, *cmd = NULL;
	char *ucase_appname = NULL, *ucase_appname_itr = NULL;
	char *lastcomponent_handler = NULL;
	int varnamelen = 0;
	char *local_lc_errfile;
	int local_lc_errline;

	/* Make sure we have an environment to screw with, if not,
	   no arguments were found to be in error */
	if (environ == NULL || appname == NULL) {
		return(0);
	}

	local_lc_errfile = "<environment>";
	local_lc_errline = 0;

	/* Allocate and create our uppercase appname. */
	ucase_appname = strdup(appname);
	if (ucase_appname == NULL) {
		lc_errfile = local_lc_errfile;
		lc_errline = local_lc_errline;
		lc_errno = LC_ERR_ENOMEM;
		return(-1);
	}
	for (ucase_appname_itr = ucase_appname; *ucase_appname_itr != '\0'; ucase_appname_itr++) {
		*ucase_appname_itr = toupper(*ucase_appname_itr);
	}

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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

594
595
596
597
598
599
600
601







+
+
+
+
+




+
+









+
+
















-
+







	char *lastcomponent_handler = NULL;
	char **newargv = NULL;
	char *usedargv = NULL;
	int cmdargidx = 0;
	int newargvidx = 0;
	int retval = 0, chkretval = 0;
	int ch = 0;
	char *local_lc_errfile;
	int local_lc_errline;

	local_lc_errfile = "<cmdline>";
	local_lc_errline = 0;

	/* Allocate "argc + 1" (+1 for the NULL terminator) elements. */
	newargv = malloc((argc + 1) * sizeof(*newargv));
	if (newargv == NULL) {
		lc_errfile = local_lc_errfile;
		lc_errline = local_lc_errline;
		lc_errno = LC_ERR_ENOMEM;
		return(-1);
	}
	newargv[newargvidx++] = argv[0];
	newargv[argc] = NULL;

	/* Allocate space to indicate which arguments have been used. */
	usedargv = malloc(argc * sizeof(*usedargv));
	if (usedargv == NULL) {
		lc_errfile = local_lc_errfile;
		lc_errline = local_lc_errline;
		lc_errno = LC_ERR_ENOMEM;
		free(newargv);
		return(-1);
	}
	for (cmdargidx = 0; cmdargidx < argc; cmdargidx++) {
		usedargv[cmdargidx] = 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 the argument isn't an option, skip. */
		if (cmdarg[0] != '-') {
			continue;
		}

		/* Setup a pointer in the new array for the actual argument. */
		newargv[newargvidx++] = cmdarg;
		usedargv[cmdargidx] = 1;
581
582
583
584
585
586
587


588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607


608
609
610
611
612
613
614
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690







+
+




















+
+








				if (handler->type == LC_VAR_NONE || handler->type == LC_VAR_BOOL_BY_EXISTANCE) {
					cmdoptarg = NULL;
				} else {
					cmdargidx++;
					if (cmdargidx >= argc) {
						fprintf(stderr, "Argument required.\n");
						lc_errfile = local_lc_errfile;
						lc_errline = local_lc_errline;
						lc_errno = LC_ERR_BADFORMAT;
						free(usedargv);
						free(newargv);
						return(-1);
					}
					cmdoptarg = argv[cmdargidx];
					newargv[newargvidx++] = cmdoptarg;
					usedargv[cmdargidx] = 1;
				}

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

				break;
			}

			if (handler == NULL) {
				fprintf(stderr, "Unknown option: --%s\n", cmdarg);
				lc_errfile = local_lc_errfile;
				lc_errline = local_lc_errline;
				lc_errno = LC_ERR_INVCMD;
				free(usedargv);
				free(newargv);
				return(-1);
			}
		} else {
			for (; *cmdarg != '\0'; cmdarg++) {
629
630
631
632
633
634
635


636
637
638
639
640
641
642
643
644
645
646
647


648
649
650
651
652
653
654
655


656
657
658
659
660
661
662
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744







+
+












+
+








+
+








					if (handler->type == LC_VAR_NONE || handler->type == LC_VAR_BOOL_BY_EXISTANCE) {
						cmdoptarg = NULL;
					} else {
						cmdargidx++;
						if (cmdargidx >= argc) {
							fprintf(stderr, "Argument required.\n");
							lc_errfile = local_lc_errfile;
							lc_errline = local_lc_errline;
							lc_errno = LC_ERR_BADFORMAT;
							free(usedargv);
							free(newargv);
							return(-1);
						}
						cmdoptarg = argv[cmdargidx];
						newargv[newargvidx++] = cmdoptarg;
						usedargv[cmdargidx] = 1;
					}

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

					break;
				}

				if (handler == NULL) {
					fprintf(stderr, "Unknown option: -%c\n", ch);
					lc_errfile = local_lc_errfile;
					lc_errline = local_lc_errline;
					lc_errno = LC_ERR_INVCMD;
					free(usedargv);
					free(newargv);
					return(-1);
				}
			}
		}
937
938
939
940
941
942
943

944

945
946
947
948

949
950
951

952
953
954

955
956
957

958
959
960

961
962
963

964
965
966

967
968
969

970
971
972







973
974
975
976
977
978










979
1019
1020
1021
1022
1023
1024
1025
1026

1027
1028
1029
1030

1031
1032
1033

1034
1035
1036

1037
1038
1039

1040
1041
1042

1043
1044
1045

1046
1047
1048

1049
1050
1051

1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062






1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073







+
-
+



-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+



+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+


	lc_errno = LC_ERR_NONE;

	return(retval);
}

char *lc_geterrstr(void) {
	static char retval[512];
	char *retval = NULL;
	char *errmsg = NULL;

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

	/*
	 * This is not part of the switch statement so we will get warnings
	 * about unhandled enum values.
	 */
	if (errmsg == NULL) {
		errmsg = "Unknown error";
	}
	lc_errno = LC_ERR_NONE;

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

	if (lc_errfile == NULL) {
		snprintf(retval, sizeof(retval), "%s:%i: %s", "<no file>", lc_errline, errmsg);
	} else {
		snprintf(retval, sizeof(retval), "%s:%i: %s", lc_errfile, lc_errline, errmsg);
	}

	retval[sizeof(retval) - 1] = '\0';

	return(retval);
}