2012-12-12 12:16:24 +08:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2012
|
|
|
|
* Joe Hershberger, National Instruments, joe.hershberger@ni.com
|
|
|
|
*
|
2013-07-08 15:37:19 +08:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2012-12-12 12:16:24 +08:00
|
|
|
*/
|
|
|
|
|
2012-12-12 12:16:32 +08:00
|
|
|
#ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <linux/linux_string.h>
|
|
|
|
#else
|
2012-12-12 12:16:24 +08:00
|
|
|
#include <common.h>
|
2012-12-12 12:16:32 +08:00
|
|
|
#endif
|
|
|
|
|
2012-12-12 12:16:24 +08:00
|
|
|
#include <env_attr.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterate through the whole list calling the callback for each found element.
|
|
|
|
* "attr_list" takes the form:
|
|
|
|
* attributes = [^,:\s]*
|
|
|
|
* entry = name[:attributes]
|
|
|
|
* list = entry[,list]
|
|
|
|
*/
|
|
|
|
int env_attr_walk(const char *attr_list,
|
2015-05-21 03:27:19 +08:00
|
|
|
int (*callback)(const char *name, const char *attributes, void *priv),
|
|
|
|
void *priv)
|
2012-12-12 12:16:24 +08:00
|
|
|
{
|
|
|
|
const char *entry, *entry_end;
|
|
|
|
char *name, *attributes;
|
|
|
|
|
|
|
|
if (!attr_list)
|
|
|
|
/* list not found */
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
entry = attr_list;
|
|
|
|
do {
|
|
|
|
char *entry_cpy = NULL;
|
|
|
|
|
|
|
|
entry_end = strchr(entry, ENV_ATTR_LIST_DELIM);
|
|
|
|
/* check if this is the last entry in the list */
|
|
|
|
if (entry_end == NULL) {
|
|
|
|
int entry_len = strlen(entry);
|
|
|
|
|
|
|
|
if (entry_len) {
|
|
|
|
/*
|
|
|
|
* allocate memory to copy the entry into since
|
|
|
|
* we will need to inject '\0' chars and squash
|
|
|
|
* white-space before calling the callback
|
|
|
|
*/
|
|
|
|
entry_cpy = malloc(entry_len + 1);
|
|
|
|
if (entry_cpy)
|
|
|
|
/* copy the rest of the list */
|
|
|
|
strcpy(entry_cpy, entry);
|
|
|
|
else
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int entry_len = entry_end - entry;
|
|
|
|
|
|
|
|
if (entry_len) {
|
|
|
|
/*
|
|
|
|
* allocate memory to copy the entry into since
|
|
|
|
* we will need to inject '\0' chars and squash
|
|
|
|
* white-space before calling the callback
|
|
|
|
*/
|
|
|
|
entry_cpy = malloc(entry_len + 1);
|
|
|
|
if (entry_cpy) {
|
|
|
|
/* copy just this entry and null term */
|
|
|
|
strncpy(entry_cpy, entry, entry_len);
|
|
|
|
entry_cpy[entry_len] = '\0';
|
|
|
|
} else
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if there is anything to process (e.g. not ",,,") */
|
|
|
|
if (entry_cpy != NULL) {
|
|
|
|
attributes = strchr(entry_cpy, ENV_ATTR_SEP);
|
|
|
|
/* check if there is a ':' */
|
|
|
|
if (attributes != NULL) {
|
|
|
|
/* replace the ':' with '\0' to term name */
|
|
|
|
*attributes++ = '\0';
|
|
|
|
/* remove white-space from attributes */
|
|
|
|
attributes = strim(attributes);
|
|
|
|
}
|
|
|
|
/* remove white-space from name */
|
|
|
|
name = strim(entry_cpy);
|
|
|
|
|
|
|
|
/* only call the callback if there is a name */
|
|
|
|
if (strlen(name) != 0) {
|
|
|
|
int retval = 0;
|
|
|
|
|
2015-05-21 03:27:19 +08:00
|
|
|
retval = callback(name, attributes, priv);
|
2012-12-12 12:16:24 +08:00
|
|
|
if (retval) {
|
|
|
|
free(entry_cpy);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(entry_cpy);
|
|
|
|
entry = entry_end + 1;
|
|
|
|
} while (entry_end != NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-05-21 03:27:18 +08:00
|
|
|
* Search for the last exactly matching name in an attribute list
|
2012-12-12 12:16:24 +08:00
|
|
|
*/
|
2015-05-21 03:27:18 +08:00
|
|
|
static int reverse_name_search(const char *searched, const char *search_for,
|
|
|
|
const char **result)
|
2012-12-12 12:16:24 +08:00
|
|
|
{
|
2015-05-21 03:27:18 +08:00
|
|
|
int result_size = 0;
|
|
|
|
const char *cur_searched = searched;
|
2012-12-12 12:16:24 +08:00
|
|
|
|
2015-05-21 03:27:18 +08:00
|
|
|
if (result)
|
|
|
|
*result = NULL;
|
|
|
|
|
|
|
|
if (*search_for == '\0') {
|
|
|
|
if (result)
|
|
|
|
*result = searched;
|
|
|
|
return strlen(searched);
|
|
|
|
}
|
2012-12-12 12:16:24 +08:00
|
|
|
|
|
|
|
for (;;) {
|
2015-05-21 03:27:18 +08:00
|
|
|
const char *match = strstr(cur_searched, search_for);
|
|
|
|
const char *prevch;
|
|
|
|
const char *nextch;
|
|
|
|
|
|
|
|
/* Stop looking if no new match is found */
|
|
|
|
if (match == NULL)
|
2012-12-12 12:16:24 +08:00
|
|
|
break;
|
|
|
|
|
2015-05-21 03:27:18 +08:00
|
|
|
prevch = match - 1;
|
|
|
|
nextch = match + strlen(search_for);
|
|
|
|
|
|
|
|
/* Skip spaces */
|
|
|
|
while (*prevch == ' ' && prevch >= searched)
|
|
|
|
prevch--;
|
|
|
|
while (*nextch == ' ')
|
|
|
|
nextch++;
|
|
|
|
|
|
|
|
/* Start looking past the current match so last is found */
|
|
|
|
cur_searched = match + 1;
|
|
|
|
/* Check for an exact match */
|
|
|
|
if (match != searched &&
|
|
|
|
*prevch != ENV_ATTR_LIST_DELIM &&
|
|
|
|
prevch != searched - 1)
|
|
|
|
continue;
|
|
|
|
if (*nextch != ENV_ATTR_SEP &&
|
|
|
|
*nextch != ENV_ATTR_LIST_DELIM &&
|
|
|
|
*nextch != '\0')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
*result = match;
|
|
|
|
result_size = strlen(search_for);
|
2012-12-12 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
2015-05-21 03:27:18 +08:00
|
|
|
return result_size;
|
2012-12-12 12:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the attributes string associated with a single name in the list
|
|
|
|
* There is no protection on attributes being too small for the value
|
|
|
|
*/
|
|
|
|
int env_attr_lookup(const char *attr_list, const char *name, char *attributes)
|
|
|
|
{
|
|
|
|
const char *entry = NULL;
|
2015-05-21 03:27:18 +08:00
|
|
|
int entry_len;
|
2012-12-12 12:16:24 +08:00
|
|
|
|
|
|
|
if (!attributes)
|
|
|
|
/* bad parameter */
|
2015-05-21 03:27:17 +08:00
|
|
|
return -EINVAL;
|
2012-12-12 12:16:24 +08:00
|
|
|
if (!attr_list)
|
|
|
|
/* list not found */
|
2015-05-21 03:27:17 +08:00
|
|
|
return -EINVAL;
|
2012-12-12 12:16:24 +08:00
|
|
|
|
2015-05-21 03:27:18 +08:00
|
|
|
entry_len = reverse_name_search(attr_list, name, &entry);
|
2012-12-12 12:16:24 +08:00
|
|
|
if (entry != NULL) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* skip the name */
|
2015-05-21 03:27:18 +08:00
|
|
|
entry += entry_len;
|
2012-12-12 12:16:24 +08:00
|
|
|
/* skip spaces */
|
|
|
|
while (*entry == ' ')
|
|
|
|
entry++;
|
|
|
|
if (*entry != ENV_ATTR_SEP)
|
|
|
|
len = 0;
|
|
|
|
else {
|
|
|
|
const char *delim;
|
|
|
|
static const char delims[] = {
|
|
|
|
ENV_ATTR_LIST_DELIM, ' ', '\0'};
|
|
|
|
|
|
|
|
/* skip the attr sep */
|
|
|
|
entry += 1;
|
|
|
|
/* skip spaces */
|
|
|
|
while (*entry == ' ')
|
|
|
|
entry++;
|
|
|
|
|
|
|
|
delim = strpbrk(entry, delims);
|
|
|
|
if (delim == NULL)
|
|
|
|
len = strlen(entry);
|
|
|
|
else
|
|
|
|
len = delim - entry;
|
|
|
|
memcpy(attributes, entry, len);
|
|
|
|
}
|
|
|
|
attributes[len] = '\0';
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not found in list */
|
2015-05-21 03:27:17 +08:00
|
|
|
return -ENOENT;
|
2012-12-12 12:16:24 +08:00
|
|
|
}
|