cfgparser.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /****************************************************************************
  2. * tools/cfgpaser.c
  3. *
  4. * Copyright (C) 2007-2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <string.h>
  39. #include <stdlib.h>
  40. #include <ctype.h>
  41. #include "cfgparser.h"
  42. /****************************************************************************
  43. * Pre-processor Definitions
  44. ****************************************************************************/
  45. /****************************************************************************
  46. * Public Data
  47. ****************************************************************************/
  48. char line[LINESIZE+1];
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /****************************************************************************
  53. * Private Functions
  54. ****************************************************************************/
  55. /* Skip over any spaces */
  56. static char *skip_space(char *ptr)
  57. {
  58. while (*ptr && isspace((int)*ptr)) ptr++;
  59. return ptr;
  60. }
  61. /* Find the end of a variable string */
  62. static char *find_name_end(char *ptr)
  63. {
  64. while (*ptr && (isalnum((int)*ptr) || *ptr == '_')) ptr++;
  65. return ptr;
  66. }
  67. /* Find the end of a value string */
  68. static char *find_value_end(char *ptr)
  69. {
  70. while (*ptr && !isspace((int)*ptr))
  71. {
  72. if (*ptr == '"')
  73. {
  74. do ptr++; while (*ptr && *ptr != '"');
  75. if (*ptr) ptr++;
  76. }
  77. else
  78. {
  79. do ptr++; while (*ptr && !isspace((int)*ptr) && *ptr != '"');
  80. }
  81. }
  82. return ptr;
  83. }
  84. /* Read the next line from the configuration file */
  85. static char *read_line(FILE *stream)
  86. {
  87. char *ptr;
  88. for (;;)
  89. {
  90. line[LINESIZE] = '\0';
  91. if (!fgets(line, LINESIZE, stream))
  92. {
  93. return NULL;
  94. }
  95. else
  96. {
  97. ptr = skip_space(line);
  98. if (*ptr && *ptr != '#' && *ptr != '\n')
  99. {
  100. return ptr;
  101. }
  102. }
  103. }
  104. }
  105. /* Parse the line from the configuration file into a variable name
  106. * string and a value string.
  107. */
  108. static void parse_line(char *ptr, char **varname, char **varval)
  109. {
  110. /* Skip over any leading spaces */
  111. ptr = skip_space(ptr);
  112. /* The first no-space is the beginning of the variable name */
  113. *varname = skip_space(ptr);
  114. *varval = NULL;
  115. /* Parse to the end of the variable name */
  116. ptr = find_name_end(ptr);
  117. /* An equal sign is expected next, perhaps after some white space */
  118. if (*ptr && *ptr != '=')
  119. {
  120. /* Some else follows the variable name. Terminate the variable
  121. * name and skip over any spaces.
  122. */
  123. *ptr = '\0';
  124. ptr = skip_space(ptr + 1);
  125. }
  126. /* Verify that the equal sign is present */
  127. if (*ptr == '=')
  128. {
  129. /* Make sure that the variable name is terminated (this was already
  130. * done if the name was followed by white space.
  131. */
  132. *ptr = '\0';
  133. /* The variable value should follow =, perhaps separated by some
  134. * white space.
  135. */
  136. ptr = skip_space(ptr + 1);
  137. if (*ptr)
  138. {
  139. /* Yes.. a variable follows. Save the pointer to the start
  140. * of the variable string.
  141. */
  142. *varval = ptr;
  143. /* Find the end of the variable string and make sure that it
  144. * is terminated.
  145. */
  146. ptr = find_value_end(ptr);
  147. *ptr = '\0';
  148. }
  149. }
  150. }
  151. /****************************************************************************
  152. * Public Functions
  153. ****************************************************************************/
  154. void parse_file(FILE *stream, struct variable_s **list)
  155. {
  156. struct variable_s *curr;
  157. struct variable_s *prev;
  158. struct variable_s *next;
  159. char *varname;
  160. char *varval;
  161. char *ptr;
  162. /* Loop until the entire file has been parsed. */
  163. do
  164. {
  165. /* Read the next line from the file */
  166. ptr = read_line(stream);
  167. if (ptr)
  168. {
  169. /* Parse the line into a variable and a value field */
  170. parse_line(ptr, &varname, &varval);
  171. /* If the variable has no value (or the special value 'n'), then
  172. * ignore it.
  173. */
  174. if (!varval || strcmp(varval, "n") == 0)
  175. {
  176. continue;
  177. }
  178. /* Make sure that a variable name was found. */
  179. if (varname)
  180. {
  181. int varlen = strlen(varname) + 1;
  182. int vallen = 0;
  183. /* Get the size of the value, including the NUL terminating
  184. * character.
  185. */
  186. if (varval)
  187. {
  188. vallen = strlen(varval) + 1;
  189. }
  190. /* Allocate memory to hold the struct variable_s with the
  191. * variable name and the value.
  192. */
  193. curr = (struct variable_s *)malloc(sizeof(struct variable_s) + varlen + vallen - 1);
  194. if (curr)
  195. {
  196. /* Add the variable to the list */
  197. curr->var = &curr->storage[0];
  198. strcpy(curr->var, varname);
  199. curr->val = NULL;
  200. if (varval)
  201. {
  202. curr->val = &curr->storage[varlen];
  203. strcpy(curr->val, varval);
  204. }
  205. prev = 0;
  206. next = *list;
  207. while (next && strcmp(next->var, curr->var) <= 0)
  208. {
  209. prev = next;
  210. next = next->flink;
  211. }
  212. if (prev)
  213. {
  214. prev->flink = curr;
  215. }
  216. else
  217. {
  218. *list = curr;
  219. }
  220. curr->flink = next;
  221. }
  222. }
  223. }
  224. }
  225. while (ptr);
  226. }
  227. struct variable_s *find_variable(const char *varname, struct variable_s *list)
  228. {
  229. while (list)
  230. {
  231. if (strcmp(varname, list->var) == 0)
  232. {
  233. return list;
  234. }
  235. list = list->flink;
  236. }
  237. return NULL;
  238. }