ReactOS  0.4.14-dev-52-g6116262
wpp.c
Go to the documentation of this file.
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 
22 #include "config.h"
23 #include "wine/port.h"
24 
25 #include <time.h>
26 #include <stdlib.h>
27 
28 #include "wpp_private.h"
29 #include "wine/wpp.h"
30 
32 
33 struct define
34 {
35  struct define *next;
36  char *name;
37  char *value;
38 };
39 
40 static struct define *cmdline_defines;
41 
42 static void add_cmdline_defines(void)
43 {
44  struct define *def;
45 
46  for (def = cmdline_defines; def; def = def->next)
47  {
48  if (def->value) pp_add_define( def->name, def->value );
49  }
50 }
51 
52 static void del_cmdline_defines(void)
53 {
54  struct define *def;
55 
56  for (def = cmdline_defines; def; def = def->next)
57  {
58  if (def->value) pp_del_define( def->name );
59  }
60 }
61 
62 static void add_special_defines(void)
63 {
64  time_t now = time(NULL);
65  pp_entry_t *ppp;
66  char buf[32];
67 
68  strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
69  pp_add_define( "__DATE__", buf );
70 
71  strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
72  pp_add_define( "__TIME__", buf );
73 
74  ppp = pp_add_define( "__FILE__", "" );
75  if(ppp)
76  ppp->type = def_special;
77 
78  ppp = pp_add_define( "__LINE__", "" );
79  if(ppp)
80  ppp->type = def_special;
81 }
82 
83 static void del_special_defines(void)
84 {
85  pp_del_define( "__DATE__" );
86  pp_del_define( "__TIME__" );
87  pp_del_define( "__FILE__" );
88  pp_del_define( "__LINE__" );
89 }
90 
91 
92 /* add a define to the preprocessor list */
93 int wpp_add_define( const char *name, const char *value )
94 {
95  struct define *def;
96 
97  if (!value) value = "";
98 
99  for (def = cmdline_defines; def; def = def->next)
100  {
101  if (!strcmp( def->name, name ))
102  {
103  char *new_value = pp_xstrdup(value);
104  if(!new_value)
105  return 1;
106  free( def->value );
107  def->value = new_value;
108 
109  return 0;
110  }
111  }
112 
113  def = pp_xmalloc( sizeof(*def) );
114  if(!def)
115  return 1;
116  def->next = cmdline_defines;
117  def->name = pp_xstrdup(name);
118  if(!def->name)
119  {
120  free(def);
121  return 1;
122  }
123  def->value = pp_xstrdup(value);
124  if(!def->value)
125  {
126  free(def->name);
127  free(def);
128  return 1;
129  }
130  cmdline_defines = def;
131  return 0;
132 }
133 
134 
135 /* undefine a previously added definition */
136 void wpp_del_define( const char *name )
137 {
138  struct define *def;
139 
140  for (def = cmdline_defines; def; def = def->next)
141  {
142  if (!strcmp( def->name, name ))
143  {
144  free( def->value );
145  def->value = NULL;
146  return;
147  }
148  }
149 }
150 
151 
152 /* add a command-line define of the form NAME=VALUE */
153 int wpp_add_cmdline_define( const char *value )
154 {
155  char *p;
156  char *str = pp_xstrdup(value);
157  if(!str)
158  return 1;
159  p = strchr( str, '=' );
160  if (p) *p++ = 0;
161  wpp_add_define( str, p );
162  free( str );
163  return 0;
164 }
165 
166 
167 /* set the various debug flags */
168 void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
169 {
170  pp_flex_debug = lex_debug;
172  pp_status.debug = msg_debug;
173 }
174 
175 
176 /* set the pedantic mode */
177 void wpp_set_pedantic( int on )
178 {
179  pp_status.pedantic = on;
180 }
181 
182 
183 /* the main preprocessor parsing loop */
184 int wpp_parse( const char *input, FILE *output )
185 {
186  int ret;
187 
188  pp_status.input = NULL;
191  pp_status.state = 0;
192 
194  if(ret)
195  return ret;
198 
199  if (!input) pp_status.file = stdin;
200  else if (!(pp_status.file = wpp_callbacks->open(input, 1)))
201  {
202  ppy_error("Could not open %s\n", input);
206  return 2;
207  }
208 
210 
211  ppy_out = output;
212  pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
213 
214  ret = ppy_parse();
215  /* If there were errors during processing, return an error code */
216  if (!ret && pp_status.state) ret = pp_status.state;
217 
218  if (input)
219  {
222  }
223  /* Clean if_stack, it could remain dirty on errors */
224  while (pp_get_if_depth()) pp_pop_if();
228  return ret;
229 }
230 
231 
233 {
235 }
void wpp_set_callbacks(const struct wpp_callbacks *callbacks)
Definition: wpp.c:232
int ppy_debug
Definition: wpp.c:31
char * name
Definition: wpp.c:36
static UCHAR ULONG UCHAR ULONG UCHAR * output
Definition: bcrypt.c:29
static void add_cmdline_defines(void)
Definition: wpp.c:42
void wpp_set_pedantic(int on)
Definition: wpp.c:177
void *(* open)(const char *filename, int type)
Definition: wpp.h:38
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define free
Definition: debug_ros.c:5
int line_number
Definition: wpp_private.h:233
void pp_writestring(const char *format,...)
Definition: ppl.yy.c:1351
int pp_get_if_depth(void)
Definition: preproc.c:681
FILE * stdin
char * value
Definition: wpp.c:37
__u16 time
Definition: mkdosfs.c:366
int wpp_add_define(const char *name, const char *value)
Definition: wpp.c:93
Definition: wpp.c:33
void(* close)(void *file)
Definition: wpp.h:40
pp_if_state_t pp_pop_if(void)
Definition: preproc.c:618
char * input
Definition: wpp_private.h:231
int wpp_add_cmdline_define(const char *value)
Definition: wpp.c:153
int ppy_parse(void)
void pp_pop_define_state(void)
Definition: preproc.c:287
time_t now
Definition: finger.c:65
static void del_cmdline_defines(void)
Definition: wpp.c:52
const WCHAR * str
int char_number
Definition: wpp_private.h:234
smooth NULL
Definition: ftsmooth.c:416
int wpp_parse(const char *input, FILE *output)
Definition: wpp.c:184
void * file
Definition: wpp_private.h:232
void * pp_xmalloc(size_t size)
Definition: preproc.c:77
int ppy_error(const char *s,...)
Definition: preproc.c:735
size_t CDECL strftime(char *str, size_t max, const char *format, const struct tm *mstm)
Definition: strftime.c:293
Definition: wpp_private.h:87
pp_entry_t * pp_add_define(const char *def, const char *text)
Definition: preproc.c:323
char * pp_xstrdup(const char *str)
Definition: preproc.c:105
int ret
void pp_del_define(const char *name)
Definition: preproc.c:302
int parser_debug
Definition: widl.c:111
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:424
FILE * ppy_out
Definition: ppl.yy.c:370
void wpp_del_define(const char *name)
Definition: wpp.c:136
GLenum GLenum GLenum input
Definition: glext.h:9031
__kernel_time_t time_t
Definition: linux.h:252
static void add_special_defines(void)
Definition: wpp.c:62
int pp_push_define_state(void)
Definition: preproc.c:274
char * strchr(const char *String, int ch)
Definition: utclib.c:501
int pp_flex_debug
Definition: wpp.c:31
struct define * next
Definition: wpp.c:35
def_type_t type
Definition: wpp_private.h:90
Definition: name.c:36
void wpp_set_debug(int lex_debug, int parser_debug, int msg_debug)
Definition: wpp.c:168
int pedantic
Definition: wpp_private.h:236
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
GLfloat GLfloat p
Definition: glext.h:8902
static int callbacks
Definition: xmllint.c:873
static void del_special_defines(void)
Definition: wpp.c:83
static struct define * cmdline_defines
Definition: wpp.c:40