env_attr.c 6.6 KB

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