2000-12-11 20:43:04 +01:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
prepro.cpp
|
|
|
|
|
|
|
|
Author: PKG
|
|
|
|
Created:
|
|
|
|
Project: Spongebob
|
|
|
|
Purpose:
|
|
|
|
|
|
|
|
Copyright (c) 2000 Climax Development Ltd
|
|
|
|
|
|
|
|
===========================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Includes
|
|
|
|
-------- */
|
|
|
|
|
|
|
|
#include "prepro.h"
|
2000-12-12 17:29:42 +01:00
|
|
|
|
|
|
|
#ifndef __PFILE_H__
|
2000-12-11 23:18:54 +01:00
|
|
|
#include "pfile.h"
|
2000-12-12 17:29:42 +01:00
|
|
|
#endif
|
2000-12-11 20:43:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Std Lib
|
|
|
|
------- */
|
|
|
|
|
2000-12-11 21:28:41 +01:00
|
|
|
#include <stdlib.h>
|
2000-12-11 20:43:04 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* Data
|
|
|
|
---- */
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Tyepdefs && Defines
|
|
|
|
------------------- */
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Structure defintions
|
|
|
|
-------------------- */
|
2000-12-11 21:28:41 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *m_cmd;
|
|
|
|
int (*m_func)(char *_cmd);
|
|
|
|
} PreproCmd;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct _macro
|
|
|
|
{
|
|
|
|
char *m_name;
|
|
|
|
char *m_replacement;
|
|
|
|
_macro *m_next;
|
|
|
|
} Macro;
|
|
|
|
|
2000-12-11 20:43:04 +01:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function Prototypes
|
|
|
|
------------------- */
|
|
|
|
|
2000-12-12 17:29:42 +01:00
|
|
|
static int ppf_define(char *_cmd);
|
|
|
|
static int ppf_include(char *_cmd);
|
|
|
|
static int ppf_print(char *_cmd);
|
|
|
|
static int ppf_undefine(char *_cmd);
|
2001-01-02 17:02:35 +01:00
|
|
|
static int ppf_ignore(char *_cmd);
|
2000-12-11 21:28:41 +01:00
|
|
|
|
|
|
|
static int addMacro(char *_name,char *_replacement);
|
2000-12-12 17:29:42 +01:00
|
|
|
static int removeMacro(char *_name);
|
2000-12-11 21:28:41 +01:00
|
|
|
|
|
|
|
|
2000-12-11 20:43:04 +01:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Vars
|
|
|
|
---- */
|
2000-12-11 21:28:41 +01:00
|
|
|
static PreproCmd s_preproCmds[]=
|
|
|
|
{
|
2000-12-12 17:29:42 +01:00
|
|
|
{ "define", ppf_define },
|
|
|
|
{ "include", ppf_include },
|
|
|
|
{ "print", ppf_print },
|
|
|
|
{ "undef", ppf_undefine },
|
2001-01-02 17:02:35 +01:00
|
|
|
|
|
|
|
// This is bad but it means we can use the include files generated by parkgrab
|
|
|
|
// ( might have to fix this one day.. )
|
|
|
|
{ "ifdef", ppf_ignore },
|
|
|
|
{ "ifndef", ppf_ignore },
|
|
|
|
{ "endif", ppf_ignore },
|
2000-12-11 21:28:41 +01:00
|
|
|
};
|
|
|
|
static int s_numPreproCmds=sizeof(s_preproCmds)/sizeof(PreproCmd);
|
|
|
|
|
2000-12-12 21:51:59 +01:00
|
|
|
// Macro list ( alphabetically sorted from lowest to highest )
|
2000-12-11 21:28:41 +01:00
|
|
|
static Macro *s_macros;
|
|
|
|
|
|
|
|
static char s_seps[]=" \t";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
2000-12-12 17:29:42 +01:00
|
|
|
Returns: true on success, false on failure
|
2000-12-11 21:28:41 +01:00
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
extern int preprocessorCmd(char *_cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
PreproCmd *pp;
|
|
|
|
|
|
|
|
pp=s_preproCmds;
|
|
|
|
for(i=0;i<s_numPreproCmds;i++)
|
|
|
|
{
|
2000-12-12 17:29:42 +01:00
|
|
|
if(strncmp(_cmd,pp->m_cmd,strlen(pp->m_cmd))==0)
|
2000-12-11 21:28:41 +01:00
|
|
|
{
|
|
|
|
return pp->m_func(_cmd);
|
|
|
|
}
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("UNKNOWN PREPROCESSOR CMD '%s'\n",_cmd);
|
2001-01-02 17:02:35 +01:00
|
|
|
return false;
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int ppf_define(char *_cmd)
|
|
|
|
{
|
|
|
|
char *macroName;
|
|
|
|
char *macroReplacement;
|
|
|
|
|
|
|
|
macroName=strtok(_cmd,s_seps);
|
|
|
|
macroName=strtok(NULL,s_seps);
|
|
|
|
macroReplacement=strtok(NULL,s_seps);
|
|
|
|
|
|
|
|
return addMacro(macroName,macroReplacement);
|
|
|
|
}
|
|
|
|
|
2000-12-11 20:43:04 +01:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
2000-12-11 21:28:41 +01:00
|
|
|
static int ppf_include(char *_cmd)
|
|
|
|
{
|
|
|
|
char *includeFile;
|
|
|
|
|
|
|
|
includeFile=strtok(_cmd,s_seps);
|
|
|
|
includeFile=strtok(NULL,s_seps);
|
|
|
|
|
2000-12-12 17:29:42 +01:00
|
|
|
return openPFile(includeFile);
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int ppf_print(char *_cmd)
|
|
|
|
{
|
|
|
|
char *printMessage;
|
|
|
|
|
|
|
|
printMessage=strtok(_cmd,s_seps);
|
|
|
|
printMessage=strtok(NULL,s_seps);
|
2000-12-12 17:29:42 +01:00
|
|
|
|
2000-12-11 21:28:41 +01:00
|
|
|
printf("#print %s\n",printMessage);
|
|
|
|
|
2000-12-12 17:29:42 +01:00
|
|
|
return true;
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-12 17:29:42 +01:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int ppf_undefine(char *_cmd)
|
|
|
|
{
|
|
|
|
char *macroName;
|
|
|
|
|
|
|
|
macroName=strtok(_cmd,s_seps);
|
|
|
|
macroName=strtok(NULL,s_seps);
|
|
|
|
|
|
|
|
return removeMacro(macroName);
|
|
|
|
}
|
2000-12-11 21:28:41 +01:00
|
|
|
|
|
|
|
|
2001-01-02 17:02:35 +01:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int ppf_ignore(char *_cmd)
|
|
|
|
{
|
|
|
|
printf("PRE-PROCESSOR COMMAND IGNORED..\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-11 21:28:41 +01:00
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int addMacro(char *_name,char *_replacement)
|
|
|
|
{
|
2000-12-12 21:51:59 +01:00
|
|
|
Macro *mac,*newMac,*prev;
|
2000-12-11 21:28:41 +01:00
|
|
|
|
2000-12-12 21:51:59 +01:00
|
|
|
// Is the macro already there?
|
2000-12-11 21:28:41 +01:00
|
|
|
mac=s_macros;
|
2000-12-12 17:29:42 +01:00
|
|
|
while(mac)
|
2000-12-11 21:28:41 +01:00
|
|
|
{
|
2000-12-12 21:51:59 +01:00
|
|
|
if(strcmp(_name,mac->m_name)==0) // pkg
|
2000-12-11 21:28:41 +01:00
|
|
|
{
|
|
|
|
printf("MACRO '%s' ALREADY DEFINED\n",_name);
|
2000-12-12 17:29:42 +01:00
|
|
|
return false;
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
2000-12-12 17:29:42 +01:00
|
|
|
mac=mac->m_next;
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
|
|
|
|
2000-12-12 21:51:59 +01:00
|
|
|
// Create new macro
|
2000-12-11 21:28:41 +01:00
|
|
|
newMac=(Macro*)malloc(sizeof(Macro));
|
2000-12-12 17:29:42 +01:00
|
|
|
newMac->m_name=(char*)malloc(strlen(_name)+1);
|
2000-12-11 21:28:41 +01:00
|
|
|
strcpy(newMac->m_name,_name);
|
2001-01-02 17:02:35 +01:00
|
|
|
if(_replacement)
|
|
|
|
{
|
|
|
|
newMac->m_replacement=(char*)malloc(strlen(_replacement)+1);
|
|
|
|
strcpy(newMac->m_replacement,_replacement);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newMac->m_replacement=NULL;
|
|
|
|
}
|
2000-12-12 21:51:59 +01:00
|
|
|
|
|
|
|
// Insert it into the list
|
|
|
|
mac=s_macros;
|
|
|
|
prev=NULL;
|
|
|
|
while(mac&&strcmp(_name,mac->m_name)>0)
|
|
|
|
{
|
|
|
|
prev=mac;
|
|
|
|
mac=mac->m_next;
|
|
|
|
};
|
|
|
|
if(prev)
|
|
|
|
{
|
|
|
|
prev->m_next=newMac;
|
|
|
|
newMac->m_next=mac;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newMac->m_next=s_macros;
|
|
|
|
s_macros=newMac;
|
|
|
|
}
|
2000-12-11 21:28:41 +01:00
|
|
|
|
2000-12-12 17:29:42 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
|
|
|
static int removeMacro(char *_name)
|
|
|
|
{
|
|
|
|
Macro *mac,*prev;
|
2000-12-12 21:51:59 +01:00
|
|
|
int cmpResult;
|
2000-12-12 17:29:42 +01:00
|
|
|
|
|
|
|
mac=s_macros;
|
|
|
|
prev=NULL;
|
|
|
|
while(mac)
|
|
|
|
{
|
2000-12-12 21:51:59 +01:00
|
|
|
cmpResult=strcmp(_name,mac->m_name);
|
|
|
|
if(cmpResult==0)
|
2000-12-12 17:29:42 +01:00
|
|
|
{
|
|
|
|
if(prev)
|
|
|
|
{
|
|
|
|
prev->m_next=mac->m_next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s_macros=mac->m_next;
|
|
|
|
}
|
|
|
|
free(mac->m_name);
|
2001-01-02 17:02:35 +01:00
|
|
|
if(mac->m_replacement)
|
|
|
|
{
|
|
|
|
free(mac->m_replacement);
|
|
|
|
}
|
2000-12-12 17:29:42 +01:00
|
|
|
free(mac);
|
|
|
|
return true;
|
|
|
|
}
|
2000-12-12 21:51:59 +01:00
|
|
|
else if(cmpResult<0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2000-12-12 17:29:42 +01:00
|
|
|
prev=mac;
|
|
|
|
mac=mac->m_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("MACRO '%s' NOT DEFINED, CANNOT UNDEF IT\n",_name);
|
|
|
|
return false;
|
2000-12-11 21:28:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------
|
|
|
|
Function:
|
|
|
|
Purpose:
|
|
|
|
Params:
|
|
|
|
Returns:
|
|
|
|
---------------------------------------------------------------------- */
|
2000-12-12 21:51:59 +01:00
|
|
|
extern char *lookupMacro(char *_name,int *_result)
|
2000-12-11 21:28:41 +01:00
|
|
|
{
|
2000-12-12 21:51:59 +01:00
|
|
|
Macro *mac;
|
|
|
|
size_t nameLength;
|
|
|
|
int cmpResult;
|
|
|
|
|
|
|
|
mac=s_macros;
|
|
|
|
nameLength=strlen(_name);
|
|
|
|
while(mac)
|
|
|
|
{
|
|
|
|
cmpResult=strncmp(_name,mac->m_name,nameLength);
|
|
|
|
if(cmpResult==0)
|
|
|
|
{
|
|
|
|
if(nameLength==strlen(mac->m_name))
|
|
|
|
{
|
|
|
|
*_result=KNOWN_MACRO;
|
|
|
|
return mac->m_replacement;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*_result=POSSIBLE_KNOWN_MACRO;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(cmpResult<0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mac=mac->m_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_result=UNKNOWN_MACRO;
|
2000-12-11 21:28:41 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-11 20:43:04 +01:00
|
|
|
/*===========================================================================
|
|
|
|
end */
|