-/*-
- * BSD LICENSE
- *
- * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- * * Neither the name of Intel Corporation nor the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
+#include <rte_string_fns.h>
#include <rte_common.h>
+#include <rte_log.h>
#include "rte_cfgfile.h"
struct rte_cfgfile_section *sections;
};
+RTE_LOG_REGISTER(cfgfile_logtype, lib.cfgfile, INFO);
+
+#define CFG_LOG(level, fmt, args...) \
+ rte_log(RTE_LOG_ ## level, cfgfile_logtype, "%s(): " fmt "\n", \
+ __func__, ## args)
+
/** when we resize a file structure, how many extra entries
* for new sections do we add in */
#define CFG_ALLOC_SECTION_BATCH 8
struct rte_cfgfile_entry *curr_entry =
§ion->entries[section->num_entries];
- snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
- snprintf(curr_entry->value,
- sizeof(curr_entry->value), "%s", entryvalue);
+ strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
+ strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
section->num_entries++;
return 0;
unsigned int i;
if (!params) {
- printf("Error - missing cfgfile parameters\n");
+ CFG_LOG(ERR, "missing cfgfile parameters\n");
return -EINVAL;
}
}
if (valid_comment == 0) {
- printf("Error - invalid comment characters %c\n",
+ CFG_LOG(ERR, "invalid comment characters %c\n",
params->comment_character);
return -ENOTSUP;
}
rte_cfgfile_load_with_params(const char *filename, int flags,
const struct rte_cfgfile_parameters *params)
{
- int allocated_sections = CFG_ALLOC_SECTION_BATCH;
- int allocated_entries = 0;
- int curr_section = -1;
- int curr_entry = -1;
- char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4] = {0};
+ char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4];
int lineno = 0;
- struct rte_cfgfile *cfg = NULL;
+ struct rte_cfgfile *cfg;
if (rte_cfgfile_check_params(params))
return NULL;
if (f == NULL)
return NULL;
- cfg = malloc(sizeof(*cfg) + sizeof(cfg->sections[0]) *
- allocated_sections);
- if (cfg == NULL)
- goto error2;
-
- memset(cfg->sections, 0, sizeof(cfg->sections[0]) * allocated_sections);
-
- if (flags & CFG_FLAG_GLOBAL_SECTION) {
- curr_section = 0;
- allocated_entries = CFG_ALLOC_ENTRY_BATCH;
- cfg->sections = malloc(
- sizeof(cfg->sections[0]) +
- sizeof(cfg->sections[0].entries) *
- allocated_entries);
- if (cfg->sections == NULL) {
- printf("Error - no memory for global section\n");
- goto error1;
- }
-
- snprintf(cfg->sections[curr_section].name,
- sizeof(cfg->sections[0].name), "GLOBAL");
- }
+ cfg = rte_cfgfile_create(flags);
while (fgets(buffer, sizeof(buffer), f) != NULL) {
- char *pos = NULL;
+ char *pos;
size_t len = strnlen(buffer, sizeof(buffer));
lineno++;
if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
- printf("Error line %d - no \\n found on string. "
+ CFG_LOG(ERR, " line %d - no \\n found on string. "
"Check if line too long\n", lineno);
goto error1;
}
+ /* skip parsing if comment character found */
pos = memchr(buffer, params->comment_character, len);
- if (pos != NULL) {
+ if (pos != NULL &&
+ (pos == buffer || *(pos-1) != '\\')) {
*pos = '\0';
len = pos - buffer;
}
len = _strip(buffer, len);
+ /* skip lines without useful content */
if (buffer[0] != '[' && memchr(buffer, '=', len) == NULL)
continue;
/* section heading line */
char *end = memchr(buffer, ']', len);
if (end == NULL) {
- printf("Error line %d - no terminating '['"
- "character found\n", lineno);
+ CFG_LOG(ERR,
+ "line %d - no terminating ']' character found\n",
+ lineno);
goto error1;
}
*end = '\0';
_strip(&buffer[1], end - &buffer[1]);
- /* close off old section and add start new one */
- if (curr_section >= 0)
- cfg->sections[curr_section].num_entries =
- curr_entry + 1;
- curr_section++;
-
- /* resize overall struct if we don't have room for more
- sections */
- if (curr_section == allocated_sections) {
- allocated_sections += CFG_ALLOC_SECTION_BATCH;
- struct rte_cfgfile *n_cfg = realloc(cfg,
- sizeof(*cfg) + sizeof(cfg->sections[0])
- * allocated_sections);
- if (n_cfg == NULL) {
- curr_section--;
- printf("Error - no more memory\n");
- goto error1;
- }
- cfg = n_cfg;
- }
-
- /* allocate space for new section */
- allocated_entries = CFG_ALLOC_ENTRY_BATCH;
- curr_entry = -1;
- cfg->sections = malloc(
- sizeof(cfg->sections[0]) +
- sizeof(cfg->sections[0].entries) *
- allocated_entries);
- if (cfg->sections == NULL) {
- printf("Error - no more memory\n");
- goto error1;
- }
-
- snprintf(cfg->sections[curr_section].name,
- sizeof(cfg->sections[0].name),
- "%s", &buffer[1]);
+ rte_cfgfile_add_section(cfg, &buffer[1]);
} else {
- /* value line */
- if (curr_section < 0) {
- printf("Error line %d - value outside of"
- "section\n", lineno);
- goto error1;
- }
-
- struct rte_cfgfile_section *sect = cfg->sections;
-
+ /* key and value line */
char *split[2] = {NULL};
+
split[0] = buffer;
split[1] = memchr(buffer, '=', len);
-
- /* when delimeter not found */
if (split[1] == NULL) {
- printf("Error at line %d - cannot "
- "split string\n", lineno);
+ CFG_LOG(ERR,
+ "line %d - no '=' character found\n",
+ lineno);
goto error1;
- } else {
- /* when delimeter found */
- *split[1] = '\0';
- split[1]++;
-
- if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
- (*split[1] == '\0')) {
- printf("Error at line %d - cannot "
- "split string\n", lineno);
- goto error1;
- }
}
-
- curr_entry++;
- if (curr_entry == allocated_entries) {
- allocated_entries += CFG_ALLOC_ENTRY_BATCH;
- struct rte_cfgfile_section *n_sect = realloc(
- sect, sizeof(*sect) +
- sizeof(sect->entries[0]) *
- allocated_entries);
- if (n_sect == NULL) {
- curr_entry--;
- printf("Error - no more memory\n");
- goto error1;
- }
- sect = cfg->sections = n_sect;
+ *split[1] = '\0';
+ split[1]++;
+
+ _strip(split[0], strlen(split[0]));
+ _strip(split[1], strlen(split[1]));
+ char *end = memchr(split[1], '\\', strlen(split[1]));
+
+ size_t split_len = strlen(split[1]) + 1;
+ while (end != NULL) {
+ if (*(end+1) == params->comment_character) {
+ *end = '\0';
+ strlcat(split[1], end+1, split_len);
+ } else
+ end++;
+ end = memchr(end, '\\', strlen(end));
}
- sect->entries = malloc(
- sizeof(sect->entries[0]));
- if (sect->entries == NULL) {
- printf("Error - no more memory\n");
+ if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
+ (*split[1] == '\0')) {
+ CFG_LOG(ERR,
+ "line %d - cannot use empty values\n",
+ lineno);
goto error1;
}
- struct rte_cfgfile_entry *entry = sect->entries;
- snprintf(entry->name, sizeof(entry->name), "%s",
- split[0]);
- snprintf(entry->value, sizeof(entry->value), "%s",
- split[1] ? split[1] : "");
- _strip(entry->name, strnlen(entry->name,
- sizeof(entry->name)));
- _strip(entry->value, strnlen(entry->value,
- sizeof(entry->value)));
+ if (cfg->num_sections == 0)
+ goto error1;
+
+ _add_entry(&cfg->sections[cfg->num_sections - 1],
+ split[0], split[1]);
}
}
fclose(f);
- cfg->flags = flags;
- cfg->num_sections = curr_section + 1;
- /* curr_section will still be -1 if we have an empty file */
- if (curr_section >= 0)
- cfg->sections[curr_section].num_entries = curr_entry + 1;
return cfg;
-
error1:
- cfg->num_sections = curr_section + 1;
- if (curr_section >= 0)
- cfg->sections[curr_section].num_entries = curr_entry + 1;
rte_cfgfile_close(cfg);
-error2:
fclose(f);
return NULL;
}
rte_cfgfile_create(int flags)
{
int i;
- struct rte_cfgfile *cfg = NULL;
+ struct rte_cfgfile *cfg;
+
+ /* future proof flags usage */
+ if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
+ return NULL;
cfg = malloc(sizeof(*cfg));
cfg->num_sections = 0;
/* allocate first batch of sections and entries */
- cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
- CFG_ALLOC_SECTION_BATCH);
-
+ cfg->sections = calloc(CFG_ALLOC_SECTION_BATCH,
+ sizeof(struct rte_cfgfile_section));
if (cfg->sections == NULL)
- return NULL;
+ goto error1;
cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
- cfg->sections[i].entries = malloc(sizeof(
- struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH);
+ cfg->sections[i].entries = calloc(CFG_ALLOC_ENTRY_BATCH,
+ sizeof(struct rte_cfgfile_entry));
if (cfg->sections[i].entries == NULL)
- return NULL;
+ goto error1;
cfg->sections[i].num_entries = 0;
cfg->sections[i].allocated_entries = CFG_ALLOC_ENTRY_BATCH;
if (flags & CFG_FLAG_GLOBAL_SECTION)
rte_cfgfile_add_section(cfg, "GLOBAL");
+
return cfg;
+error1:
+ if (cfg->sections != NULL) {
+ for (i = 0; i < cfg->allocated_sections; i++) {
+ if (cfg->sections[i].entries != NULL) {
+ free(cfg->sections[i].entries);
+ cfg->sections[i].entries = NULL;
+ }
+ }
+ free(cfg->sections);
+ cfg->sections = NULL;
+ }
+ free(cfg);
+ return NULL;
}
int
cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
}
- snprintf(cfg->sections[cfg->num_sections].name,
- sizeof(cfg->sections[0].name), "%s", sectionname);
+ strlcpy(cfg->sections[cfg->num_sections].name, sectionname,
+ sizeof(cfg->sections[0].name));
cfg->sections[cfg->num_sections].num_entries = 0;
cfg->num_sections++;
for (i = 0; i < curr_section->num_entries; i++)
if (!strcmp(curr_section->entries[i].name, entryname)) {
- snprintf(curr_section->entries[i].value,
- sizeof(curr_section->entries[i].value),
- "%s", entryvalue);
+ strlcpy(curr_section->entries[i].value, entryvalue,
+ sizeof(curr_section->entries[i].value));
return 0;
}
- printf("Error - entry name doesn't exist\n");
+
+ CFG_LOG(ERR, "entry name doesn't exist\n");
return -EINVAL;
}
int i;
for (i = 0; i < cfg->num_sections && i < max_sections; i++)
- snprintf(sections[i], CFG_NAME_LEN, "%s",
- cfg->sections[i].name);
+ strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
return i;
}
const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
- snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
+ strlcpy(sectionname, sect->name, CFG_NAME_LEN);
return sect->num_entries;
}
int
if (index < 0 || index >= cfg->num_sections)
return -1;
sect = &cfg->sections[index];
- snprintf(sectionname, CFG_NAME_LEN, "%s", sect->name);
+ strlcpy(sectionname, sect->name, CFG_NAME_LEN);
for (i = 0; i < max_entries && i < sect->num_entries; i++)
entries[i] = sect->entries[i];
return i;