15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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, *cmdend = NULL;
char *currsection = NULL;
char *fgetsret = NULL;
int lcpvret = -1;
if (appname == NULL || configfile == NULL) {
return(-1);
}
configfp = fopen(configfile, "r");
if (configfp == NULL) {
return(-1);
}
while (1) {
fgetsret = fgets(linebuf, sizeof(linebuf) - 1, configfp);
if (fgetsret == NULL) {
break;
|
>
>
>
>
>
|
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
|
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, *cmdend = NULL;
char *currsection = NULL;
char *fgetsret = NULL;
int lcpvret = -1;
int invalid_section = 1, ignore_section = 0;
int retval = 0;
lc_err_t save_lc_errno = LC_ERR_NONE;
if (appname == NULL || configfile == NULL) {
lc_errno = LC_ERR_INVDATA;
return(-1);
}
configfp = fopen(configfile, "r");
if (configfp == NULL) {
lc_errno = LC_ERR_CANTOPEN;
return(-1);
}
while (1) {
fgetsret = fgets(linebuf, sizeof(linebuf) - 1, configfp);
if (fgetsret == NULL) {
break;
|
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
|
}
/* 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. */
cmdend = sep = strpbrk(linebuf_ptr, "=");
if (sep == NULL) {
PRINTERR("Invalid line: \"%s\"", linebuf);
continue;
}
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
}
/* 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);
invalid_section = 1;
lc_errno = LC_ERR_INVSECTION;
retval = -1;
} else {
invalid_section = 0;
ignore_section = 0;
}
if (lcpvret == LC_CBRET_IGNORESECTION) {
ignore_section = 1;
}
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;
}
/* Don't handle things for a section that doesn't exist. */
if (invalid_section == 1) {
PRINTERR_D("Ignoring line (because invalid section): %s", linebuf);
continue;
}
/* Don't process commands if this section is specifically ignored. */
if (ignore_section == 1) {
PRINTERR_D("Ignoring line (because ignored section): %s", linebuf);
continue;
}
/* Find the command and the data in the line. */
cmdend = sep = strpbrk(linebuf_ptr, "=");
if (sep == NULL) {
PRINTERR("Invalid line: \"%s\"", linebuf);
continue;
}
|
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
|
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);
}
|
>
>
>
|
>
>
>
>
>
>
>
|
|
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
|
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. */
save_lc_errno = lc_errno;
lc_errno = LC_ERR_NONE;
lcpvret = lc_process_var(qualifbuf, NULL, value, LC_FLAGS_VAR);
if (lcpvret < 0) {
if (lc_errno == LC_ERR_NONE) {
PRINTERR_D("Invalid command: \"%s\"", cmd);
lc_errno = LC_ERR_INVCMD;
} else {
PRINTERR_D("Error processing command (command was valid, but an error occured, errno was set)");
}
retval = -1;
} else {
lc_errno = save_lc_errno;
}
}
/* 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(retval);
}
|