2 * Exported functions of the Wine preprocessor
4 * Copyright 1998 Bertho A. Stultiens
5 * Copyright 2002 Alexandre Julliard
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.
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.
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
23 #include "wine/port.h"
28 #include "wpp_private.h"
31 int ppy_debug
, pp_flex_debug
;
40 static struct define
*cmdline_defines
;
42 static void add_cmdline_defines(void)
46 for (def
= cmdline_defines
; def
; def
= def
->next
)
48 if (def
->value
) pp_add_define( def
->name
, def
->value
);
52 static void del_cmdline_defines(void)
56 for (def
= cmdline_defines
; def
; def
= def
->next
)
58 if (def
->value
) pp_del_define( def
->name
);
62 static void add_special_defines(void)
64 time_t now
= time(NULL
);
68 strftime(buf
, sizeof(buf
), "\"%b %d %Y\"", localtime(&now
));
69 pp_add_define( "__DATE__", buf
);
71 strftime(buf
, sizeof(buf
), "\"%H:%M:%S\"", localtime(&now
));
72 pp_add_define( "__TIME__", buf
);
74 ppp
= pp_add_define( "__FILE__", "" );
76 ppp
->type
= def_special
;
78 ppp
= pp_add_define( "__LINE__", "" );
80 ppp
->type
= def_special
;
83 static void del_special_defines(void)
85 pp_del_define( "__DATE__" );
86 pp_del_define( "__TIME__" );
87 pp_del_define( "__FILE__" );
88 pp_del_define( "__LINE__" );
92 /* add a define to the preprocessor list */
93 int wpp_add_define( const char *name
, const char *value
)
97 if (!value
) value
= "";
99 for (def
= cmdline_defines
; def
; def
= def
->next
)
101 if (!strcmp( def
->name
, name
))
103 char *new_value
= pp_xstrdup(value
);
107 def
->value
= new_value
;
113 def
= pp_xmalloc( sizeof(*def
) );
116 def
->next
= cmdline_defines
;
117 def
->name
= pp_xstrdup(name
);
123 def
->value
= pp_xstrdup(value
);
130 cmdline_defines
= def
;
135 /* undefine a previously added definition */
136 void wpp_del_define( const char *name
)
140 for (def
= cmdline_defines
; def
; def
= def
->next
)
142 if (!strcmp( def
->name
, name
))
152 /* add a command-line define of the form NAME=VALUE */
153 int wpp_add_cmdline_define( const char *value
)
156 char *str
= pp_xstrdup(value
);
159 p
= strchr( str
, '=' );
161 wpp_add_define( str
, p
);
167 /* set the various debug flags */
168 void wpp_set_debug( int lex_debug
, int parser_debug
, int msg_debug
)
170 pp_flex_debug
= lex_debug
;
171 ppy_debug
= parser_debug
;
172 pp_status
.debug
= msg_debug
;
176 /* set the pedantic mode */
177 void wpp_set_pedantic( int on
)
179 pp_status
.pedantic
= on
;
183 /* the main preprocessor parsing loop */
184 int wpp_parse( const char *input
, FILE *output
)
188 pp_status
.input
= NULL
;
189 pp_status
.line_number
= 1;
190 pp_status
.char_number
= 1;
193 ret
= pp_push_define_state();
196 add_cmdline_defines();
197 add_special_defines();
199 if (!input
) pp_status
.file
= stdin
;
200 else if (!(pp_status
.file
= fopen(input
, "rt")))
202 ppy_error("Could not open %s\n", input
);
203 del_special_defines();
204 del_cmdline_defines();
205 pp_pop_define_state();
209 pp_status
.input
= input
? pp_xstrdup(input
) : NULL
;
212 pp_writestring("# 1 \"%s\" 1\n", input
? input
: "");
215 /* If there were errors during processing, return an error code */
216 if (!ret
&& pp_status
.state
) ret
= pp_status
.state
;
220 fclose(pp_status
.file
);
221 free(pp_status
.input
);
223 /* Clean if_stack, it could remain dirty on errors */
224 while (pp_get_if_depth()) pp_pop_if();
225 del_special_defines();
226 del_cmdline_defines();
227 pp_pop_define_state();