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( pp_xstrdup(def
->name
), pp_xstrdup(def
->value
) );
52 static void add_special_defines(void)
54 time_t now
= time(NULL
);
58 strftime(buf
, sizeof(buf
), "\"%b %d %Y\"", localtime(&now
));
59 pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf
) );
61 strftime(buf
, sizeof(buf
), "\"%H:%M:%S\"", localtime(&now
));
62 pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf
) );
64 ppp
= pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
66 ppp
->type
= def_special
;
68 ppp
= pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
70 ppp
->type
= def_special
;
74 /* add a define to the preprocessor list */
75 int wpp_add_define( const char *name
, const char *value
)
79 if (!value
) value
= "";
81 for (def
= cmdline_defines
; def
; def
= def
->next
)
83 if (!strcmp( def
->name
, name
))
85 char *new_value
= pp_xstrdup(value
);
89 def
->value
= new_value
;
95 def
= pp_xmalloc( sizeof(*def
) );
98 def
->next
= cmdline_defines
;
99 def
->name
= pp_xstrdup(name
);
105 def
->value
= pp_xstrdup(value
);
112 cmdline_defines
= def
;
117 /* undefine a previously added definition */
118 void wpp_del_define( const char *name
)
122 for (def
= cmdline_defines
; def
; def
= def
->next
)
124 if (!strcmp( def
->name
, name
))
134 /* add a command-line define of the form NAME=VALUE */
135 int wpp_add_cmdline_define( const char *value
)
138 char *str
= pp_xstrdup(value
);
141 p
= strchr( str
, '=' );
143 wpp_add_define( str
, p
);
149 /* set the various debug flags */
150 void wpp_set_debug( int lex_debug
, int parser_debug
, int msg_debug
)
152 pp_flex_debug
= lex_debug
;
153 ppy_debug
= parser_debug
;
154 pp_status
.debug
= msg_debug
;
158 /* set the pedantic mode */
159 void wpp_set_pedantic( int on
)
161 pp_status
.pedantic
= on
;
165 /* the main preprocessor parsing loop */
166 int wpp_parse( const char *input
, FILE *output
)
170 pp_status
.input
= NULL
;
171 pp_status
.line_number
= 0;
172 pp_status
.char_number
= 0;
175 ret
= pp_push_define_state();
178 add_cmdline_defines();
179 add_special_defines();
181 if (!input
) pp_status
.file
= stdin
;
182 else if (!(pp_status
.file
= wpp_callbacks
->open(input
, 1)))
184 ppy_error("Could not open %s\n", input
);
185 pp_pop_define_state();
189 pp_status
.input
= input
;
192 pp_writestring("# 1 \"%s\" 1\n", input
? input
: "");
195 /* If there were errors during processing, return an error code */
196 if (!ret
&& pp_status
.state
) ret
= pp_status
.state
;
198 if (input
) wpp_callbacks
->close(pp_status
.file
);
199 /* Clean if_stack, it could remain dirty on errors */
200 while (pp_get_if_depth()) pp_pop_if();
201 pp_pop_define_state();
206 /* parse into a temporary file */
207 int wpp_parse_temp( const char *input
, const char *output_base
, char **output_name
)
213 if (!output_base
|| !output_base
[0]) output_base
= "wpptmp";
215 temp_name
= pp_xmalloc( strlen(output_base
) + 8 );
218 strcpy( temp_name
, output_base
);
219 strcat( temp_name
, ".XXXXXX" );
221 if((fd
= mkstemps( temp_name
, 0 )) == -1)
223 ppy_error("Could not generate a temp name from %s\n", temp_name
);
228 if (!(output
= fdopen(fd
, "wt")))
230 ppy_error("Could not open fd %s for writing\n", temp_name
);
237 *output_name
= temp_name
;
238 ret
= wpp_parse( input
, output
);
243 void wpp_set_callbacks( const struct wpp_callbacks
*callbacks
)
245 wpp_callbacks
= callbacks
;