Include <stdlib.h> needed by exit().
[wine/multimedia.git] / tools / wpp / wpp.c
blob7ceee3f3463b6e7f0b365b113ad7d4177a9ecfb3
1 /*
2 * Exported functions of the Wine preprocessor
4 * Copyrignt 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <time.h>
23 #include <stdlib.h>
25 #include "wpp_private.h"
26 #include "wpp.h"
28 static void add_special_defines(void)
30 time_t now = time(NULL);
31 pp_entry_t *ppp;
32 char buf[32];
34 strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
35 pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) );
37 strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
38 pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
40 ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
41 ppp->type = def_special;
43 ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
44 ppp->type = def_special;
48 /* add a define to the preprocessor list */
49 void wpp_add_define( const char *name, const char *value )
51 if (!value) value = "";
52 pp_add_define( pp_xstrdup(name), pp_xstrdup(value) );
56 /* add a command-line define of the form NAME=VALUE */
57 void wpp_add_cmdline_define( const char *value )
59 char *str = pp_xstrdup(value);
60 char *p = strchr( str, '=' );
61 if (p) *p++ = 0;
62 else p = "";
63 pp_add_define( str, pp_xstrdup(p) );
67 /* set the various debug flags */
68 void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
70 pp_flex_debug = lex_debug;
71 ppdebug = parser_debug;
72 pp_status.debug = msg_debug;
76 /* set the pedantic mode */
77 void wpp_set_pedantic( int on )
79 pp_status.pedantic = on;
83 /* the main preprocessor parsing loop */
84 int wpp_parse( const char *input, FILE *output )
86 int ret;
88 add_special_defines();
90 if (!input) ppin = stdin;
91 else if (!(ppin = fopen(input, "rt")))
93 fprintf(stderr,"Could not open %s\n", input);
94 exit(2);
97 pp_status.input = input;
99 ppout = output;
100 fprintf(ppout, "# 1 \"%s\" 1\n", input ? input : "");
102 ret = ppparse();
104 if (input) fclose(ppin);
105 return ret;
109 /* parse into a temporary file */
110 int wpp_parse_temp( const char *input, char **output_name )
112 char *temp_name;
113 FILE *output;
114 int ret;
116 if(!(temp_name = tmpnam(NULL)))
118 fprintf(stderr, "Could not generate a temp-name\n");
119 exit(2);
121 temp_name = pp_xstrdup(temp_name);
123 if (!(output = fopen(temp_name, "wt")))
125 fprintf(stderr,"Could not open %s for writing\n", temp_name);
126 exit(2);
129 *output_name = temp_name;
130 ret = wpp_parse( input, output );
131 fclose( output );
132 return ret;