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"
|
|
|
|
|
2020-12-03 14:01:54 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2002-07-29 00:15:31 +00:00
|
|
|
#include <stdlib.h>
|
2020-12-03 14:01:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <time.h>
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2021-09-29 07:32:01 +00:00
|
|
|
#include "../tools.h"
|
2021-09-24 06:06:03 +00:00
|
|
|
#include "utils.h"
|
2002-07-16 02:33:10 +00:00
|
|
|
#include "wpp_private.h"
|
|
|
|
|
2020-12-03 14:01:54 +00:00
|
|
|
struct pp_status pp_status;
|
|
|
|
|
|
|
|
#define HASHKEY 2039
|
|
|
|
|
|
|
|
static struct list pp_defines[HASHKEY];
|
|
|
|
|
|
|
|
#define MAXIFSTACK 64
|
|
|
|
static pp_if_state_t if_stack[MAXIFSTACK];
|
|
|
|
static int if_stack_idx = 0;
|
|
|
|
|
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
|
|
|
|
{
|
2020-12-03 12:27:15 +00:00
|
|
|
struct list entry;
|
2003-07-01 04:36:22 +00:00
|
|
|
char *name;
|
|
|
|
char *value;
|
|
|
|
};
|
|
|
|
|
2020-12-03 12:27:15 +00:00
|
|
|
static struct list cmdline_defines = LIST_INIT( cmdline_defines );
|
2021-11-12 10:39:31 +00:00
|
|
|
static struct strarray includes;
|
2003-07-01 04:36:22 +00:00
|
|
|
|
2021-11-12 10:39:31 +00:00
|
|
|
static char *wpp_lookup(const char *name, int type, const char *parent_name)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
|
|
|
char *cpy;
|
|
|
|
char *cptr;
|
|
|
|
char *path;
|
|
|
|
const char *ccptr;
|
|
|
|
int i, fd;
|
|
|
|
|
2021-09-24 06:06:03 +00:00
|
|
|
cpy = xmalloc(strlen(name)+1);
|
2020-12-03 14:01:54 +00:00
|
|
|
cptr = cpy;
|
|
|
|
|
|
|
|
for(ccptr = name; *ccptr; ccptr++)
|
|
|
|
{
|
|
|
|
/* Convert to forward slash */
|
|
|
|
if(*ccptr == '\\') {
|
|
|
|
/* kill double backslash */
|
|
|
|
if(ccptr[1] == '\\')
|
|
|
|
ccptr++;
|
|
|
|
*cptr = '/';
|
|
|
|
}else {
|
|
|
|
*cptr = *ccptr;
|
|
|
|
}
|
|
|
|
cptr++;
|
|
|
|
}
|
|
|
|
*cptr = '\0';
|
|
|
|
|
|
|
|
if(type && parent_name)
|
|
|
|
{
|
|
|
|
/* Search directory of parent include and then -I path */
|
2021-09-29 09:13:36 +00:00
|
|
|
path = strmake( "%s/%s", get_dirname(parent_name), cpy );
|
2020-12-03 14:01:54 +00:00
|
|
|
fd = open( path, O_RDONLY );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
free( cpy );
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
free( path );
|
|
|
|
}
|
|
|
|
/* Search -I path */
|
2021-11-12 10:39:31 +00:00
|
|
|
for(i = 0; i < includes.count; i++)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
2021-11-12 10:39:31 +00:00
|
|
|
path = strmake("%s/%s", includes.str[i], cpy);
|
2020-12-03 14:01:54 +00:00
|
|
|
fd = open( path, O_RDONLY );
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
close( fd );
|
|
|
|
free( cpy );
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
free( path );
|
|
|
|
}
|
|
|
|
free( cpy );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't comment on the hash, it's primitive but functional... */
|
|
|
|
static int pphash(const char *str)
|
|
|
|
{
|
|
|
|
int sum = 0;
|
|
|
|
while(*str)
|
|
|
|
sum += *str++;
|
|
|
|
return sum % HASHKEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_entry_t *pplookup(const char *ident)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
pp_entry_t *ppp;
|
|
|
|
|
|
|
|
if(!ident)
|
|
|
|
return NULL;
|
|
|
|
idx = pphash(ident);
|
|
|
|
LIST_FOR_EACH_ENTRY( ppp, &pp_defines[idx], pp_entry_t, entry )
|
|
|
|
{
|
|
|
|
if(!strcmp(ident, ppp->ident))
|
|
|
|
return ppp;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_pp_entry( pp_entry_t *ppp, int idx )
|
|
|
|
{
|
|
|
|
if(ppp->iep)
|
|
|
|
{
|
|
|
|
list_remove( &ppp->iep->entry );
|
|
|
|
free(ppp->iep->filename);
|
|
|
|
free(ppp->iep);
|
|
|
|
}
|
|
|
|
list_remove( &ppp->entry );
|
|
|
|
free(ppp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the define state */
|
2021-09-24 06:06:03 +00:00
|
|
|
static void pp_init_define_state(void)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < HASHKEY; i++) list_init( &pp_defines[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free the current define state */
|
2021-09-24 06:06:03 +00:00
|
|
|
static void pp_free_define_state(void)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
pp_entry_t *ppp, *ppp2;
|
|
|
|
|
|
|
|
for (i = 0; i < HASHKEY; i++)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( ppp, ppp2, &pp_defines[i], pp_entry_t, entry )
|
|
|
|
{
|
|
|
|
free( ppp->ident );
|
|
|
|
free( ppp->subst.text );
|
|
|
|
free( ppp->filename );
|
|
|
|
free_pp_entry( ppp, i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pp_del_define(const char *name)
|
|
|
|
{
|
|
|
|
pp_entry_t *ppp;
|
|
|
|
int idx = pphash(name);
|
|
|
|
|
|
|
|
if((ppp = pplookup(name)) == NULL)
|
|
|
|
{
|
2021-09-24 07:50:54 +00:00
|
|
|
if(pedantic)
|
2020-12-03 14:01:54 +00:00
|
|
|
ppy_warning("%s was not defined", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pp_status.debug)
|
|
|
|
printf("Deleting (%s, %d) <%s>\n", pp_status.input, pp_status.line_number, name);
|
|
|
|
|
|
|
|
free( ppp->ident );
|
|
|
|
free( ppp->subst.text );
|
|
|
|
free( ppp->filename );
|
|
|
|
free_pp_entry( ppp, idx );
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_entry_t *pp_add_define(const char *def, const char *text)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *cptr;
|
|
|
|
int idx;
|
|
|
|
pp_entry_t *ppp;
|
|
|
|
|
|
|
|
idx = pphash(def);
|
|
|
|
if((ppp = pplookup(def)) != NULL)
|
|
|
|
{
|
2021-09-24 07:50:54 +00:00
|
|
|
if(pedantic)
|
|
|
|
ppy_warning("Redefinition of %s\n%s:%d: note: previous definition was here",
|
|
|
|
def, ppp->filename, ppp->linenumber);
|
2020-12-03 14:01:54 +00:00
|
|
|
pp_del_define(def);
|
|
|
|
}
|
2021-09-24 06:06:03 +00:00
|
|
|
ppp = xmalloc(sizeof(pp_entry_t));
|
2020-12-03 14:01:54 +00:00
|
|
|
memset( ppp, 0, sizeof(*ppp) );
|
2021-09-24 06:06:03 +00:00
|
|
|
ppp->ident = xstrdup(def);
|
2020-12-03 14:01:54 +00:00
|
|
|
ppp->type = def_define;
|
2021-09-24 06:06:03 +00:00
|
|
|
ppp->subst.text = text ? xstrdup(text) : NULL;
|
|
|
|
ppp->filename = xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
|
2020-12-03 14:01:54 +00:00
|
|
|
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
|
|
|
|
list_add_head( &pp_defines[idx], &ppp->entry );
|
|
|
|
if(ppp->subst.text)
|
|
|
|
{
|
|
|
|
/* Strip trailing white space from subst text */
|
|
|
|
len = strlen(ppp->subst.text);
|
|
|
|
while(len && strchr(" \t\r\n", ppp->subst.text[len-1]))
|
|
|
|
{
|
|
|
|
ppp->subst.text[--len] = '\0';
|
|
|
|
}
|
|
|
|
/* Strip leading white space from subst text */
|
|
|
|
for(cptr = ppp->subst.text; *cptr && strchr(" \t\r", *cptr); cptr++)
|
|
|
|
;
|
|
|
|
if(ppp->subst.text != cptr)
|
|
|
|
memmove(ppp->subst.text, cptr, strlen(cptr)+1);
|
|
|
|
}
|
|
|
|
if(pp_status.debug)
|
|
|
|
printf("Added define (%s, %d) <%s> to <%s>\n", pp_status.input, pp_status.line_number, ppp->ident, ppp->subst.text ? ppp->subst.text : "(null)");
|
|
|
|
|
|
|
|
return ppp;
|
|
|
|
}
|
|
|
|
|
2022-04-29 04:13:15 +00:00
|
|
|
pp_entry_t *pp_add_macro(char *id, char *args[], int nargs, int variadic, mtext_t *exp)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
pp_entry_t *ppp;
|
|
|
|
|
|
|
|
idx = pphash(id);
|
|
|
|
if((ppp = pplookup(id)) != NULL)
|
|
|
|
{
|
2021-09-24 07:50:54 +00:00
|
|
|
if(pedantic)
|
|
|
|
ppy_warning("Redefinition of %s\n%s:%d: note: previous definition was here",
|
|
|
|
id, ppp->filename, ppp->linenumber);
|
2020-12-03 14:01:54 +00:00
|
|
|
pp_del_define(id);
|
|
|
|
}
|
2021-09-24 06:06:03 +00:00
|
|
|
ppp = xmalloc(sizeof(pp_entry_t));
|
2020-12-03 14:01:54 +00:00
|
|
|
memset( ppp, 0, sizeof(*ppp) );
|
|
|
|
ppp->ident = id;
|
|
|
|
ppp->type = def_macro;
|
|
|
|
ppp->margs = args;
|
|
|
|
ppp->nargs = nargs;
|
2022-04-29 04:13:15 +00:00
|
|
|
ppp->variadic = variadic;
|
2020-12-03 14:01:54 +00:00
|
|
|
ppp->subst.mtext= exp;
|
2021-09-24 06:06:03 +00:00
|
|
|
ppp->filename = xstrdup(pp_status.input ? pp_status.input : "<internal or cmdline>");
|
2020-12-03 14:01:54 +00:00
|
|
|
ppp->linenumber = pp_status.input ? pp_status.line_number : 0;
|
|
|
|
list_add_head( &pp_defines[idx], &ppp->entry );
|
|
|
|
if(pp_status.debug)
|
|
|
|
{
|
2022-04-29 04:13:15 +00:00
|
|
|
fprintf(stderr, "Added macro (%s, %d) <%s(%d%s)> to <", pp_status.input, pp_status.line_number, ppp->ident, nargs, variadic ? ",va" : "");
|
2020-12-03 14:01:54 +00:00
|
|
|
for(; exp; exp = exp->next)
|
|
|
|
{
|
|
|
|
switch(exp->type)
|
|
|
|
{
|
|
|
|
case exp_text:
|
|
|
|
fprintf(stderr, " \"%s\" ", exp->subst.text);
|
|
|
|
break;
|
|
|
|
case exp_stringize:
|
|
|
|
fprintf(stderr, " #(%d) ", exp->subst.argidx);
|
|
|
|
break;
|
|
|
|
case exp_concat:
|
|
|
|
fprintf(stderr, "##");
|
|
|
|
break;
|
|
|
|
case exp_subst:
|
|
|
|
fprintf(stderr, " <%d> ", exp->subst.argidx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, ">\n");
|
|
|
|
}
|
|
|
|
return ppp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
* Include management
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
void wpp_add_include_path(const char *path)
|
|
|
|
{
|
2021-11-12 10:39:31 +00:00
|
|
|
char *dir = xstrdup(path);
|
|
|
|
char *cptr;
|
2020-12-03 14:01:54 +00:00
|
|
|
|
2021-11-12 10:39:31 +00:00
|
|
|
for(cptr = dir; *cptr; cptr++)
|
2020-12-03 14:01:54 +00:00
|
|
|
{
|
2021-11-12 10:39:31 +00:00
|
|
|
/* Convert to forward slash */
|
|
|
|
if(*cptr == '\\')
|
|
|
|
*cptr = '/';
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
2021-11-12 10:39:31 +00:00
|
|
|
/* Kill eventual trailing '/' */
|
|
|
|
if(*(cptr = dir + strlen(dir)-1) == '/') *cptr = '\0';
|
|
|
|
|
|
|
|
strarray_add( &includes, dir );
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *wpp_find_include(const char *name, const char *parent_name)
|
|
|
|
{
|
2021-11-12 10:39:31 +00:00
|
|
|
return wpp_lookup(name, !!parent_name, parent_name);
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *pp_open_include(const char *name, int type, const char *parent_name, char **newpath)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
void *fp;
|
|
|
|
|
2021-11-12 10:39:31 +00:00
|
|
|
if (!(path = wpp_lookup(name, type, parent_name))) return NULL;
|
2020-12-03 14:01:54 +00:00
|
|
|
fp = fopen(path, "rt");
|
|
|
|
|
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
if (pp_status.debug)
|
|
|
|
printf("Going to include <%s>\n", path);
|
|
|
|
if (newpath) *newpath = path;
|
|
|
|
else free( path );
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
free( path );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
* #if, #ifdef, #ifndef, #else, #elif and #endif state management
|
|
|
|
*
|
|
|
|
* #if state transitions are made on basis of the current TOS and the next
|
|
|
|
* required state. The state transitions are required to housekeep because
|
|
|
|
* #if:s can be nested. The ignore case is activated to prevent output from
|
|
|
|
* within a false clause.
|
|
|
|
* Some special cases come from the fact that the #elif cases are not
|
|
|
|
* binary, but three-state. The problem is that all other elif-cases must
|
|
|
|
* be false when one true one has been found. A second problem is that the
|
|
|
|
* #else clause is a final clause. No extra #else:s may follow.
|
|
|
|
*
|
|
|
|
* The states mean:
|
|
|
|
* if_true Process input to output
|
|
|
|
* if_false Process input but no output
|
|
|
|
* if_ignore Process input but no output
|
|
|
|
* if_elif Process input but no output
|
|
|
|
* if_elsefalse Process input but no output
|
|
|
|
* if_elsettrue Process input to output
|
|
|
|
*
|
|
|
|
* The possible state-sequences are [state(stack depth)] (rest can be deduced):
|
|
|
|
* TOS #if 1 #else #endif
|
|
|
|
* if_true(n) if_true(n+1) if_elsefalse(n+1)
|
|
|
|
* if_false(n) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elsetrue(n) if_true(n+1) if_elsefalse(n+1)
|
|
|
|
* if_elsefalse(n) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elif(n) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_ignore(n) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
*
|
|
|
|
* TOS #if 1 #elif 0 #else #endif
|
|
|
|
* if_true(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
|
|
|
|
* if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elsetrue(n) if_true(n+1) if_elif(n+1) if_elif(n+1)
|
|
|
|
* if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
*
|
|
|
|
* TOS #if 0 #elif 1 #else #endif
|
|
|
|
* if_true(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
|
|
|
|
* if_false(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elsetrue(n) if_false(n+1) if_true(n+1) if_elsefalse(n+1)
|
|
|
|
* if_elsefalse(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_elif(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
* if_ignore(n) if_ignore(n+1) if_ignore(n+1) if_ignore(n+1)
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static const char * const pp_if_state_str[] = {
|
|
|
|
"if_false",
|
|
|
|
"if_true",
|
|
|
|
"if_elif",
|
|
|
|
"if_elsefalse",
|
|
|
|
"if_elsetrue",
|
|
|
|
"if_ignore"
|
|
|
|
};
|
|
|
|
|
|
|
|
void pp_push_if(pp_if_state_t s)
|
|
|
|
{
|
|
|
|
if(if_stack_idx >= MAXIFSTACK)
|
2021-10-20 14:40:11 +00:00
|
|
|
error("#if-stack overflow; #{if,ifdef,ifndef} nested too deeply (> %d)\n", MAXIFSTACK);
|
2020-12-03 14:01:54 +00:00
|
|
|
|
|
|
|
if(pp_flex_debug)
|
|
|
|
fprintf(stderr, "Push if %s:%d: %s(%d) -> %s(%d)\n", pp_status.input, pp_status.line_number, pp_if_state_str[pp_if_state()], if_stack_idx, pp_if_state_str[s], if_stack_idx+1);
|
|
|
|
|
|
|
|
if_stack[if_stack_idx++] = s;
|
|
|
|
|
|
|
|
switch(s)
|
|
|
|
{
|
|
|
|
case if_true:
|
|
|
|
case if_elsetrue:
|
|
|
|
break;
|
|
|
|
case if_false:
|
|
|
|
case if_elsefalse:
|
|
|
|
case if_elif:
|
|
|
|
case if_ignore:
|
|
|
|
pp_push_ignore_state();
|
|
|
|
break;
|
2021-09-24 07:43:31 +00:00
|
|
|
case if_error:
|
|
|
|
assert(0);
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_if_state_t pp_pop_if(void)
|
|
|
|
{
|
|
|
|
if(if_stack_idx <= 0)
|
|
|
|
{
|
|
|
|
ppy_error("#{endif,else,elif} without #{if,ifdef,ifndef} (#if-stack underflow)");
|
|
|
|
return if_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(pp_if_state())
|
|
|
|
{
|
|
|
|
case if_true:
|
|
|
|
case if_elsetrue:
|
|
|
|
break;
|
|
|
|
case if_false:
|
|
|
|
case if_elsefalse:
|
|
|
|
case if_elif:
|
|
|
|
case if_ignore:
|
|
|
|
pp_pop_ignore_state();
|
|
|
|
break;
|
2021-09-24 07:43:31 +00:00
|
|
|
case if_error:
|
|
|
|
assert(0);
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(pp_flex_debug)
|
|
|
|
fprintf(stderr, "Pop if %s:%d: %s(%d) -> %s(%d)\n",
|
|
|
|
pp_status.input,
|
|
|
|
pp_status.line_number,
|
|
|
|
pp_if_state_str[pp_if_state()],
|
|
|
|
if_stack_idx,
|
|
|
|
pp_if_state_str[if_stack[if_stack_idx <= 1 ? if_true : if_stack_idx-2]],
|
|
|
|
if_stack_idx-1);
|
|
|
|
|
|
|
|
return if_stack[--if_stack_idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_if_state_t pp_if_state(void)
|
|
|
|
{
|
|
|
|
if(!if_stack_idx)
|
|
|
|
return if_true;
|
|
|
|
else
|
|
|
|
return if_stack[if_stack_idx-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void pp_next_if_state(int i)
|
|
|
|
{
|
|
|
|
switch(pp_if_state())
|
|
|
|
{
|
|
|
|
case if_true:
|
|
|
|
case if_elsetrue:
|
|
|
|
pp_push_if(i ? if_true : if_false);
|
|
|
|
break;
|
|
|
|
case if_false:
|
|
|
|
case if_elsefalse:
|
|
|
|
case if_elif:
|
|
|
|
case if_ignore:
|
|
|
|
pp_push_if(if_ignore);
|
|
|
|
break;
|
2021-09-24 07:43:31 +00:00
|
|
|
case if_error:
|
|
|
|
assert(0);
|
2020-12-03 14:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int pp_get_if_depth(void)
|
|
|
|
{
|
|
|
|
return if_stack_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s:%d:%d: %s: ", pp_status.input ? pp_status.input : "stdin",
|
|
|
|
pp_status.line_number, pp_status.char_number, t);
|
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int ppy_error(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
generic_msg(s, "error", ppy_text, ap);
|
|
|
|
va_end(ap);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ppy_warning(const char *s, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
generic_msg(s, "warning", ppy_text, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-07-01 04:36:22 +00:00
|
|
|
static void add_cmdline_defines(void)
|
|
|
|
{
|
|
|
|
struct define *def;
|
|
|
|
|
2020-12-03 12:27:15 +00:00
|
|
|
LIST_FOR_EACH_ENTRY( def, &cmdline_defines, struct define, entry )
|
2003-07-01 04:36:22 +00:00
|
|
|
{
|
2011-02-07 14:15:13 +00:00
|
|
|
if (def->value) pp_add_define( def->name, def->value );
|
2003-07-01 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2011-02-07 14:15:13 +00:00
|
|
|
pp_add_define( "__DATE__", buf );
|
2002-07-16 02:33:10 +00:00
|
|
|
|
|
|
|
strftime(buf, sizeof(buf), "\"%H:%M:%S\"", localtime(&now));
|
2011-02-07 14:15:13 +00:00
|
|
|
pp_add_define( "__TIME__", buf );
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2011-02-07 14:15:13 +00:00
|
|
|
ppp = pp_add_define( "__FILE__", "" );
|
2020-12-03 12:23:45 +00:00
|
|
|
ppp->type = def_special;
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2011-02-07 14:15:13 +00:00
|
|
|
ppp = pp_add_define( "__LINE__", "" );
|
2020-12-03 12:23:45 +00:00
|
|
|
ppp->type = def_special;
|
2002-07-16 02:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add a define to the preprocessor list */
|
2020-12-03 13:17:14 +00:00
|
|
|
static void 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
|
|
|
|
2020-12-03 12:27:15 +00:00
|
|
|
LIST_FOR_EACH_ENTRY( def, &cmdline_defines, struct define, entry )
|
2003-07-01 04:36:22 +00:00
|
|
|
{
|
|
|
|
if (!strcmp( def->name, name ))
|
|
|
|
{
|
2006-10-09 21:35:08 +00:00
|
|
|
free( def->value );
|
2021-09-24 06:06:03 +00:00
|
|
|
def->value = xstrdup(value);
|
2020-12-03 12:23:45 +00:00
|
|
|
return;
|
2003-07-01 04:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 06:06:03 +00:00
|
|
|
def = xmalloc( sizeof(*def) );
|
|
|
|
def->name = xstrdup(name);
|
|
|
|
def->value = xstrdup(value);
|
2020-12-03 12:27:15 +00:00
|
|
|
list_add_head( &cmdline_defines, &def->entry );
|
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;
|
|
|
|
|
2020-12-03 12:27:15 +00:00
|
|
|
LIST_FOR_EACH_ENTRY( def, &cmdline_defines, struct define, entry )
|
2003-07-01 04:36:22 +00:00
|
|
|
{
|
|
|
|
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 */
|
2020-12-03 12:23:45 +00:00
|
|
|
void 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;
|
2021-09-24 06:06:03 +00:00
|
|
|
char *str = xstrdup(value);
|
2020-12-03 12:23:45 +00:00
|
|
|
|
2009-09-28 19:32:39 +00:00
|
|
|
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 );
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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-12-30 12:08:43 +00:00
|
|
|
pp_status.line_number = 1;
|
|
|
|
pp_status.char_number = 1;
|
2005-01-09 16:53:47 +00:00
|
|
|
|
2020-12-03 13:17:14 +00:00
|
|
|
pp_init_define_state();
|
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;
|
2019-07-02 11:49:37 +00:00
|
|
|
else if (!(pp_status.file = fopen(input, "rt")))
|
2009-09-28 12:21:00 +00:00
|
|
|
ppy_error("Could not open %s\n", input);
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2021-09-24 06:06:03 +00:00
|
|
|
pp_status.input = input ? xstrdup(input) : NULL;
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2006-09-12 07:04:55 +00:00
|
|
|
ppy_out = output;
|
2021-09-24 06:06:03 +00:00
|
|
|
fprintf(ppy_out, "# 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();
|
2002-07-16 02:33:10 +00:00
|
|
|
|
2014-06-26 17:52:31 +00:00
|
|
|
if (input)
|
|
|
|
{
|
2019-07-02 11:49:37 +00:00
|
|
|
fclose(pp_status.file);
|
2014-06-26 17:52:31 +00:00
|
|
|
free(pp_status.input);
|
|
|
|
}
|
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();
|
2020-12-03 13:17:14 +00:00
|
|
|
pp_free_define_state();
|
2002-07-16 02:33:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|