cmd_ini.c 6.1 KB

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