Added a __wine_dbg_set_channel_flags function to allow changing flags
[wine] / libs / wpp / wpp.c
1 /*
2  * Exported functions of the Wine preprocessor
3  *
4  * Copyrignt 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
31 int ppdebug;
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( pp_xstrdup(def->name), pp_xstrdup(def->value) );
49     }
50 }
51
52 static void add_special_defines(void)
53 {
54     time_t now = time(NULL);
55     pp_entry_t *ppp;
56     char buf[32];
57
58     strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
59     pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) );
60
61     strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
62     pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
63
64     ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
65     ppp->type = def_special;
66
67     ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
68     ppp->type = def_special;
69 }
70
71
72 /* add a define to the preprocessor list */
73 void wpp_add_define( const char *name, const char *value )
74 {
75     struct define *def;
76
77     if (!value) value = "";
78
79     for (def = cmdline_defines; def; def = def->next)
80     {
81         if (!strcmp( def->name, name ))
82         {
83             if (def->value) free( def->value );
84             def->value = pp_xstrdup(value);
85             return;
86         }
87     }
88
89     def = pp_xmalloc( sizeof(*def) );
90     def->next  = cmdline_defines;
91     def->name  = pp_xstrdup(name);
92     def->value = pp_xstrdup(value);
93     cmdline_defines = def;
94 }
95
96
97 /* undefine a previously added definition */
98 void wpp_del_define( const char *name )
99 {
100     struct define *def;
101
102     for (def = cmdline_defines; def; def = def->next)
103     {
104         if (!strcmp( def->name, name ))
105         {
106             if (def->value) free( def->value );
107             def->value = NULL;
108             return;
109         }
110     }
111 }
112
113
114 /* add a command-line define of the form NAME=VALUE */
115 void wpp_add_cmdline_define( const char *value )
116 {
117     char *str = pp_xstrdup(value);
118     char *p = strchr( str, '=' );
119     if (p) *p++ = 0;
120     wpp_add_define( str, p );
121     free( str );
122 }
123
124
125 /* set the various debug flags */
126 void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
127 {
128     pp_flex_debug   = lex_debug;
129     ppdebug         = parser_debug;
130     pp_status.debug = msg_debug;
131 }
132
133
134 /* set the pedantic mode */
135 void wpp_set_pedantic( int on )
136 {
137     pp_status.pedantic = on;
138 }
139
140
141 /* the main preprocessor parsing loop */
142 int wpp_parse( const char *input, FILE *output )
143 {
144     int ret;
145
146     pp_status.input = NULL;
147
148     pp_push_define_state();
149     add_cmdline_defines();
150     add_special_defines();
151
152     if (!input) ppin = stdin;
153     else if (!(ppin = fopen(input, "rt")))
154     {
155         fprintf(stderr,"Could not open %s\n", input);
156         exit(2);
157     }
158
159     pp_status.input = input;
160
161     ppout = output;
162     fprintf(ppout, "# 1 \"%s\" 1\n", input ? input : "");
163
164     ret = ppparse();
165
166     if (input) fclose(ppin);
167     pp_pop_define_state();
168     return ret;
169 }
170
171
172 /* parse into a temporary file */
173 int wpp_parse_temp( const char *input, const char *output_base, char **output_name )
174 {
175     FILE *output;
176     int ret, fd;
177     char *temp_name;
178
179     if (!output_base || !output_base[0]) output_base = "wpptmp";
180
181     temp_name = pp_xmalloc( strlen(output_base) + 8 );
182     strcpy( temp_name, output_base );
183     strcat( temp_name, ".XXXXXX" );
184
185     if((fd = mkstemps( temp_name, 0 )) == -1)
186     {
187         fprintf(stderr, "Could not generate a temp name from %s\n", temp_name);
188         exit(2);
189     }
190
191     if (!(output = fdopen(fd, "wt")))
192     {
193         fprintf(stderr,"Could not open fd %s for writing\n", temp_name);
194         exit(2);
195     }
196
197     *output_name = temp_name;
198     ret = wpp_parse( input, output );
199     fclose( output );
200     return ret;
201 }