Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 |
| 3 | * Joe Hershberger, National Instruments, joe.hershberger@ni.com |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 8 | #ifdef USE_HOSTCC /* Eliminate "ANSI does not permit..." warnings */ |
| 9 | #include <stdint.h> |
| 10 | #include <stdio.h> |
| 11 | #include <linux/linux_string.h> |
| 12 | #else |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 13 | #include <common.h> |
Joe Hershberger | 30fd4fad | 2012-12-11 22:16:32 -0600 | [diff] [blame] | 14 | #endif |
| 15 | |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 16 | #include <env_attr.h> |
| 17 | #include <errno.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <malloc.h> |
| 20 | |
| 21 | /* |
| 22 | * Iterate through the whole list calling the callback for each found element. |
| 23 | * "attr_list" takes the form: |
| 24 | * attributes = [^,:\s]* |
| 25 | * entry = name[:attributes] |
| 26 | * list = entry[,list] |
| 27 | */ |
| 28 | int env_attr_walk(const char *attr_list, |
| 29 | int (*callback)(const char *name, const char *attributes)) |
| 30 | { |
| 31 | const char *entry, *entry_end; |
| 32 | char *name, *attributes; |
| 33 | |
| 34 | if (!attr_list) |
| 35 | /* list not found */ |
| 36 | return 1; |
| 37 | |
| 38 | entry = attr_list; |
| 39 | do { |
| 40 | char *entry_cpy = NULL; |
| 41 | |
| 42 | entry_end = strchr(entry, ENV_ATTR_LIST_DELIM); |
| 43 | /* check if this is the last entry in the list */ |
| 44 | if (entry_end == NULL) { |
| 45 | int entry_len = strlen(entry); |
| 46 | |
| 47 | if (entry_len) { |
| 48 | /* |
| 49 | * allocate memory to copy the entry into since |
| 50 | * we will need to inject '\0' chars and squash |
| 51 | * white-space before calling the callback |
| 52 | */ |
| 53 | entry_cpy = malloc(entry_len + 1); |
| 54 | if (entry_cpy) |
| 55 | /* copy the rest of the list */ |
| 56 | strcpy(entry_cpy, entry); |
| 57 | else |
| 58 | return -ENOMEM; |
| 59 | } |
| 60 | } else { |
| 61 | int entry_len = entry_end - entry; |
| 62 | |
| 63 | if (entry_len) { |
| 64 | /* |
| 65 | * allocate memory to copy the entry into since |
| 66 | * we will need to inject '\0' chars and squash |
| 67 | * white-space before calling the callback |
| 68 | */ |
| 69 | entry_cpy = malloc(entry_len + 1); |
| 70 | if (entry_cpy) { |
| 71 | /* copy just this entry and null term */ |
| 72 | strncpy(entry_cpy, entry, entry_len); |
| 73 | entry_cpy[entry_len] = '\0'; |
| 74 | } else |
| 75 | return -ENOMEM; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* check if there is anything to process (e.g. not ",,,") */ |
| 80 | if (entry_cpy != NULL) { |
| 81 | attributes = strchr(entry_cpy, ENV_ATTR_SEP); |
| 82 | /* check if there is a ':' */ |
| 83 | if (attributes != NULL) { |
| 84 | /* replace the ':' with '\0' to term name */ |
| 85 | *attributes++ = '\0'; |
| 86 | /* remove white-space from attributes */ |
| 87 | attributes = strim(attributes); |
| 88 | } |
| 89 | /* remove white-space from name */ |
| 90 | name = strim(entry_cpy); |
| 91 | |
| 92 | /* only call the callback if there is a name */ |
| 93 | if (strlen(name) != 0) { |
| 94 | int retval = 0; |
| 95 | |
| 96 | retval = callback(name, attributes); |
| 97 | if (retval) { |
| 98 | free(entry_cpy); |
| 99 | return retval; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | free(entry_cpy); |
| 105 | entry = entry_end + 1; |
| 106 | } while (entry_end != NULL); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /* |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 112 | * Search for the last exactly matching name in an attribute list |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 113 | */ |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 114 | static int reverse_name_search(const char *searched, const char *search_for, |
| 115 | const char **result) |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 116 | { |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 117 | int result_size = 0; |
| 118 | const char *cur_searched = searched; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 119 | |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 120 | if (result) |
| 121 | *result = NULL; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 122 | |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 123 | if (*search_for == '\0') { |
| 124 | if (result) |
| 125 | *result = searched; |
| 126 | return strlen(searched); |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 129 | for (;;) { |
| 130 | const char *match = strstr(cur_searched, search_for); |
| 131 | const char *prevch; |
| 132 | const char *nextch; |
| 133 | |
| 134 | /* Stop looking if no new match is found */ |
| 135 | if (match == NULL) |
| 136 | break; |
| 137 | |
| 138 | prevch = match - 1; |
| 139 | nextch = match + strlen(search_for); |
| 140 | |
| 141 | /* Skip spaces */ |
| 142 | while (*prevch == ' ' && prevch >= searched) |
| 143 | prevch--; |
| 144 | while (*nextch == ' ') |
| 145 | nextch++; |
| 146 | |
| 147 | /* Start looking past the current match so last is found */ |
| 148 | cur_searched = match + 1; |
| 149 | /* Check for an exact match */ |
| 150 | if (match != searched && |
| 151 | *prevch != ENV_ATTR_LIST_DELIM && |
| 152 | prevch != searched - 1) |
| 153 | continue; |
| 154 | if (*nextch != ENV_ATTR_SEP && |
| 155 | *nextch != ENV_ATTR_LIST_DELIM && |
| 156 | *nextch != '\0') |
| 157 | continue; |
| 158 | |
| 159 | if (result) |
| 160 | *result = match; |
| 161 | result_size = strlen(search_for); |
| 162 | } |
| 163 | |
| 164 | return result_size; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* |
| 168 | * Retrieve the attributes string associated with a single name in the list |
| 169 | * There is no protection on attributes being too small for the value |
| 170 | */ |
| 171 | int env_attr_lookup(const char *attr_list, const char *name, char *attributes) |
| 172 | { |
| 173 | const char *entry = NULL; |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 174 | int entry_len; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 175 | |
| 176 | if (!attributes) |
| 177 | /* bad parameter */ |
Joe Hershberger | 7a0ad2c | 2015-05-20 14:27:17 -0500 | [diff] [blame] | 178 | return -EINVAL; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 179 | if (!attr_list) |
| 180 | /* list not found */ |
Joe Hershberger | 7a0ad2c | 2015-05-20 14:27:17 -0500 | [diff] [blame] | 181 | return -EINVAL; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 182 | |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 183 | entry_len = reverse_name_search(attr_list, name, &entry); |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 184 | if (entry != NULL) { |
| 185 | int len; |
| 186 | |
| 187 | /* skip the name */ |
Joe Hershberger | 032ea18 | 2015-05-20 14:27:18 -0500 | [diff] [blame^] | 188 | entry += entry_len; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 189 | /* skip spaces */ |
| 190 | while (*entry == ' ') |
| 191 | entry++; |
| 192 | if (*entry != ENV_ATTR_SEP) |
| 193 | len = 0; |
| 194 | else { |
| 195 | const char *delim; |
| 196 | static const char delims[] = { |
| 197 | ENV_ATTR_LIST_DELIM, ' ', '\0'}; |
| 198 | |
| 199 | /* skip the attr sep */ |
| 200 | entry += 1; |
| 201 | /* skip spaces */ |
| 202 | while (*entry == ' ') |
| 203 | entry++; |
| 204 | |
| 205 | delim = strpbrk(entry, delims); |
| 206 | if (delim == NULL) |
| 207 | len = strlen(entry); |
| 208 | else |
| 209 | len = delim - entry; |
| 210 | memcpy(attributes, entry, len); |
| 211 | } |
| 212 | attributes[len] = '\0'; |
| 213 | |
| 214 | /* success */ |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* not found in list */ |
Joe Hershberger | 7a0ad2c | 2015-05-20 14:27:17 -0500 | [diff] [blame] | 219 | return -ENOENT; |
Joe Hershberger | 170ab11 | 2012-12-11 22:16:24 -0600 | [diff] [blame] | 220 | } |