ini.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * inih -- simple .INI file parser
  4. *
  5. * Copyright (c) 2009, Brush Technology
  6. * Copyright (c) 2012:
  7. * Joe Hershberger, National Instruments, joe.hershberger@ni.com
  8. * All rights reserved.
  9. *
  10. * Go to the project home page for more info:
  11. * http://code.google.com/p/inih/
  12. */
  13. #include <common.h>
  14. #include <command.h>
  15. #include <env.h>
  16. #include <linux/ctype.h>
  17. #include <linux/string.h>
  18. #ifdef CONFIG_INI_MAX_LINE
  19. #define MAX_LINE CONFIG_INI_MAX_LINE
  20. #else
  21. #define MAX_LINE 200
  22. #endif
  23. #ifdef CONFIG_INI_MAX_SECTION
  24. #define MAX_SECTION CONFIG_INI_MAX_SECTION
  25. #else
  26. #define MAX_SECTION 50
  27. #endif
  28. #ifdef CONFIG_INI_MAX_NAME
  29. #define MAX_NAME CONFIG_INI_MAX_NAME
  30. #else
  31. #define MAX_NAME 50
  32. #endif
  33. /* Strip whitespace chars off end of given string, in place. Return s. */
  34. static char *rstrip(char *s)
  35. {
  36. char *p = s + strlen(s);
  37. while (p > s && isspace(*--p))
  38. *p = '\0';
  39. return s;
  40. }
  41. /* Return pointer to first non-whitespace char in given string. */
  42. static char *lskip(const char *s)
  43. {
  44. while (*s && isspace(*s))
  45. s++;
  46. return (char *)s;
  47. }
  48. /* Return pointer to first char c or ';' comment in given string, or pointer to
  49. null at end of string if neither found. ';' must be prefixed by a whitespace
  50. character to register as a comment. */
  51. static char *find_char_or_comment(const char *s, char c)
  52. {
  53. int was_whitespace = 0;
  54. while (*s && *s != c && !(was_whitespace && *s == ';')) {
  55. was_whitespace = isspace(*s);
  56. s++;
  57. }
  58. return (char *)s;
  59. }
  60. /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
  61. static char *strncpy0(char *dest, const char *src, size_t size)
  62. {
  63. strncpy(dest, src, size);
  64. dest[size - 1] = '\0';
  65. return dest;
  66. }
  67. /* Emulate the behavior of fgets but on memory */
  68. static char *memgets(char *str, int num, char **mem, size_t *memsize)
  69. {
  70. char *end;
  71. int len;
  72. int newline = 1;
  73. end = memchr(*mem, '\n', *memsize);
  74. if (end == NULL) {
  75. if (*memsize == 0)
  76. return NULL;
  77. end = *mem + *memsize;
  78. newline = 0;
  79. }
  80. len = min((end - *mem) + newline, num);
  81. memcpy(str, *mem, len);
  82. if (len < num)
  83. str[len] = '\0';
  84. /* prepare the mem vars for the next call */
  85. *memsize -= (end - *mem) + newline;
  86. *mem += (end - *mem) + newline;
  87. return str;
  88. }
  89. /* Parse given INI-style file. May have [section]s, name=value pairs
  90. (whitespace stripped), and comments starting with ';' (semicolon). Section
  91. is "" if name=value pair parsed before any section heading. name:value
  92. pairs are also supported as a concession to Python's ConfigParser.
  93. For each name=value pair parsed, call handler function with given user
  94. pointer as well as section, name, and value (data only valid for duration
  95. of handler call). Handler should return nonzero on success, zero on error.
  96. Returns 0 on success, line number of first error on parse error (doesn't
  97. stop on first error).
  98. */
  99. static int ini_parse(char *filestart, size_t filelen,
  100. int (*handler)(void *, char *, char *, char *), void *user)
  101. {
  102. /* Uses a fair bit of stack (use heap instead if you need to) */
  103. char line[MAX_LINE];
  104. char section[MAX_SECTION] = "";
  105. char prev_name[MAX_NAME] = "";
  106. char *curmem = filestart;
  107. char *start;
  108. char *end;
  109. char *name;
  110. char *value;
  111. size_t memleft = filelen;
  112. int lineno = 0;
  113. int error = 0;
  114. /* Scan through file line by line */
  115. while (memgets(line, sizeof(line), &curmem, &memleft) != NULL) {
  116. lineno++;
  117. start = lskip(rstrip(line));
  118. if (*start == ';' || *start == '#') {
  119. /*
  120. * Per Python ConfigParser, allow '#' comments at start
  121. * of line
  122. */
  123. }
  124. #if CONFIG_INI_ALLOW_MULTILINE
  125. else if (*prev_name && *start && start > line) {
  126. /*
  127. * Non-blank line with leading whitespace, treat as
  128. * continuation of previous name's value (as per Python
  129. * ConfigParser).
  130. */
  131. if (!handler(user, section, prev_name, start) && !error)
  132. error = lineno;
  133. }
  134. #endif
  135. else if (*start == '[') {
  136. /* A "[section]" line */
  137. end = find_char_or_comment(start + 1, ']');
  138. if (*end == ']') {
  139. *end = '\0';
  140. strncpy0(section, start + 1, sizeof(section));
  141. *prev_name = '\0';
  142. } else if (!error) {
  143. /* No ']' found on section line */
  144. error = lineno;
  145. }
  146. } else if (*start && *start != ';') {
  147. /* Not a comment, must be a name[=:]value pair */
  148. end = find_char_or_comment(start, '=');
  149. if (*end != '=')
  150. end = find_char_or_comment(start, ':');
  151. if (*end == '=' || *end == ':') {
  152. *end = '\0';
  153. name = rstrip(start);
  154. value = lskip(end + 1);
  155. end = find_char_or_comment(value, '\0');
  156. if (*end == ';')
  157. *end = '\0';
  158. rstrip(value);
  159. /* Strip double-quotes */
  160. if (value[0] == '"' &&
  161. value[strlen(value)-1] == '"') {
  162. value[strlen(value)-1] = '\0';
  163. value += 1;
  164. }
  165. /*
  166. * Valid name[=:]value pair found, call handler
  167. */
  168. strncpy0(prev_name, name, sizeof(prev_name));
  169. if (!handler(user, section, name, value) &&
  170. !error)
  171. error = lineno;
  172. } else if (!error)
  173. /* No '=' or ':' found on name[=:]value line */
  174. error = lineno;
  175. }
  176. }
  177. return error;
  178. }
  179. static int ini_handler(void *user, char *section, char *name, char *value)
  180. {
  181. char *requested_section = (char *)user;
  182. #ifdef CONFIG_INI_CASE_INSENSITIVE
  183. int i;
  184. for (i = 0; i < strlen(requested_section); i++)
  185. requested_section[i] = tolower(requested_section[i]);
  186. for (i = 0; i < strlen(section); i++)
  187. section[i] = tolower(section[i]);
  188. #endif
  189. if (!strcmp(section, requested_section)) {
  190. #ifdef CONFIG_INI_CASE_INSENSITIVE
  191. for (i = 0; i < strlen(name); i++)
  192. name[i] = tolower(name[i]);
  193. for (i = 0; i < strlen(value); i++)
  194. value[i] = tolower(value[i]);
  195. #endif
  196. env_set(name, value);
  197. printf("ini: Imported %s as %s\n", name, value);
  198. }
  199. /* success */
  200. return 1;
  201. }
  202. static int do_ini(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  203. {
  204. const char *section;
  205. char *file_address;
  206. size_t file_size;
  207. if (argc == 1)
  208. return CMD_RET_USAGE;
  209. section = argv[1];
  210. file_address = (char *)hextoul(argc < 3 ? env_get("loadaddr") : argv[2],
  211. NULL);
  212. file_size = (size_t)hextoul(argc < 4 ? env_get("filesize") : argv[3],
  213. NULL);
  214. return ini_parse(file_address, file_size, ini_handler, (void *)section);
  215. }
  216. U_BOOT_CMD(
  217. ini, 4, 0, do_ini,
  218. "parse an ini file in memory and merge the specified section into the env",
  219. "section [[file-address] file-size]"
  220. );