Diff

Differences From Artifact [e0f704afa3]:

To Artifact [97302025c7]:


11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
11
12
13
14
15
16
17

18
19
20
21
22
23
24
25







-
+







#include <string.h>
#endif

int lc_process_conf_section(const char *appname, const char *configfile) {
	FILE *configfp = NULL;
	char linebuf[LC_LINEBUF_LEN] = {0}, *linebuf_ptr = NULL;
	char qualifbuf[LC_LINEBUF_LEN] = {0};
	char *cmd = NULL, *value = NULL, *sep = NULL;
	char *cmd = NULL, *value = NULL, *sep = NULL, *cmdend = NULL;
	char *currsection = NULL;
	char *fgetsret = NULL;
	int lcpvret = -1;

	if (appname == NULL || configfile == NULL) {
		return(-1);
	}
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
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







+
+







+
+



















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

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





+
+






+












			*linebuf_ptr--;
		}

		/* Handle section header. */
		if (linebuf[0] == '[' && linebuf[strlen(linebuf) - 1] == ']') {
			linebuf[strlen(linebuf) - 1] = '\0';
			linebuf_ptr = &linebuf[1];

			/* If a section was open, close it. */
			if (currsection != NULL) {
				lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONEND);
				if (lcpvret < 0) {
					PRINTERR_D("Invalid section terminating: \"%s\"", currsection);
				}
				free(currsection);
			}

			/* Open new section. */
			currsection = strdup(linebuf_ptr);
			lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONSTART);
			if (lcpvret < 0) {
				PRINTERR_D("Invalid section: \"%s\"", currsection);
			}
			continue;
		}

		/* Remove leading spaces. */
		linebuf_ptr = &linebuf[0];
		while (*linebuf_ptr == ' ') {
			*linebuf_ptr++;
		}

		/* Drop comments and blank lines. */
		if (*linebuf_ptr == ';' || *linebuf_ptr == '\0') {
			continue;
		}

		/* Find the command and the data in the line. */
		sep = strpbrk(linebuf_ptr, " \t");
		cmdend = sep = strpbrk(linebuf_ptr, "=");
		if (sep == NULL) {
			PRINTERR("Invalid line: \"%s\"", linebuf);
			continue;
		}

		/* Delete space at the end of the command. */
		*cmdend--; /* It currently derefs to the seperator.. */
		while (*cmdend <= ' ') {
			*cmdend = '\0';
			*cmdend--;
		}

		cmd = linebuf_ptr;
		if (sep != NULL) {
			*sep = '\0';
			*sep++;
			while (*sep == ' ' || *sep == '\t') {
				*sep++;
			}
			value = sep;

		/* Delete the seperator char and any leading space. */
		*sep = '\0';
		*sep++;
		while (*sep == ' ' || *sep == '\t') {
			*sep++;
		}
		value = sep;
		} else {
			value = NULL;
		}


		/* Create the fully qualified variable name. */
		if (currsection == NULL) {
			strncpy(qualifbuf, cmd, sizeof(qualifbuf) - 1);
		} else {
			snprintf(qualifbuf, sizeof(qualifbuf) - 1, "%s.%s", currsection, cmd);
		}

		/* Call the parent and tell them we have data. */
		lcpvret = lc_process_var(qualifbuf, NULL, value, LC_FLAGS_VAR);
		if (lcpvret < 0) {
			PRINTERR_D("Invalid command: \"%s\"", qualifbuf);
		}
	}

	/* Close any open section, and clean-up. */
	if (currsection != NULL) {
		lcpvret = lc_process_var(currsection, NULL, NULL, LC_FLAGS_SECTIONEND);
		if (lcpvret < 0) {
			PRINTERR_D("Invalid section terminating: \"%s\"", currsection);
		}
		free(currsection);
	}

	fclose(configfp);

	return(0);
}