cfgdefine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /****************************************************************************
  2. * tools/cfgdefine.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 <ctype.h>
  40. #include "cfgdefine.h"
  41. /****************************************************************************
  42. * Pre-processor Definitions
  43. ****************************************************************************/
  44. /****************************************************************************
  45. * Public Data
  46. ****************************************************************************/
  47. char line[LINESIZE + 1];
  48. /****************************************************************************
  49. * Private Data
  50. ****************************************************************************/
  51. /* These are configuration variable name that are quoted by configuration
  52. * tool but which must be unquoted when used in C code.
  53. */
  54. static const char *dequote_list[] =
  55. {
  56. /* NuttX */
  57. "CONFIG_DEBUG_OPTLEVEL", /* Custom debug level */
  58. "CONFIG_EXECFUNCS_NSYMBOLS_VAR", /* Variable holding number of symbols in the table */
  59. "CONFIG_EXECFUNCS_SYMTAB_ARRAY", /* Symbol table array used by exec[l|v] */
  60. "CONFIG_INIT_ARGS", /* Argument list of entry point */
  61. "CONFIG_INIT_SYMTAB", /* Global symbol table */
  62. "CONFIG_INIT_NEXPORTS", /* Global symbol table size */
  63. "CONFIG_MODLIB_SYMTAB_ARRAY", /* Symbol table array used by modlib functions */
  64. "CONFIG_MODLIB_NSYMBOLS_VAR", /* Variable holding number of symbols in the table */
  65. "CONFIG_PASS1_BUILDIR", /* Pass1 build directory */
  66. "CONFIG_PASS1_TARGET", /* Pass1 build target */
  67. "CONFIG_PASS1_OBJECT", /* Pass1 build object */
  68. "CONFIG_USER_ENTRYPOINT", /* Name of entry point function */
  69. /* NxWidgets/NxWM */
  70. "CONFIG_NXWM_BACKGROUND_IMAGE", /* Name of bitmap image class */
  71. "CONFIG_NXWM_CALIBRATION_ICON", /* Name of bitmap image class */
  72. "CONFIG_NXWM_HEXCALCULATOR_ICON", /* Name of bitmap image class */
  73. "CONFIG_NXWM_MINIMIZE_BITMAP", /* Name of bitmap image class */
  74. "CONFIG_NXWM_NXTERM_ICON", /* Name of bitmap image class */
  75. "CONFIG_NXWM_STARTWINDOW_ICON", /* Name of bitmap image class */
  76. "CONFIG_NXWM_STOP_BITMAP", /* Name of bitmap image class */
  77. /* apps/ definitions */
  78. "CONFIG_SYSTEM_NSH_SYMTAB_ARRAYNAME", /* Symbol table array name */
  79. "CONFIG_SYSTEM_NSH_SYMTAB_COUNTNAME", /* Name of the variable holding the number of symbols */
  80. NULL /* Marks the end of the list */
  81. };
  82. /****************************************************************************
  83. * Private Functions
  84. ****************************************************************************/
  85. /* Skip over any spaces */
  86. static char *skip_space(char *ptr)
  87. {
  88. while (*ptr && isspace((int)*ptr)) ptr++;
  89. return ptr;
  90. }
  91. /* Find the end of a variable string */
  92. static char *find_name_end(char *ptr)
  93. {
  94. while (*ptr && (isalnum((int)*ptr) || *ptr == '_')) ptr++;
  95. return ptr;
  96. }
  97. /* Find the end of a value string */
  98. static char *find_value_end(char *ptr)
  99. {
  100. while (*ptr && !isspace((int)*ptr))
  101. {
  102. if (*ptr == '"')
  103. {
  104. do ptr++; while (*ptr && *ptr != '"');
  105. if (*ptr) ptr++;
  106. }
  107. else
  108. {
  109. do ptr++; while (*ptr && !isspace((int)*ptr) && *ptr != '"');
  110. }
  111. }
  112. return ptr;
  113. }
  114. /* Read the next line from the configuration file */
  115. static char *read_line(FILE *stream)
  116. {
  117. char *ptr;
  118. for (; ; )
  119. {
  120. line[LINESIZE] = '\0';
  121. if (!fgets(line, LINESIZE, stream))
  122. {
  123. return NULL;
  124. }
  125. else
  126. {
  127. ptr = skip_space(line);
  128. if (*ptr && *ptr != '#' && *ptr != '\n')
  129. {
  130. return ptr;
  131. }
  132. }
  133. }
  134. }
  135. /* Parse the line from the configuration file into a variable name
  136. * string and a value string.
  137. */
  138. static void parse_line(char *ptr, char **varname, char **varval)
  139. {
  140. /* Skip over any leading spaces */
  141. ptr = skip_space(ptr);
  142. /* The first no-space is the beginning of the variable name */
  143. *varname = skip_space(ptr);
  144. *varval = NULL;
  145. /* Parse to the end of the variable name */
  146. ptr = find_name_end(ptr);
  147. /* An equal sign is expected next, perhaps after some white space */
  148. if (*ptr && *ptr != '=')
  149. {
  150. /* Some else follows the variable name. Terminate the variable
  151. * name and skip over any spaces.
  152. */
  153. *ptr = '\0';
  154. ptr = skip_space(ptr + 1);
  155. }
  156. /* Verify that the equal sign is present */
  157. if (*ptr == '=')
  158. {
  159. /* Make sure that the variable name is terminated (this was already
  160. * done if the name was followed by white space.
  161. */
  162. *ptr = '\0';
  163. /* The variable value should follow =, perhaps separated by some
  164. * white space.
  165. */
  166. ptr = skip_space(ptr + 1);
  167. if (*ptr)
  168. {
  169. /* Yes.. a variable follows. Save the pointer to the start
  170. * of the variable string.
  171. */
  172. *varval = ptr;
  173. /* Find the end of the variable string and make sure that it
  174. * is terminated.
  175. */
  176. ptr = find_value_end(ptr);
  177. *ptr = '\0';
  178. }
  179. }
  180. }
  181. static char *dequote_value(const char *varname, char *varval)
  182. {
  183. const char **dqnam;
  184. char *dqval = varval;
  185. char *ptr;
  186. int len;
  187. int i;
  188. if (dqval)
  189. {
  190. /* Check if the variable name is in the dequoted list of strings */
  191. for (dqnam = dequote_list; *dqnam; dqnam++)
  192. {
  193. if (strcmp(*dqnam, varname) == 0)
  194. {
  195. break;
  196. }
  197. }
  198. /* Did we find the variable name in the list of configuration variables
  199. * to be dequoted?
  200. */
  201. if (*dqnam)
  202. {
  203. /* Yes... Check if there is a trailing quote */
  204. len = strlen(dqval);
  205. if (dqval[len - 1] == '"')
  206. {
  207. /* Yes... replace it with a terminator */
  208. dqval[len - 1] = '\0';
  209. len--;
  210. }
  211. /* Is there a leading quote? */
  212. if (dqval[0] == '"')
  213. {
  214. /* Yes.. skip over the leading quote */
  215. dqval++;
  216. len--;
  217. }
  218. /* A special case is a quoted list of quoted strings. In that case
  219. * we will need to remove the backspaces from the internally quoted
  220. * strings. NOTE: this will not handle nested quoted quotes.
  221. */
  222. for (ptr = dqval; *ptr; ptr++)
  223. {
  224. /* Check for a quoted quote */
  225. if (ptr[0] == '\\' && ptr[1] == '"')
  226. {
  227. /* Delete the backslash by moving the rest of the string */
  228. for (i = 0; ptr[i]; i++)
  229. {
  230. ptr[i] = ptr[i + 1];
  231. }
  232. len--;
  233. }
  234. }
  235. /* Handle the case where nothing is left after dequoting */
  236. if (len <= 0)
  237. {
  238. dqval = NULL;
  239. }
  240. }
  241. }
  242. return dqval;
  243. }
  244. /****************************************************************************
  245. * Public Functions
  246. ****************************************************************************/
  247. void generate_definitions(FILE *stream)
  248. {
  249. char *varname;
  250. char *varval;
  251. char *ptr;
  252. /* Loop until the entire file has been parsed. */
  253. do
  254. {
  255. /* Read the next line from the file */
  256. ptr = read_line(stream);
  257. if (ptr)
  258. {
  259. /* Parse the line into a variable and a value field */
  260. parse_line(ptr, &varname, &varval);
  261. /* Was a variable name found? */
  262. if (varname)
  263. {
  264. /* Yes.. dequote the value if necessary */
  265. varval = dequote_value(varname, varval);
  266. /* If no value was provided or if the special value 'n' was
  267. * provided, then undefine the configuration variable.
  268. */
  269. if (!varval || strcmp(varval, "n") == 0)
  270. {
  271. printf("#undef %s\n", varname);
  272. }
  273. /* Simply define the configuration variable to '1' if it has
  274. * the special value "y"
  275. */
  276. else if (strcmp(varval, "y") == 0)
  277. {
  278. printf("#define %s 1\n", varname);
  279. }
  280. /* Or to '2' if it has the special value 'm' */
  281. else if (strcmp(varval, "m") == 0)
  282. {
  283. printf("#define %s 2\n", varname);
  284. }
  285. /* Otherwise, use the value as provided */
  286. else
  287. {
  288. printf("#define %s %s\n", varname, varval);
  289. }
  290. }
  291. }
  292. }
  293. while (ptr);
  294. }