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];
195 parse_single_var(val, sizeof(val), pend);
197 strncpy(val, pend, sizeof(val));
199 for (len = 0; val[len] != '\0'; ++len) {
200 if (strchr(valid, val[len]) == NULL) {
205 psec->nbindex = parse_list_set(psec->indexp, pend, MAX_INDEX);
206 PROX_PANIC(psec->nbindex == -1, "\t\tError in cfg_check_section('%s'): %s\n", buffer, get_parse_err());
208 for (int i = 0; i < psec->nbindex; ++i) {
209 psec->indexp[i] |= CFG_INDEXED;
215 static char *cfg_get_section_name(struct cfg_section *psec)
219 if (!(psec->indexp[0] & CFG_INDEXED)) {
220 return strdup(psec->name);
223 name = malloc(strlen(psec->name) + strlen(UINT32_MAX_STR));
225 strcpy(name, psec->name);
226 char *pidx = strchr(name, '#');
228 sprintf(pidx, "%u", psec->indexp[0] & ~CFG_INDEXED);
235 * Reads configuration file and parses section specified by psec pointer.
236 * Returns 0 on success, -1 otherwise
238 int cfg_parse(struct cfg_file *pcfg, struct cfg_section *psec)
244 struct cfg_section *section = NULL;
245 char buffer[sizeof(pcfg->cur_line)] = {0};
247 if (pcfg == NULL || psec == NULL) {
252 fseek(pcfg->pfile, 0, SEEK_SET);
254 /* read configuration file and parse section specified by psec pointer */
256 if (psec->raw_lines) {
257 /* skip until section starts */
258 char *lines = pcfg->cur_line;
259 size_t max_len = sizeof(pcfg->cur_line);
263 ret = fgets(lines, max_len, pcfg->pfile);
264 if (ret && *ret == '[') {
265 section = cfg_check_section(lines + 1, psec);
267 } while (!section && ret);
274 ret = fgets(buffer, sizeof(buffer), pcfg->pfile);
275 if (ret && *ret != '[') {
276 size_t l = strlen(buffer);
277 strncpy(lines, buffer, max_len);
281 } while ((ret && *ret != '['));
283 if (section != NULL) {
284 error = section->parser(section->indexp[index_count], pcfg->cur_line, section->data);
286 section->error = error;
287 /* log only the very first error */
288 if (!pcfg->err_section) {
289 pcfg->err_line = pcfg->line;
290 pcfg->err_entry = entry;
291 pcfg->err_section = cfg_get_section_name(section);
300 while (cfg_get_line(pcfg, buffer, MAX_CFG_STRING_LEN, psec->raw_lines) > 0) {
301 strncpy(pcfg->cur_line, buffer, sizeof(pcfg->cur_line));
302 if (*buffer == '[') {
303 if (index_count + 1 < psec->nbindex) {
304 // Need to loop - go back to recorded postion in file
305 cfg_set_pos(pcfg, &pos);
310 section = cfg_check_section(buffer + 1, psec);
313 cfg_get_pos(pcfg, &pos);
317 /* call parser procedure for each line in the section */
318 if (section != NULL) {
319 error = section->parser(section->indexp[index_count], buffer, section->data);
321 section->error = error;
322 /* log only the very first error */
323 if (!pcfg->err_section) {
324 pcfg->err_line = pcfg->line;
325 pcfg->err_entry = entry;
326 pcfg->err_section = cfg_get_section_name(section);
333 if (index_count + 1 < psec->nbindex) {
334 // Last core config contained multiple cores - loop back
335 cfg_set_pos(pcfg, &pos);