wpp.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Exported functions of the Wine preprocessor
  3. *
  4. * Copyright 1998 Bertho A. Stultiens
  5. * Copyright 2002 Alexandre Julliard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <time.h>
  24. #include <stdlib.h>
  25. #include "wpp_private.h"
  26. #include "wine/wpp.h"
  27. int ppy_debug, pp_flex_debug;
  28. struct define
  29. {
  30. struct define *next;
  31. char *name;
  32. char *value;
  33. };
  34. static struct define *cmdline_defines;
  35. static void add_cmdline_defines(void)
  36. {
  37. struct define *def;
  38. for (def = cmdline_defines; def; def = def->next)
  39. {
  40. if (def->value) pp_add_define( def->name, def->value );
  41. }
  42. }
  43. static void del_cmdline_defines(void)
  44. {
  45. struct define *def;
  46. for (def = cmdline_defines; def; def = def->next)
  47. {
  48. if (def->value) pp_del_define( def->name );
  49. }
  50. }
  51. static void add_special_defines(void)
  52. {
  53. time_t now = time(NULL);
  54. pp_entry_t *ppp;
  55. char buf[32];
  56. strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
  57. pp_add_define( "__DATE__", buf );
  58. strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
  59. pp_add_define( "__TIME__", buf );
  60. ppp = pp_add_define( "__FILE__", "" );
  61. if(ppp)
  62. ppp->type = def_special;
  63. ppp = pp_add_define( "__LINE__", "" );
  64. if(ppp)
  65. ppp->type = def_special;
  66. }
  67. static void del_special_defines(void)
  68. {
  69. pp_del_define( "__DATE__" );
  70. pp_del_define( "__TIME__" );
  71. pp_del_define( "__FILE__" );
  72. pp_del_define( "__LINE__" );
  73. }
  74. /* add a define to the preprocessor list */
  75. int wpp_add_define( const char *name, const char *value )
  76. {
  77. struct define *def;
  78. if (!value) value = "";
  79. for (def = cmdline_defines; def; def = def->next)
  80. {
  81. if (!strcmp( def->name, name ))
  82. {
  83. char *new_value = pp_xstrdup(value);
  84. if(!new_value)
  85. return 1;
  86. free( def->value );
  87. def->value = new_value;
  88. return 0;
  89. }
  90. }
  91. def = pp_xmalloc( sizeof(*def) );
  92. if(!def)
  93. return 1;
  94. def->next = cmdline_defines;
  95. def->name = pp_xstrdup(name);
  96. if(!def->name)
  97. {
  98. free(def);
  99. return 1;
  100. }
  101. def->value = pp_xstrdup(value);
  102. if(!def->value)
  103. {
  104. free(def->name);
  105. free(def);
  106. return 1;
  107. }
  108. cmdline_defines = def;
  109. return 0;
  110. }
  111. /* undefine a previously added definition */
  112. void wpp_del_define( const char *name )
  113. {
  114. struct define *def;
  115. for (def = cmdline_defines; def; def = def->next)
  116. {
  117. if (!strcmp( def->name, name ))
  118. {
  119. free( def->value );
  120. def->value = NULL;
  121. return;
  122. }
  123. }
  124. }
  125. /* add a command-line define of the form NAME=VALUE */
  126. int wpp_add_cmdline_define( const char *value )
  127. {
  128. char *p;
  129. char *str = pp_xstrdup(value);
  130. if(!str)
  131. return 1;
  132. p = strchr( str, '=' );
  133. if (p) *p++ = 0;
  134. wpp_add_define( str, p );
  135. free( str );
  136. return 0;
  137. }
  138. /* set the various debug flags */
  139. void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
  140. {
  141. pp_flex_debug = lex_debug;
  142. ppy_debug = parser_debug;
  143. pp_status.debug = msg_debug;
  144. }
  145. /* set the pedantic mode */
  146. void wpp_set_pedantic( int on )
  147. {
  148. pp_status.pedantic = on;
  149. }
  150. /* the main preprocessor parsing loop */
  151. int wpp_parse( const char *input, FILE *output )
  152. {
  153. int ret;
  154. pp_status.input = NULL;
  155. pp_status.line_number = 1;
  156. pp_status.char_number = 1;
  157. pp_status.state = 0;
  158. ret = pp_push_define_state();
  159. if(ret)
  160. return ret;
  161. add_cmdline_defines();
  162. add_special_defines();
  163. if (!input) pp_status.file = stdin;
  164. else if (!(pp_status.file = fopen(input, "rt")))
  165. {
  166. ppy_error("Could not open %s\n", input);
  167. del_special_defines();
  168. del_cmdline_defines();
  169. pp_pop_define_state();
  170. return 2;
  171. }
  172. pp_status.input = input ? pp_xstrdup(input) : NULL;
  173. ppy_out = output;
  174. pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
  175. ret = ppy_parse();
  176. /* If there were errors during processing, return an error code */
  177. if (!ret && pp_status.state) ret = pp_status.state;
  178. if (input)
  179. {
  180. fclose(pp_status.file);
  181. free(pp_status.input);
  182. }
  183. /* Clean if_stack, it could remain dirty on errors */
  184. while (pp_get_if_depth()) pp_pop_if();
  185. del_special_defines();
  186. del_cmdline_defines();
  187. pp_pop_define_state();
  188. return ret;
  189. }