~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/libs/wpp/wpp.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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 
 31 int ppy_debug, pp_flex_debug;
 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     if(ppp)
 66         ppp->type = def_special;
 67 
 68     ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
 69     if(ppp)
 70         ppp->type = def_special;
 71 }
 72 
 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 
 79     if (!value) value = "";
 80 
 81     for (def = cmdline_defines; def; def = def->next)
 82     {
 83         if (!strcmp( def->name, name ))
 84         {
 85             char *new_value = pp_xstrdup(value);
 86             if(!new_value)
 87                 return 1;
 88             free( def->value );
 89             def->value = new_value;
 90 
 91             return 0;
 92         }
 93     }
 94 
 95     def = pp_xmalloc( sizeof(*def) );
 96     if(!def)
 97         return 1;
 98     def->next  = cmdline_defines;
 99     def->name  = pp_xstrdup(name);
100     if(!def->name)
101     {
102         free(def);
103         return 1;
104     }
105     def->value = pp_xstrdup(value);
106     if(!def->value)
107     {
108         free(def->name);
109         free(def);
110         return 1;
111     }
112     cmdline_defines = def;
113     return 0;
114 }
115 
116 
117 /* undefine a previously added definition */
118 void wpp_del_define( const char *name )
119 {
120     struct define *def;
121 
122     for (def = cmdline_defines; def; def = def->next)
123     {
124         if (!strcmp( def->name, name ))
125         {
126             free( def->value );
127             def->value = NULL;
128             return;
129         }
130     }
131 }
132 
133 
134 /* add a command-line define of the form NAME=VALUE */
135 int wpp_add_cmdline_define( const char *value )
136 {
137     char *p;
138     char *str = pp_xstrdup(value);
139     if(!str)
140         return 1;
141     p = strchr( str, '=' );
142     if (p) *p++ = 0;
143     wpp_add_define( str, p );
144     free( str );
145     return 0;
146 }
147 
148 
149 /* set the various debug flags */
150 void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
151 {
152     pp_flex_debug   = lex_debug;
153     ppy_debug       = parser_debug;
154     pp_status.debug = msg_debug;
155 }
156 
157 
158 /* set the pedantic mode */
159 void wpp_set_pedantic( int on )
160 {
161     pp_status.pedantic = on;
162 }
163 
164 
165 /* the main preprocessor parsing loop */
166 int wpp_parse( const char *input, FILE *output )
167 {
168     int ret;
169 
170     pp_status.input = NULL;
171     pp_status.state = 0;
172 
173     ret = pp_push_define_state();
174     if(ret)
175         return ret;
176     add_cmdline_defines();
177     add_special_defines();
178 
179     if (!input) pp_status.file = stdin;
180     else if (!(pp_status.file = wpp_callbacks->open(input, 1)))
181     {
182         ppy_error("Could not open %s\n", input);
183         return 2;
184     }
185 
186     pp_status.input = input;
187 
188     ppy_out = output;
189     pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
190 
191     ret = ppy_parse();
192     /* If there were errors during processing, return an error code */
193     if(!ret && pp_status.state) ret = pp_status.state;
194 
195     if (input) wpp_callbacks->close(pp_status.file);
196     pp_pop_define_state();
197     return ret;
198 }
199 
200 
201 /* parse into a temporary file */
202 int wpp_parse_temp( const char *input, const char *output_base, char **output_name )
203 {
204     FILE *output;
205     int ret, fd;
206     char *temp_name;
207 
208     if (!output_base || !output_base[0]) output_base = "wpptmp";
209 
210     temp_name = pp_xmalloc( strlen(output_base) + 8 );
211     if(!temp_name)
212         return 1;
213     strcpy( temp_name, output_base );
214     strcat( temp_name, ".XXXXXX" );
215 
216     if((fd = mkstemps( temp_name, 0 )) == -1)
217     {
218         ppy_error("Could not generate a temp name from %s\n", temp_name);
219         free( temp_name );
220         return 2;
221     }
222 
223     if (!(output = fdopen(fd, "wt")))
224     {
225         ppy_error("Could not open fd %s for writing\n", temp_name);
226         close( fd );
227         unlink( temp_name );
228         free( temp_name );
229         return 2;
230     }
231 
232     *output_name = temp_name;
233     ret = wpp_parse( input, output );
234     fclose( output );
235     return ret;
236 }
237 
238 void wpp_set_callbacks( const struct wpp_callbacks *callbacks )
239 {
240     wpp_callbacks = callbacks;
241 }
242 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.