2 // Copyright (c) 2010-2017 Intel Corporation
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
19 #include <rte_string_fns.h>
26 #include "parse_utils.h"
30 #define UINT32_MAX_STR "4294967295"
33 * Allocate cfg_file structure.
34 * Returns pointer to the allocated structure, NULL otherwise.
36 struct cfg_file *cfg_open(const char *cfg_name)
38 if (cfg_name == NULL) {
39 plog_err("\tNo config file name provided\n");
42 if (access(cfg_name, F_OK)) {
43 plog_err("\tError opening config file '%s': %s\n", cfg_name, strerror(errno));
47 FILE *pf = fopen(cfg_name, "rb");
49 plog_err("\tError opening config file '%s'\n", cfg_name);
53 struct cfg_file *pcfg = calloc(1, sizeof(struct cfg_file));
57 plog_err("\tCouldn't allocate memory for config file struct\n");
62 pcfg->name = strdup(cfg_name);
67 /* Free memory allocated for cfg_file structure.
68 * Returns 0 on success, -1 if the pointer to the pcfg is invalid */
69 int cfg_close(struct cfg_file *pcfg)
75 if (pcfg->name != NULL) {
78 if (pcfg->err_section != NULL) {
79 free(pcfg->err_section);
81 if (pcfg->pfile != NULL) {
89 static int cfg_get_pos(struct cfg_file *pcfg, fpos_t *pos)
91 pcfg->index_line = pcfg->line;
92 return fgetpos(pcfg->pfile, pos);
95 static int cfg_set_pos(struct cfg_file *pcfg, fpos_t *pos)
97 pcfg->line = pcfg->index_line;
98 return fsetpos(pcfg->pfile, pos);
102 * Read a line from the configuration file.
103 * Returns: on success length of the line read from the file is returned,
104 * 0 to indicate End of File,
105 * -1 in case of wrong function parameters
107 static int cfg_get_line(struct cfg_file *pcfg, char *buffer, unsigned len, int raw_lines)
111 if (pcfg == NULL || pcfg->pfile == NULL || buffer == NULL || len == 0) {
116 ptr = fgets(buffer, len, pcfg->pfile);
118 return 0; /* end of file */
126 /* remove comments */
127 ptr = strchr(buffer, ';');
132 ptr = strchr(buffer, '\0');
135 /* remove trailing spaces */
138 while (isspace(*ptr)) {
145 /* remove leading spaces */
146 while (*ptr && isspace(*ptr)) {
154 while (*ptr == '\0'); /* skip empty strings */
156 return strlen(buffer);
160 * Checks if buffer contains section name specified by the cfg_section pointer.
161 * Returns NULL if section name does not match, cfg_section pointer otherwise
163 static struct cfg_section *cfg_check_section(char *buffer, struct cfg_section *psec)
167 static const char *valid = "0123456789,hs- \t";
169 pend = strchr(buffer, ']');
171 return NULL; /* ']' not found: invalid section name */
176 /* check if section is indexed */
177 pend = strchr(psec->name, '#');
179 return (strcmp(buffer, psec->name) == 0) ? psec : NULL;
182 /* get section index */
183 len = pend - psec->name;
184 if (strncmp(buffer, psec->name, len) != 0) {
192 /* only numeric characters are valid for section index */
193 char val[MAX_CFG_STRING_LEN];
194 if (pend[0] == '$') {
195 if (parse_vars(val, sizeof(val), pend))
198 strncpy(val, pend, sizeof(val));
200 for (len = 0; val[len] != '\0'; ++len) {
201 if (strchr(valid, val[len]) == NULL) {
206 psec->nbindex = parse_list_set(psec->indexp, pend, MAX_INDEX);
207 PROX_PANIC(psec->nbindex == -1, "\t\tError in cfg_check_section('%s'): %s\n", buffer, get_parse_err());
209 for (int i = 0; i < psec->nbindex; ++i) {
210 psec->indexp[i] |= CFG_INDEXED;
216 static char *cfg_get_section_name(struct cfg_section *psec)
220 if (!(psec->indexp[0] & CFG_INDEXED)) {
221 return strdup(psec->name);
224 name = malloc(strlen(psec->name) + strlen(UINT32_MAX_STR));
226 strcpy(name, psec->name);
227 char *pidx = strchr(name, '#');
229 sprintf(pidx, "%u", psec->indexp[0] & ~CFG_INDEXED);
236 * Reads configuration file and parses section specified by psec pointer.
237 * Returns 0 on success, -1 otherwise
239 int cfg_parse(struct cfg_file *pcfg, struct cfg_section *psec)
245 struct cfg_section *section = NULL;
246 char buffer[sizeof(pcfg->cur_line)] = {0};
248 if (pcfg == NULL || psec == NULL) {
253 fseek(pcfg->pfile, 0, SEEK_SET);
255 /* read configuration file and parse section specified by psec pointer */
257 if (psec->raw_lines) {
258 /* skip until section starts */
259 char *lines = pcfg->cur_line;
260 size_t max_len = sizeof(pcfg->cur_line);
264 ret = fgets(lines, max_len, pcfg->pfile);
265 if (ret && *ret == '[') {
266 section = cfg_check_section(lines + 1, psec);
268 } while (!section && ret);
275 ret = fgets(buffer, sizeof(buffer), pcfg->pfile);
276 if (ret && *ret != '[') {
277 size_t l = strlen(buffer);
278 strncpy(lines, buffer, max_len);
282 } while ((ret && *ret != '['));
284 if (section != NULL) {
285 error = section->parser(section->indexp[index_count], pcfg->cur_line, section->data);
287 section->error = error;
288 /* log only the very first error */
289 if (!pcfg->err_section) {
290 pcfg->err_line = pcfg->line;
291 pcfg->err_entry = entry;
292 pcfg->err_section = cfg_get_section_name(section);
301 while (cfg_get_line(pcfg, buffer, MAX_CFG_STRING_LEN, psec->raw_lines) > 0) {
302 strncpy(pcfg->cur_line, buffer, sizeof(pcfg->cur_line));
303 if (*buffer == '[') {
304 if (index_count + 1 < psec->nbindex) {
305 // Need to loop - go back to recorded postion in file
306 cfg_set_pos(pcfg, &pos);
311 section = cfg_check_section(buffer + 1, psec);
314 cfg_get_pos(pcfg, &pos);
318 /* call parser procedure for each line in the section */
319 if (section != NULL) {
320 error = section->parser(section->indexp[index_count], buffer, section->data);
322 section->error = error;
323 /* log only the very first error */
324 if (!pcfg->err_section) {
325 pcfg->err_line = pcfg->line;
326 pcfg->err_entry = entry;
327 pcfg->err_section = cfg_get_section_name(section);
334 if (index_count + 1 < psec->nbindex) {
335 // Last core config contained multiple cores - loop back
336 cfg_set_pos(pcfg, &pos);