Fix for missing ppdebug variable.
[wine] / tools / 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 "wpp.h"
30
31 int ppdebug;
32
33 static void add_special_defines(void)
34 {
35     time_t now = time(NULL);
36     pp_entry_t *ppp;
37     char buf[32];
38
39     strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
40     pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) );
41
42     strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
43     pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
44
45     ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
46     ppp->type = def_special;
47
48     ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
49     ppp->type = def_special;
50 }
51
52
53 /* add a define to the preprocessor list */
54 void wpp_add_define( const char *name, const char *value )
55 {
56     if (!value) value = "";
57     pp_add_define( pp_xstrdup(name), pp_xstrdup(value) );
58 }
59
60
61 /* add a command-line define of the form NAME=VALUE */
62 void wpp_add_cmdline_define( const char *value )
63 {
64     char *str = pp_xstrdup(value);
65     char *p = strchr( str, '=' );
66     if (p) *p++ = 0;
67     else p = "";
68     pp_add_define( str, pp_xstrdup(p) );
69 }
70
71
72 /* set the various debug flags */
73 void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
74 {
75     pp_flex_debug   = lex_debug;
76     ppdebug         = parser_debug;
77     pp_status.debug = msg_debug;
78 }
79
80
81 /* set the pedantic mode */
82 void wpp_set_pedantic( int on )
83 {
84     pp_status.pedantic = on;
85 }
86
87
88 /* the main preprocessor parsing loop */
89 int wpp_parse( const char *input, FILE *output )
90 {
91     int ret;
92
93     add_special_defines();
94
95     if (!input) ppin = stdin;
96     else if (!(ppin = fopen(input, "rt")))
97     {
98         fprintf(stderr,"Could not open %s\n", input);
99         exit(2);
100     }
101
102     pp_status.input = input;
103
104     ppout = output;
105     fprintf(ppout, "# 1 \"%s\" 1\n", input ? input : "");
106
107     ret = ppparse();
108
109     if (input) fclose(ppin);
110     return ret;
111 }
112
113
114 /* parse into a temporary file */
115 int wpp_parse_temp( const char *input, const char *output_base, char **output_name )
116 {
117     FILE *output;
118     int ret, fd;
119     char *temp_name;
120
121     if (!output_base || !output_base[0]) output_base = "wpptmp";
122
123     temp_name = pp_xmalloc( strlen(output_base) + 8 );
124     strcpy( temp_name, output_base );
125     strcat( temp_name, ".XXXXXX" );
126
127     if((fd = mkstemp( temp_name )) == -1)
128     {
129         fprintf(stderr, "Could not generate a temp name from %s\n", temp_name);
130         exit(2);
131     }
132
133     if (!(output = fdopen(fd, "wt")))
134     {
135         fprintf(stderr,"Could not open fd %s for writing\n", temp_name);
136         exit(2);
137     }
138
139     *output_name = temp_name;
140     ret = wpp_parse( input, output );
141     fclose( output );
142     return ret;
143 }