cmd_ini.c 6.2 KB

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