attr.c 6.7 KB

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