cmd_ini.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * The "inih" library is distributed under the following license, which is
  10. * derived from and very similar to the 3-clause BSD license:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of Brush Technology nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY BRUSH TECHNOLOGY ''AS IS'' AND ANY
  24. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL BRUSH TECHNOLOGY BE LIABLE FOR ANY
  27. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Go to the project home page for more info:
  35. * http://code.google.com/p/inih/
  36. */
  37. #include <common.h>
  38. #include <command.h>
  39. #include <environment.h>
  40. #include <linux/ctype.h>
  41. #include <linux/string.h>
  42. #ifdef CONFIG_INI_MAX_LINE
  43. #define MAX_LINE CONFIG_INI_MAX_LINE
  44. #else
  45. #define MAX_LINE 200
  46. #endif
  47. #ifdef CONFIG_INI_MAX_SECTION
  48. #define MAX_SECTION CONFIG_INI_MAX_SECTION
  49. #else
  50. #define MAX_SECTION 50
  51. #endif
  52. #ifdef CONFIG_INI_MAX_NAME
  53. #define MAX_NAME CONFIG_INI_MAX_NAME
  54. #else
  55. #define MAX_NAME 50
  56. #endif
  57. /* Strip whitespace chars off end of given string, in place. Return s. */
  58. static char *rstrip(char *s)
  59. {
  60. char *p = s + strlen(s);
  61. while (p > s && isspace(*--p))
  62. *p = '\0';
  63. return s;
  64. }
  65. /* Return pointer to first non-whitespace char in given string. */
  66. static char *lskip(const char *s)
  67. {
  68. while (*s && isspace(*s))
  69. s++;
  70. return (char *)s;
  71. }
  72. /* Return pointer to first char c or ';' comment in given string, or pointer to
  73. null at end of string if neither found. ';' must be prefixed by a whitespace
  74. character to register as a comment. */
  75. static char *find_char_or_comment(const char *s, char c)
  76. {
  77. int was_whitespace = 0;
  78. while (*s && *s != c && !(was_whitespace && *s == ';')) {
  79. was_whitespace = isspace(*s);
  80. s++;
  81. }
  82. return (char *)s;
  83. }
  84. /* Version of strncpy that ensures dest (size bytes) is null-terminated. */
  85. static char *strncpy0(char *dest, const char *src, size_t size)
  86. {
  87. strncpy(dest, src, size);
  88. dest[size - 1] = '\0';
  89. return dest;
  90. }
  91. /* Emulate the behavior of fgets but on memory */
  92. static char *memgets(char *str, int num, char **mem, size_t *memsize)
  93. {
  94. char *end;
  95. int len;
  96. int newline = 1;
  97. end = memchr(*mem, '\n', *memsize);
  98. if (end == NULL) {
  99. if (*memsize == 0)
  100. return NULL;
  101. end = *mem + *memsize;
  102. newline = 0;
  103. }
  104. len = min((end - *mem) + newline, num);
  105. memcpy(str, *mem, len);
  106. if (len < num)
  107. str[len] = '\0';
  108. /* prepare the mem vars for the next call */
  109. *memsize -= (end - *mem) + newline;
  110. *mem += (end - *mem) + newline;
  111. return str;
  112. }
  113. /* Parse given INI-style file. May have [section]s, name=value pairs
  114. (whitespace stripped), and comments starting with ';' (semicolon). Section
  115. is "" if name=value pair parsed before any section heading. name:value
  116. pairs are also supported as a concession to Python's ConfigParser.
  117. For each name=value pair parsed, call handler function with given user
  118. pointer as well as section, name, and value (data only valid for duration
  119. of handler call). Handler should return nonzero on success, zero on error.
  120. Returns 0 on success, line number of first error on parse error (doesn't
  121. stop on first error).
  122. */
  123. static int ini_parse(char *filestart, size_t filelen,
  124. int (*handler)(void *, char *, char *, char *), void *user)
  125. {
  126. /* Uses a fair bit of stack (use heap instead if you need to) */
  127. char line[MAX_LINE];
  128. char section[MAX_SECTION] = "";
  129. char prev_name[MAX_NAME] = "";
  130. char *curmem = filestart;
  131. char *start;
  132. char *end;
  133. char *name;
  134. char *value;
  135. size_t memleft = filelen;
  136. int lineno = 0;
  137. int error = 0;
  138. /* Scan through file line by line */
  139. while (memgets(line, sizeof(line), &curmem, &memleft) != NULL) {
  140. lineno++;
  141. start = lskip(rstrip(line));
  142. if (*start == ';' || *start == '#') {
  143. /*
  144. * Per Python ConfigParser, allow '#' comments at start
  145. * of line
  146. */
  147. }
  148. #if CONFIG_INI_ALLOW_MULTILINE
  149. else if (*prev_name && *start && start > line) {
  150. /*
  151. * Non-blank line with leading whitespace, treat as
  152. * continuation of previous name's value (as per Python
  153. * ConfigParser).
  154. */
  155. if (!handler(user, section, prev_name, start) && !error)
  156. error = lineno;
  157. }
  158. #endif
  159. else if (*start == '[') {
  160. /* A "[section]" line */
  161. end = find_char_or_comment(start + 1, ']');
  162. if (*end == ']') {
  163. *end = '\0';
  164. strncpy0(section, start + 1, sizeof(section));
  165. *prev_name = '\0';
  166. } else if (!error) {
  167. /* No ']' found on section line */
  168. error = lineno;
  169. }
  170. } else if (*start && *start != ';') {
  171. /* Not a comment, must be a name[=:]value pair */
  172. end = find_char_or_comment(start, '=');
  173. if (*end != '=')
  174. end = find_char_or_comment(start, ':');
  175. if (*end == '=' || *end == ':') {
  176. *end = '\0';
  177. name = rstrip(start);
  178. value = lskip(end + 1);
  179. end = find_char_or_comment(value, '\0');
  180. if (*end == ';')
  181. *end = '\0';
  182. rstrip(value);
  183. /* Strip double-quotes */
  184. if (value[0] == '"' &&
  185. value[strlen(value)-1] == '"') {
  186. value[strlen(value)-1] = '\0';
  187. value += 1;
  188. }
  189. /*
  190. * Valid name[=:]value pair found, call handler
  191. */
  192. strncpy0(prev_name, name, sizeof(prev_name));
  193. if (!handler(user, section, name, value) &&
  194. !error)
  195. error = lineno;
  196. } else if (!error)
  197. /* No '=' or ':' found on name[=:]value line */
  198. error = lineno;
  199. }
  200. }
  201. return error;
  202. }
  203. static int ini_handler(void *user, char *section, char *name, char *value)
  204. {
  205. char *requested_section = (char *)user;
  206. #ifdef CONFIG_INI_CASE_INSENSITIVE
  207. int i;
  208. for (i = 0; i < strlen(requested_section); i++)
  209. requested_section[i] = tolower(requested_section[i]);
  210. for (i = 0; i < strlen(section); i++)
  211. section[i] = tolower(section[i]);
  212. #endif
  213. if (!strcmp(section, requested_section)) {
  214. #ifdef CONFIG_INI_CASE_INSENSITIVE
  215. for (i = 0; i < strlen(name); i++)
  216. name[i] = tolower(name[i]);
  217. for (i = 0; i < strlen(value); i++)
  218. value[i] = tolower(value[i]);
  219. #endif
  220. setenv(name, value);
  221. printf("ini: Imported %s as %s\n", name, value);
  222. }
  223. /* success */
  224. return 1;
  225. }
  226. static int do_ini(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  227. {
  228. const char *section;
  229. char *file_address;
  230. size_t file_size;
  231. if (argc == 1)
  232. return CMD_RET_USAGE;
  233. section = argv[1];
  234. file_address = (char *)simple_strtoul(
  235. argc < 3 ? getenv("loadaddr") : argv[2], NULL, 16);
  236. file_size = (size_t)simple_strtoul(
  237. argc < 4 ? getenv("filesize") : argv[3], NULL, 16);
  238. return ini_parse(file_address, file_size, ini_handler, (void *)section);
  239. }
  240. U_BOOT_CMD(
  241. ini, 4, 0, do_ini,
  242. "parse an ini file in memory and merge the specified section into the env",
  243. "section [[file-address] file-size]"
  244. );