2002-07-16 02:33:10 +00:00
|
|
|
/*
|
|
|
|
* Exported functions of the Wine preprocessor
|
|
|
|
*
|
2008-01-16 11:20:50 +00:00
|
|
|
* Copyright 1998 Bertho A. Stultiens
|
2002-07-16 02:33:10 +00:00
|
|
|
* Copyright 2002 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 12:49:52 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-07-16 02:33:10 +00:00
|
|
|
*/
|
|
|
|
|
2002-07-29 23:55:39 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2002-07-16 02:33:10 +00:00
|
|
|
#include <time.h>
|
2002-07-29 00:15:31 +00:00
|
|
|
#include <stdlib.h>
|
2002-07-16 02:33:10 +00:00
|
|
|
|
|
|
|
#include "wpp_private.h"
|
2003-05-01 03:16:21 +00:00
|
|
|
#include "wine/wpp.h"
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2006-09-12 07:04:55 +00:00
|
|
|
int ppy_debug, pp_flex_debug;
|
2002-12-19 23:38:11 +00:00
|
|
|
|
2003-07-01 04:36:22 +00:00
|
|
|
struct define
|
|
|
|
{
|
|
|
|
struct define *next;
|
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct define *cmdline_defines;
|
|
|
|
|
|
|
|
static void add_cmdline_defines(void)
|
|
|
|
{
|
|
|
|
struct define *def;
|
|
|
|
|
|
|
|
for (def = cmdline_defines; def; def = def->next)
|
|
|
|
{
|
|
|
|
if (def->value) pp_add_define( pp_xstrdup(def->name), pp_xstrdup(def->value) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-16 02:33:10 +00:00
|
|
|
static void add_special_defines(void)
|
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
pp_entry_t *ppp;
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
strftime(buf, sizeof(buf), "\"%b %d %Y\"", localtime(&now));
|
|
|
|
pp_add_define( pp_xstrdup("__DATE__"), pp_xstrdup(buf) );
|
|
|
|
|
|
|
|
strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
|
|
|
|
pp_add_define( pp_xstrdup("__TIME__"), pp_xstrdup(buf) );
|
|
|
|
|
|
|
|
ppp = pp_add_define( pp_xstrdup("__FILE__"), pp_xstrdup("") );
|
2009-09-28 19:32:39 +00:00
|
|
|
if(ppp)
|
|
|
|
ppp->type = def_special;
|
2002-07-16 02:33:10 +00:00
|
|
|
|
|
|
|
ppp = pp_add_define( pp_xstrdup("__LINE__"), pp_xstrdup("") );
|
2009-09-28 19:32:39 +00:00
|
|
|
if(ppp)
|
|
|
|
ppp->type = def_special;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* add a define to the preprocessor list */
|
2009-09-28 19:32:39 +00:00
|
|
|
int wpp_add_define( const char *name, const char *value )
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
2003-07-01 04:36:22 +00:00
|
|
|
struct define *def;
|
|
|
|
|
2002-07-16 02:33:10 +00:00
|
|
|
if (!value) value = "";
|
2003-07-01 04:36:22 +00:00
|
|
|
|
|
|
|
for (def = cmdline_defines; def; def = def->next)
|
|
|
|
{
|
|
|
|
if (!strcmp( def->name, name ))
|
|
|
|
{
|
2009-09-28 19:32:39 +00:00
|
|
|
char *new_value = pp_xstrdup(value);
|
|
|
|
if(!new_value)
|
|
|
|
return 1;
|
2006-10-09 21:35:08 +00:00
|
|
|
free( def->value );
|
2009-09-28 19:32:39 +00:00
|
|
|
def->value = new_value;
|
|
|
|
|
|
|
|
return 0;
|
2003-07-01 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def = pp_xmalloc( sizeof(*def) );
|
2009-09-28 19:32:39 +00:00
|
|
|
if(!def)
|
|
|
|
return 1;
|
2003-07-01 04:36:22 +00:00
|
|
|
def->next = cmdline_defines;
|
|
|
|
def->name = pp_xstrdup(name);
|
2009-09-28 19:32:39 +00:00
|
|
|
if(!def->name)
|
|
|
|
{
|
|
|
|
free(def);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-07-01 04:36:22 +00:00
|
|
|
def->value = pp_xstrdup(value);
|
2009-09-28 19:32:39 +00:00
|
|
|
if(!def->value)
|
|
|
|
{
|
|
|
|
free(def->name);
|
|
|
|
free(def);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-07-01 04:36:22 +00:00
|
|
|
cmdline_defines = def;
|
2009-09-28 19:32:39 +00:00
|
|
|
return 0;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-27 18:50:14 +00:00
|
|
|
/* undefine a previously added definition */
|
2003-07-01 04:36:22 +00:00
|
|
|
void wpp_del_define( const char *name )
|
2003-03-27 18:50:14 +00:00
|
|
|
{
|
2003-07-01 04:36:22 +00:00
|
|
|
struct define *def;
|
|
|
|
|
|
|
|
for (def = cmdline_defines; def; def = def->next)
|
|
|
|
{
|
|
|
|
if (!strcmp( def->name, name ))
|
|
|
|
{
|
2006-10-09 21:35:08 +00:00
|
|
|
free( def->value );
|
2003-07-01 04:36:22 +00:00
|
|
|
def->value = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2003-03-27 18:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-16 02:33:10 +00:00
|
|
|
/* add a command-line define of the form NAME=VALUE */
|
2009-09-28 19:32:39 +00:00
|
|
|
int wpp_add_cmdline_define( const char *value )
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
2009-09-28 19:32:39 +00:00
|
|
|
char *p;
|
2002-07-16 02:33:10 +00:00
|
|
|
char *str = pp_xstrdup(value);
|
2009-09-28 19:32:39 +00:00
|
|
|
if(!str)
|
|
|
|
return 1;
|
|
|
|
p = strchr( str, '=' );
|
2002-07-16 02:33:10 +00:00
|
|
|
if (p) *p++ = 0;
|
2003-07-01 04:36:22 +00:00
|
|
|
wpp_add_define( str, p );
|
|
|
|
free( str );
|
2009-09-28 19:32:39 +00:00
|
|
|
return 0;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set the various debug flags */
|
|
|
|
void wpp_set_debug( int lex_debug, int parser_debug, int msg_debug )
|
|
|
|
{
|
|
|
|
pp_flex_debug = lex_debug;
|
2006-09-12 07:04:55 +00:00
|
|
|
ppy_debug = parser_debug;
|
2002-07-16 02:33:10 +00:00
|
|
|
pp_status.debug = msg_debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* set the pedantic mode */
|
|
|
|
void wpp_set_pedantic( int on )
|
|
|
|
{
|
|
|
|
pp_status.pedantic = on;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* the main preprocessor parsing loop */
|
|
|
|
int wpp_parse( const char *input, FILE *output )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2005-01-09 16:53:47 +00:00
|
|
|
pp_status.input = NULL;
|
2010-01-04 12:00:43 +00:00
|
|
|
pp_status.line_number = 0;
|
|
|
|
pp_status.char_number = 0;
|
2009-09-28 12:21:00 +00:00
|
|
|
pp_status.state = 0;
|
2005-01-09 16:53:47 +00:00
|
|
|
|
2009-09-28 19:32:39 +00:00
|
|
|
ret = pp_push_define_state();
|
|
|
|
if(ret)
|
|
|
|
return ret;
|
2003-07-01 04:36:22 +00:00
|
|
|
add_cmdline_defines();
|
2002-07-16 02:33:10 +00:00
|
|
|
add_special_defines();
|
|
|
|
|
2009-09-30 18:15:38 +00:00
|
|
|
if (!input) pp_status.file = stdin;
|
|
|
|
else if (!(pp_status.file = wpp_callbacks->open(input, 1)))
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
2009-09-28 12:21:00 +00:00
|
|
|
ppy_error("Could not open %s\n", input);
|
2009-12-26 15:19:06 +00:00
|
|
|
pp_pop_define_state();
|
2009-09-28 12:21:00 +00:00
|
|
|
return 2;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pp_status.input = input;
|
|
|
|
|
2006-09-12 07:04:55 +00:00
|
|
|
ppy_out = output;
|
2009-09-30 18:15:38 +00:00
|
|
|
pp_writestring("# 1 \"%s\" 1\n", input ? input : "");
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2006-09-12 07:04:55 +00:00
|
|
|
ret = ppy_parse();
|
2009-09-28 12:21:00 +00:00
|
|
|
/* If there were errors during processing, return an error code */
|
2010-02-04 17:56:52 +00:00
|
|
|
if (!ret && pp_status.state) ret = pp_status.state;
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2009-09-30 18:15:38 +00:00
|
|
|
if (input) wpp_callbacks->close(pp_status.file);
|
2010-02-04 17:56:52 +00:00
|
|
|
/* Clean if_stack, it could remain dirty on errors */
|
|
|
|
while (pp_get_if_depth()) pp_pop_if();
|
2003-07-01 04:36:22 +00:00
|
|
|
pp_pop_define_state();
|
2002-07-16 02:33:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* parse into a temporary file */
|
2002-08-27 22:29:26 +00:00
|
|
|
int wpp_parse_temp( const char *input, const char *output_base, char **output_name )
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
|
|
|
FILE *output;
|
2002-07-29 23:55:39 +00:00
|
|
|
int ret, fd;
|
2002-08-27 22:29:26 +00:00
|
|
|
char *temp_name;
|
2002-07-29 23:55:39 +00:00
|
|
|
|
2002-08-27 22:29:26 +00:00
|
|
|
if (!output_base || !output_base[0]) output_base = "wpptmp";
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2002-08-27 22:29:26 +00:00
|
|
|
temp_name = pp_xmalloc( strlen(output_base) + 8 );
|
2009-09-28 19:32:39 +00:00
|
|
|
if(!temp_name)
|
|
|
|
return 1;
|
2002-08-27 22:29:26 +00:00
|
|
|
strcpy( temp_name, output_base );
|
|
|
|
strcat( temp_name, ".XXXXXX" );
|
|
|
|
|
2003-03-20 21:07:49 +00:00
|
|
|
if((fd = mkstemps( temp_name, 0 )) == -1)
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
2009-09-28 12:21:00 +00:00
|
|
|
ppy_error("Could not generate a temp name from %s\n", temp_name);
|
2009-10-07 06:59:22 +00:00
|
|
|
free( temp_name );
|
2009-09-28 12:21:00 +00:00
|
|
|
return 2;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
2002-07-29 23:55:39 +00:00
|
|
|
if (!(output = fdopen(fd, "wt")))
|
2002-07-16 02:33:10 +00:00
|
|
|
{
|
2009-09-28 12:21:00 +00:00
|
|
|
ppy_error("Could not open fd %s for writing\n", temp_name);
|
2009-10-07 06:59:22 +00:00
|
|
|
close( fd );
|
|
|
|
unlink( temp_name );
|
|
|
|
free( temp_name );
|
2009-09-28 12:21:00 +00:00
|
|
|
return 2;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*output_name = temp_name;
|
|
|
|
ret = wpp_parse( input, output );
|
|
|
|
fclose( output );
|
|
|
|
return ret;
|
|
|
|
}
|
2009-09-29 10:34:17 +00:00
|
|
|
|
|
|
|
void wpp_set_callbacks( const struct wpp_callbacks *callbacks )
|
|
|
|
{
|
|
|
|
wpp_callbacks = callbacks;
|
|
|
|
}
|